lustrec / src / lexer_lustre.mll @ c00d0b42
History | View | Annotate | Download (5.26 KB)
1 |
(* ---------------------------------------------------------------------------- |
---|---|
2 |
* SchedMCore - A MultiCore Scheduling Framework |
3 |
* Copyright (C) 2009-2011, ONERA, Toulouse, FRANCE - LIFL, Lille, FRANCE |
4 |
* |
5 |
* This file is part of Prelude |
6 |
* |
7 |
* Prelude is free software; you can redistribute it and/or |
8 |
* modify it under the terms of the GNU Lesser General Public License |
9 |
* as published by the Free Software Foundation ; either version 2 of |
10 |
* the License, or (at your option) any later version. |
11 |
* |
12 |
* Prelude is distributed in the hope that it will be useful, but |
13 |
* WITHOUT ANY WARRANTY ; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* Lesser General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU Lesser General Public |
18 |
* License along with this program ; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
*---------------------------------------------------------------------------- *) |
22 |
|
23 |
{ |
24 |
open Parser_lustre |
25 |
open Utils |
26 |
|
27 |
exception Error of Location.t |
28 |
|
29 |
(* As advised by Caml documentation. This way a single lexer rule is |
30 |
used to handle all the possible keywords. *) |
31 |
let keyword_table = |
32 |
create_hashtable 20 [ |
33 |
"function", FUNCTION; |
34 |
"struct", STRUCT; |
35 |
"enum", ENUM; |
36 |
"automaton", AUTOMATON; |
37 |
"state", STATE; |
38 |
"until", UNTIL; |
39 |
"unless", UNLESS; |
40 |
"last", LAST; |
41 |
"resume", RESUME; |
42 |
"restart", RESTART; |
43 |
"if", IF; |
44 |
"then", THEN; |
45 |
"else", ELSE; |
46 |
"merge", MERGE; |
47 |
"arrow", ARROW; |
48 |
"fby", FBY; |
49 |
"when", WHEN; |
50 |
"whenot", WHENNOT; |
51 |
"every", EVERY; |
52 |
"node", NODE; |
53 |
"let", LET; |
54 |
"tel", TEL; |
55 |
"returns", RETURNS; |
56 |
"var", VAR; |
57 |
"imported", IMPORTED; |
58 |
"wcet", WCET; |
59 |
"type", TYPE; |
60 |
"int", TINT; |
61 |
"bool", TBOOL; |
62 |
"float", TFLOAT; |
63 |
"real", TREAL; |
64 |
"clock", TCLOCK; |
65 |
"not", NOT; |
66 |
"tail", TAIL; |
67 |
"and", AND; |
68 |
"or", OR; |
69 |
"xor", XOR; |
70 |
"mod", MOD; |
71 |
"pre", PRE; |
72 |
"div", DIV; |
73 |
"const", CONST; |
74 |
"assert", ASSERT; |
75 |
"in", IN; |
76 |
"prototype", PROTOTYPE; |
77 |
] |
78 |
|
79 |
|
80 |
(* Buffer for parsing specification/annotation *) |
81 |
let buf = Buffer.create 1024 |
82 |
|
83 |
let make_annot lexbuf s = |
84 |
try |
85 |
let ann = LexerLustreSpec.annot s in |
86 |
ANNOT ann |
87 |
with _ -> (Format.eprintf "Impossible to parse the following annotation:@.%s@.@?" s; exit 1) |
88 |
|
89 |
let make_spec lexbuf s = |
90 |
try |
91 |
let ns = LexerLustreSpec.spec s in |
92 |
NODESPEC ns |
93 |
with _ -> (Format.eprintf "Impossible to parse the following node specification:@.%s@.@?" s; exit 1) |
94 |
|
95 |
} |
96 |
|
97 |
let newline = ('\010' | '\013' | "\013\010") |
98 |
let notnewline = [^ '\010' '\013'] |
99 |
let blank = [' ' '\009' '\012'] |
100 |
|
101 |
rule token = parse |
102 |
| "--@" { Buffer.clear buf; |
103 |
spec_singleline lexbuf } |
104 |
| "(*@" { Buffer.clear buf; |
105 |
spec_multiline 0 lexbuf } |
106 |
| "--!" { Buffer.clear buf; |
107 |
annot_singleline lexbuf } |
108 |
| "(*!" { Buffer.clear buf; |
109 |
annot_multiline 0 lexbuf } |
110 |
| "(*" |
111 |
{ comment 0 lexbuf } |
112 |
| "--" [^ '!' '@'] notnewline* (newline|eof) |
113 |
{ incr_line lexbuf; |
114 |
token lexbuf } |
115 |
| newline |
116 |
{ incr_line lexbuf; |
117 |
token lexbuf } |
118 |
| blank + |
119 |
{token lexbuf} |
120 |
| ['0'-'9'] ['0'-'9']* '.' ['0'-'9']* |
121 |
{FLOAT (float_of_string (Lexing.lexeme lexbuf))} |
122 |
| ['0'-'9']+ |
123 |
{INT (int_of_string (Lexing.lexeme lexbuf)) } |
124 |
| ['0'-'9']+ '.' ['0'-'9']+ ('E'|'e') ('+'|'-') ['0'-'9'] ['0'-'9']* as s {REAL s} |
125 |
| "tel." {TEL} |
126 |
| "tel;" {TEL} |
127 |
| "#open" { OPEN } |
128 |
| ['_' 'a'-'z' 'A'-'Z'] [ '_' 'a'-'z' 'A'-'Z' '0'-'9']* |
129 |
{let s = Lexing.lexeme lexbuf in |
130 |
try |
131 |
Hashtbl.find keyword_table s |
132 |
with Not_found -> |
133 |
IDENT s} |
134 |
| "->" {ARROW} |
135 |
| "=>" {IMPL} |
136 |
| "<=" {LTE} |
137 |
| ">=" {GTE} |
138 |
| "<>" {NEQ} |
139 |
| '<' {LT} |
140 |
| '>' {GT} |
141 |
| "!=" {NEQ} |
142 |
| '-' {MINUS} |
143 |
| '+' {PLUS} |
144 |
| '/' {DIV} |
145 |
| '*' {MULT} |
146 |
| '=' {EQ} |
147 |
| '(' {LPAR} |
148 |
| ')' {RPAR} |
149 |
| '[' {LBRACKET} |
150 |
| ']' {RBRACKET} |
151 |
| '{' {LCUR} |
152 |
| '}' {RCUR} |
153 |
| ';' {SCOL} |
154 |
| ':' {COL} |
155 |
| ',' {COMMA} |
156 |
| '=' {EQ} |
157 |
| '/' {DIV} |
158 |
| "&&" {AMPERAMPER} |
159 |
| "||" {BARBAR} |
160 |
| "::" {COLCOL} |
161 |
| "^" {POWER} |
162 |
| '"' {QUOTE} |
163 |
| eof { EOF } |
164 |
| _ { raise (Error (Location.curr lexbuf)) } |
165 |
|
166 |
and comment n = parse |
167 |
| eof |
168 |
{ raise (Error (Location.curr lexbuf)) } |
169 |
| "(*" |
170 |
{ comment (n+1) lexbuf } |
171 |
| "*)" |
172 |
{ if n > 0 then comment (n-1) lexbuf else token lexbuf } |
173 |
| newline |
174 |
{ incr_line lexbuf; |
175 |
comment n lexbuf } |
176 |
| _ { comment n lexbuf } |
177 |
|
178 |
and annot_singleline = parse |
179 |
| newline { incr_line lexbuf; make_annot lexbuf (Buffer.contents buf) } |
180 |
| _ as c { Buffer.add_char buf c; annot_singleline lexbuf } |
181 |
|
182 |
and annot_multiline n = parse |
183 |
| "*)" as s { |
184 |
if n > 0 then |
185 |
(Buffer.add_string buf s; annot_multiline (n-1) lexbuf) |
186 |
else |
187 |
make_annot lexbuf (Buffer.contents buf) } |
188 |
| "(*" as s { Buffer.add_string buf s; annot_multiline (n+1) lexbuf } |
189 |
| newline as s { incr_line lexbuf; Buffer.add_string buf s; annot_multiline n lexbuf } |
190 |
| _ as c { Buffer.add_char buf c; annot_multiline n lexbuf } |
191 |
|
192 |
and spec_singleline = parse |
193 |
| newline { incr_line lexbuf; make_spec lexbuf (Buffer.contents buf) } |
194 |
| _ as c { Buffer.add_char buf c; spec_singleline lexbuf } |
195 |
|
196 |
and spec_multiline n = parse |
197 |
| "*)" as s { if n > 0 then |
198 |
(Buffer.add_string buf s; spec_multiline (n-1) lexbuf) |
199 |
else |
200 |
make_spec lexbuf (Buffer.contents buf) } |
201 |
| "(*" as s { Buffer.add_string buf s; spec_multiline (n+1) lexbuf } |
202 |
| newline as s { incr_line lexbuf; Buffer.add_string buf s; spec_multiline n lexbuf } |
203 |
| _ as c { Buffer.add_char buf c; spec_multiline n lexbuf } |
204 |
|