lexer.mll 6.93 KB
Newer Older
Andrei Paskevich's avatar
Andrei Paskevich committed
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
Andrei Paskevich's avatar
Andrei Paskevich committed
4
(*  Copyright 2010-2016   --   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
        "as", AS;
        "axiom", AXIOM;
Martin Clochard's avatar
Martin Clochard committed
29
        "by", BY;
30
        "clone", CLONE;
31
        "coinductive", COINDUCTIVE;
32
        "constant", CONSTANT;
33 34 35 36 37
        "else", ELSE;
        "end", END;
        "epsilon", EPSILON;
        "exists", EXISTS;
        "export", EXPORT;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
38
        "false", FALSE;
39
        "forall", FORALL;
Andrei Paskevich's avatar
Andrei Paskevich committed
40
        "function", FUNCTION;
41 42 43 44 45 46 47 48 49 50 51
        "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
52
        "predicate", PREDICATE;
53
        "prop", PROP;
Martin Clochard's avatar
Martin Clochard committed
54
        "so", SO;
55 56 57 58 59 60 61 62 63 64 65 66 67
        "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;
68
        "check", CHECK;
69
        "diverges", DIVERGES;
70 71
        "do", DO;
        "done", DONE;
Andrei Paskevich's avatar
Andrei Paskevich committed
72
        "downto", DOWNTO;
73
        "ensures", ENSURES;
74 75 76
        "exception", EXCEPTION;
        "for", FOR;
        "fun", FUN;
77
        "ghost", GHOST;
78 79 80 81 82
        "invariant", INVARIANT;
        "loop", LOOP;
        "model", MODEL;
        "module", MODULE;
        "mutable", MUTABLE;
83
        "private", PRIVATE;
84 85 86 87
        "raise", RAISE;
        "raises", RAISES;
        "reads", READS;
        "rec", REC;
88 89
        "requires", REQUIRES;
        "returns", RETURNS;
90 91
        "to", TO;
        "try", TRY;
Andrei Paskevich's avatar
Andrei Paskevich committed
92
        "val", VAL;
93 94
        "variant", VARIANT;
        "while", WHILE;
95
        "writes", WRITES;
96
      ]
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
97 98 99
}

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

108
let op_char_1 = ['=' '<' '>' '~']
109
let op_char_2 = ['+' '-']
110
let op_char_3 = ['*' '/' '\\' '%']
111
let op_char_4 = ['!' '$' '&' '?' '@' '^' '.' ':' '|' '#']
112 113 114 115
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

116 117
let op_char_pref = ['!' '?']

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

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

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

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

244 245
  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
246 247 248
}

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