Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / parsers / lexerLustreSpec.mll @ f4cba4b8

History | View | Annotate | Download (4.61 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
  "function", FUNCTION;
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
  "import", IMPORT;
44
  "imported", IMPORTED;
45
  "int", TINT;
46
  "bool", TBOOL;
47
  (* "float", TFLOAT; *)
48
  "real", TREAL;
49
  "clock", TCLOCK;
50
  "not", NOT;
51
  "and", AND;
52
  "or", OR;
53
  "xor", OR;
54
  "mod", MOD;
55
  "pre", PRE;
56
  "div", DIV;
57
  "const", CONST;
58
  (* "include", INCLUDE; *)
59
  "assert", ASSERT;
60
   "ensure", ENSURE;
61
  "require", REQUIRE;
62
  (* "observer", OBSERVER; *)
63
  "invariant", INVARIANT;
64
  "mode", MODE;
65
  "assume", ASSUME;
66
  "contract", CONTRACT;
67
  "guarantees", GUARANTEES;
68
  "exists", EXISTS;
69
  "forall", FORALL;
70
  "c_code", CCODE;
71
  "matlab", MATLAB;
72
  ]
73

    
74
}
75

    
76

    
77
let newline = ('\010' | '\013' | "\013\010")
78
let notnewline = [^ '\010' '\013']
79
let blank = [' ' '\009' '\012']
80

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

    
151
{
152

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

    
164
  let spec s =
165
    let lexbuf = Lexing.from_string s in
166
    try
167
      Parser_lustre.lustre_spec (*ParserLustreSpec.lustre_spec*) token lexbuf
168
    with Parsing.Parse_error ->
169
      raise (Error (Location.curr lexbuf))
170
}