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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
let full_mode_switch =
Report.Warning.mk ~code:Code.generic ~mnemonic:"full-mode-switch"
~message:(fun fmt lang ->
Format.fprintf fmt
"The@ %s@ format@ does@ not@ support@ \
incremental@ mode,@ switching@ to@ full@ mode"
lang)
~name:"Forced switch to full mode" ()
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 (p_error_ref, perr) ->
match perr with
| `Regular msg ->
Format.fprintf fmt "%t" msg
| `Advanced (error_ref, prod, lexed, expected) ->
let p_ref fmt = if p_error_ref then Format.fprintf fmt "(%s)@ " error_ref in
Format.fprintf fmt
"@[<v>@[<hv>%twhile parsing %t,@ read %t,@]@ @[<hov>but expected %t.@]@]"
p_ref prod lexed expected)
~name:"Parsing error" ()
let stdin_prelude =
Report.Error.mk ~code:Code.parsing ~mnemonic:"stdin-prelude"
~message:(fun fmt () ->
Format.fprintf fmt
"Standard input can't be used as a prelude.")
~name:"Prelude error" ()
type 'a file = 'a State.file
module type S = Parser_intf.S
module Make(State : State.S) = struct
type nonrec 'a file = 'a file
module S = Dolmen.Std.Statement
let pipe = "Parser"
let syntax_error_ref = State.create_key ~pipe "syntax_error_ref"
let interactive_prompt = State.create_key ~pipe "interactive_prompt"
let interactive_prompt_default _ = None
let interactive_prompt_lang st =
match State.get State.logic_file st with
| { source = `Stdin; _ } ->
begin match (State.get State.logic_file st).lang with
| None -> Some "prompt> @?"
| Some l ->
Some (Format.asprintf "(%s)# @?" (Logic.string_of_language l))
end
| _ -> None
let init
?syntax_error_ref:(syntax_error_ref_value=false)
?interactive_prompt:(interactive_prompt_value=interactive_prompt_default)
= fun st ->
st
|> State.set syntax_error_ref syntax_error_ref_value
|> State.set interactive_prompt interactive_prompt_value
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 switch_to_full_mode ?loc lang st (old : _ file) (file: _ file) =
let st =
match old.mode with
| Some `Incremental -> State.warn ?loc st full_mode_switch lang
| _ -> st
in
st, { file with mode = Some `Full }
let switch_to_full_mode_if_needed ?loc st old (file : _ file) =
match file.lang with
| Some Logic.Alt_ergo -> switch_to_full_mode ?loc "Alt-Ergo" st old file
| _ -> st, file
let set_logic_file ?loc st (file : _ file) =
let old = State.get State.logic_file st in
match old.lang, file.lang with
| Some l, Some l' when l <> l' ->
State.error ~file ?loc st input_lang_changed (l', l)
| _ ->
let st, new_file = switch_to_full_mode_if_needed ?loc st old file in
State.set State.logic_file new_file st
let gen_finally (gen : 'a Gen.t) cl : 'a Gen.t =
let () = Gc.finalise (fun _ -> 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 ~file g = fun st ->
begin match (State.get interactive_prompt st) st with
| None -> ()
| Some prelude -> Format.printf "%s @?" prelude
end;
match g () with
| ret -> st, ret
| exception Dolmen.Std.Loc.Uncaught (loc, exn, bt) ->
let st =
State.error st ~file ~loc:{ file = file.loc; loc; }
Report.Error.uncaught_exn (exn, bt)
in
st, None
| exception Dolmen.Std.Loc.Lexing_error (loc, lex) ->
let st = State.error st ~file ~loc:{ file = file.loc; loc; } lexing_error lex in
st, None
| exception Dolmen.Std.Loc.Syntax_error (loc, perr) ->
let syntax_error_ref = State.get_or ~default:false syntax_error_ref st in
let st = State.error st ~file ~loc:{ file = file.loc; loc; } parsing_error (syntax_error_ref, perr) in
st, None
let parse_stdin st (file : Logic.language file) =
match file.mode with
| None
| Some `Incremental ->
let lang, file_loc, gen, _ = Logic.parse_input
?language:file.lang (`Stdin (Logic.Smtlib2 `Latest))
in
let file = { file with loc = file_loc; lang = Some lang; } in
st, file, gen
| Some `Full ->
let lang, file_loc, l = Logic.parse_all
?language:file.lang (`Stdin (Logic.Smtlib2 `Latest))
in
let file = { file with loc = file_loc; lang = Some lang; } in
st, file, gen_of_llist l
let parse_file st source (file : Logic.language file) lang =
match source with
| `Raw (filename, contents) ->
begin match file.mode with
| None
| Some `Incremental ->
let lang, file_loc, gen, cl = Logic.parse_input
~language:lang (`Raw (filename, lang, contents)) in
let file = { file with loc = file_loc; lang = Some lang; } in
st, file, gen_finally gen cl
| Some `Full ->
let lang, file_loc, l = Logic.parse_all
~language:lang (`Raw (filename, lang, contents))
in
let file = { file with loc = file_loc; lang = Some lang; } in
st, file, gen_of_llist l
end
| `File f ->
let s = Dolmen.Std.Statement.include_ f [] 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
let file = { file with lang = Some lang; } in
st, file, (Gen.singleton s')
type 'a gen' = {g: State.t -> State.t * 'a option} [@@unboxed]
let flat_map (f : 'a -> 'b gen') ({ g } : 'a gen') : 'b gen' =
let cur_f = ref None in
let rec aux st =
match !cur_f with
| None ->
let st, x = g st in
begin match x with
| None -> st, None
| Some x ->
cur_f := Some (f x);
aux st
end
| Some { g = c } ->
let st, c' = c st in
match c' with
| Some _ -> st, c'
| None ->
cur_f := None;
aux st
in
{ g = aux }
let map_st_gen f (g : _ Gen.t) : _ gen' =
{ g = fun st ->
begin match g () with
| None -> st, None
| Some x -> f st x
end }
let parse_logic ?(preludes = []) file =
Gen.(
append
(of_list preludes |> map (fun p -> (p, true)))
(singleton (file, false)))
|> map_st_gen (fun st ((file : _ file), is_prelude) ->
match file.source with
| `Stdin when is_prelude ->
State.error ~file st stdin_prelude (), None
| `Stdin ->
let st, file, g = parse_stdin st file in
let st = State.set State.logic_file file st in
st, Some ({ g = wrap_parser ~file g })
| `Raw (filename, _) | `File filename as source ->
try
let file, lang =
match file.lang with
| Some l -> file, l
| None ->
let l, _, _ = Logic.of_filename filename in
{ file with lang = Some l }, l
in
let st, file = switch_to_full_mode_if_needed st file file in
let st, file, g = parse_file st source file lang in
let st = State.set State.logic_file file st in
st, Some ({ g = wrap_parser ~file g })
with Logic.Extension_not_found ext ->
State.error st extension_not_found ext, None
) |> flat_map Fun.id |> fun { g } -> g
let parse_response prelude st (file : Response.language file) =
let st, file, g =
try
match file.source with
| `Stdin ->
let lang, file_loc, gen, _ = Response.parse_input
?language:file.lang (`Stdin (Response.Smtlib2 `Latest))
in
let file = { file with loc = file_loc; lang = Some lang; } in
st, file, gen
| `Raw (filename, contents) ->
let lang =
match file.lang with
| Some l -> l
| None ->
let res, _, _ = Response.of_filename filename in
res
in
begin match file.mode with
| None
| Some `Incremental ->
let lang, file_loc, gen, cl = Response.parse_input
~language:lang (`Raw (filename, lang, contents)) in
let file = { file with loc = file_loc; lang = Some lang; } in
st, file, gen_finally gen cl
| Some `Full ->
let lang, file_loc, l =
Response.parse_all ?language:file.lang
(`Raw (filename, Response.Smtlib2 `Latest, contents))
in
let file = { file with loc = file_loc; lang = Some lang; } in
st, file, gen_of_llist l
end
| `File f ->
begin match Response.find ?language:file.lang ~dir:file.dir f with
| None ->
let st = State.error st file_not_found (file.dir, f) in
st, file, Gen.empty
| Some filename ->
begin match file.mode with
| None
| Some `Incremental ->
let lang, file_loc, gen, cl =
Response.parse_input ?language:file.lang (`File filename)
in
let file = { file with loc = file_loc; lang = Some lang; } in
st, file, gen_finally gen cl
| Some `Full ->
let lang, file_loc, l =
Response.parse_all ?language:file.lang (`File filename)
in
let file = { file with loc = file_loc; lang = Some lang; } in
st, file, gen_of_llist l
end
end
with
| Logic.Extension_not_found ext ->
State.error st extension_not_found ext, file, Gen.empty
in
let st = State.set State.response_file file st in
st, wrap_parser ~file (Gen.append (Gen.of_list prelude) g)
let merge _ st = st
let expand st c =
let ret = match c with
| { S.descr = S.Pack l; attrs; _ } ->
let file = State.get State.logic_file st in
let g =
Gen.of_list l
|> Gen.map (S.add_attrs attrs)
in
st, `Gen (merge, wrap_parser ~file g)
| { S.descr = S.Include file; _ } ->
let logic_file = State.get State.logic_file st in
let loc = { Dolmen.Std.Loc.file = logic_file.loc; loc = c.loc; } in
let language = logic_file.lang in
let dir = logic_file.dir in
begin
match Logic.find ?language ~dir file with
| None ->
State.error ~loc st file_not_found (dir, file), `Ok
| Some file ->
begin match logic_file.mode with
| None
| Some `Incremental ->
let lang, file_loc, gen, cl = Logic.parse_input ?language (`File file) in
let new_logic_file = { logic_file with loc = file_loc; lang = Some lang; } in
let st = set_logic_file st new_logic_file in
st, `Gen (merge, wrap_parser ~file:new_logic_file (gen_finally gen cl))
| Some `Full ->
let lang, file_loc, l = Logic.parse_all ?language (`File file) in
let new_logic_file = { logic_file with loc = file_loc; lang = Some lang; } in
let st = set_logic_file st new_logic_file in
st, `Gen (merge, wrap_parser ~file:new_logic_file (gen_of_llist l))
end
end
| _ -> (st, `Ok)
in
ret
end