Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / parsers / lexer_lustre.mll @ f4cba4b8

History | View | Annotate | Download (5.92 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
(* 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
  "import", IMPORT;
45
  "type", TYPE;
46
  "int", TINT;
47
  "bool", TBOOL;
48
  (* "float", TFLOAT; *)
49
  "real", TREAL;
50
  "clock", TCLOCK;
51
  "not", NOT;
52
  "true", TRUE;
53
  "false", FALSE;
54
  "and", AND;
55
  "or", OR;
56
  "xor", XOR;
57
  "mod", MOD;
58
  "pre", PRE;
59
  "div", DIV;
60
  "const", CONST;
61
  "assert", ASSERT;
62
  "contract", CONTRACT;
63
  "lib", LIB;
64
  "prototype", PROTOTYPE;
65
  "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
  "c_code", CCODE; (* not sure how it is used *)
77
  "matlab", MATLAB; (* same as above *)
78
]
79

    
80

    
81
(* Buffer for parsing specification/annotation *)
82
let buf = Buffer.create 1024
83

    
84
let make_annot lexbuf s = 
85
  try
86
    let ann = LexerLustreSpec.annot s in
87
    ANNOT ann
88
  with LexerLustreSpec.Error loc -> raise (Parse.Error (Location.shift (Location.curr lexbuf) loc, Parse.Annot_error s))
89

    
90
let make_spec lexbuf s = 
91
  try
92
    let ns = LexerLustreSpec.spec s in
93
    NODESPEC ns
94
  with LexerLustreSpec.Error loc -> raise (Parse.Error (Location.shift (Location.curr lexbuf) loc, Parse.Node_spec_error s))
95

    
96
}
97

    
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
| "(*@" { Buffer.clear buf; 
106
	  spec_multiline 0 lexbuf }
107
| "--!" { Buffer.clear buf; 
108
	  annot_singleline lexbuf }
109
| "(*!" { Buffer.clear buf; 
110
	  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
| ((['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
| ['0'-'9']+ 
126
    {INT (int_of_string (Lexing.lexeme lexbuf)) }
127
| "tel." {TEL}
128
| "tel;" {TEL}
129
| "#open" { OPEN }
130
| "include" { INCLUDE }
131
| ['_' 'a'-'z'] [ '_' 'a'-'z' 'A'-'Z' '0'-'9']*
132
    {let s = Lexing.lexeme lexbuf in
133
    try
134
      Hashtbl.find keyword_table s
135
    with Not_found ->
136
      IDENT s}
137
| ['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
      UIDENT s}     
143
| "->" {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
| '.' {POINT}
172
| eof { EOF }
173
| _ { raise (Parse.Error (Location.curr lexbuf, Parse.Undefined_token (Lexing.lexeme lexbuf))) }
174

    
175
and comment n = parse
176
| eof
177
    { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_comment)) }
178
| "(*"
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
  | eof { make_annot lexbuf (Buffer.contents buf) }
189
  | 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
  | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_annot)) }
194
  | "*)" as s { 
195
    if n > 0 then 
196
      (Buffer.add_string buf s; annot_multiline (n-1) lexbuf) 
197
    else 
198
      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
  | eof { make_spec lexbuf (Buffer.contents buf) }
205
  | 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
  | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_node_spec)) }
210
  | "*)" as s { if n > 0 then 
211
      (Buffer.add_string buf s; spec_multiline (n-1) lexbuf) 
212
    else 
213
      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 }
217