Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / lexerLustreSpec.mll @ a0b18d45

History | View | Annotate | Download (4.53 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
  "if", IF;
29
  "then", THEN;
30
  "else", ELSE;
31
  "merge", MERGE;
32
  "arrow", ARROW;
33
  "fby", FBY;
34
  "when", WHEN;
35
  "whennot", WHENNOT;
36
  "every", EVERY;
37
  "node", NODE;
38
  "let", LET;
39
  "tel", TEL;
40
  "returns", RETURNS;
41
  "var", VAR;
42
  "imported", IMPORTED;
43
  "int", TINT;
44
  "bool", TBOOL;
45
  (* "float", TFLOAT; *)
46
  "real", TREAL;
47
  "clock", TCLOCK;
48
  "not", NOT;
49
  "and", AND;
50
  "or", OR;
51
  "xor", OR;
52
  "mod", MOD;
53
  "pre", PRE;
54
  "div", DIV;
55
  "const", CONST;
56
  (* "include", INCLUDE; *)
57
  "assert", ASSERT;
58
  "ensures", ENSURES;
59
  "requires", REQUIRES;
60
  "observer", OBSERVER;
61
  "invariant", INVARIANT;
62
  "behavior", BEHAVIOR;
63
  "assumes", ASSUMES;
64
  "exists", EXISTS;
65
  "forall", FORALL;
66
  "c_code", CCODE;
67
  "matlab", MATLAB;
68
  ]
69

    
70
}
71

    
72

    
73
let newline = ('\010' | '\013' | "\013\010")
74
let notnewline = [^ '\010' '\013']
75
let blank = [' ' '\009' '\012']
76

    
77
rule token = parse
78
  | "(*"
79
      { comment_line 0 lexbuf }
80
  | "--" notnewline* (newline|eof)
81
      { incr_line lexbuf;
82
      token lexbuf }
83
  | newline
84
      { incr_line lexbuf;
85
	token lexbuf }
86
  | blank +
87
      {token lexbuf}
88
  | (('-'? ['0'-'9'] ['0'-'9']* as l) '.' (['0'-'9']* as r)) as s
89
      {REAL (Num.num_of_string (l^r), String.length r, s)}
90
  | (('-'? ['0'-'9']+ as l)  '.' (['0'-'9']+ as r) ('E'|'e') (('+'|'-') ['0'-'9'] ['0'-'9']* as exp)) as s
91
      {REAL (Num.num_of_string (l^r), String.length r + -1 * int_of_string exp, s)}
92
  | '-'? ['0'-'9']+
93
      {INT (int_of_string (Lexing.lexeme lexbuf)) }
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 (Parse.Error (Location.curr lexbuf, Parse.Unexpected_eof)) }
130
and comment_line n = parse
131
| eof
132
    { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_comment)) }
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
  | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_string)) }
143
  | "\\\"" as s { Buffer.add_string str_buf s; string_parse lexbuf}
144
  | '"' { STRING (Buffer.contents str_buf) }
145
  | _ as c  { Buffer.add_char str_buf c; string_parse lexbuf }
146

    
147
{
148

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

    
159

    
160
  let spec s =
161
    let lexbuf = Lexing.from_string s in
162
    try
163
      Parser_lustre.lustre_spec_main (*ParserLustreSpec.lustre_spec*) token lexbuf
164
    with Parsing.Parse_error ->
165
      raise (Error (Location.curr lexbuf))
166
}