Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / lexer_lustre.mll @ 52cfee34

History | View | Annotate | Download (5.22 KB)

1
(* ----------------------------------------------------------------------------
2
 * SchedMCore - A MultiCore Scheduling Framework
3
 * Copyright (C) 2009-2011, ONERA, Toulouse, FRANCE - LIFL, Lille, FRANCE
4
 *
5
 * This file is part of Prelude
6
 *
7
 * Prelude is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public License
9
 * as published by the Free Software Foundation ; either version 2 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * Prelude is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY ; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this program ; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
 * USA
21
 *---------------------------------------------------------------------------- *)
22

    
23
{
24
open Parser_lustre
25
open Utils
26

    
27
exception Error of Location.t
28

    
29
(* As advised by Caml documentation. This way a single lexer rule is
30
   used to handle all the possible keywords. *)
31
let keyword_table =
32
  create_hashtable 20 [
33
  "function", FUNCTION;
34
  "struct", STRUCT;
35
  "enum", ENUM;
36
  "automaton", AUTOMATON;
37
  "state", STATE;
38
  "until", UNTIL;
39
  "unless", UNLESS;
40
  "last", LAST;
41
  "resume", RESUME;
42
  "restart", RESTART;
43
  "if", IF;
44
  "then", THEN;
45
  "else", ELSE;
46
  "merge", MERGE;
47
  "arrow", ARROW;
48
  "fby", FBY;
49
  "when", WHEN;
50
  "whenot", WHENNOT;
51
  "every", EVERY;
52
  "node", NODE;
53
  "let", LET;
54
  "tel", TEL;
55
  "returns", RETURNS;
56
  "var", VAR;
57
  "imported", IMPORTED;
58
  "wcet", WCET;
59
  "type", TYPE;
60
  "int", TINT;
61
  "bool", TBOOL;
62
  "float", TFLOAT;
63
  "real", TREAL;
64
  "clock", TCLOCK;
65
  "not", NOT;
66
  "tail", TAIL;
67
  "and", AND;
68
  "or", OR;
69
  "xor", XOR;
70
  "mod", MOD;
71
  "pre", PRE;
72
  "div", DIV;
73
  "const", CONST;
74
  "assert", ASSERT;
75
]
76

    
77

    
78
(* Buffer for parsing specification/annotation *)
79
let buf = Buffer.create 1024
80

    
81
let make_annot lexbuf s = 
82
  try
83
    let ann = LexerLustreSpec.annot s in
84
    ANNOT ann
85
  with _ -> (Format.eprintf "Impossible to parse the following annotation:@.%s@.@?" s; exit 1)
86

    
87
let make_spec lexbuf s = 
88
  try
89
    let ns = LexerLustreSpec.spec s in
90
    NODESPEC ns
91
  with _ -> (Format.eprintf "Impossible to parse the following node specification:@.%s@.@?" s; exit 1)
92
   
93
}
94

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

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

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

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

    
180
and annot_multiline n = parse
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
  | 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
  | "*)" as s { if n > 0 then 
196
      (Buffer.add_string buf s; spec_multiline (n-1) lexbuf) 
197
    else 
198
      make_spec lexbuf (Buffer.contents buf) }
199
  | "(*" as s { Buffer.add_string buf s; spec_multiline (n+1) lexbuf }
200
  | newline as s { incr_line lexbuf; Buffer.add_string buf s; spec_multiline n lexbuf }
201
  | _ as c { Buffer.add_char buf c; spec_multiline n lexbuf }
202