lexer.mll 6.9 KB
Newer Older
Andrei Paskevich's avatar
Andrei Paskevich committed
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
4
(*  Copyright 2010-2015   --   INRIA - CNRS - Paris-Sud University  *)
Andrei Paskevich's avatar
Andrei Paskevich committed
5 6 7 8
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
9
(*                                                                  *)
Andrei Paskevich's avatar
Andrei Paskevich committed
10
(********************************************************************)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
11 12

{
13
  open Format
14
  open Parser
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
15

16
  exception IllegalCharacter of char
17

18
  let () = Exn_printer.register (fun fmt e -> match e with
19
    | IllegalCharacter c -> fprintf fmt "illegal character %c" c
20
    | _ -> raise e)
21

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
22
  let keywords = Hashtbl.create 97
23 24
  let () =
    List.iter
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
25
      (fun (x,y) -> Hashtbl.add keywords x y)
26
      [
27 28 29
        "as", AS;
        "axiom", AXIOM;
        "clone", CLONE;
30
        "coinductive", COINDUCTIVE;
31
        "constant", CONSTANT;
32 33 34 35 36
        "else", ELSE;
        "end", END;
        "epsilon", EPSILON;
        "exists", EXISTS;
        "export", EXPORT;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
37
        "false", FALSE;
38
        "forall", FORALL;
Andrei Paskevich's avatar
Andrei Paskevich committed
39
        "function", FUNCTION;
40 41 42 43 44 45 46 47 48 49 50
        "goal", GOAL;
        "if", IF;
        "import", IMPORT;
        "in", IN;
        "inductive", INDUCTIVE;
        "lemma", LEMMA;
        "let", LET;
        "match", MATCH;
        "meta", META;
        "namespace", NAMESPACE;
        "not", NOT;
Andrei Paskevich's avatar
Andrei Paskevich committed
51
        "predicate", PREDICATE;
52
        "prop", PROP;
53 54 55 56 57 58 59 60 61 62 63 64 65
        "then", THEN;
        "theory", THEORY;
        "true", TRUE;
        "type", TYPE;
        "use", USE;
        "with", WITH;
        (* programs *)
        "abstract", ABSTRACT;
        "absurd", ABSURD;
        "any", ANY;
        "assert", ASSERT;
        "assume", ASSUME;
        "begin", BEGIN;
66
        "check", CHECK;
67
        "diverges", DIVERGES;
68 69
        "do", DO;
        "done", DONE;
Andrei Paskevich's avatar
Andrei Paskevich committed
70
        "downto", DOWNTO;
71
        "ensures", ENSURES;
72 73 74
        "exception", EXCEPTION;
        "for", FOR;
        "fun", FUN;
75
        "ghost", GHOST;
76 77 78 79 80
        "invariant", INVARIANT;
        "loop", LOOP;
        "model", MODEL;
        "module", MODULE;
        "mutable", MUTABLE;
81
        "private", PRIVATE;
82 83 84 85
        "raise", RAISE;
        "raises", RAISES;
        "reads", READS;
        "rec", REC;
86 87
        "requires", REQUIRES;
        "returns", RETURNS;
88 89
        "to", TO;
        "try", TRY;
Andrei Paskevich's avatar
Andrei Paskevich committed
90
        "val", VAL;
91 92
        "variant", VARIANT;
        "while", WHILE;
93
        "writes", WRITES;
94
      ]
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
95 96 97
}

let space = [' ' '\t' '\r']
98 99 100
let lalpha = ['a'-'z' '_']
let ualpha = ['A'-'Z']
let alpha = lalpha | ualpha
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
101
let digit = ['0'-'9']
102 103
let lident = lalpha (alpha | digit | '\'')*
let uident = ualpha (alpha | digit | '\'')*
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
104 105
let hexadigit = ['0'-'9' 'a'-'f' 'A'-'F']

106
let op_char_1 = ['=' '<' '>' '~']
107
let op_char_2 = ['+' '-']
108
let op_char_3 = ['*' '/' '\\' '%']
109
let op_char_4 = ['!' '$' '&' '?' '@' '^' '.' ':' '|' '#']
110 111 112 113
let op_char_34 = op_char_3 | op_char_4
let op_char_234 = op_char_2 | op_char_34
let op_char_1234 = op_char_1 | op_char_234

114 115
let op_char_pref = ['!' '?']

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
116
rule token = parse
117 118
  | "##" space* ("\"" ([^ '\010' '\013' '"' ]* as file) "\"")?
    space* (digit+ as line) space* (digit+ as char) space* "##"
119 120
      { Lexlib.update_loc lexbuf file (int_of_string line) (int_of_string char);
        token lexbuf }
121 122 123 124 125
  | "#" space* "\"" ([^ '\010' '\013' '"' ]* as file) "\""
    space* (digit+ as line) space* (digit+ as bchar) space*
    (digit+ as echar) space* "#"
      { POSITION (Loc.user_position file (int_of_string line)
                 (int_of_string bchar) (int_of_string echar)) }
126 127
  | '\n'
      { Lexlib.newline lexbuf; token lexbuf }
128
  | space+
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
129
      { token lexbuf }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
130 131
  | '_'
      { UNDERSCORE }
132
  | lident as id
133
      { try Hashtbl.find keywords id with Not_found -> LIDENT id }
134
  | uident as id
135
      { UIDENT id }
136
  | ['0'-'9'] ['0'-'9' '_']* as s
137
      { INTEGER (Number.int_const_dec (Lexlib.remove_underscores s)) }
138
  | '0' ['x' 'X'] (['0'-'9' 'A'-'F' 'a'-'f']['0'-'9' 'A'-'F' 'a'-'f' '_']* as s)
139
      { INTEGER (Number.int_const_hex (Lexlib.remove_underscores s)) }
140
  | '0' ['o' 'O'] (['0'-'7'] ['0'-'7' '_']* as s)
141
      { INTEGER (Number.int_const_oct (Lexlib.remove_underscores s)) }
142
  | '0' ['b' 'B'] (['0'-'1'] ['0'-'1' '_']* as s)
143
      { INTEGER (Number.int_const_bin (Lexlib.remove_underscores s)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
144 145 146
  | (digit+ as i) ("" as f) ['e' 'E'] (['-' '+']? digit+ as e)
  | (digit+ as i) '.' (digit* as f) (['e' 'E'] (['-' '+']? digit+ as e))?
  | (digit* as i) '.' (digit+ as f) (['e' 'E'] (['-' '+']? digit+ as e))?
147 148
      { FLOAT (Number.real_const_dec i f
          (Opt.map Lexlib.remove_leading_plus e)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
149 150
  | '0' ['x' 'X'] (hexadigit+ as i) ("" as f) ['p' 'P'] (['-' '+']? digit+ as e)
  | '0' ['x' 'X'] (hexadigit+ as i) '.' (hexadigit* as f)
151
        (['p' 'P'] (['-' '+']? digit+ as e))?
Andrei Paskevich's avatar
Andrei Paskevich committed
152
  | '0' ['x' 'X'] (hexadigit* as i) '.' (hexadigit+ as f)
153
        (['p' 'P'] (['-' '+']? digit+ as e))?
154 155
      { FLOAT (Number.real_const_hex i f
          (Opt.map Lexlib.remove_leading_plus e)) }
156 157
  | "(*)"
      { LEFTPAR_STAR_RIGHTPAR }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
158
  | "(*"
159
      { Lexlib.comment lexbuf; token lexbuf }
160 161 162 163 164 165
  | "~'" (lident as id)
      { OPAQUE_QUOTE_LIDENT id }
  | "'" (lident as id)
      { QUOTE_LIDENT id }
  | "'" (uident as id)
      { QUOTE_UIDENT id }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
166 167 168 169 170 171
  | ","
      { COMMA }
  | "("
      { LEFTPAR }
  | ")"
      { RIGHTPAR }
172 173 174 175
  | "{"
      { LEFTBRC }
  | "}"
      { RIGHTBRC }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
176 177
  | ":"
      { COLON }
178 179
  | ";"
      { SEMICOLON }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
180 181
  | "->"
      { ARROW }
182 183
  | "<-"
      { LARROW }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
184 185
  | "<->"
      { LRARROW }
186 187 188 189
  | "&&"
      { AMPAMP }
  | "||"
      { BARBAR }
190 191 192 193
  | "/\\"
      { AND }
  | "\\/"
      { OR }
Andrei Paskevich's avatar
Andrei Paskevich committed
194
  | "\\"
Andrei Paskevich's avatar
Andrei Paskevich committed
195
      { LAMBDA }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
196 197
  | "."
      { DOT }
198 199
  | ".."
      { DOTDOT }
200 201 202 203
  | "|"
      { BAR }
  | "="
      { EQUAL }
204 205
  | "<>"
      { LTGT }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
206 207 208 209
  | "["
      { LEFTSQ }
  | "]"
      { RIGHTSQ }
210 211
  | op_char_pref op_char_4* as s
      { OPPREF s }
212 213 214 215 216 217 218 219
  | op_char_1234* op_char_1 op_char_1234* as s
      { OP1 s }
  | op_char_234*  op_char_2 op_char_234*  as s
      { OP2 s }
  | op_char_34*   op_char_3 op_char_34*  as s
      { OP3 s }
  | op_char_4+ as s
      { OP4 s }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
220
  | "\""
221
      { STRING (Lexlib.string lexbuf) }
222
  | eof
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
223 224
      { EOF }
  | _ as c
225
      { raise (IllegalCharacter c) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
226 227

{
228
  let parse_logic_file env path lb =
229
    open_file token (Lexing.from_string "") (Typing.open_file env path);
230
    Loc.with_location (logic_file token) lb;
231
    Typing.close_file ()
232

233 234
  let parse_program_file inc lb =
    open_file token (Lexing.from_string "") inc;
235
    Loc.with_location (program_file token) lb
236

237
  let read_channel env path file c =
238 239
    let lb = Lexing.from_channel c in
    Loc.set_file file lb;
240
    parse_logic_file env path lb
241

242 243
  let () = Env.register_format Env.base_language "why" ["why"] read_channel
    ~desc:"WhyML@ logical@ language"
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
244 245 246
}

(*
247
Local Variables:
248
compile-command: "unset LANG; make -C ../.. test"
249
End:
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
250 251
*)