Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / lexerLustreSpec.mll @ 53206908

History | View | Annotate | Download (4.26 KB)

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']* as l) '.' (['0'-'9']* as r)) as s
90
      {REAL (Num.num_of_string (l^r), String.length r, s)}
91
  | '-'? ['0'-'9']+ 
92
      {INT (int_of_string (Lexing.lexeme lexbuf)) }
93
  | (('-'? ['0'-'9']+ as l)  '.' (['0'-'9']+ as r) ('E'|'e') (('+'|'-') ['0'-'9'] ['0'-'9']* as exp)) as s
94
      {REAL (Num.num_of_string (l^r), String.length r + -1 * int_of_string exp, s)}
95

    
96
 (* | '/' (['_' 'A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '_' '0'-'9']* '/')+ as s
97
      {IDENT s}
98
 *)
99
  | ['_' 'A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '_' '0'-'9']*
100
      {let s = Lexing.lexeme lexbuf in
101
       try
102
	 Hashtbl.find keyword_table s
103
       with Not_found ->
104
	 IDENT s}
105
  | "->" {ARROW}
106
  | "=>" {IMPL}
107
  | "<=" {LTE}
108
  | ">=" {GTE}
109
  | "<>" {NEQ}
110
  | '<' {LT}
111
  | '>' {GT}
112
  | "!=" {NEQ}
113
  | '-' {MINUS}
114
  | '+' {PLUS}
115
  | '/' {DIV}
116
  | '*' {MULT}
117
  | '=' {EQ}
118
  | '(' {LPAR}
119
  | ')' {RPAR}
120
  | ';' {SCOL}
121
  | ':' {COL}
122
  | ',' {COMMA}
123
  | '=' {EQ}
124
  | '/' {DIV}
125
  | "&&" {AMPERAMPER}
126
  | "||" {BARBAR}
127
  | "::" {COLCOL}
128
  | "^" {POWER}
129
  | '"' { Buffer.clear str_buf; string_parse lexbuf }
130
  | eof { EOF }
131
  | _ { raise (Error (Location.curr lexbuf)) }
132
and comment_line n = parse
133
| eof
134
    { raise (Error (Location.curr lexbuf)) }
135
| "(*"
136
    { comment_line (n+1) lexbuf }
137
| "*)"
138
    { if n > 0 then comment_line (n-1) lexbuf else token lexbuf }
139
| newline
140
    { incr_line lexbuf;
141
      comment_line n lexbuf }
142
| _ { comment_line n lexbuf }
143
and string_parse = parse
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 lb = Lexing.from_string s in
152
   try
153
     Parser_lustre.lustre_annot(* ParserLustreSpec.lustre_annot *) token lb
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) lb.Lexing.lex_curr_p
157
       (Lexing.lexeme lb);
158
     raise Parsing.Parse_error)
159
     
160

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

    
165
}