lexer.mll 9.12 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
3
(*  Copyright (C) 2010-2011                                               *)
MARCHE Claude's avatar
MARCHE Claude committed
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
  exception AmbiguousPath of string * string
33

34
  let () = Exn_printer.register (fun fmt e -> match e with
35 36 37
    | IllegalCharacter c -> fprintf fmt "illegal character %c" c
    | UnterminatedComment -> fprintf fmt "unterminated comment"
    | UnterminatedString -> fprintf fmt "unterminated string"
38 39
    | AmbiguousPath (f1, f2) ->
        fprintf fmt "ambiguous path:@ both `%s'@ and `%s'@ match" f1 f2
40
    | _ -> raise e)
41

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

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

115
  let string_start_loc = ref Loc.dummy_position
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
116 117
  let string_buf = Buffer.create 1024

118 119
  let comment_start_loc = ref Loc.dummy_position

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
120 121 122 123 124 125 126 127
  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
128
    lexbuf.lex_curr_p <-
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
129
      { pos with
130 131 132
          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
133 134 135
      }

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

139
  let loc lb = Loc.extract (lexeme_start_p lb, lexeme_end_p lb)
140

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
141 142 143 144
}

let newline = '\n'
let space = [' ' '\t' '\r']
145 146 147
let lalpha = ['a'-'z' '_']
let ualpha = ['A'-'Z']
let alpha = lalpha | ualpha
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
148
let digit = ['0'-'9']
149 150
let lident = lalpha (alpha | digit | '\'')*
let uident = ualpha (alpha | digit | '\'')*
151 152 153 154 155 156 157 158 159 160
let decimal_literal =
  ['0'-'9'] ['0'-'9' '_']*
let hex_literal =
  '0' ['x' 'X'] ['0'-'9' 'A'-'F' 'a'-'f']['0'-'9' 'A'-'F' 'a'-'f' '_']*
let oct_literal =
  '0' ['o' 'O'] ['0'-'7'] ['0'-'7' '_']*
let bin_literal =
  '0' ['b' 'B'] ['0'-'1'] ['0'-'1' '_']*
let int_literal =
  decimal_literal | hex_literal | oct_literal | bin_literal
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
161 162
let hexadigit = ['0'-'9' 'a'-'f' 'A'-'F']

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

171 172
let op_char_pref = ['!' '?']

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

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

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

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

313 314 315
  let parse_logic_file env lb =
    pre_logic_file token (Lexing.from_string "") env;
    with_location (logic_file token) lb
316

317 318
  let parse_program_file = with_location (program_file token)

319 320 321 322
  let read_channel env file c =
    let lb = Lexing.from_channel c in
    Loc.set_file file lb;
    parse_logic_file env lb
323

324
  let () = Env.register_format "why" ["why"] read_channel
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
325 326 327
}

(*
328
Local Variables:
329
compile-command: "unset LANG; make -C ../.. test"
330
End:
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
331 332
*)