Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / lexerLustreSpec.mll @ 22fe1c93

History | View | Annotate | Download (3.33 KB)

1
{
2

    
3
  open ParserLustreSpec
4
  open Utils
5

    
6
  let str_buf = Buffer.create 1024
7

    
8
  exception Error of Location.t
9

    
10
(* As advised by Caml documentation. This way a single lexer rule is
11
   used to handle all the possible keywords. *)
12
let keyword_table =
13
  create_hashtable 20 [
14
  "true", TRUE;
15
  "false", FALSE;
16
  "stateless", STATELESS;
17
  "if", IF;
18
  "then", THEN;
19
  "else", ELSE;
20
  "merge", MERGE;
21
  "arrow", ARROW;
22
  "fby", FBY;
23
  "when", WHEN;
24
  "whennot", WHENNOT;
25
  "every", EVERY;
26
  "node", NODE;
27
  "let", LET;
28
  "tel", TEL;
29
  "returns", RETURNS;
30
  "var", VAR;
31
  "imported", IMPORTED;
32
  "wcet", WCET;
33
  "int", TINT;
34
  "bool", TBOOL;
35
  "float", TFLOAT;
36
  "real", TREAL;
37
  "clock", TCLOCK;
38
  "not", NOT;
39
  "tail", TAIL;
40
  "and", AND;
41
  "or", OR;
42
  "xor", OR;
43
  "mod", MOD;
44
  "pre", PRE;
45
  "div", DIV;
46
  "const", CONST;
47
  "include", INCLUDE;
48
  "assert", ASSERT;
49
  "ensures", ENSURES;
50
  "requires", REQUIRES;
51
  "observer", OBSERVER;
52
  "invariant", INVARIANT;
53
  "behavior", BEHAVIOR;
54
  "assumes", ASSUMES;
55
  "exists", EXISTS;
56
  "forall", FORALL;
57
  ]
58

    
59
}
60

    
61

    
62
let newline = ('\010' | '\013' | "\013\010")
63
let notnewline = [^ '\010' '\013']
64
let blank = [' ' '\009' '\012']
65

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

    
134
{
135

    
136
  let annot s =
137
    let lb = Lexing.from_string s in
138
   try
139
     ParserLustreSpec.lustre_annot token lb
140
   with Parsing.Parse_error as _e -> (
141
     Format.eprintf "Lexing error at position %a:@.unexpected token %s@.@?"
142
       (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
143
       (Lexing.lexeme lb);
144
     raise Parsing.Parse_error)
145
     
146

    
147
  let spec s =
148
    let lb = Lexing.from_string s in
149
    ParserLustreSpec.lustre_spec token lb
150

    
151
}