Project

General

Profile

Download (4.64 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

    
14
  (* open ParserLustreSpec *)
15
  open Parser_lustre
16
  open Utils
17

    
18
  let str_buf = Buffer.create 1024
19

    
20
  exception Error of Location.t
21

    
22
(* As advised by Caml documentation. This way a single lexer rule is
23
   used to handle all the possible keywords. *)
24
let keyword_table =
25
  create_hashtable 20 [
26
  (* "true", TRUE; *)
27
  (* "false", FALSE; *)
28
  "function", FUNCTION;
29
  "if", IF;
30
  "then", THEN;
31
  "else", ELSE;
32
  "merge", MERGE;
33
  "arrow", ARROW;
34
  "fby", FBY;
35
  "when", WHEN;
36
  "whennot", WHENNOT;
37
  "every", EVERY;
38
  "node", NODE;
39
  "let", LET;
40
  "tel", TEL;
41
  "returns", RETURNS;
42
  "var", VAR;
43
  "import", IMPORT;
44
  (* "imported", IMPORTED; *)
45
  "int", TINT;
46
  "bool", TBOOL;
47
  (* "float", TFLOAT; *)
48
  "real", TREAL;
49
  "clock", TCLOCK;
50
  "not", NOT;
51
  "and", AND;
52
  "or", OR;
53
  "xor", OR;
54
  "mod", MOD;
55
  "pre", PRE;
56
  "div", DIV;
57
  "const", CONST;
58
  (* "include", INCLUDE; *)
59
  "assert", ASSERT;
60
   "ensure", ENSURE;
61
  "require", REQUIRE;
62
  (* "observer", OBSERVER; *)
63
  "invariant", INVARIANT;
64
  "mode", MODE;
65
  "assume", ASSUME;
66
  "contract", CONTRACT;
67
  "guarantee", GUARANTEES;
68
  "exists", EXISTS;
69
  "forall", FORALL;
70
  "c_code", CCODE;
71
  "matlab", MATLAB;
72
  ]
73

    
74
}
75

    
76

    
77
let newline = ('\010' | '\013' | "\013\010")
78
let notnewline = [^ '\010' '\013']
79
let blank = [' ' '\009' '\012']
80

    
81
rule token = parse
82
  | "(*"
83
      { comment_line 0 lexbuf }
84
  | "--" notnewline* (newline|eof)
85
      { incr_line lexbuf;
86
      token lexbuf }
87
  | newline
88
      { incr_line lexbuf;
89
	token lexbuf }
90
  | blank +
91
      {token lexbuf}
92
  | (('-'? ['0'-'9'] ['0'-'9']* as l) '.' (['0'-'9']* as r)) as s
93
      {REAL (Real.create (l^r) (String.length r) s)}
94
  | (('-'? ['0'-'9']+ as l)  '.' (['0'-'9']+ as r) ('E'|'e') (('+'|'-') ['0'-'9'] ['0'-'9']* as exp)) as s
95
      {REAL (Real.create (l^r) (String.length r + -1 * int_of_string exp) s)}
96
  | '-'? ['0'-'9']+ 
97
      {INT (int_of_string (Lexing.lexeme lexbuf)) }
98
 (* | '/' (['_' 'A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '_' '0'-'9']* '/')+ as s
99
      {IDENT s}
100
 *)
101
  | ['_' 'A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '_' '0'-'9']*
102
      {let s = Lexing.lexeme lexbuf in
103
       try
104
	 Hashtbl.find keyword_table s
105
       with Not_found ->
106
	 IDENT s}
107
  | "->" {ARROW}
108
  | "=>" {IMPL}
109
  | "<=" {LTE}
110
  | ">=" {GTE}
111
  | "<>" {NEQ}
112
  | '<' {LT}
113
  | '>' {GT}
114
  | "!=" {NEQ}
115
  | '-' {MINUS}
116
  | '+' {PLUS}
117
  | '/' {DIV}
118
  | '*' {MULT}
119
  | '=' {EQ}
120
  | '(' {LPAR}
121
  | ')' {RPAR}
122
  | '[' {LBRACKET}
123
  | ']' {RBRACKET}
124
  | ';' {SCOL}
125
  | ':' {COL}
126
  | ',' {COMMA}
127
  | '=' {EQ}
128
  | '/' {DIV}
129
  | "&&" {AMPERAMPER}
130
  | "||" {BARBAR}
131
  | "::" {COLCOL}
132
  | "^" {POWER}
133
  | '"' { Buffer.clear str_buf; string_parse lexbuf }
134
  | eof { EOF }
135
  | _ { raise (Parse.Error (Location.curr lexbuf, Parse.Unexpected_eof)) }
136
and comment_line n = parse
137
| eof
138
    { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_comment)) }
139
| "(*"
140
    { comment_line (n+1) lexbuf }
141
| "*)"
142
    { if n > 0 then comment_line (n-1) lexbuf else token lexbuf }
143
| newline
144
    { incr_line lexbuf;
145
      comment_line n lexbuf }
146
| _ { comment_line n lexbuf }
147
and string_parse = parse
148
  | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_string)) }
149
  | "\\\"" as s { Buffer.add_string str_buf s; string_parse lexbuf}
150
  | '"' { STRING (Buffer.contents str_buf) }
151
  | _ as c  { Buffer.add_char str_buf c; string_parse lexbuf }
152

    
153
{
154

    
155
  let annot s =
156
    let lexbuf = Lexing.from_string s in
157
   try
158
     Parser_lustre.lustre_annot(* ParserLustreSpec.lustre_annot *) token lexbuf
159
   with Parsing.Parse_error as _e -> (
160
     Format.eprintf "Lexing error at position %a:@.unexpected token %s when parsing annotation %s@.@?"
161
       (fun fmt p -> Format.fprintf fmt "%s l%i c%i" p.Lexing.pos_fname p.Lexing.pos_lnum p.Lexing.pos_cnum) lexbuf.Lexing.lex_curr_p
162
       (Lexing.lexeme lexbuf) s;
163
     raise (Error (Location.curr lexbuf)))
164
     
165

    
166
  let spec s =
167
    let lexbuf = Lexing.from_string s in
168
    try
169
      Parser_lustre.lustre_spec (*ParserLustreSpec.lustre_spec*) token lexbuf
170
    with Parsing.Parse_error ->
171
      raise (Error (Location.curr lexbuf))
172
}
(2-2/7)