Project

General

Profile

Download (5.9 KB) Statistics
| Branch: | Tag: | Revision:
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
  "type", TYPE;
45
  "int", TINT;
46
  "bool", TBOOL;
47
  (* "float", TFLOAT; *)
48
  "real", TREAL;
49
  "clock", TCLOCK;
50
  "not", NOT;
51
  "true", TRUE;
52
  "false", FALSE;
53
  "and", AND;
54
  "or", OR;
55
  "xor", XOR;
56
  "mod", MOD;
57
  "pre", PRE;
58
  "div", DIV;
59
  "const", CONST;
60
  "assert", ASSERT;
61
  "contract", CONTRACT;
62
  "lib", LIB;
63
  "prototype", PROTOTYPE;
64
  "ensure", ENSURE;
65
  "require", REQUIRE;
66
  (* "observer", OBSERVER; *)
67
  "invariant", INVARIANT;
68
  "mode", MODE;
69
  "assume", ASSUME;
70
  "contract", CONTRACT;
71
  "guarantees", GUARANTEES;
72
  "exists", EXISTS;
73
  "forall", FORALL;
74
 
75
  "c_code", CCODE; (* not sure how it is used *)
76
  "matlab", MATLAB; (* same as above *)
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 LexerLustreSpec.Error loc -> raise (Parse.Error (Location.shift (Location.curr lexbuf) loc, Parse.Annot_error s))
88

    
89
let make_spec lexbuf s = 
90
  try
91
    let ns = LexerLustreSpec.spec s in
92
    NODESPEC ns
93
  with LexerLustreSpec.Error loc -> raise (Parse.Error (Location.shift (Location.curr lexbuf) loc, Parse.Node_spec_error s))
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']+ as l)  '.' (['0'-'9']* as r) ('E'|'e') (('+'|'-')? ['0'-'9']+ as exp)) as s
121
    {REAL (Num.num_of_string (l^r), String.length r + -1 * int_of_string exp , s)}
122
| ((['0'-'9']+ as l) '.' (['0'-'9']* as r)) as s
123
    {REAL (Num.num_of_string (l^r), String.length r, s)}
124
| ['0'-'9']+ 
125
    {INT (int_of_string (Lexing.lexeme lexbuf)) }
126
| "tel." {TEL}
127
| "tel;" {TEL}
128
| "#open" { OPEN }
129
| "include" { INCLUDE }
130
| ['_' 'a'-'z'] [ '_' 'a'-'z' 'A'-'Z' '0'-'9']*
131
    {let s = Lexing.lexeme lexbuf in
132
    try
133
      Hashtbl.find keyword_table s
134
    with Not_found ->
135
      IDENT s}
136
| ['A'-'Z'] [ '_' 'a'-'z' 'A'-'Z' '0'-'9']*
137
    {let s = Lexing.lexeme lexbuf in
138
    try
139
      Hashtbl.find keyword_table s
140
    with Not_found ->
141
      UIDENT s}     
142
| "->" {ARROW}
143
| "=>" {IMPL}
144
| "<=" {LTE}
145
| ">=" {GTE}
146
| "<>" {NEQ}
147
| '<' {LT}
148
| '>' {GT}
149
| "!=" {NEQ}
150
| '-' {MINUS}
151
| '+' {PLUS}
152
| '/' {DIV}
153
| '*' {MULT}
154
| '=' {EQ}
155
| '(' {LPAR}
156
| ')' {RPAR}
157
| '[' {LBRACKET}
158
| ']' {RBRACKET}
159
| '{' {LCUR}
160
| '}' {RCUR}
161
| ';' {SCOL}
162
| ':' {COL}
163
| ',' {COMMA}
164
| '=' {EQ}
165
| "&&" {AMPERAMPER}
166
| "||" {BARBAR}
167
| "::" {COLCOL}
168
| "^" {POWER}
169
| '"' {QUOTE}
170
| '.' {POINT}
171
| eof { EOF }
172
| _ { raise (Parse.Error (Location.curr lexbuf, Parse.Undefined_token (Lexing.lexeme lexbuf))) }
173

    
174
and comment n = parse
175
| eof
176
    { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_comment)) }
177
| "(*"
178
    { comment (n+1) lexbuf }
179
| "*)"
180
    { if n > 0 then comment (n-1) lexbuf else token lexbuf }
181
| newline
182
    { incr_line lexbuf;
183
      comment n lexbuf }
184
| _ { comment n lexbuf }
185

    
186
and annot_singleline = parse
187
  | eof { make_annot lexbuf (Buffer.contents buf) }
188
  | newline { incr_line lexbuf; make_annot lexbuf (Buffer.contents buf) }
189
  | _ as c { Buffer.add_char buf c; annot_singleline lexbuf }
190

    
191
and annot_multiline n = parse
192
  | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_annot)) }
193
  | "*)" as s { 
194
    if n > 0 then 
195
      (Buffer.add_string buf s; annot_multiline (n-1) lexbuf) 
196
    else 
197
      make_annot lexbuf (Buffer.contents buf) }
198
  | "(*" as s { Buffer.add_string buf s; annot_multiline (n+1) lexbuf }
199
  | newline as s { incr_line lexbuf; Buffer.add_string buf s; annot_multiline n lexbuf }
200
  | _ as c { Buffer.add_char buf c; annot_multiline n lexbuf }
201

    
202
and spec_singleline = parse
203
  | eof { make_spec lexbuf (Buffer.contents buf) }
204
  | newline { incr_line lexbuf; make_spec lexbuf (Buffer.contents buf) }
205
  | _ as c { Buffer.add_char buf c; spec_singleline lexbuf }
206

    
207
and spec_multiline n = parse
208
  | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_node_spec)) }
209
  | "*)" as s { if n > 0 then 
210
      (Buffer.add_string buf s; spec_multiline (n-1) lexbuf) 
211
    else 
212
      make_spec lexbuf (Buffer.contents buf) }
213
  | "(*" as s { Buffer.add_string buf s; spec_multiline (n+1) lexbuf }
214
  | newline as s { incr_line lexbuf; Buffer.add_string buf s; spec_multiline n lexbuf }
215
  | _ as c { Buffer.add_char buf c; spec_multiline n lexbuf }
216

    
(3-3/7)