lexer.mll 6.8 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 9
(*                                                                  *)
(*  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.                           *)
(********************************************************************)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
10 11

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

15
  exception IllegalCharacter of char
16

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

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
21
  let keywords = Hashtbl.create 97
22 23
  let () =
    List.iter
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
24
      (fun (x,y) -> Hashtbl.add keywords x y)
25
      [
26 27 28
        "as", AS;
        "axiom", AXIOM;
        "clone", CLONE;
29
        "coinductive", COINDUCTIVE;
30
        "constant", CONSTANT;
31 32 33 34 35
        "else", ELSE;
        "end", END;
        "epsilon", EPSILON;
        "exists", EXISTS;
        "export", EXPORT;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
36
        "false", FALSE;
37
        "forall", FORALL;
Andrei Paskevich's avatar
Andrei Paskevich committed
38
        "function", FUNCTION;
39 40 41 42 43 44 45 46 47 48 49
        "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
50
        "predicate", PREDICATE;
51
        "prop", PROP;
52 53 54 55 56 57 58 59 60 61 62 63 64
        "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;
65
        "check", CHECK;
66
        "diverges", DIVERGES;
67 68
        "do", DO;
        "done", DONE;
Andrei Paskevich's avatar
Andrei Paskevich committed
69
        "downto", DOWNTO;
70
        "ensures", ENSURES;
71 72 73
        "exception", EXCEPTION;
        "for", FOR;
        "fun", FUN;
74
        "ghost", GHOST;
75 76 77 78 79
        "invariant", INVARIANT;
        "loop", LOOP;
        "model", MODEL;
        "module", MODULE;
        "mutable", MUTABLE;
80
        "private", PRIVATE;
81 82 83 84
        "raise", RAISE;
        "raises", RAISES;
        "reads", READS;
        "rec", REC;
85 86
        "requires", REQUIRES;
        "returns", RETURNS;
87 88
        "to", TO;
        "try", TRY;
Andrei Paskevich's avatar
Andrei Paskevich committed
89
        "val", VAL;
90 91
        "variant", VARIANT;
        "while", WHILE;
92
        "writes", WRITES;
93
      ]
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
94 95 96
}

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

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

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

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

{
225
  let parse_logic_file env path lb =
226
    open_file token (Lexing.from_string "") (Typing.open_file env path);
227
    Loc.with_location (logic_file token) lb;
228
    Typing.close_file ()
229

230 231
  let parse_program_file inc lb =
    open_file token (Lexing.from_string "") inc;
232
    Loc.with_location (program_file token) lb
233

234
  let read_channel env path file c =
235 236
    let lb = Lexing.from_channel c in
    Loc.set_file file lb;
237
    parse_logic_file env path lb
238

239 240
  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
241 242 243
}

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