Project

General

Profile

Download (5.58 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
  "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
]
67

    
68

    
69
(* Buffer for parsing specification/annotation *)
70
let buf = Buffer.create 1024
71

    
72
let make_annot lexbuf s = 
73
  try
74
    let ann = LexerLustreSpec.annot s in
75
    ANNOT ann
76
  with LexerLustreSpec.Error loc -> raise (Parse.Error (Location.shift (Location.curr lexbuf) loc, Parse.Annot_error s))
77

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

    
86
let newline = ('\010' | '\013' | "\013\010")
87
let notnewline = [^ '\010' '\013']
88
let blank = [' ' '\009' '\012']
89

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

    
162
and comment n = parse
163
| eof
164
    { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_comment)) }
165
| "(*"
166
    { comment (n+1) lexbuf }
167
| "*)"
168
    { if n > 0 then comment (n-1) lexbuf else token lexbuf }
169
| newline
170
    { incr_line lexbuf;
171
      comment n lexbuf }
172
| _ { comment n lexbuf }
173

    
174
and annot_singleline = parse
175
  | eof { make_annot lexbuf (Buffer.contents buf) }
176
  | newline { incr_line lexbuf; make_annot lexbuf (Buffer.contents buf) }
177
  | _ as c { Buffer.add_char buf c; annot_singleline lexbuf }
178

    
179
and annot_multiline n = parse
180
  | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_annot)) }
181
  | "*)" as s { 
182
    if n > 0 then 
183
      (Buffer.add_string buf s; annot_multiline (n-1) lexbuf) 
184
    else 
185
      make_annot lexbuf (Buffer.contents buf) }
186
  | "(*" as s { Buffer.add_string buf s; annot_multiline (n+1) lexbuf }
187
  | newline as s { incr_line lexbuf; Buffer.add_string buf s; annot_multiline n lexbuf }
188
  | _ as c { Buffer.add_char buf c; annot_multiline n lexbuf }
189

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

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

    
(24-24/53)