Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / lexer_lustre.mll @ 31027df4

History | View | Annotate | Download (6.25 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
  try 
89
    let s_lexbuf = Lexing.from_string s in
90
    (*Format.printf "KIND SPEC \"%s\"@." s;*)
91
    let contract = KindLustreParser.contract_in_block_main KindLustreLexer.token s_lexbuf in
92
    let dummy_ns = { Lustre_types.requires = []; ensures = []; behaviors = []; spec_loc = Location.dummy_loc} in
93
    begin
94
      List.iter (fun item ->
95
      		Format.eprintf "CONTRACT: %a@." KindLustreAst.pp_print_contract_item item) contract;
96
      NODESPEC dummy_ns
97
    end
98
  with exn -> ((*Printexc.print_backtrace stderr; *) raise exn)
99

    
100
let make_spec = make_kind_spec
101
}
102

    
103
let newline = ('\010' | '\013' | "\013\010")
104
let notnewline = [^ '\010' '\013']
105
let blank = [' ' '\009' '\012']
106

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

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

    
191
and annot_singleline = parse
192
  | eof { make_annot lexbuf (Buffer.contents buf) }
193
  | newline { incr_line lexbuf; make_annot lexbuf (Buffer.contents buf) }
194
  | _ as c { Buffer.add_char buf c; annot_singleline lexbuf }
195

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

    
207
and spec_singleline = parse
208
  | eof { make_spec lexbuf (Buffer.contents buf) }
209
  | newline { incr_line lexbuf; make_spec lexbuf (Buffer.contents buf) }
210
  | _ as c { Buffer.add_char buf c; spec_singleline lexbuf }
211

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