Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / lexer_lustre.mll @ 57392da1

History | View | Annotate | Download (5.62 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
  "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
  "lib", LIB;
62
  "prototype", PROTOTYPE;
63
  "c_code", CCODE; (* not sure how it is used *)
64
  "matlab", MATLAB; (* same as above *)
65
]
66

    
67

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

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

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

    
83
}
84

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

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

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

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

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

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

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