lustrec / src / lexer_lustre.mll @ e42fb618
History | View | Annotate | Download (5.12 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 |
{ |
13 |
open Parser_lustre |
14 |
open Utils |
15 |
|
16 |
exception Error of Location.t |
17 |
|
18 |
(* As advised by Caml documentation. This way a single lexer rule is |
19 |
used to handle all the possible keywords. *) |
20 |
let keyword_table = |
21 |
create_hashtable 20 [ |
22 |
"function", FUNCTION; |
23 |
"struct", STRUCT; |
24 |
"enum", ENUM; |
25 |
"automaton", AUTOMATON; |
26 |
"state", STATE; |
27 |
"until", UNTIL; |
28 |
"unless", UNLESS; |
29 |
"last", LAST; |
30 |
"resume", RESUME; |
31 |
"restart", RESTART; |
32 |
"if", IF; |
33 |
"then", THEN; |
34 |
"else", ELSE; |
35 |
"merge", MERGE; |
36 |
"arrow", ARROW; |
37 |
"fby", FBY; |
38 |
"when", WHEN; |
39 |
"whenot", WHENNOT; |
40 |
"every", EVERY; |
41 |
"node", NODE; |
42 |
"let", LET; |
43 |
"tel", TEL; |
44 |
"returns", RETURNS; |
45 |
"var", VAR; |
46 |
"imported", IMPORTED; |
47 |
"wcet", WCET; |
48 |
"type", TYPE; |
49 |
"int", TINT; |
50 |
"bool", TBOOL; |
51 |
"float", TFLOAT; |
52 |
"real", TREAL; |
53 |
"clock", TCLOCK; |
54 |
"not", NOT; |
55 |
"tail", TAIL; |
56 |
"true", TRUE; |
57 |
"false", FALSE; |
58 |
"and", AND; |
59 |
"or", OR; |
60 |
"xor", XOR; |
61 |
"mod", MOD; |
62 |
"pre", PRE; |
63 |
"div", DIV; |
64 |
"const", CONST; |
65 |
"assert", ASSERT; |
66 |
"lib", LIB; |
67 |
"prototype", PROTOTYPE; |
68 |
] |
69 |
|
70 |
|
71 |
(* Buffer for parsing specification/annotation *) |
72 |
let buf = Buffer.create 1024 |
73 |
|
74 |
let make_annot lexbuf s = |
75 |
try |
76 |
let ann = LexerLustreSpec.annot s in |
77 |
ANNOT ann |
78 |
with _ -> (Format.eprintf "Impossible to parse the following annotation:@.%s@.@?" s; exit 1) |
79 |
|
80 |
let make_spec lexbuf s = |
81 |
try |
82 |
let ns = LexerLustreSpec.spec s in |
83 |
NODESPEC ns |
84 |
with _ -> (Format.eprintf "Impossible to parse the following node specification:@.%s@.@?" s; exit 1) |
85 |
|
86 |
} |
87 |
|
88 |
let newline = ('\010' | '\013' | "\013\010") |
89 |
let notnewline = [^ '\010' '\013'] |
90 |
let blank = [' ' '\009' '\012'] |
91 |
|
92 |
rule token = parse |
93 |
| "--@" { Buffer.clear buf; |
94 |
spec_singleline lexbuf } |
95 |
| "(*@" { Buffer.clear buf; |
96 |
spec_multiline 0 lexbuf } |
97 |
| "--!" { Buffer.clear buf; |
98 |
annot_singleline lexbuf } |
99 |
| "(*!" { Buffer.clear buf; |
100 |
annot_multiline 0 lexbuf } |
101 |
| "(*" |
102 |
{ comment 0 lexbuf } |
103 |
| "--" [^ '!' '@'] notnewline* (newline|eof) |
104 |
{ incr_line lexbuf; |
105 |
token lexbuf } |
106 |
| newline |
107 |
{ incr_line lexbuf; |
108 |
token lexbuf } |
109 |
| blank + |
110 |
{token lexbuf} |
111 |
| ['0'-'9'] ['0'-'9']* '.' ['0'-'9']* |
112 |
{FLOAT (float_of_string (Lexing.lexeme lexbuf))} |
113 |
| ['0'-'9']+ |
114 |
{INT (int_of_string (Lexing.lexeme lexbuf)) } |
115 |
| ['0'-'9']+ '.' ['0'-'9']+ ('E'|'e') ('+'|'-') ['0'-'9'] ['0'-'9']* as s {REAL s} |
116 |
| "tel." {TEL} |
117 |
| "tel;" {TEL} |
118 |
| "#open" { OPEN } |
119 |
| ['_' 'a'-'z'] [ '_' 'a'-'z' 'A'-'Z' '0'-'9']* |
120 |
{let s = Lexing.lexeme lexbuf in |
121 |
try |
122 |
Hashtbl.find keyword_table s |
123 |
with Not_found -> |
124 |
IDENT s} |
125 |
| ['A'-'Z'] [ '_' 'a'-'z' 'A'-'Z' '0'-'9']* |
126 |
{let s = Lexing.lexeme lexbuf in |
127 |
try |
128 |
Hashtbl.find keyword_table s |
129 |
with Not_found -> |
130 |
UIDENT s} |
131 |
| "->" {ARROW} |
132 |
| "=>" {IMPL} |
133 |
| "<=" {LTE} |
134 |
| ">=" {GTE} |
135 |
| "<>" {NEQ} |
136 |
| '<' {LT} |
137 |
| '>' {GT} |
138 |
| "!=" {NEQ} |
139 |
| '-' {MINUS} |
140 |
| '+' {PLUS} |
141 |
| '/' {DIV} |
142 |
| '*' {MULT} |
143 |
| '=' {EQ} |
144 |
| '(' {LPAR} |
145 |
| ')' {RPAR} |
146 |
| '[' {LBRACKET} |
147 |
| ']' {RBRACKET} |
148 |
| '{' {LCUR} |
149 |
| '}' {RCUR} |
150 |
| ';' {SCOL} |
151 |
| ':' {COL} |
152 |
| ',' {COMMA} |
153 |
| '=' {EQ} |
154 |
| '/' {DIV} |
155 |
| "&&" {AMPERAMPER} |
156 |
| "||" {BARBAR} |
157 |
| "::" {COLCOL} |
158 |
| "^" {POWER} |
159 |
| '"' {QUOTE} |
160 |
| eof { EOF } |
161 |
| _ { raise (Error (Location.curr lexbuf)) } |
162 |
|
163 |
and comment n = parse |
164 |
| eof |
165 |
{ raise (Error (Location.curr lexbuf)) } |
166 |
| "(*" |
167 |
{ comment (n+1) lexbuf } |
168 |
| "*)" |
169 |
{ if n > 0 then comment (n-1) lexbuf else token lexbuf } |
170 |
| newline |
171 |
{ incr_line lexbuf; |
172 |
comment n lexbuf } |
173 |
| _ { comment n lexbuf } |
174 |
|
175 |
and annot_singleline = parse |
176 |
| newline { incr_line lexbuf; make_annot lexbuf (Buffer.contents buf) } |
177 |
| _ as c { Buffer.add_char buf c; annot_singleline lexbuf } |
178 |
|
179 |
and annot_multiline n = parse |
180 |
| "*)" as s { |
181 |
if n > 0 then |
182 |
(Buffer.add_string buf s; annot_multiline (n-1) lexbuf) |
183 |
else |
184 |
make_annot lexbuf (Buffer.contents buf) } |
185 |
| "(*" as s { Buffer.add_string buf s; annot_multiline (n+1) lexbuf } |
186 |
| newline as s { incr_line lexbuf; Buffer.add_string buf s; annot_multiline n lexbuf } |
187 |
| _ as c { Buffer.add_char buf c; annot_multiline n lexbuf } |
188 |
|
189 |
and spec_singleline = parse |
190 |
| newline { incr_line lexbuf; make_spec lexbuf (Buffer.contents buf) } |
191 |
| _ as c { Buffer.add_char buf c; spec_singleline lexbuf } |
192 |
|
193 |
and spec_multiline n = parse |
194 |
| "*)" as s { if n > 0 then |
195 |
(Buffer.add_string buf s; spec_multiline (n-1) lexbuf) |
196 |
else |
197 |
make_spec lexbuf (Buffer.contents buf) } |
198 |
| "(*" as s { Buffer.add_string buf s; spec_multiline (n+1) lexbuf } |
199 |
| newline as s { incr_line lexbuf; Buffer.add_string buf s; spec_multiline n lexbuf } |
200 |
| _ as c { Buffer.add_char buf c; spec_multiline n lexbuf } |
201 |
|