Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / lexer_lustre.mll @ eccb3f63

History | View | Annotate | Download (5.98 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
  "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
  (* "float", TFLOAT; *)
50
  "real", TREAL;
51
  "clock", TCLOCK;
52
  "not", NOT;
53
  "tail", TAIL;
54
  "true", TRUE;
55
  "false", FALSE;
56
  "and", AND;
57
  "or", OR;
58
  "xor", XOR;
59
  "mod", MOD;
60
  "pre", PRE;
61
  "div", DIV;
62
  "const", CONST;
63
  "assert", ASSERT;
64
  "lib", LIB;
65
  "prototype", PROTOTYPE;
66
  "c_code", CCODE; (* not sure how it is used *)
67
  "matlab", MATLAB; (* same as above *)
68
]
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
  with LexerLustreSpec.Error loc -> raise (Parse.Error (Location.shift (Location.curr lexbuf) loc, Parse.Annot_error s))
79

    
80
let make_spec lexbuf s = 
81
  try
82
    let ns = LexerLustreSpec.spec s in
83
    NODESPEC ns
84
  with LexerLustreSpec.Error loc -> raise (Parse.Error (Location.shift (Location.curr lexbuf) loc, Parse.Node_spec_error s))
85

    
86

    
87
let make_kind_spec lexbuf s =
88
    let s_lexbuf = Lexing.from_string s in
89
    let _ = KindLustreParser.contract_in_block KindLustreLexer.token s_lexbuf in
90
    let dummy_ns = { Lustre_types.requires = []; ensures = []; behaviors = []; spec_loc = Location.dummy_loc} in
91
    NODESPEC dummy_ns
92

    
93
(*let make_spec = make_kind_spec*)
94
}
95

    
96
let newline = ('\010' | '\013' | "\013\010")
97
let notnewline = [^ '\010' '\013']
98
let blank = [' ' '\009' '\012']
99

    
100
rule token = parse
101
| "--@" { Buffer.clear buf;
102
	  spec_singleline lexbuf }
103
| "(*@" { Buffer.clear buf; 
104
	  spec_multiline 0 lexbuf }
105
| "--!" { Buffer.clear buf; 
106
	  annot_singleline lexbuf }
107
| "(*!" { Buffer.clear buf; 
108
	  annot_multiline 0 lexbuf }
109
| "(*"
110
    { comment 0 lexbuf }
111
| "--" [^ '!' '@'] notnewline* (newline|eof)
112
    { incr_line lexbuf;
113
      token lexbuf }
114
| newline
115
    { incr_line lexbuf;
116
      token lexbuf }
117
| blank +
118
    {token lexbuf}
119
| ((['0'-'9']+ as l)  '.' (['0'-'9']* as r) ('E'|'e') (('+'|'-')? ['0'-'9']+ as exp)) as s
120
    {REAL (Num.num_of_string (l^r), String.length r + -1 * int_of_string exp , s)}
121
| ((['0'-'9']+ as l) '.' (['0'-'9']* as r)) as s
122
    {REAL (Num.num_of_string (l^r), String.length r, s)}
123
| ['0'-'9']+ 
124
    {INT (int_of_string (Lexing.lexeme lexbuf)) }
125
| "tel." {TEL}
126
| "tel;" {TEL}
127
| "#open" { OPEN }
128
| ['_' '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
| ['A'-'Z'] [ '_' 'a'-'z' 'A'-'Z' '0'-'9']*
135
    {let s = Lexing.lexeme lexbuf in
136
    try
137
      Hashtbl.find keyword_table s
138
    with Not_found ->
139
      UIDENT s}
140
| "->" {ARROW}
141
| "=>" {IMPL}
142
| "<=" {LTE}
143
| ">=" {GTE}
144
| "<>" {NEQ}
145
| '<' {LT}
146
| '>' {GT}
147
| "!=" {NEQ}
148
| '-' {MINUS}
149
| '+' {PLUS}
150
| '/' {DIV}
151
| '*' {MULT}
152
| '=' {EQ}
153
| '(' {LPAR}
154
| ')' {RPAR}
155
| '[' {LBRACKET}
156
| ']' {RBRACKET}
157
| '{' {LCUR}
158
| '}' {RCUR}
159
| ';' {SCOL}
160
| ':' {COL}
161
| ',' {COMMA}
162
| '=' {EQ}
163
| '/' {DIV}
164
| "&&" {AMPERAMPER}
165
| "||" {BARBAR}
166
| "::" {COLCOL}
167
| "^" {POWER}
168
| '"' {QUOTE}
169
| eof { EOF }
170
| _ { raise (Parse.Error (Location.curr lexbuf, Parse.Undefined_token (Lexing.lexeme lexbuf))) }
171

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

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

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

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

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