lustrec / src / lexer_lustre.mll @ e7cc5186
History | View | Annotate | Download (5.67 KB)
1 | a2d97a3e | ploc | (********************************************************************) |
---|---|---|---|
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 | 22fe1c93 | ploc | |
12 | { |
||
13 | open Parser_lustre |
||
14 | open Utils |
||
15 | |||
16 | (* As advised by Caml documentation. This way a single lexer rule is |
||
17 | used to handle all the possible keywords. *) |
||
18 | let keyword_table = |
||
19 | create_hashtable 20 [ |
||
20 | "function", FUNCTION; |
||
21 | "struct", STRUCT; |
||
22 | "enum", ENUM; |
||
23 | "automaton", AUTOMATON; |
||
24 | "state", STATE; |
||
25 | "until", UNTIL; |
||
26 | "unless", UNLESS; |
||
27 | "last", LAST; |
||
28 | "resume", RESUME; |
||
29 | "restart", RESTART; |
||
30 | "if", IF; |
||
31 | "then", THEN; |
||
32 | "else", ELSE; |
||
33 | "merge", MERGE; |
||
34 | "arrow", ARROW; |
||
35 | "fby", FBY; |
||
36 | "when", WHEN; |
||
37 | "whenot", WHENNOT; |
||
38 | "every", EVERY; |
||
39 | "node", NODE; |
||
40 | "let", LET; |
||
41 | "tel", TEL; |
||
42 | "returns", RETURNS; |
||
43 | "var", VAR; |
||
44 | "imported", IMPORTED; |
||
45 | "wcet", WCET; |
||
46 | "type", TYPE; |
||
47 | "int", TINT; |
||
48 | "bool", TBOOL; |
||
49 | 04a63d25 | xthirioux | (* "float", TFLOAT; *) |
50 | 22fe1c93 | ploc | "real", TREAL; |
51 | "clock", TCLOCK; |
||
52 | "not", NOT; |
||
53 | "tail", TAIL; |
||
54 | ef34b4ae | xthirioux | "true", TRUE; |
55 | "false", FALSE; |
||
56 | 22fe1c93 | ploc | "and", AND; |
57 | "or", OR; |
||
58 | "xor", XOR; |
||
59 | "mod", MOD; |
||
60 | "pre", PRE; |
||
61 | "div", DIV; |
||
62 | "const", CONST; |
||
63 | "assert", ASSERT; |
||
64 | 3826f8cb | ploc | "lib", LIB; |
65 | 54ae8ac7 | ploc | "prototype", PROTOTYPE; |
66 | dcafc99b | Ploc | "c_code", CCODE; (* not sure how it is used *) |
67 | "matlab", MATLAB; (* same as above *) |
||
68 | 22fe1c93 | ploc | ] |
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 | f3d244c1 | xthirioux | with LexerLustreSpec.Error loc -> raise (Parse.Error (Location.shift (Location.curr lexbuf) loc, Parse.Annot_error s)) |
79 | 22fe1c93 | ploc | |
80 | let make_spec lexbuf s = |
||
81 | try |
||
82 | let ns = LexerLustreSpec.spec s in |
||
83 | NODESPEC ns |
||
84 | f3d244c1 | xthirioux | with LexerLustreSpec.Error loc -> raise (Parse.Error (Location.shift (Location.curr lexbuf) loc, Parse.Node_spec_error s)) |
85 | 22fe1c93 | ploc | |
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 | 04a63d25 | xthirioux | | ((['0'-'9']+ as l) '.' (['0'-'9']* as r) ('E'|'e') (('+'|'-')? ['0'-'9']+ as exp)) as s |
112 | {REAL (Num.num_of_string (l^r), String.length r + -1 * int_of_string exp , s)} |
||
113 | | ((['0'-'9']+ as l) '.' (['0'-'9']* as r)) as s |
||
114 | {REAL (Num.num_of_string (l^r), String.length r, s)} |
||
115 | 22fe1c93 | ploc | | ['0'-'9']+ |
116 | {INT (int_of_string (Lexing.lexeme lexbuf)) } |
||
117 | | "tel." {TEL} |
||
118 | | "tel;" {TEL} |
||
119 | b3381ae8 | xthirioux | | "#open" { OPEN } |
120 | ef34b4ae | xthirioux | | ['_' 'a'-'z'] [ '_' 'a'-'z' 'A'-'Z' '0'-'9']* |
121 | 22fe1c93 | ploc | {let s = Lexing.lexeme lexbuf in |
122 | try |
||
123 | Hashtbl.find keyword_table s |
||
124 | with Not_found -> |
||
125 | IDENT s} |
||
126 | ef34b4ae | xthirioux | | ['A'-'Z'] [ '_' 'a'-'z' 'A'-'Z' '0'-'9']* |
127 | {let s = Lexing.lexeme lexbuf in |
||
128 | try |
||
129 | Hashtbl.find keyword_table s |
||
130 | with Not_found -> |
||
131 | UIDENT s} |
||
132 | 22fe1c93 | ploc | | "->" {ARROW} |
133 | | "=>" {IMPL} |
||
134 | | "<=" {LTE} |
||
135 | | ">=" {GTE} |
||
136 | | "<>" {NEQ} |
||
137 | | '<' {LT} |
||
138 | | '>' {GT} |
||
139 | | "!=" {NEQ} |
||
140 | | '-' {MINUS} |
||
141 | | '+' {PLUS} |
||
142 | | '/' {DIV} |
||
143 | | '*' {MULT} |
||
144 | | '=' {EQ} |
||
145 | | '(' {LPAR} |
||
146 | | ')' {RPAR} |
||
147 | | '[' {LBRACKET} |
||
148 | | ']' {RBRACKET} |
||
149 | | '{' {LCUR} |
||
150 | | '}' {RCUR} |
||
151 | | ';' {SCOL} |
||
152 | | ':' {COL} |
||
153 | | ',' {COMMA} |
||
154 | | '=' {EQ} |
||
155 | | '/' {DIV} |
||
156 | | "&&" {AMPERAMPER} |
||
157 | | "||" {BARBAR} |
||
158 | | "::" {COLCOL} |
||
159 | | "^" {POWER} |
||
160 | | '"' {QUOTE} |
||
161 | | eof { EOF } |
||
162 | f3d244c1 | xthirioux | | _ { raise (Parse.Error (Location.curr lexbuf, Parse.Undefined_token (Lexing.lexeme lexbuf))) } |
163 | 22fe1c93 | ploc | |
164 | and comment n = parse |
||
165 | | eof |
||
166 | f3d244c1 | xthirioux | { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_comment)) } |
167 | 22fe1c93 | ploc | | "(*" |
168 | { comment (n+1) lexbuf } |
||
169 | | "*)" |
||
170 | { if n > 0 then comment (n-1) lexbuf else token lexbuf } |
||
171 | | newline |
||
172 | { incr_line lexbuf; |
||
173 | comment n lexbuf } |
||
174 | | _ { comment n lexbuf } |
||
175 | |||
176 | and annot_singleline = parse |
||
177 | 04a63d25 | xthirioux | | eof { make_annot lexbuf (Buffer.contents buf) } |
178 | 22fe1c93 | ploc | | newline { incr_line lexbuf; make_annot lexbuf (Buffer.contents buf) } |
179 | | _ as c { Buffer.add_char buf c; annot_singleline lexbuf } |
||
180 | |||
181 | and annot_multiline n = parse |
||
182 | f3d244c1 | xthirioux | | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_annot)) } |
183 | 22fe1c93 | ploc | | "*)" 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 | 04a63d25 | xthirioux | | eof { make_spec lexbuf (Buffer.contents buf) } |
194 | 22fe1c93 | ploc | | newline { incr_line lexbuf; make_spec lexbuf (Buffer.contents buf) } |
195 | | _ as c { Buffer.add_char buf c; spec_singleline lexbuf } |
||
196 | |||
197 | and spec_multiline n = parse |
||
198 | f3d244c1 | xthirioux | | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_node_spec)) } |
199 | 22fe1c93 | ploc | | "*)" as s { if n > 0 then |
200 | (Buffer.add_string buf s; spec_multiline (n-1) lexbuf) |
||
201 | else |
||
202 | make_spec lexbuf (Buffer.contents buf) } |
||
203 | | "(*" as s { Buffer.add_string buf s; spec_multiline (n+1) lexbuf } |
||
204 | | newline as s { incr_line lexbuf; Buffer.add_string buf s; spec_multiline n lexbuf } |
||
205 | | _ as c { Buffer.add_char buf c; spec_multiline n lexbuf } |