Project

General

Profile

Download (4.5 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
  exception Error of Location.t
19
 
20
  let str_buf = Buffer.create 1024
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
  "stateless", STATELESS;
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
  "imported", IMPORTED;
44
  "wcet", WCET;
45
  "int", TINT;
46
  "bool", TBOOL;
47
  (* "float", TFLOAT; *)
48
  "real", TREAL;
49
  "clock", TCLOCK;
50
  "not", NOT;
51
  "tail", TAIL;
52
  "and", AND;
53
  "or", OR;
54
  "xor", OR;
55
  "mod", MOD;
56
  "pre", PRE;
57
  "div", DIV;
58
  "const", CONST;
59
  (* "include", INCLUDE; *)
60
  "assert", ASSERT;
61
  "ensures", ENSURES;
62
  "requires", REQUIRES;
63
  "observer", OBSERVER;
64
  "invariant", INVARIANT;
65
  "behavior", BEHAVIOR;
66
  "assumes", ASSUMES;
67
  "exists", EXISTS;
68
  "forall", FORALL;
69
  ]
70

    
71
}
72

    
73

    
74
let newline = ('\010' | '\013' | "\013\010")
75
let notnewline = [^ '\010' '\013']
76
let blank = [' ' '\009' '\012']
77

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

    
148
{
149

    
150
  let annot s =
151
    let lexbuf = Lexing.from_string s in
152
   try
153
     Parser_lustre.lustre_annot(* ParserLustreSpec.lustre_annot *) token lexbuf
154
   with Parsing.Parse_error as _e -> (
155
     Format.eprintf "Lexing error at position %a:@.unexpected token %s@.@?"
156
       (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
157
       (Lexing.lexeme lexbuf);
158
     raise (Error (Location.curr lexbuf)))
159
     
160

    
161
  let spec s =
162
    let lexbuf = Lexing.from_string s in
163
    try
164
      Parser_lustre.lustre_spec (*ParserLustreSpec.lustre_spec*) token lexbuf
165
    with Parsing.Parse_error ->
166
      raise (Error (Location.curr lexbuf))
167
}
(23-23/51)