Project

General

Profile

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

    
146
{
147

    
148
  let annot s =
149
    let lb = Lexing.from_string s in
150
   try
151
     Parser_lustre.lustre_annot(* ParserLustreSpec.lustre_annot *) token lb
152
   with Parsing.Parse_error as _e -> (
153
     Format.eprintf "Lexing error at position %a:@.unexpected token %s@.@?"
154
       (fun fmt p -> Format.fprintf fmt "%s l%i c%i" p.Lexing.pos_fname p.Lexing.pos_lnum p.Lexing.pos_cnum) lb.Lexing.lex_curr_p
155
       (Lexing.lexeme lb);
156
     raise Parsing.Parse_error)
157
     
158

    
159
  let spec s =
160
    let lb = Lexing.from_string s in
161
    Parser_lustre.lustre_spec (*ParserLustreSpec.lustre_spec*) token lb
162

    
163
}
(19-19/45)