Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / location.ml @ 684d39e7

History | View | Annotate | Download (3.44 KB)

1
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
5
(*                                                                  *)
6
(*  LustreC is free software, distributed WITHOUT ANY WARRANTY      *)
7
(*  under the terms of the GNU Lesser General Public License        *)
8
(*  version 2.1.                                                    *)
9
(*                                                                  *)
10
(********************************************************************)
11

    
12
type t = { loc_start: Lexing.position; loc_end: Lexing.position }
13

    
14
type filename = string
15

    
16
let dummy_loc = {loc_start=Lexing.dummy_pos; loc_end=Lexing.dummy_pos}
17

    
18
let set_input, get_input, get_module =
19
  let input_name : filename ref = ref "__UNINITIALIZED__" in
20
  let module_name : filename ref = ref "__UNINITIALIZED__" in
21
  (fun name -> input_name := name; module_name := Filename.chop_extension name),
22
  (fun () -> !input_name),
23
  (fun () -> !module_name)
24

    
25
let curr lexbuf = {
26
  loc_start = lexbuf.Lexing.lex_start_p;
27
  loc_end = lexbuf.Lexing.lex_curr_p
28
}
29

    
30
let init lexbuf fname =
31
  lexbuf.Lexing.lex_curr_p <- {
32
    Lexing.pos_fname = fname;
33
    Lexing.pos_lnum = 1;
34
    Lexing.pos_bol = 0;
35
    Lexing.pos_cnum = 0;
36
  }
37

    
38
let shift_pos pos1 pos2 =
39
  assert (pos1.Lexing.pos_fname = pos2.Lexing.pos_fname);
40
  {Lexing.pos_fname = pos1.Lexing.pos_fname;
41
    Lexing.pos_lnum = pos1.Lexing.pos_lnum + pos2.Lexing.pos_lnum;
42
    Lexing.pos_bol = pos1.Lexing.pos_bol + pos2.Lexing.pos_bol;
43
    Lexing.pos_cnum =if pos2.Lexing.pos_lnum = 1 then pos1.Lexing.pos_cnum + pos2.Lexing.pos_cnum else pos2.Lexing.pos_cnum
44
  }
45

    
46
let shift loc1 loc2 =
47
  {loc_start = shift_pos loc1.loc_start loc2.loc_start;
48
    loc_end  = shift_pos loc1.loc_start loc2.loc_end
49
  }
50
    
51
let symbol_rloc () = 
52
  {
53
    loc_start = Parsing.symbol_start_pos ();
54
    loc_end = Parsing.symbol_end_pos ()
55
  }
56
    
57

    
58
open Format
59

    
60
let print loc =
61
  let filename = loc.loc_start.Lexing.pos_fname in
62
  let line = loc.loc_start.Lexing.pos_lnum in
63
  let start_char =
64
    loc.loc_start.Lexing.pos_cnum - loc.loc_start.Lexing.pos_bol
65
  in
66
  let end_char =
67
    loc.loc_end.Lexing.pos_cnum - loc.loc_start.Lexing.pos_cnum + start_char
68
  in
69
  let (start_char, end_char) =
70
    if start_char < 0 then (0,1) else (start_char, end_char)
71
  in
72
  print_string ("File \""^filename^"\", line ");
73
  print_int line;
74
  print_string ", characters ";
75
  print_int start_char;
76
  print_string "-";
77
  print_int end_char;
78
  print_string ":";
79
  print_newline ()
80

    
81
let loc_line loc = loc.loc_start.Lexing.pos_lnum 
82
  
83
let pp_loc fmt loc =
84
  if loc == dummy_loc then () else
85
  let filename = loc.loc_start.Lexing.pos_fname in
86
  let line = loc_line loc in
87
  let start_char =
88
    loc.loc_start.Lexing.pos_cnum - loc.loc_start.Lexing.pos_bol
89
  in
90
  let end_char =
91
    loc.loc_end.Lexing.pos_cnum - loc.loc_start.Lexing.pos_cnum + start_char
92
  in
93
  let (start_char, end_char) =
94
    if start_char < 0 then (0,1) else (start_char, end_char)
95
  in
96
  Format.fprintf fmt "File \"%s\", line %i, characters %i-%i:" filename line start_char end_char
97

    
98
let pp_c_loc fmt loc =
99
  let filename = loc.loc_start.Lexing.pos_fname in
100
  let line = loc.loc_start.Lexing.pos_lnum in
101
  Format.fprintf fmt "#line %i \"%s\"" line filename
102

    
103
(* Local Variables: *)
104
(* compile-command:"make -C .." *)
105
(* End: *)