Project

General

Profile

Download (4.41 KB) Statistics
| Branch: | Tag: | Revision:
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
  (* Format.eprintf "Shift pos %s by pos %s@." pos1.Lexing.pos_fname pos2.Lexing.pos_fname;
40
   * assert (pos1.Lexing.pos_fname = pos2.Lexing.pos_fname); *)
41
  {Lexing.pos_fname = pos1.Lexing.pos_fname;
42
    Lexing.pos_lnum = pos1.Lexing.pos_lnum + pos2.Lexing.pos_lnum -1;
43

    
44
    (* New try *)
45
    Lexing.pos_bol = pos2.Lexing.pos_bol;
46
    Lexing.pos_cnum = pos2.Lexing.pos_cnum;
47
    (*
48
    Lexing.pos_bol = pos1.Lexing.pos_bol + pos2.Lexing.pos_bol;
49
    Lexing.pos_cnum =if pos2.Lexing.pos_lnum = 1 then pos1.Lexing.pos_cnum + pos2.Lexing.pos_cnum else pos2.Lexing.pos_cnum
50
     *)
51
}
52

    
53
    
54

    
55
open Format
56

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

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

    
103
  ()
104
  
105
let pp_c_loc fmt loc =
106
  let filename = loc.loc_start.Lexing.pos_fname in
107
  let line = loc.loc_start.Lexing.pos_lnum in
108
  Format.fprintf fmt "#line %i \"%s\"" line filename
109

    
110
let shift loc1 loc2 =
111
  let new_loc = 
112
    {loc_start = shift_pos loc1.loc_start loc2.loc_start;
113
     loc_end  = shift_pos loc1.loc_start loc2.loc_end
114
    }
115
  in
116
  (* Format.eprintf "loc1: %a@.loc2: %a@.nloc: %a@."
117
   *   pp_loc loc1
118
   *   pp_loc loc2
119
   *   pp_loc new_loc
120
   * ; *)
121
  new_loc
122

    
123
let loc_pile = ref []
124
let push_loc l =
125
  loc_pile := l::!loc_pile
126
let pop_loc () = loc_pile := List.tl !loc_pile
127
  
128
let symbol_rloc () =
129
  let curr_loc =
130
  {
131
    loc_start = Parsing.symbol_start_pos ();
132
    loc_end = Parsing.symbol_end_pos ()
133
  }
134
  in
135

    
136
  let res =
137
    if List.length !loc_pile > 0 then
138
    shift (List.hd !loc_pile) curr_loc
139
  else
140
    curr_loc
141
  in
142
  (* Format.eprintf "Loc: %a@." pp_loc res; *)
143
  res
144
    (* Local Variables: *)
145
    (* compile-command:"make -C .." *)
146
    (* End: *)
(23-23/49)