Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / parsers / lexer_lustre.mll @ f4cba4b8

History | View | Annotate | Download (5.92 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
  "resume", RESUME;
28
  "restart", RESTART;
29
  "if", IF;
30
  "then", THEN;
31
  "else", ELSE;
32
  "merge", MERGE;
33
  "arrow", ARROW;
34
  "fby", FBY;
35
  "when", WHEN;
36
  "whenot", WHENNOT;
37
  "every", EVERY;
38
  "node", NODE;
39
  "let", LET;
40
  "tel", TEL;
41
  "returns", RETURNS;
42
  "var", VAR;
43
  "imported", IMPORTED;
44 f4cba4b8 ploc
  "import", IMPORT;
45 22fe1c93 ploc
  "type", TYPE;
46
  "int", TINT;
47
  "bool", TBOOL;
48 04a63d25 xthirioux
  (* "float", TFLOAT; *)
49 22fe1c93 ploc
  "real", TREAL;
50
  "clock", TCLOCK;
51
  "not", NOT;
52 ef34b4ae xthirioux
  "true", TRUE;
53
  "false", FALSE;
54 22fe1c93 ploc
  "and", AND;
55
  "or", OR;
56
  "xor", XOR;
57
  "mod", MOD;
58
  "pre", PRE;
59
  "div", DIV;
60
  "const", CONST;
61
  "assert", ASSERT;
62 2d27eedd ploc
  "contract", CONTRACT;
63 3826f8cb ploc
  "lib", LIB;
64 54ae8ac7 ploc
  "prototype", PROTOTYPE;
65 f9f06e7d ploc
  "ensure", ENSURE;
66
  "require", REQUIRE;
67
  (* "observer", OBSERVER; *)
68
  "invariant", INVARIANT;
69
  "mode", MODE;
70
  "assume", ASSUME;
71
  "contract", CONTRACT;
72
  "guarantees", GUARANTEES;
73
  "exists", EXISTS;
74
  "forall", FORALL;
75
 
76 dcafc99b Ploc
  "c_code", CCODE; (* not sure how it is used *)
77
  "matlab", MATLAB; (* same as above *)
78 22fe1c93 ploc
]
79
80
81
(* Buffer for parsing specification/annotation *)
82
let buf = Buffer.create 1024
83
84 57392da1 Christophe Garion
let make_annot lexbuf s = 
85 22fe1c93 ploc
  try
86
    let ann = LexerLustreSpec.annot s in
87
    ANNOT ann
88 f3d244c1 xthirioux
  with LexerLustreSpec.Error loc -> raise (Parse.Error (Location.shift (Location.curr lexbuf) loc, Parse.Annot_error s))
89 22fe1c93 ploc
90 57392da1 Christophe Garion
let make_spec lexbuf s = 
91 22fe1c93 ploc
  try
92
    let ns = LexerLustreSpec.spec s in
93
    NODESPEC ns
94 f3d244c1 xthirioux
  with LexerLustreSpec.Error loc -> raise (Parse.Error (Location.shift (Location.curr lexbuf) loc, Parse.Node_spec_error s))
95 2ae3ef3f ploc
96 57392da1 Christophe Garion
}
97 22fe1c93 ploc
98
let newline = ('\010' | '\013' | "\013\010")
99
let notnewline = [^ '\010' '\013']
100
let blank = [' ' '\009' '\012']
101
102
rule token = parse
103
| "--@" { Buffer.clear buf;
104
	  spec_singleline lexbuf }
105 57392da1 Christophe Garion
| "(*@" { Buffer.clear buf; 
106 22fe1c93 ploc
	  spec_multiline 0 lexbuf }
107 57392da1 Christophe Garion
| "--!" { Buffer.clear buf; 
108 22fe1c93 ploc
	  annot_singleline lexbuf }
109 57392da1 Christophe Garion
| "(*!" { Buffer.clear buf; 
110 22fe1c93 ploc
	  annot_multiline 0 lexbuf }
111
| "(*"
112
    { comment 0 lexbuf }
113
| "--" [^ '!' '@'] notnewline* (newline|eof)
114
    { incr_line lexbuf;
115
      token lexbuf }
116
| newline
117
    { incr_line lexbuf;
118
      token lexbuf }
119
| blank +
120
    {token lexbuf}
121 04a63d25 xthirioux
| ((['0'-'9']+ as l)  '.' (['0'-'9']* as r) ('E'|'e') (('+'|'-')? ['0'-'9']+ as exp)) as s
122
    {REAL (Num.num_of_string (l^r), String.length r + -1 * int_of_string exp , s)}
123
| ((['0'-'9']+ as l) '.' (['0'-'9']* as r)) as s
124
    {REAL (Num.num_of_string (l^r), String.length r, s)}
125 57392da1 Christophe Garion
| ['0'-'9']+ 
126 22fe1c93 ploc
    {INT (int_of_string (Lexing.lexeme lexbuf)) }
127
| "tel." {TEL}
128
| "tel;" {TEL}
129 b3381ae8 xthirioux
| "#open" { OPEN }
130 19a1e66b ploc
| "include" { INCLUDE }
131 ef34b4ae xthirioux
| ['_' 'a'-'z'] [ '_' 'a'-'z' 'A'-'Z' '0'-'9']*
132 22fe1c93 ploc
    {let s = Lexing.lexeme lexbuf in
133
    try
134
      Hashtbl.find keyword_table s
135
    with Not_found ->
136
      IDENT s}
137 ef34b4ae xthirioux
| ['A'-'Z'] [ '_' 'a'-'z' 'A'-'Z' '0'-'9']*
138
    {let s = Lexing.lexeme lexbuf in
139
    try
140
      Hashtbl.find keyword_table s
141
    with Not_found ->
142 19a1e66b ploc
      UIDENT s}     
143 22fe1c93 ploc
| "->" {ARROW}
144
| "=>" {IMPL}
145
| "<=" {LTE}
146
| ">=" {GTE}
147
| "<>" {NEQ}
148
| '<' {LT}
149
| '>' {GT}
150
| "!=" {NEQ}
151
| '-' {MINUS}
152
| '+' {PLUS}
153
| '/' {DIV}
154
| '*' {MULT}
155
| '=' {EQ}
156
| '(' {LPAR}
157
| ')' {RPAR}
158
| '[' {LBRACKET}
159
| ']' {RBRACKET}
160
| '{' {LCUR}
161
| '}' {RCUR}
162
| ';' {SCOL}
163
| ':' {COL}
164
| ',' {COMMA}
165
| '=' {EQ}
166
| "&&" {AMPERAMPER}
167
| "||" {BARBAR}
168
| "::" {COLCOL}
169
| "^" {POWER}
170
| '"' {QUOTE}
171 19a1e66b ploc
| '.' {POINT}
172 22fe1c93 ploc
| eof { EOF }
173 f3d244c1 xthirioux
| _ { raise (Parse.Error (Location.curr lexbuf, Parse.Undefined_token (Lexing.lexeme lexbuf))) }
174 22fe1c93 ploc
175
and comment n = parse
176
| eof
177 f3d244c1 xthirioux
    { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_comment)) }
178 22fe1c93 ploc
| "(*"
179
    { comment (n+1) lexbuf }
180
| "*)"
181
    { if n > 0 then comment (n-1) lexbuf else token lexbuf }
182
| newline
183
    { incr_line lexbuf;
184
      comment n lexbuf }
185
| _ { comment n lexbuf }
186
187
and annot_singleline = parse
188 04a63d25 xthirioux
  | eof { make_annot lexbuf (Buffer.contents buf) }
189 22fe1c93 ploc
  | newline { incr_line lexbuf; make_annot lexbuf (Buffer.contents buf) }
190
  | _ as c { Buffer.add_char buf c; annot_singleline lexbuf }
191
192
and annot_multiline n = parse
193 f3d244c1 xthirioux
  | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_annot)) }
194 57392da1 Christophe Garion
  | "*)" as s { 
195
    if n > 0 then 
196
      (Buffer.add_string buf s; annot_multiline (n-1) lexbuf) 
197
    else 
198 22fe1c93 ploc
      make_annot lexbuf (Buffer.contents buf) }
199
  | "(*" as s { Buffer.add_string buf s; annot_multiline (n+1) lexbuf }
200
  | newline as s { incr_line lexbuf; Buffer.add_string buf s; annot_multiline n lexbuf }
201
  | _ as c { Buffer.add_char buf c; annot_multiline n lexbuf }
202
203
and spec_singleline = parse
204 04a63d25 xthirioux
  | eof { make_spec lexbuf (Buffer.contents buf) }
205 22fe1c93 ploc
  | newline { incr_line lexbuf; make_spec lexbuf (Buffer.contents buf) }
206
  | _ as c { Buffer.add_char buf c; spec_singleline lexbuf }
207
208
and spec_multiline n = parse
209 f3d244c1 xthirioux
  | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_node_spec)) }
210 57392da1 Christophe Garion
  | "*)" as s { if n > 0 then 
211
      (Buffer.add_string buf s; spec_multiline (n-1) lexbuf) 
212
    else 
213 22fe1c93 ploc
      make_spec lexbuf (Buffer.contents buf) }
214
  | "(*" as s { Buffer.add_string buf s; spec_multiline (n+1) lexbuf }
215
  | newline as s { incr_line lexbuf; Buffer.add_string buf s; spec_multiline n lexbuf }
216
  | _ as c { Buffer.add_char buf c; spec_multiline n lexbuf }