Project

General

Profile

Download (4.37 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
  type error =
21
    | Undefined_token of string
22
    | Unfinished_string
23
    | Unfinished_comment
24

    
25
  exception Error of Location.t * error
26

    
27
let pp_error fmt =
28
  let open Format in
29
  function
30
  | Undefined_token s ->
31
    fprintf fmt "undefined token '%s'" s
32
  | Unfinished_comment ->
33
    fprintf fmt "unfinished comment"
34
  | Unfinished_string ->
35
    fprintf fmt "unfinished string"
36

    
37
let error lexbuf err =
38
  raise (Error (Location.curr lexbuf, err))
39

    
40
let newline token lexbuf =
41
  Location.Lex.newline lexbuf;
42
  token lexbuf
43

    
44
(* As advised by Caml documentation. This way a single lexer rule is
45
   used to handle all the possible keywords. *)
46
let keyword_table =
47
  create_hashtable 20 [
48
  (* "true", TRUE; *)
49
  (* "false", FALSE; *)
50
  "function", FUNCTION;
51
  "if", IF;
52
  "then", THEN;
53
  "else", ELSE;
54
  "merge", MERGE;
55
  "arrow", ARROW;
56
  "fby", FBY;
57
  "when", WHEN;
58
  "whennot", WHENNOT;
59
  "every", EVERY;
60
  "node", NODE;
61
  "let", LET;
62
  "tel", TEL;
63
  "returns", RETURNS;
64
  "var", VAR;
65
  "import", IMPORT;
66
  (* "imported", IMPORTED; *)
67
  "int", TINT;
68
  "bool", TBOOL;
69
  (* "float", TFLOAT; *)
70
  "real", TREAL;
71
  "clock", TCLOCK;
72
  "not", NOT;
73
  "and", AND;
74
  "or", OR;
75
  "xor", OR;
76
  "mod", MOD;
77
  "pre", PRE;
78
  "div", DIV;
79
  "const", CONST;
80
  (* "include", INCLUDE; *)
81
  "assert", ASSERT;
82
   "ensure", ENSURE;
83
  "require", REQUIRE;
84
  (* "observer", OBSERVER; *)
85
  "invariant", INVARIANT;
86
  "mode", MODE;
87
  "assume", ASSUME;
88
  "contract", CONTRACT;
89
  "guarantee", GUARANTEES;
90
  "exists", EXISTS;
91
  "forall", FORALL;
92
  "c_code", CCODE;
93
  "matlab", MATLAB;
94
  ]
95

    
96
}
97

    
98

    
99
let newline = ('\010' | '\013' | "\013\010")
100
let notnewline = [^ '\010' '\013']
101
let blank = [' ' '\009' '\012']
102

    
103
rule token = parse
104
  | "(*"                           { comment_line 0 lexbuf }
105
  | "--" notnewline* (newline|eof) { newline token lexbuf }
106
  | newline                        { newline token lexbuf }
107
  | blank +                        { token lexbuf }
108
  | (('-'? ['0'-'9'] ['0'-'9']* as l) '.' (['0'-'9']* as r)) as s
109
    {REAL (Real.create (l^r) (String.length r) s)}
110
  | (('-'? ['0'-'9']+ as l)  '.' (['0'-'9']+ as r) ('E'|'e') (('+'|'-') ['0'-'9'] ['0'-'9']* as exp)) as s
111
    {REAL (Real.create (l^r) (String.length r + -1 * int_of_string exp) s)}
112
  | '-'? ['0'-'9']+ 
113
    {INT (int_of_string (Lexing.lexeme lexbuf)) }
114
  (* | '/' (['_' 'A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '_' '0'-'9']* '/')+ as s
115
       {IDENT s}
116
  *)
117
  | ['_' 'A'-'Z' '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
  | "->" {ARROW}
124
  | "=>" {IMPL}
125
  | "<=" {LTE}
126
  | ">=" {GTE}
127
  | "<>" {NEQ}
128
  | '<' {LT}
129
  | '>' {GT}
130
  | "!=" {NEQ}
131
  | '-' {MINUS}
132
  | '+' {PLUS}
133
  | '/' {DIV}
134
  | '*' {MULT}
135
  | '=' {EQ}
136
  | '(' {LPAR}
137
  | ')' {RPAR}
138
  | '[' {LBRACKET}
139
  | ']' {RBRACKET}
140
  | ';' {SCOL}
141
  | ':' {COL}
142
  | ',' {COMMA}
143
  | '=' {EQ}
144
  | '/' {DIV}
145
  | "&&" {AMPERAMPER}
146
  | "||" {BARBAR}
147
  | "::" {COLCOL}
148
  | "^" {POWER}
149
  | '"' { Buffer.clear str_buf; string_parse lexbuf }
150
  | eof { EOF }
151
  | _   { error lexbuf (Undefined_token (Lexing.lexeme lexbuf)) }
152

    
153
and comment_line n = parse
154
  | eof     { error lexbuf Unfinished_comment }
155
  | "(*"    { comment_line (n+1) lexbuf }
156
  | "*)"    { if n > 0 then comment_line (n-1) lexbuf else token lexbuf }
157
  | newline { newline (comment_line n) lexbuf }
158
  | _       { comment_line n lexbuf }
159

    
160
and string_parse = parse
161
  | eof         { error lexbuf Unfinished_string }
162
  | "\\\"" as s { Buffer.add_string str_buf s; string_parse lexbuf}
163
  | '"'         { STRING (Buffer.contents str_buf) }
164
  | _ as c      { Buffer.add_char str_buf c; string_parse lexbuf }
(2-2/8)