Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / lexerLustreSpec.mll @ 4f26dcf5

History | View | Annotate | Download (4.52 KB)

1 a2d97a3e ploc
(********************************************************************)
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 22fe1c93 ploc
{
13
14 01c7d5e1 ploc
  (* open ParserLustreSpec *)
15
  open Parser_lustre
16 22fe1c93 ploc
  open Utils
17
18 04a63d25 xthirioux
  let str_buf = Buffer.create 1024
19 22fe1c93 ploc
20 0cbf0839 ploc
  exception Error of Location.t
21
22 22fe1c93 ploc
(* 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 01c7d5e1 ploc
  (* "true", TRUE; *)
27
  (* "false", FALSE; *)
28 22fe1c93 ploc
  "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 04a63d25 xthirioux
  (* "float", TFLOAT; *)
46 22fe1c93 ploc
  "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 01c7d5e1 ploc
  (* "include", INCLUDE; *)
57 22fe1c93 ploc
  "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 1b57e111 Teme
  "c_code", CCODE;
67
  "matlab", MATLAB;
68 22fe1c93 ploc
  ]
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 04a63d25 xthirioux
  | (('-'? ['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 22fe1c93 ploc
  | '-'? ['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 dcafc99b Ploc
  | _ { raise (Parse.Error (Location.curr lexbuf, Parse.Unexpected_eof)) }
130 22fe1c93 ploc
and comment_line n = parse
131
| eof
132 dcafc99b Ploc
    { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_comment)) }
133 22fe1c93 ploc
| "(*"
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 dcafc99b Ploc
  | eof { raise (Parse.Error (Location.curr lexbuf, Parse.Unfinished_string)) }
143 22fe1c93 ploc
  | "\\\"" 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 04a63d25 xthirioux
    let lexbuf = Lexing.from_string s in
151 22fe1c93 ploc
   try
152 04a63d25 xthirioux
     Parser_lustre.lustre_annot(* ParserLustreSpec.lustre_annot *) token lexbuf
153 22fe1c93 ploc
   with Parsing.Parse_error as _e -> (
154 66359a5e ploc
     Format.eprintf "Lexing error at position %a:@.unexpected token %s when parsing annotation %s@.@?"
155 04a63d25 xthirioux
       (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 66359a5e ploc
       (Lexing.lexeme lexbuf) s;
157 04a63d25 xthirioux
     raise (Error (Location.curr lexbuf)))
158 22fe1c93 ploc
     
159
160
  let spec s =
161 04a63d25 xthirioux
    let lexbuf = Lexing.from_string s in
162
    try
163
      Parser_lustre.lustre_spec (*ParserLustreSpec.lustre_spec*) token lexbuf
164
    with Parsing.Parse_error ->
165
      raise (Error (Location.curr lexbuf))
166 22fe1c93 ploc
}