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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
let extension_not_found =
Report.Error.mk ~code:Code.generic ~mnemonic:"ext-unknown"
~message:(fun fmt ext ->
Format.fprintf fmt
"@[<hv>The following extension was not recognized: '%s'.@ %s" ext
"Please use a recognised extension or specify an input language on the command line")
~name:"File extension unknown" ()
let file_not_found =
Report.Error.mk ~code:Code.generic ~mnemonic:"file-not-found"
~message:(fun fmt (dir, f) ->
if dir = "." then
Format.fprintf fmt "File not found: '%s'" f
else
Format.fprintf fmt
"File not found: '%s' in directory '%s'" f dir)
~name:"File not Found" ()
let input_lang_changed =
Report.Error.mk ~code:Code.generic ~mnemonic:"input-lang-changed"
~message:(fun fmt (old_lang, new_lang) ->
Format.fprintf fmt
"Input language changed from %s to %s (probably because of an include statement)"
(Logic.string_of_language old_lang)
(Logic.string_of_language new_lang))
~name:"Input language change" ()
let lexing_error =
Report.Error.mk ~code:Code.parsing ~mnemonic:"lexing-error"
~message:(fun fmt lex ->
Format.fprintf fmt
"Lexing error: invalid character '%s'" lex)
~name:"Lexing error" ()
let parsing_error =
Report.Error.mk ~code:Code.parsing ~mnemonic:"parsing-error"
~message:(fun fmt perr ->
match perr with
| `Regular msg ->
Format.fprintf fmt "%t" msg
| `Advanced (prod, lexed, expected) ->
Format.fprintf fmt
"@[<v>@[<hv>while parsing %t,@ read %t,@]@ @[<hov>but expected %t.@]@]"
prod lexed expected)
~name:"Parsing error" ()
module Pipe
(Expr : Expr_intf.S)
(State : State_intf.Parser_pipe
with type term := Expr.term)
= struct
module S = Dolmen.Std.Statement
let set_lang ?loc st l =
match State.input_lang st with
| None -> State.set_lang st l
| Some l' ->
if l = l'
then State.set_lang st l
else State.error ?loc st input_lang_changed (l', l)
let gen_finally (gen : 'a Gen.t) cl : 'a Gen.t =
let () = Gc.finalise_last cl gen in
let aux () =
match gen () with
| Some _ as res -> res
| None -> cl (); None
| exception exn ->
let bt = Printexc.get_raw_backtrace () in
cl ();
Printexc.raise_with_backtrace exn bt
in
aux
let wrap_parser g = fun st ->
if State.is_interactive st then
Format.printf "%s @?" (State.prelude st);
match g () with
| ret -> st, ret
| exception Dolmen.Std.Loc.Uncaught (loc, exn, bt) ->
let file = State.input_file_loc st in
let st =
State.error st ~loc:{ file; loc; } Report.Error.uncaught_exn (exn, bt)
in
st, None
| exception Dolmen.Std.Loc.Lexing_error (loc, lex) ->
let file = State.input_file_loc st in
let st = State.error st ~loc:{ file; loc; } lexing_error lex in
st, None
| exception Dolmen.Std.Loc.Syntax_error (loc, perr) ->
let file = State.input_file_loc st in
let st = State.error st ~loc:{ file; loc; } parsing_error perr in
st, None
let parse prelude st =
let st', g =
try
match State.input_source st with
| `Stdin ->
let lang, file_loc, gen, _ = Logic.parse_input
?language:(State.input_lang st)
(`Stdin (Logic.Smtlib2 `Latest))
in
let st = State.set_input_file_loc st file_loc in
let st = set_lang st lang in
st, gen
| `Raw (filename, contents) ->
let lang =
match State.input_lang st with
| Some l -> l
| None ->
let res, _, _ = Logic.of_filename filename in
res
in
let lang, file_loc, gen, cl = Logic.parse_input
~language:lang (`Raw (filename, lang, contents)) in
let st = State.set_input_file_loc st file_loc in
let st = set_lang st lang in
st, gen_finally gen cl
| `File f ->
let s = Dolmen.Std.Statement.include_ f [] in
let lang =
match State.input_lang st with
| Some l -> l
| None ->
let res, _, _ = Logic.of_filename f in
res
in
let s' =
match lang with
| Logic.Zf
| Logic.ICNF
| Logic.Smtlib2 _
| Logic.Alt_ergo -> s
| Logic.Dimacs
| Logic.Tptp _ ->
Dolmen.Std.Statement.pack [s; Dolmen.Std.Statement.prove ()]
in
set_lang st lang,
(Gen.singleton s')
with
| Logic.Extension_not_found ext ->
State.error st extension_not_found ext, Gen.empty
in
st', wrap_parser (Gen.append (Gen.of_list prelude) g)
let merge _ st = st
let gen_of_llist l =
let l = ref l in
(fun () -> match Lazy.force !l with
| [] -> None
| x :: r ->
l := (lazy r); Some x
)
let expand st c =
let ret = match c with
| { S.descr = S.Pack l; _ } ->
st, `Gen (merge, wrap_parser (Gen.of_list l))
| { S.descr = S.Include file; _ } ->
let loc = { Dolmen.Std.Loc.file = State.input_file_loc st; loc = c.loc; } in
let language = State.input_lang st in
let dir = State.input_dir st in
begin
match Logic.find ?language ~dir file with
| None ->
State.error ~loc st file_not_found (dir, file), `Ok
| Some file ->
begin match State.input_mode st with
| None
| Some `Incremental ->
let lang, file_loc, gen, cl = Logic.parse_input ?language (`File file) in
let st = State.set_input_file_loc st file_loc in
let st = set_lang ~loc st lang in
st, `Gen (merge, wrap_parser (gen_finally gen cl))
| Some `Full ->
let lang, file_loc, l = Logic.parse_file_lazy ?language file in
let st = State.set_input_file_loc st file_loc in
let st = set_lang ~loc st lang in
st, `Gen (merge, wrap_parser (gen_of_llist l))
end
end
| _ -> (st, `Ok)
in
ret
end