Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / lexer_lustre.mll @ bc504848

History | View | Annotate | Download (6.1 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 _ = 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
    NODESPEC dummy_ns
94
  with exn -> ((*Printexc.print_backtrace stderr; *) raise exn)
95

    
96
let make_spec = make_kind_spec
97
}
98

    
99
let newline = ('\010' | '\013' | "\013\010")
100
let notnewline = [^ '\010' '\013']
101
let blank = [' ' '\009' '\012']
102

    
103
rule token = parse
104
| "--@" { Buffer.clear buf;
105
	  spec_singleline lexbuf }
106
| "(*@" { Buffer.clear buf; 
107
	  spec_multiline 0 lexbuf }
108
| "--!" { Buffer.clear buf; 
109
	  annot_singleline lexbuf }
110
| "(*!" { Buffer.clear buf; 
111
	  annot_multiline 0 lexbuf }
112
| "(*"
113
    { comment 0 lexbuf }
114
| "--" [^ '!' '@'] notnewline* (newline|eof)
115
    { incr_line lexbuf;
116
      token lexbuf }
117
| newline
118
    { incr_line lexbuf;
119
      token lexbuf }
120
| blank +
121
    {token lexbuf}
122
| ((['0'-'9']+ as l)  '.' (['0'-'9']* as r) ('E'|'e') (('+'|'-')? ['0'-'9']+ as exp)) as s
123
    {REAL (Num.num_of_string (l^r), String.length r + -1 * int_of_string exp , s)}
124
| ((['0'-'9']+ as l) '.' (['0'-'9']* as r)) as s
125
    {REAL (Num.num_of_string (l^r), String.length r, s)}
126
| ['0'-'9']+ 
127
    {INT (int_of_string (Lexing.lexeme lexbuf)) }
128
| "tel." {TEL}
129
| "tel;" {TEL}
130
| "#open" { OPEN }
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
| '/' {DIV}
167
| "&&" {AMPERAMPER}
168
| "||" {BARBAR}
169
| "::" {COLCOL}
170
| "^" {POWER}
171
| '"' {QUOTE}
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