lexer.mll 9.83 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
3
(*  Copyright (C) 2010-2011                                               *)
4 5 6
(*    François Bobot                                                      *)
(*    Jean-Christophe Filliâtre                                           *)
(*    Claude Marché                                                       *)
Jean-Christophe Filliâtre's avatar
headers  
Jean-Christophe Filliâtre committed
7
(*    Andrei Paskevich                                                    *)
8 9 10 11 12 13 14 15 16 17 18
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
19 20

{
21
  open Format
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
22
  open Lexing
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
23
  open Term
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
24 25 26
  open Ptree
  open Parser

27 28
  (* lexical errors *)

29 30 31
  exception IllegalCharacter of char
  exception UnterminatedComment
  exception UnterminatedString
32

33
  let () = Exn_printer.register (fun fmt e -> match e with
34 35 36
    | IllegalCharacter c -> fprintf fmt "illegal character %c" c
    | UnterminatedComment -> fprintf fmt "unterminated comment"
    | UnterminatedString -> fprintf fmt "unterminated string"
37
    | _ -> raise e)
38

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

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
107 108
  let newline lexbuf =
    let pos = lexbuf.lex_curr_p in
109
    lexbuf.lex_curr_p <-
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
110 111
      { pos with pos_lnum = pos.pos_lnum + 1; pos_bol = pos.pos_cnum }

112
  let string_start_loc = ref Loc.dummy_position
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
113 114
  let string_buf = Buffer.create 1024

115 116
  let comment_start_loc = ref Loc.dummy_position

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
117 118 119 120 121 122 123 124
  let char_for_backslash = function
    | 'n' -> '\n'
    | 't' -> '\t'
    | c -> c

  let update_loc lexbuf file line chars =
    let pos = lexbuf.lex_curr_p in
    let new_file = match file with None -> pos.pos_fname | Some s -> s in
125
    lexbuf.lex_curr_p <-
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
126
      { pos with
127 128 129
          pos_fname = new_file;
          pos_lnum = int_of_string line;
          pos_bol = pos.pos_cnum - int_of_string chars;
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
130 131 132
      }

  let remove_leading_plus s =
133
    let n = String.length s in
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
134 135
    if n > 0 && s.[0] = '+' then String.sub s 1 (n-1) else s

136
  let loc lb = Loc.extract (lexeme_start_p lb, lexeme_end_p lb)
137

138 139 140 141 142 143 144 145 146 147 148
  let remove_underscores s =
    if String.contains s '_' then begin
      let count =
        let nb = ref 0 in
        String.iter (fun c -> if c = '_' then incr nb) s;
        !nb in
      let t = String.create (String.length s - count) in
      let i = ref 0 in
      String.iter (fun c -> if c <> '_' then (t.[!i] <-c; incr i)) s;
      t
    end else s
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
149 150 151 152
}

let newline = '\n'
let space = [' ' '\t' '\r']
153 154 155
let lalpha = ['a'-'z' '_']
let ualpha = ['A'-'Z']
let alpha = lalpha | ualpha
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
156
let digit = ['0'-'9']
157 158
let lident = lalpha (alpha | digit | '\'')*
let uident = ualpha (alpha | digit | '\'')*
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
159 160
let hexadigit = ['0'-'9' 'a'-'f' 'A'-'F']

161
let op_char_1 = ['=' '<' '>' '~']
162 163
let op_char_2 = ['+' '-']
let op_char_3 = ['*' '/' '%']
164
let op_char_4 = ['!' '$' '&' '?' '@' '^' '.' ':' '|' '#']
165 166 167 168
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

169 170
let op_char_pref = ['!' '?']

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
171
rule token = parse
172 173
  | "##" space* ("\"" ([^ '\010' '\013' '"' ]* as file) "\"")?
    space* (digit+ as line) space* (digit+ as char) space* "##"
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
174
      { update_loc lexbuf file line char; token lexbuf }
175 176 177 178 179
  | "#" 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)) }
180
  | newline
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
181
      { newline lexbuf; token lexbuf }
182
  | space+
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
183
      { token lexbuf }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
184 185
  | '_'
      { UNDERSCORE }
186
  | lident as id
187
      { try Hashtbl.find keywords id with Not_found -> LIDENT id }
188
  | uident as id
189
      { UIDENT id }
190 191 192 193 194 195 196 197
  | ['0'-'9'] ['0'-'9' '_']* as s
      { INTEGER (IConstDecimal (remove_underscores s)) }
  | '0' ['x' 'X'] (['0'-'9' 'A'-'F' 'a'-'f']['0'-'9' 'A'-'F' 'a'-'f' '_']* as s)
      { INTEGER (IConstHexa (remove_underscores s)) }
  | '0' ['o' 'O'] (['0'-'7'] ['0'-'7' '_']* as s)
      { INTEGER (IConstOctal (remove_underscores s)) }
  | '0' ['b' 'B'] (['0'-'1'] ['0'-'1' '_']* as s)
      { INTEGER (IConstBinary (remove_underscores s)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
198 199 200
  | (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))?
201
      { FLOAT (RConstDecimal (i, f, Util.option_map remove_leading_plus e)) }
202
  | '0' ['x' 'X'] ((hexadigit* as i) '.' (hexadigit+ as f)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
203
                  |(hexadigit+ as i) '.' (hexadigit* as f)
204
                  |(hexadigit+ as i) ("" as f))
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
205 206
    ['p' 'P'] (['-' '+']? digit+ as e)
      { FLOAT (RConstHexa (i, f, remove_leading_plus e)) }
207 208
  | "(*)"
      { LEFTPAR_STAR_RIGHTPAR }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
209
  | "(*"
210
      { comment_start_loc := loc lexbuf; comment lexbuf; token lexbuf }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
211 212 213 214 215 216 217 218
  | "'"
      { QUOTE }
  | ","
      { COMMA }
  | "("
      { LEFTPAR }
  | ")"
      { RIGHTPAR }
219 220 221 222
  | "{"
      { LEFTBRC }
  | "}"
      { RIGHTBRC }
223 224 225 226
  | "{|"
      { LEFTREC }
  | "|}"
      { RIGHTREC }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
227 228
  | ":"
      { COLON }
229 230
  | ";"
      { SEMICOLON }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
231 232
  | "->"
      { ARROW }
233 234
  | "<-"
      { LARROW }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
235 236
  | "<->"
      { LRARROW }
237 238 239 240
  | "&&"
      { AMPAMP }
  | "||"
      { BARBAR }
241 242 243 244 245
  | "/\\"
      { AND }
  | "\\/"
      { OR }
   | "\\"
Andrei Paskevich's avatar
Andrei Paskevich committed
246
      { LAMBDA }
247 248 249 250
  | "\\?"
      { PRED }
  | "\\!"
      { FUNC }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
251 252
  | "."
      { DOT }
253 254 255 256
  | "|"
      { BAR }
  | "="
      { EQUAL }
257 258
  | "<>"
      { LTGT }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
259 260 261 262
  | "["
      { LEFTSQ }
  | "]"
      { RIGHTSQ }
263 264
  | op_char_pref op_char_4* as s
      { OPPREF s }
265 266 267 268 269 270 271 272
  | 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
273
  | "\""
274
      { string_start_loc := loc lexbuf; STRING (string lexbuf) }
275
  | eof
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
276 277
      { EOF }
  | _ as c
278
      { raise (IllegalCharacter c) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
279 280

and comment = parse
281 282
  | "(*)"
      { comment lexbuf }
283
  | "*)"
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
284
      { () }
285
  | "(*"
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
286
      { comment lexbuf; comment lexbuf }
287
  | newline
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
288 289
      { newline lexbuf; comment lexbuf }
  | eof
290
      { raise (Loc.Located (!comment_start_loc, UnterminatedComment)) }
291
  | _
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
292 293 294 295
      { comment lexbuf }

and string = parse
  | "\""
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
296
      { let s = Buffer.contents string_buf in
297 298
        Buffer.clear string_buf;
        s }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
299 300
  | "\\" (_ as c)
      { Buffer.add_char string_buf (char_for_backslash c); string lexbuf }
301
  | newline
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
302 303
      { newline lexbuf; Buffer.add_char string_buf '\n'; string lexbuf }
  | eof
304
      { raise (Loc.Located (!string_start_loc, UnterminatedString)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
305 306 307 308 309
  | _ as c
      { Buffer.add_char string_buf c; string lexbuf }

{
  let with_location f lb =
310 311
    if Debug.test_flag Debug.stack_trace then f lb else
    try f lb with
312 313
      | Loc.Located _ as e -> raise e
      | e -> raise (Loc.Located (loc lb, e))
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
314

315 316
  let parse_logic_file env path lb =
    pre_logic_file token (Lexing.from_string "") env path;
317
    with_location (logic_file token) lb
318

319 320
  let parse_program_file = with_location (program_file token)

321 322 323 324 325 326 327 328 329 330 331
  let token_counter lb =
    let rec loop in_annot a p =
      match token lb with
        | LEFTBRC -> assert (not in_annot); loop true a p
        | RIGHTBRC -> assert in_annot; loop false a p
        | EOF -> assert (not in_annot); (a,p)
        | _ ->
            if in_annot
            then loop in_annot (a+1) p
            else loop in_annot a (p+1)
    in
Andrei Paskevich's avatar
Andrei Paskevich committed
332 333
    loop false 0 0

334
  let read_channel env path file c =
335 336
    let lb = Lexing.from_channel c in
    Loc.set_file file lb;
Andrei Paskevich's avatar
Andrei Paskevich committed
337
    (), parse_logic_file env path lb
338

Andrei Paskevich's avatar
Andrei Paskevich committed
339
  let library_of_env = Env.register_format "why" ["why"] read_channel
340

Andrei Paskevich's avatar
Andrei Paskevich committed
341
  let parse_logic_file env = parse_logic_file (library_of_env env)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
342 343 344
}

(*
345
Local Variables:
346
compile-command: "unset LANG; make -C ../.. test"
347
End:
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
348 349
*)