| 46844 |      1 | (*  Title:      HOL/TPTP/TPTP_Parser/tptp_parser.ML
 | 
|  |      2 |     Author:     Nik Sultana, Cambridge University Computer Laboratory
 | 
|  |      3 | 
 | 
|  |      4 | An interface for a parser, generated using ML-Yacc, to parse TPTP languages.
 | 
|  |      5 | *)
 | 
|  |      6 | 
 | 
|  |      7 | 
 | 
|  |      8 | (* Build the parser structure *)
 | 
|  |      9 | 
 | 
|  |     10 | structure TPTPLrVals = TPTPLrValsFun(structure Token = LrParser.Token)
 | 
|  |     11 | structure TPTPLex = TPTPLexFun(structure Tokens = TPTPLrVals.Tokens)
 | 
|  |     12 | structure TPTPParser
 | 
|  |     13 |   = JoinWithArg
 | 
|  |     14 |      (structure ParserData = TPTPLrVals.ParserData
 | 
|  |     15 |       structure Lex = TPTPLex
 | 
|  |     16 |       structure LrParser = LrParser)
 | 
|  |     17 | 
 | 
|  |     18 | 
 | 
|  |     19 | (* Parser interface *)
 | 
|  |     20 | structure TPTP_Parser :
 | 
|  |     21 | sig
 | 
|  |     22 |   val parse_file : string -> TPTP_Syntax.tptp_problem
 | 
|  |     23 |   val parse_expression : string -> string -> TPTP_Syntax.tptp_problem
 | 
|  |     24 |   exception TPTP_PARSE_ERROR
 | 
|  |     25 | end =
 | 
|  |     26 | struct
 | 
|  |     27 | 
 | 
|  |     28 | exception TPTP_PARSE_ERROR
 | 
|  |     29 | 
 | 
|  |     30 | val LOOKAHEAD = 0 (*usually set to 15*)
 | 
|  |     31 | 
 | 
|  |     32 | local
 | 
|  |     33 |   fun print_error file_name (msg, line, col) =
 | 
|  |     34 |     error (file_name ^ "[" ^ Int.toString line ^ ":" ^ Int.toString col ^ "] " ^
 | 
|  |     35 |       msg ^ "\n")
 | 
|  |     36 |   fun parse lookahead grab file_name =
 | 
|  |     37 |     TPTPParser.parse
 | 
|  |     38 |       (lookahead,
 | 
|  |     39 |        TPTPParser.makeLexer grab file_name,
 | 
|  |     40 |        print_error file_name,
 | 
|  |     41 |        file_name)
 | 
|  |     42 | in
 | 
|  |     43 |   fun parse_expression file_name expression =
 | 
|  |     44 |     (*file_name only used in reporting error messages*)
 | 
|  |     45 |     let
 | 
|  |     46 |       val currentPos = Unsynchronized.ref 0
 | 
|  |     47 |       fun grab n =
 | 
|  |     48 |         if !currentPos = String.size expression then ""
 | 
|  |     49 |         else
 | 
|  |     50 |           let
 | 
|  |     51 |             fun extractStr n =
 | 
|  |     52 |               let
 | 
|  |     53 |                 val s = String.extract (expression, !currentPos, SOME n)
 | 
|  |     54 |               in
 | 
|  |     55 |                 currentPos := !currentPos + n;
 | 
|  |     56 |                 s
 | 
|  |     57 |               end
 | 
|  |     58 |             val remaining = String.size expression - !currentPos
 | 
|  |     59 |           in if remaining < n then extractStr remaining else extractStr n
 | 
|  |     60 |           end
 | 
|  |     61 |       val (tree, _ (*remainder*)) =
 | 
|  |     62 |         parse LOOKAHEAD grab file_name
 | 
|  |     63 |     in tree end
 | 
|  |     64 | 
 | 
|  |     65 |   fun parse_file' lookahead file_name =
 | 
|  |     66 |     parse_expression
 | 
|  |     67 |      file_name
 | 
|  |     68 |      (File.open_input TextIO.inputAll (Path.explode file_name))
 | 
|  |     69 | end
 | 
|  |     70 | 
 | 
|  |     71 | val parse_file = parse_file' LOOKAHEAD
 | 
|  |     72 | 
 | 
|  |     73 | end
 |