tptp_lexer.mll 7.3 KB
Newer Older
Andrei Paskevich's avatar
Andrei Paskevich committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
(**************************************************************************)
(*                                                                        *)
(*  Copyright (C) 2010-2011                                               *)
(*    François Bobot                                                      *)
(*    Jean-Christophe Filliâtre                                           *)
(*    Claude Marché                                                       *)
(*    Andrei Paskevich                                                    *)
(*                                                                        *)
(*  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.                  *)
(*                                                                        *)
(**************************************************************************)

{
  open Format
  open Lexing
  open Tptp_ast
  open Tptp_parser
Andrei Paskevich's avatar
Andrei Paskevich committed
25
  open Tptp_typing
Andrei Paskevich's avatar
Andrei Paskevich committed
26 27

  open Why3
Andrei Paskevich's avatar
Andrei Paskevich committed
28 29 30 31 32 33

  (* lexical errors *)

  exception IllegalCharacter of char
  exception UnterminatedComment
  exception UnterminatedString
Andrei Paskevich's avatar
Andrei Paskevich committed
34 35
  exception UnknownDDW of string
  exception UnknownDW of string
Andrei Paskevich's avatar
Andrei Paskevich committed
36 37 38 39 40

  let () = Exn_printer.register (fun fmt e -> match e with
    | IllegalCharacter c -> fprintf fmt "illegal character %c" c
    | UnterminatedComment -> fprintf fmt "unterminated comment"
    | UnterminatedString -> fprintf fmt "unterminated string"
Andrei Paskevich's avatar
Andrei Paskevich committed
41 42
    | UnknownDDW s -> fprintf fmt "unknown system_word %s" s
    | UnknownDW s -> fprintf fmt "unknown defined_word %s" s
Andrei Paskevich's avatar
Andrei Paskevich committed
43 44 45 46
    | _ -> raise e)

  let defwords = Hashtbl.create 97
  let () = List.iter (fun (x,y) -> Hashtbl.add defwords x y) [
Andrei Paskevich's avatar
Andrei Paskevich committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    "ceiling", DWORD (DF DFceil);
    "difference", DWORD (DF DFdiff);
    "distinct", DWORD (DP DPdistinct);
    "false", DWORD (DP DPfalse);
    "floor", DWORD (DF DFfloor);
    "greater", DWORD (DP DPgreater);
    "greatereq", DWORD (DP DPgreatereq);
    "i", DWORD (DT DTuniv);
    "int", DWORD (DT DTint);
    "is_int", DWORD (DP DPisint);
    "is_rat", DWORD (DP DPisrat);
    "ite_f", ITE_F;
    "ite_t", ITE_T;
    "iType", DWORD (DT DTuniv);
    "let_tt", LET_TT;
    "let_ft", LET_FT;
    "let_tf", LET_TF;
    "let_ff", LET_FF;
    "less", DWORD (DP DPless);
    "lesseq", DWORD (DP DPlesseq);
    "o", DWORD (DT DTprop);
    "oType", DWORD (DT DTprop);
    "product", DWORD (DF DFprod);
    "quotient", DWORD (DF DFquot);
    "quotient_e", DWORD (DF DFquot_e);
    "quotient_t", DWORD (DF DFquot_t);
    "quotient_f", DWORD (DF DFquot_f);
    "rat", DWORD (DT DTrat);
    "real", DWORD (DT DTreal);
    "remainder_e", DWORD (DF DFrem_e);
    "remainder_t", DWORD (DF DFrem_t);
    "remainder_f", DWORD (DF DFrem_f);
    "round", DWORD (DF DFround);
    "sum", DWORD (DF DFsum);
    "to_int", DWORD (DF DFtoint);
    "to_rat", DWORD (DF DFtorat);
    "to_real", DWORD (DF DFtoreal);
    "true", DWORD (DP DPtrue);
    "truncate", DWORD (DF DFtrunc);
    "tType", DWORD (DT DTtype);
    "uminus", DWORD (DF DFumin);
Andrei Paskevich's avatar
Andrei Paskevich committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  ]

  let keywords = Hashtbl.create 97
  let () = List.iter (fun (x,y) -> Hashtbl.add keywords x y) [
    "assumption", ASSUMPTION;
    "axiom", AXIOM;
    "cnf", CNF;
    "conjecture", CONJECTURE;
    "definition", DEFINITION;
    "fof", FOF;
    "hypothesis", HYPOTHESIS;
    "include", INCLUDE;
    "lemma", LEMMA;
    "negated_conjecture", NEGATED_CONJECTURE;
    "tff", TFF;
    "theorem", THEOREM;
    "type", TYPE;
  ]

  let newline lexbuf =
    let pos = lexbuf.lex_curr_p in
    lexbuf.lex_curr_p <-
      { pos with pos_lnum = pos.pos_lnum + 1; pos_bol = pos.pos_cnum }

  let comment_start_loc = ref Loc.dummy_position

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

let newline = '\n'
let space = [' ' '\t' '\r']

let lalpha = ['a'-'z']
let ualpha = ['A'-'Z']
let digit  = ['0'-'9']
let nzero  = ['1'-'9']

let alnum = lalpha | ualpha | digit | '_'
Andrei Paskevich's avatar
Andrei Paskevich committed
126 127
let lword = lalpha alnum*
let uword = ualpha alnum*
Andrei Paskevich's avatar
Andrei Paskevich committed
128 129 130

let positive = nzero digit*
let natural  = '0' | positive
Andrei Paskevich's avatar
Andrei Paskevich committed
131 132 133

let sq_char = [' '-'&' '('-'[' ']'-'~'] | '\\' ['\\' '\'']
let do_char = [' '-'!' '#'-'[' ']'-'~'] | '\\' ['\\' '"']
Andrei Paskevich's avatar
Andrei Paskevich committed
134 135 136 137 138 139 140 141 142 143

rule token = parse
  | newline
      { newline lexbuf; token lexbuf }
  | space+
      { token lexbuf }
  | lword as id
      { try Hashtbl.find keywords id with Not_found -> LWORD id }
  | uword as id
      { UWORD id }
Andrei Paskevich's avatar
Andrei Paskevich committed
144 145 146 147
  | '\'' (sq_char+ as sq) '\''
      { SINGLE_QUOTED sq }
  | '"' (do_char* as dob) '"'
      { DISTINCT_OBJECT dob }
Andrei Paskevich's avatar
Andrei Paskevich committed
148 149
  | "$_"
      { DWORD (DT DTdummy) }
Andrei Paskevich's avatar
Andrei Paskevich committed
150
  | '$' (lword as id)
Andrei Paskevich's avatar
Andrei Paskevich committed
151 152 153
      { try Hashtbl.find defwords id with Not_found -> raise (UnknownDW id) }
  | "$$" (lword as id)
      { raise (UnknownDDW id) }
Andrei Paskevich's avatar
Andrei Paskevich committed
154
  | '+'? (natural as s)
Andrei Paskevich's avatar
Andrei Paskevich committed
155
  | '-'   natural as s
Andrei Paskevich's avatar
Andrei Paskevich committed
156
      { INTNUM s }
Andrei Paskevich's avatar
Andrei Paskevich committed
157 158
  | '+'? (natural as n) '/' (positive as d)
  | ('-'  natural as n) '/' (positive as d)
Andrei Paskevich's avatar
Andrei Paskevich committed
159
      { RATNUM (n,d) }
Andrei Paskevich's avatar
Andrei Paskevich committed
160 161 162 163
  | '+'? (natural as i) ('.' (digit+ as f))? (['e' 'E'] ('+'? (natural as e)))?
  | ('-'  natural as i) ('.' (digit+ as f))? (['e' 'E'] ('+'? (natural as e)))?
  | '+'? (natural as i) ('.' (digit+ as f))? (['e' 'E'] ('-'   natural as e))?
  | ('-'  natural as i) ('.' (digit+ as f))? (['e' 'E'] ('-'   natural as e))?
Andrei Paskevich's avatar
Andrei Paskevich committed
164
      { REALNUM (i,f,e) }
Andrei Paskevich's avatar
Andrei Paskevich committed
165 166 167 168 169 170
  | "/*/"
      { SLASH_STAR_SLASH }
  | "/*"
      { comment_start_loc := loc lexbuf; comment_block lexbuf; token lexbuf }
  | "%"
      { comment_start_loc := loc lexbuf; comment_line  lexbuf; token lexbuf }
Andrei Paskevich's avatar
Andrei Paskevich committed
171 172
  | "."
      { DOT }
Andrei Paskevich's avatar
Andrei Paskevich committed
173 174
  | ","
      { COMMA }
Andrei Paskevich's avatar
Andrei Paskevich committed
175 176
  | ":"
      { COLON }
Andrei Paskevich's avatar
Andrei Paskevich committed
177 178 179 180
  | "("
      { LEFTPAR }
  | ")"
      { RIGHTPAR }
Andrei Paskevich's avatar
Andrei Paskevich committed
181 182 183 184 185 186 187
  | "["
      { LEFTSQ }
  | "]"
      { RIGHTSQ }
  | "-->"
      { LONGARROW }
  | "<="
Andrei Paskevich's avatar
Andrei Paskevich committed
188
      { LARROW }
Andrei Paskevich's avatar
Andrei Paskevich committed
189 190 191
  | "=>"
      { RARROW }
  | "<=>"
Andrei Paskevich's avatar
Andrei Paskevich committed
192
      { LRARROW }
Andrei Paskevich's avatar
Andrei Paskevich committed
193 194 195 196 197 198 199 200
  | "<~>"
      { NLRARROW }
  | "~&"
      { NAMP }
  | "~|"
      { NBAR }
  | "~"
      { TILDE }
Andrei Paskevich's avatar
Andrei Paskevich committed
201 202 203 204
  | "&"
      { AMP }
  | "|"
      { BAR }
Andrei Paskevich's avatar
Andrei Paskevich committed
205 206
  | "="
      { EQUAL }
Andrei Paskevich's avatar
Andrei Paskevich committed
207 208 209 210 211 212 213 214 215 216 217 218
  | "!="
      { NEQUAL }
  | "*"
      { STAR }
  | ">"
      { GT }
  | "!>"
      { PI }
  | "!"
      { BANG }
  | "?"
      { QUES }
Andrei Paskevich's avatar
Andrei Paskevich committed
219 220 221 222 223
  | eof
      { EOF }
  | _ as c
      { raise (IllegalCharacter c) }

Andrei Paskevich's avatar
Andrei Paskevich committed
224 225
and comment_block = parse
  | "*/"
Andrei Paskevich's avatar
Andrei Paskevich committed
226 227
      { () }
  | newline
Andrei Paskevich's avatar
Andrei Paskevich committed
228
      { newline lexbuf; comment_block lexbuf }
Andrei Paskevich's avatar
Andrei Paskevich committed
229 230 231
  | eof
      { raise (Loc.Located (!comment_start_loc, UnterminatedComment)) }
  | _
Andrei Paskevich's avatar
Andrei Paskevich committed
232
      { comment_block lexbuf }
Andrei Paskevich's avatar
Andrei Paskevich committed
233

Andrei Paskevich's avatar
Andrei Paskevich committed
234
and comment_line = parse
Andrei Paskevich's avatar
Andrei Paskevich committed
235
  | newline
Andrei Paskevich's avatar
Andrei Paskevich committed
236
      { newline lexbuf; () }
Andrei Paskevich's avatar
Andrei Paskevich committed
237
  | eof
Andrei Paskevich's avatar
Andrei Paskevich committed
238 239
      { () }
  | _
Andrei Paskevich's avatar
Andrei Paskevich committed
240
      { comment_line lexbuf }
Andrei Paskevich's avatar
Andrei Paskevich committed
241 242 243 244 245 246 247 248 249 250 251

{
  let with_location f lb =
    if Debug.test_flag Debug.stack_trace then f lb else
    try f lb with
      | Loc.Located _ as e -> raise e
      | e -> raise (Loc.Located (loc lb, e))

  let read_channel env path file c =
    let lb = Lexing.from_channel c in
    Loc.set_file file lb;
Andrei Paskevich's avatar
Andrei Paskevich committed
252
    let ast = with_location (tptp_file token) lb in
Andrei Paskevich's avatar
Andrei Paskevich committed
253
    Tptp_typing.typecheck env path ast
Andrei Paskevich's avatar
Andrei Paskevich committed
254

Andrei Paskevich's avatar
Andrei Paskevich committed
255 256
(*  let () = Env.register_format "tptp" ["p";"ax"] read_channel *)
  let () = Env.register_format "tff1" ["p";"ax"] read_channel
Andrei Paskevich's avatar
Andrei Paskevich committed
257 258 259 260 261 262 263 264
}

(*
Local Variables:
compile-command: "unset LANG; make -C ../.. test"
End:
*)