-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseutils.ml
69 lines (62 loc) · 2.52 KB
/
parseutils.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(* This file is part of Arsenic, a proofchecker for New Lace logic.
Copyright (c) 2015 Richard Bornat.
Licensed under the MIT license (sic): see LICENCE.txt or
https://opensource.org/licenses/MIT
*)
open Sourcepos
exception Error of string
(* ********************* nowhere else to put this ******************************* *)
let parse_formula default string =
let lexbuf = Lexing.from_string string in
try
Settings.temp_setting Settings.allow_special_formulas true
(fun () -> Parser.justaformula Lexer.make_token lexbuf)
with
| Parsing.Parse_error ->
(match default with
| Some default ->
let curr = lexbuf.Lexing.lex_curr_p in
Printf.printf "\n**Parse error at character %d (just before \"%s\") \
when parsing assertion-string %s"
(curr.Lexing.pos_cnum-curr.Lexing.pos_bol)
(Lexing.lexeme lexbuf)
string;
default
| _ -> raise Parsing.Parse_error
)
| exn ->
(match default with
| Some default ->
Printf.printf "\n**Unexpected exception %s \
when parsing assertion-string %s"
(Printexc.to_string exn)
string;
default
| _ -> raise exn
)
let parse_axiom = parse_formula (Some Formula._recTrue)
(* ********************* what Arsenic, ToLatex and Compile fall back on ******************************* *)
let parse_program filename =
let in_channel = open_in filename in
let lexbuf = Lexing.from_channel in_channel in
try
let result = Parser.program Lexer.make_token lexbuf in
close_in in_channel;
result
with
| Parsing.Parse_error ->
(close_in in_channel;
let curr = lexbuf.Lexing.lex_curr_p in
raise (Error ("**Parse error at line "^string_of_int (curr.Lexing.pos_lnum)^
" character "^string_of_int (curr.Lexing.pos_cnum-curr.Lexing.pos_bol)^
" (just before \""^Lexing.lexeme lexbuf^"\")"))
)
| Program.ParseError(spos,s) ->
(close_in in_channel;
raise (Error ("\n**SYNTAX ERROR at "^string_of_sourcepos spos ^ ": " ^ s))
)
| Lexer.LexError(spos,s) ->
(close_in in_channel;
raise (Error ("\n**LEXING ERROR at "^string_of_sourcepos spos ^ ": " ^ s))
)
| exn -> (close_in in_channel; raise exn)