forked from hendriktews/proof-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.ml
1046 lines (887 loc) · 37.2 KB
/
input.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* prooftree --- proof tree display for Proof General
*
* Copyright (C) 2011 - 2016 Hendrik Tews
*
* This file is part of "prooftree".
*
* "prooftree" is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* "prooftree" 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. See the GNU
* General Public License in file COPYING in this or one of the parent
* directories for more details.
*
* You should have received a copy of the GNU General Public License
* along with "prooftree". If not, see <http://www.gnu.org/licenses/>.
*
* $Id: input.ml,v 1.43 2016/01/23 12:57:14 tews Exp $
*)
(** Reading commands from nonblocking stdin *)
(*****************************************************************************
*****************************************************************************)
(** {2 Communition Protocol with Proof General}
The communication protocol with Proof General is mostly one-way:
Proof General sends display messages to Prooftree and Prooftree
never requests information for the proof-tree display from Proof
General. Prooftree sends a notification to Proof General when the
proof-tree window is closed. It also sends proof commands to Proof
General on request of the user.
The communication protocol between Proof General and Prooftree is
split into two parts: The display messages, which are sent from
Proof General to Prooftree and the notification messages, which
are sent from Prooftree to Proof General.
{3 Display Messages}
The protocol for the display messages is designed such that
Prooftree always knows in advance how many bytes it has to read
until the end of a message.
All display messages consist of
{ul
{- a first line of exactly 16 bytes (including the new line) of
the form "second line 157\n", where the number is the length of
the second line (including its final newline).}
{- a second line containing the display command and the length of
the additional data sections, if the command has data sections.}
{- the data sections (if any), where the last character of each
data sections is a newline.}
}
All data is UTF-8 encoded.
Some data sections have a prover specific format. Currently,
[prooftree] only supports Coq.
In the following list
of commands, ``%d'' stands for a positive integer and %s for a string
which contains no white space. ``\{cheated | not-cheated\}'' denotes
the alternative of either ``cheated'' or ``not-cheated''. An
integer following the keyword state is a state number. An integer
following some xxx-bytes denotes the number of bytes of the next
<data> section, including the final newline of that <data> section.
A ``[ \ ]'' at the end of a line denotes line continuation without
newline.
Prooftree understands the following display commands in the
following format. The first 16-byte line that preceeds every
display-command line is ommitted in the following list.
{ul
{- {v configure for "PA" and protocol version NN v}
Configure Prooftree for proof assistant PA and communication
protocol version NN. If proof assistant PA or version NN is not
supported, Prooftree displays an error message and exits. The name
PA might contain arbitrary characters but no quotation mark ( '"' ).
There must always be exectly one configure message, which must be
the first message.
}
{- {v current-goals state %d current-sequent %s \
{cheated | not-cheated} {new-layer | current-layer} proof-name-bytes %d \
command-bytes %d sequent-text-bytes %d additional-id-bytes %d \
existential-bytes %d\n\
<data-proof-name>\n\
<data-command>\n\
<data-current-sequent>\n\
<data-additional-ids>\n\
<data-existentials>\n v}
The [current-goals] command tells [prooftree] about a new proof
state with a new set of open goals. This corresponds to either of
the following cases:
{ol
{- The initial proof state of a newly started proof}
{- A proof command has been applied to the old current sequent,
yielding a new current sequent and possibly additional new
open subgoals}
{- The old current goal has been solved (by some proof command)
and the new current sequent is one of the previously spawned
subgoals}
{- A new set of proof-tree root goal nodes is associated with the
current proof. This happens for instance, when Coq transformes
open existential variables into proof goals with the command
[Grab Existential Variables].}
}
For case 1 or case 4 [new-layer] must be given. Otherwise,
[current-layer] must be specified and [prooftree] decides with the
help of its internal state whether case 2 or 3 applies.
For the second and the third case, the set of open goals does not
need to represent the total set of all open subgoals, but it must
contain all newly spawned subgoals.
The state number in the [current-goals] command is for undo. It
is interpreted as the state that has been reached after processing
the current command.
[current-sequent %s] denotes the ID of the current sequent. The
cheated flag tells [prooftree] whether the new proof state was
obtained by a cheating command such as [admit] or [sorry].
The data sections are :
{ol
{- Full name of the proof}
{- The proof command that yielded this proof state}
{- Text of the current sequent}
{- ID's of additionally open sequents (as space separated
list of strings)}
{- Prover specific information about existential variables.}
}
The second data section is ignored for initial proof states.
The text of newly created additional goals other then the
current goal is expected to arrive separately with an
[update-sequent] command.
}
{- {v update-sequent state %d sequent %s proof-name-bytes %d \
sequent-text-bytes %d\n\
<data-proof-name>\n\
<data-sequent>\n v}
The update sequent command updates the text of some
known sequent. Such updates are necessary for newly spawned
subgoals. But also when existential variables get instantiated.
The state number is for undo and the sequent ID denotes the
sequent to update. The data sections are:
{ol
{- Full name of the proof}
{- new sequent text}
}
}
{- {v switch-goal state %d sequent %s proof-name-bytes %d\n
<data-proof-name>\n v}
Switch goal tells [proftree] that the current goal has changed
without changing or solving the old current goal.
The state number is for undo and the only data section is:
{ol
{- Full name of the proof}
}
}
{- {v branch-finished state %d {cheated | not-cheated} \
proof-name-bytes %d command-bytes %d existential-bytes %d\n\
<data-proof-name>\n\
<data-command>\n\
<data-existentials>\n v}
[branch-finished] tells [prooftree] the last proof command that
closed the current branch. If there are still open subgoals, the
proof will hopefully continue with one of them, which is not yet
known. The cheated flag tells [prooftree]
whether the new proof state was obtained by a cheating command
such as [admit] or [sorry]. The data sections are :
{ol
{- Full name of the proof}
{- The last proof command}
{- Prover specific information about existential variables.}
}
}
{- {v proof-complete state %d proof-name-bytes %d\n\
<data-proof-name>\n v}
[proof-complete] tells Prooftree that the current proof has been
completed and will further not be updated. The only data section is:
{ol
{- Full name of the proof}
}
}
{- {v undo-to state %d\n v}
The state number here is not for undo, it is the undo-state.
Undo tells [prooftree] to change the display to the state before
the first command with a state strictly greater than [undo-state]
has been processed.
}
{- {v quit-proof proof-name-bytes %d\n\
<data-proof-name>\n v}
Quit closes the window for the indicated proof.
Cloned windows are not closed.
The only data section is:
{ol
{- Full name of the proof whoose window should be delected}
}
}
}
{3 Notification Messages}
The notification messages are sent from Prooftree to Proof General
as a consequence of certain user interactions. There are 3
different notification messages: for stopping the proof-tree
display, for undo and for sending proof scripts. All notification
messages are preceeded with a newline and the string
[emacs exec:], followed by a space, for easy recognition in Proof
General.
The remaining part of the messages have the following format.
{ul
{- {v stop-displaying v}
Prooftree sends this message to Proof General when the user closed
the proof-tree display of a proof currently under development.
Proof General then stops sending display commands for that proof.
}
{- {v undo %d v}
Prooftree sends the undo message, when the user selected an undo
for a certain sequent from the context menu. The integer is the
undo state number of the proof command child node of the selected
sequent.
}
{- {v insert-proof-script %d\n<script data>\n v}
Prooftree sends this message when the user selected the Insert
command or Insert subproof items from the context menu. The
integer is the length of [<script data>] without the enclosing
newlines.
}
}
*)
(** Version number of the communication protocol described and
implemented by this module.
*)
let protocol_version = 3
(** {2 General remarks}
This module reads display commands from a pipe. It may therefore happen
that the input buffer depletes in the middle of a command. In this
case we have to return control to the GTK main loop, which will
call this module again, if the operating system decides that it is
time to make more input available. The input channel is therefore
turned into non-blocking mode, which means that reading raises an
exception instead of blocking when currently no more input is
available. As a consequence, the parsing engine in this module
must be prepared to get interrupted whenever it tries to read from
the input channel.
The state of the parser is stored in the variable
{!Input.current_parser}, which holds the function to be called when
more input becomes available. It must always be set before new
input is read from the input channel. Typically, there are
partially filled buffers and index variables in the closure of
[current_parser].
*)
(*****************************************************************************
*****************************************************************************)
open Configuration
open Util
open Gtk_ext
(**/**)
module U = Unix
(**/**)
(** {2 Module Documentation}
{3 General parsing utilities and parser state}
*)
(** Exception raised if [prooftree] encounters an unknown or malformed command.
The first argument is a description of the error. If the error was caused
by an exception, the second argument carries this exception and the
execption backtrace until the point where [Protocol_error] was raised.
*)
exception Protocol_error of string * (exn * string) option
(** Parsing function for the info string of existential variables.
This function is proof assistant specific and must therefore be
set when the configure message is received in
{!configure_prooftree}. The default value here is a valid parser
that can be used for proof assistants that have no existential
variables.
*)
let parse_existential_info =
ref(fun _ -> ([], []) : string -> (string list * (string * string list) list))
(** Forward pointer to {!message_start}. Initialized in
{!setup_input}. The forward pointer is needed, because various
functions that must be defined before [message_start] must set
{!current_parser} to [message_start].
*)
let message_start_parser = ref (fun () -> ())
(** Parsing function to be called when the next input arrives. Typically
the closure of this function contains the parsing state, such as
partially filled buffers.
*)
let current_parser = ref (fun () -> ())
(** Output channel for saving a backup copy of all material from the input.
Set by option [-tee], mainly used for debugging.
*)
let input_backup_oc = ref None
(** Filename {!input_backup_oc} is referring to. Needed in order to
decide whether {!input_backup_oc} must be changed when the current
configuration changed.
*)
let input_backup_filename = ref None
(** Set {!Input.input_backup_oc} according to the current configuration.
*)
let setup_input_backup_channel () =
if !current_config.copy_input &&
!input_backup_filename = Some !current_config.copy_input_file
then ()
else if !current_config.copy_input = false &&
!input_backup_filename = None
then ()
else begin
(match !input_backup_oc with
| None -> ()
| Some oc ->
close_out oc;
input_backup_oc := None;
input_backup_filename := None;
);
if !current_config.copy_input
then begin
(try
input_backup_oc := Some(open_out !current_config.copy_input_file);
with
| Sys_error msg -> raise (Log_input_file_error msg)
);
input_backup_filename := Some !current_config.copy_input_file;
end else begin
input_backup_oc := None;
input_backup_filename := None;
end
end
(** Input function for reading from the input channel. To make the
input backup feature work (see option [-tee]) input must always be
read with this function. Arguments are the same as for {xref
stdlib val Pervasives.input}, [local_input buf start len] reads at
most [len] bytes from [stdin] into buffer [buf], starting at
position [start]. Any material read is immediately written to
{!Input.input_backup_oc}. Before calling this function,
{!Input.current_parser} must be set to the parsing continuation
function. This will be used in case parsing is interrupted now,
because no more input is currently available, and control is given
back to the GTK main loop. When more input becomes available the
GTK main loop calls this module again and the main parsing loop in
{!Input.parse_input} continues parsing with the function stored in
[current_parser].
@raise Sys_blocked_io when no more input is available currently
*)
let local_input buf start len =
let read_len = input stdin buf start len in
(match !input_backup_oc with
| None -> ()
| Some oc ->
output oc buf start read_len;
flush oc
);
if read_len = 0 then
raise (Protocol_error("Connection closed", None));
read_len
(** [get_string_cont s i len cont ()]
fills buffer [s] and continue parsing with [cont]. This
is a utility function for {!Input.get_string}. [get_string_cont s i
len cont ()] reads [len - i] bytes from the input channel and
stores them in [s] at position [i]. When finished it calles
[cont]. This function sets {!Input.current_parser} to itself
to continue reading later if not enough input is available now.
@raise Sys_blocked_io when not enough input is available currently
*)
let rec get_string_cont s i len continuation_fn () =
(* Printf.fprintf (debugc()) "GS cont %d - %d enter\n%!" i len; *)
current_parser := (get_string_cont s i len continuation_fn);
let n = local_input s i (len - i) in
(*
* Printf.fprintf (debugc()) "GS read %d bytes: %s\n%!"
* n (String.sub s i n);
*)
let i = i + n in
if i = len
then begin
(* Printf.fprintf (debugc()) "GS %d yields %s\n%!" len s; *)
continuation_fn (Bytes.to_string s)
end
else get_string_cont s i len continuation_fn ()
(** Main input function for strings. [get_string len cont] creates a
new string of length [len] and fills it from [stdin], saving a
copy to {!input_backup_oc}, and calls [cont new_string] as
continuation when finished. This function properly deals with
parsing interrupts (by setting {!Input.current_parser}
internally).
@raise Sys_blocked_io when not enough input is available currently
*)
let get_string len continuation_fn =
(* Printf.fprintf (debugc()) "GS %d enter\n%!" len; *)
let s = Bytes.create len in
get_string_cont s 0 len continuation_fn ()
(******************************************************************************
******************************************************************************
* configure for "PA" and protocol version NN
*)
(** {3 Configure command parser} *)
(** [true] if the configure message has been received. *)
let configure_message_received = ref false
(** Raise an error if no configure message has been received yet. *)
let check_if_configured () =
if not !configure_message_received then
raise (Protocol_error ("Configure message missing", None))
(** Process the configure message. Raise an error if the proof
assistant or the communication protocol version is not supported.
This function is the place were a new proof assistant must be
added.
*)
let configure_prooftree proof_assistant pg_protocol_version =
if !configure_message_received then
raise (Protocol_error ("Received a second configure message", None));
(match proof_assistant with
| "Coq" ->
parse_existential_info := Coq.coq_parse_existential_info
| "HOL Light" -> ()
| _ ->
raise (Protocol_error ("Unknown proof assistant " ^ proof_assistant,
None))
);
if protocol_version <> pg_protocol_version then
raise (Protocol_error
((Printf.sprintf
("Communication protocol mismatch.\n"
^^ "Proof General uses version %02d,\n"
^^ "but this version of Prooftree supports only version %02d.")
pg_protocol_version protocol_version),
None));
configure_message_received := true
(** Parse the configure message and process it. *)
let parse_configure com_buf =
Scanf.bscanf com_buf
" for \"%s@\" and protocol version %d" configure_prooftree
(******************************************************************************
******************************************************************************
* current-goals state %d current-sequent %s {cheated | not-cheated} \
* {new-layer | current-layer}
* proof-name-bytes %d command-bytes %d sequent-text-bytes %d \
* additional-id-bytes %d existential-bytes %d\n\
* <data-proof-name>\n\
* <data-command>\n\
* <data-current-sequent>\n\
* <data-additional-ids>\n\
* <data-existentials>\n
*)
(** {3 Current-goals command parser} *)
(** Finish parsing of the [current-goals] command and call
{!Proof_tree.process_current_goals} to display the new proof
state. The arguments are the unprocessed strings read from the
input channel in this order:
@param state state number from the first line of the command
@param current_sequent_id ID of the current sequent from the first
line of the command
@param cheated_string either "cheated" or "not-cheated" from the
first line of the command
@param layer_string either "new-layer" of "current-layer" from the
first line of the command
@param proof_name name of the current proof
@param proof_command text of the last proof command (or garbage if
this is the first state of the proof)
@param current_sequent_text text of the current sequent
@param additional_ids_string ID's of all currently open goals
@param existentials_string prover specific information about
existentials
*)
let parse_current_goals_finish state current_sequent_id cheated_string
layer_string proof_name proof_command current_sequent_text
additional_ids_string existentials_string =
(* Printf.fprintf (debugc()) "PCGF\n%!"; *)
let cheated_flag = match cheated_string with
| "not-cheated" -> false
| "cheated" -> true
| _ ->
raise(Protocol_error
("Parse error in current-goals command. " ^
"Expected \"cheated\" or \"not-cheated\" as 6th word.",
None))
in
let layer_flag = match layer_string with
| "new-layer" -> true
| "current-layer" -> false
| _ ->
raise(Protocol_error
("Parse error in current-goals command. " ^
"Expected \"new-layer\" or \"current-layer\" as 7th word.",
None))
in
let proof_name = chop_final_newlines proof_name in
let proof_command = chop_final_newlines proof_command in
let current_sequent_text = chop_final_newlines current_sequent_text in
let additional_ids_string = chop_final_newlines additional_ids_string in
let additional_ids = string_split ' ' additional_ids_string in
let existentials_string = chop_final_newlines existentials_string in
let (ex_uninst, ex_inst) = !parse_existential_info existentials_string in
Proof_tree.process_current_goals state proof_name proof_command cheated_flag
layer_flag current_sequent_id current_sequent_text additional_ids
ex_uninst ex_inst;
current_parser := !message_start_parser
(** Start parsing of the [current-goals] command. Extracts elements and
string length' from the [Scanf] parsing buffer argument and reads
all the necessary strings from the input channel. When reading finished
{!Input.parse_current_goals_finish} is called.
*)
let parse_current_goals com_buf =
check_if_configured ();
Scanf.bscanf com_buf
(" state %d current-sequent %s %s %s proof-name-bytes %d "
^^ "command-bytes %d sequent-text-bytes %d "
^^ "additional-id-bytes %d existential-bytes %d")
(fun state current_sequent_id cheated_string layer_string
proof_name_bytes command_bytes sequent_text_bytes additional_id_bytes
existential_bytes ->
(*
* Printf.fprintf (debugc())
* ("PCGs state %d current-sequent %s cheated %s layer %s "
* ^^ "proof-name-len %d command-len %d sequent-len %d "
* ^^ "id-len %d existential-len %d\n%!")
* state current_sequent_id cheated_string layer_string
* proof_name_bytes command_bytes sequent_text_bytes
* additional_id_bytes existential_bytes;
*)
get_string proof_name_bytes
(fun proof_name ->
get_string command_bytes
(fun proof_command ->
get_string sequent_text_bytes
(fun current_sequent_text ->
get_string additional_id_bytes
(fun additional_ids_string ->
get_string existential_bytes
(fun existentials_string ->
parse_current_goals_finish state current_sequent_id
cheated_string layer_string
proof_name proof_command current_sequent_text
additional_ids_string existentials_string))))))
(******************************************************************************
* update-sequent state %d sequent %s proof-name-bytes %d \
* sequent-text-bytes %d\n\
* <data-proof-name>\n
* <data-sequent>\n
*)
(** {3 Update-sequent command parser} *)
(** Finish parsing of the [update-sequent] command and call
{!Proof_tree.update_sequent} to update the sequent. The
arguments are as follows:
@param state state number
@param sequent_id ID of sequent to update
@param proof_name full proof name (as raw data section string)
@param sequent_text new sequent text (as raw data section string)
*)
let parse_update_sequent_finish state sequent_id proof_name sequent_text =
let proof_name = chop_final_newlines proof_name in
let sequent_text = chop_final_newlines sequent_text in
Proof_tree.update_sequent state proof_name sequent_id sequent_text;
current_parser := !message_start_parser
(** Parse and process a [update-sequent] command. Extracts the state and
the data section length' from the first command line in the [Scanf]
parsing buffer argument, reads the data sections and finally call
{!Input.parse_update_sequent_finish}.
*)
let parse_update_sequent com_buf =
check_if_configured ();
Scanf.bscanf com_buf
" state %d sequent %s proof-name-bytes %d sequent-text-bytes %d"
(fun state sequent_id proof_name_bytes sequent_text_bytes ->
get_string proof_name_bytes
(fun proof_name ->
get_string sequent_text_bytes
(fun sequent_text ->
parse_update_sequent_finish state sequent_id
proof_name sequent_text)))
(******************************************************************************
* switch-goal state %d sequent %s proof-name-bytes %d\n
* <data-proof-name>\n
*)
(** {3 Switch-goal command parser} *)
(** Finish parsing of the [switch-goal] command and process it with
{!Proof_tree.switch_to}. The arguments are as follows:
@param state state number
@param new_current_id ID of new current goal
@param proof_name full proof name (as raw data section string)
*)
let parse_switch_goal_finish state new_current_id proof_name =
let proof_name = chop_final_newlines proof_name in
Proof_tree.switch_to state proof_name new_current_id;
current_parser := !message_start_parser
(** Parse and process a [switch-goal] command. Extracts the state, the
new current sequent and the data section length from the first
command line in the [Scanf] parsing buffer argument, reads the
data section and finally calls {!Input.parse_switch_goal_finish}.
*)
let parse_switch_goal com_buf =
check_if_configured ();
Scanf.bscanf com_buf
" state %d sequent %s proof-name-bytes %d"
(fun state new_current_id proof_name_bytes ->
get_string proof_name_bytes
(fun proof_name ->
parse_switch_goal_finish state new_current_id proof_name))
(******************************************************************************
* branch-finished state %d {cheated | not-cheated} \
* proof-name-bytes %d command-bytes %d existential-bytes %d\n\
* <data-proof-name>\n\
* <data-command>\n\
* <data-existentials>\n
*)
(** {3 Branch-finished command parser} *)
(** Finish parsing of the [branch-finished] command and process it
with {!Proof_tree.process_branch_finished}. The arguments are
@param state state number
@param cheated_string either "cheated" or "not-cheated"
@param proof_name full proof name (as raw data section string)
@param proof_command last proof command (as raw data section string)
@param existentials_string prover specific data about existentials
*)
let parse_branch_finished_finish
state cheated_string proof_name proof_command existentials_string =
let cheated_flag = match cheated_string with
| "not-cheated" -> false
| "cheated" -> true
| _ ->
raise(Protocol_error
("Parse error in branch-finished command. " ^
"Expected \"cheated\" or \"not-cheated\" as 4th word.",
None))
in
let proof_name = chop_final_newlines proof_name in
let proof_command = chop_final_newlines proof_command in
let existentials_string = chop_final_newlines existentials_string in
let (ex_uninst, ex_inst) = !parse_existential_info existentials_string in
Proof_tree.process_branch_finished
state proof_name proof_command cheated_flag ex_uninst ex_inst;
current_parser := !message_start_parser
(** Parse and process a [proof-finished] command. Extracts the
necessary information from the first command line in the [Scanf]
parsing buffer argument, reads the data section and finally calls
{!Input.parse_branch_finished_finish}.
*)
let parse_branch_finished com_buf =
check_if_configured ();
Scanf.bscanf com_buf
" state %d %s proof-name-bytes %d command-bytes %d existential-bytes %d"
(fun state cheated_string proof_name_bytes
command_bytes existential_bytes ->
get_string proof_name_bytes
(fun proof_name ->
get_string command_bytes
(fun proof_command ->
get_string existential_bytes
(fun existentials_string ->
parse_branch_finished_finish
state cheated_string proof_name
proof_command existentials_string))))
(******************************************************************************
* proof-complete state %d proof-name-bytes %d\n\
* <data-proof-name>\n
*)
(** {3 Proof-complete command parser} *)
(** Parse and process a [proof-complete] command. Extracts information
from the command and process it with
{!Proof_tree.process_proof_complete}.
*)
let parse_proof_complete com_buf =
check_if_configured ();
Scanf.bscanf com_buf " state %d proof-name-bytes %d"
(fun state proof_name_bytes ->
get_string proof_name_bytes
(fun proof_name ->
let proof_name = chop_final_newlines proof_name in
Proof_tree.process_proof_complete state proof_name;
current_parser := !message_start_parser))
(******************************************************************************
* undo-to state %d\n
*)
(** {3 Undo-to command parser} *)
(** Parse an [undo-to] command and call {!Proof_tree.undo} to process it.
*)
let do_undo com_buf =
check_if_configured ();
Scanf.bscanf com_buf " state %d" Proof_tree.undo
(*****************************************************************************
*
* quit-proof proof-name-bytes %d\n\
* <data-proof-name>\n
*)
(** {3 Quit-proof command parser} *)
(** Finish parsing a [quit-proof] command and process it with
{!Proof_tree.quit_proof}. The argument is
@param proof_name full proof name (as raw data section string)
*)
let parse_quit_proof_finish proof_name =
let proof_name = chop_final_newlines proof_name in
Proof_tree.quit_proof proof_name;
current_parser := !message_start_parser
(** Parse and process a [quit-proof] command. Extracts the
data-section length from the first line in the [Scanf] parsing
buffer, reads the data section and finally calls
{!Input.parse_quit_proof_finish}.
*)
let parse_quit_proof com_buf =
check_if_configured ();
Scanf.bscanf com_buf " proof-name-bytes %d"
(fun proof_name_bytes ->
get_string proof_name_bytes parse_quit_proof_finish)
(*****************************************************************************
*
* general parsing
*
*****************************************************************************)
(** {3 General command parser} *)
(** Parse and process a command. Argument [command] holds the complete
second line of the display command. This function only builds a scanning
buffer from [command] and switches to the different command
parsers, depending on the first word in [command].
@raise Sys_blocked_io if parsing gets interrupted
*)
let parse_command command =
(* Printf.fprintf (debugc()) "PC %s\n%!" command; *)
let com_buf = Scanf.Scanning.from_string command in
Scanf.bscanf com_buf "%s "
(function
| "configure" -> parse_configure com_buf
| "current-goals" -> parse_current_goals com_buf
| "update-sequent" -> parse_update_sequent com_buf
| "switch-goal" -> parse_switch_goal com_buf
| "branch-finished" -> parse_branch_finished com_buf
| "proof-complete" -> parse_proof_complete com_buf
| "undo-to" -> do_undo com_buf
| "quit-proof" -> parse_quit_proof com_buf
| _ ->
raise (Protocol_error ("Parse error on input \"" ^ command ^ "\"",
None))
);
current_parser := !message_start_parser;
()
(** [read_second_line first_line] extracts the length of the second
line from [first_line], reads the second line and switches to
{!parse_command} to process the complete display command.
@raise Sys_blocked_io if parsing gets interrupted
*)
let read_second_line first_line =
Scanf.sscanf first_line "second line %3d\n"
(fun second_line_len ->
(* Printf.fprintf (debugc()) "second line cont %d\n%!" second_line_len; *)
get_string second_line_len parse_command)
(** Read the first, fixed-length line of a display command and switch
to {!read_second_line} to process the complete display command.
This function is the entry point into the display-command parser.
All command parsing functions set {!Input.current_parser} to
[read_command_line] when they are finished with their work. This
way, this function is called again to parse the next command by
the main parsing loop in {!Input.parse_input}.
@raise Sys_blocked_io if parsing gets interrupted
@raise Protocol_error for parsing and protocol errors
*)
let message_start () =
(* every message starts with a line "second line %03d"
* where the number gives the bytes in the next line
*)
(* Printf.fprintf (debugc()) "message start\n%!"; *)
try
get_string 16 read_second_line
with
| Scanf.Scan_failure _
| Failure _
| End_of_file as e
->
let bt = Printexc.get_backtrace() in
raise (Protocol_error ("Parse error", Some(e, bt)))
(** {3 Main parsing loop, GTK callback and initialization} *)
(** Main parsing loop. Calls the function in {!Input.current_parser}
in an infinite loop until input depletes and parsing is
interrupted. If parsing is interrupted this function calls
{!Proof_tree.finish_drawing} to schedule a redisplay of the proof
tree if necessary.
All parsing functions raise [Sys_blocked_io] if no more input is
available. This exception is caught here.
This function always returns [true] to tell the GTK main loop to
keep calling this module.
@raise Protocol_error for parsing and protocol errors
*)
let parse_input () =
try
(* Printf.fprintf (debugc()) "PI first\n%!"; *)
while true do
(* Printf.fprintf (debugc()) "PI next\n%!"; *)
!current_parser ()
done;
true
with
| Sys_blocked_io ->
(* Printf.fprintf (debugc()) "PI finished\n%!"; *)
Proof_tree.finish_drawing ();
true
(** Input callback without exception handling. The argument comes from
the GTK main loop and indicates the condition on the watched
channel. Because I only registered a callback for the [`IN] (i.e.,
input available) condition, I only deal with the case where the
argument is [[`IN]]. For all other arguments
{!Input.Protocol_error} is thrown, because I either don't expect
them or I don't know what they mean.
*)
let parse_input_callback = function
| [`IN] -> parse_input ()
| clist ->
raise (Protocol_error
("Strange callback condition " ^
(String.concat " "
(List.map (function
| `IN -> "IN"
| `OUT -> "OUT"
| `PRI -> "PRI"
| `ERR -> "ERR"
| `HUP -> "HUP"
| `NVAL -> "NVAL")
clist)),
None))
(** Internal counter of fatal error conditions of the command
processing. If a fatal error occurs, it is normally displayed in a
popup message. Special circumstances might cause a fatal error to
repeately occur. Then this counter causes prooftree to terminate
at some state instead of generating an infinite number of popup
messages.
*)
let error_counter = ref 0
(** Callback for the GTK main loop when input is available on [stdin].
This is just an exception wrapper around
{!Input.parse_input_callback}. In case of an escaping exception a
popup message is displayed and the same message is printed on
[stderr]. For {!Input.Protocol_error} and [End_of_input] the
message only contains a backtrace if [debug_mode] in the current
configuration (see {!Configuration.t} and
{!Configuration.current_config}) is true. For other exceptions the
message does always contain the backtrace.
*)
let parse_input_callback_ex clist =
try
parse_input_callback clist
with
| e ->
incr error_counter;
if !error_counter > 20 then exit 2;
let backtrace = Printexc.get_backtrace() in
let buf = Buffer.create 4095 in
let print_backtrace = ref !current_config.debug_mode in
let prev_exception = ref None in
(match e with
| Protocol_error(err, prev_e) ->
Printf.bprintf buf "Protocol error!\n%s\nClosing connection." err;
prev_exception := prev_e
| _ ->
Buffer.add_string buf
"Internal error: Escaping exception in parse_input";
print_backtrace := true;
);
if !print_backtrace then begin
Buffer.add_char buf '\n';
Buffer.add_string buf (Printexc.to_string e);