Source file lp_doc.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
open Common
open Lplib
open Handle
module LSP = Lsp_base
let lp_logger = Buffer.create 100
type doc_node =
{ cmd : Pure.Command.t
; exec : bool
; goals : (Goal.info list * Pos.popt) list
}
type t = {
uri : string;
version: int;
text : string;
mutable root : Pure.state option;
mutable final : Pure.state option;
nodes : doc_node list;
logs : ((int * string) * Pos.popt) list;
map : Core.Term.qident RangeMap.t;
}
let option_default o1 d =
match o1 with | None -> d | Some x -> x
let mk_error ~doc pos msg =
LSP.mk_diagnostics ~uri:doc.uri ~version:doc.version [pos, 1, msg, None]
let buf_get_and_clear buf =
let res = Buffer.contents buf in
Buffer.clear buf; res
let format_error_msg error_msg error_pos =
let form = Format.formatter_of_buffer lp_logger in
Color.update_with_color form;
Format.fprintf (form) (Color.red "[%s] %s")
(Pos.popt_to_string
~print_dirname:false
~print_fname:false
(Some error_pos)) error_msg;
Format.pp_print_flush form ();
buf_get_and_clear lp_logger
let process_pstep (pstate,diags,logs) tac nb_subproofs =
let open Pure in
let tac_loc = Tactic.get_pos tac in
let hndl_tac_res = handle_tactic pstate tac nb_subproofs in
let logs = ((3, buf_get_and_clear lp_logger), tac_loc) :: logs in
match hndl_tac_res with
| Tac_OK (pstate, qres) ->
let goals = Some (current_goals pstate) in
let qres = match qres with None -> "OK" | Some x -> x in
pstate, (Tactic.keyword_pos tac, 4, qres, goals) :: diags, logs
| Tac_Error(loc,msg) ->
let loc = option_default loc tac_loc in
let goals = Some (current_goals pstate) in
pstate, (loc, 1, msg, goals) :: diags, ((1, msg), loc) :: logs
let process_proof pstate tacs logs =
Pure.ProofTree.fold process_pstep (pstate,[],logs) tacs
let get_goals dg_proof =
let rec get_goals_aux goals dg_proof =
match dg_proof with
| [] -> goals
| (loc,_,_, Some goalsList)::s ->
let g = (goals @ [goalsList, loc]) in get_goals_aux g s
| (loc,_,_,None)::s ->
let goals = (goals @ [[], loc]) in get_goals_aux goals s
in get_goals_aux [] dg_proof
let process_cmd _file (nodes,st,dg,logs) cmd =
let open Pure in
let cmd_loc = Command.get_pos cmd in
let hndl_cmd_res = handle_command st cmd in
let logs = ((3, buf_get_and_clear lp_logger), cmd_loc) :: logs in
match hndl_cmd_res with
| Cmd_OK (st, qres) ->
let qres = match qres with None -> "OK" | Some x -> x in
let nodes = { cmd; exec = true; goals = [] } :: nodes in
let ok_diag = Command.keyword_pos cmd, 4, qres, None in
nodes, st, ok_diag :: dg, logs
| Cmd_Proof (pst, tlist, thm_loc, qed_loc) ->
let start_goals = current_goals pst in
let pst, dg_proof, logs = process_proof pst tlist logs in
let goals =
get_goals ((thm_loc, 4, "OK", Some start_goals) :: dg_proof) in
let nodes = { cmd; exec = true; goals } :: nodes in
let dg_proof = (Command.keyword_pos cmd, 4, "OK", None) :: dg_proof in
let st, dg_proof, logs =
match end_proof pst with
| Cmd_OK (st, qres) ->
let dg_proof = match qres with
| None -> dg_proof
| Some x -> let pg = qed_loc, 4, x, None in pg :: dg_proof
in
let logs = ((3, buf_get_and_clear lp_logger), cmd_loc) :: logs in
st, dg_proof, logs
| Cmd_Error(_loc,msg) ->
let pg = qed_loc, 1, msg, None in
st, pg :: dg_proof, ((1, msg), qed_loc) :: logs
| Cmd_Proof _ ->
Lsp_io.log_error "process_cmd" "closing proof is nested";
assert false
in
nodes, st, dg_proof @ dg, logs
| Cmd_Error(err_loc, err_msg) ->
let nodes = { cmd; exec = false; goals = [] } :: nodes in
let loc, diag_msg, log_msg = match cmd_loc, err_loc with
| None, _ -> assert false
| _, None -> assert false
| _, Some None -> cmd_loc, err_msg, err_msg
| Some l, Some Some l' ->
if l.fname = l'.fname then
let log_msg = format_error_msg err_msg l' in
Some l', err_msg, log_msg
else
cmd_loc,
Pos.popt_to_string (Some l') ^ "\n" ^ err_msg,
Pos.popt_to_string (Some l') ^ "\n" ^ err_msg
in
nodes, st, (loc, 1, diag_msg, None) :: dg, ((1, log_msg), loc) :: logs
let new_doc ~uri ~version ~text =
let root, logs =
try
let uri = Uri.pct_decode uri in
assert(String.is_prefix "file://" uri);
let path = String.sub uri 7 (String.length uri - 7) in
Some(Pure.initial_state path), []
with Error.Fatal(_pos, msg, err_desc) ->
(None, [(1, msg ^ "\n" ^ err_desc), Some (Pos.file_start uri)])
in
{ uri;
text;
version;
root;
final = root;
nodes = [];
logs = logs;
map = RangeMap.empty;
}
let close_doc _modname = ()
let dummy_loc =
Lazy.from_val
Pos.{ fname = None
; start_line = 1
; start_col = 1
; start_offset = -1
; end_line = 2
; end_col = 2
; end_offset = -1
}
let check_text ~doc =
let uri, version = doc.uri, doc.version in
let cmds, error = Pure.parse_text ~fname:uri doc.text in
let root =
match doc.root with
| Some ss -> ss
| None ->
raise(Error.fatal_no_pos "Root state is missing probably because \
new_doc raised an exception")
in
let nodes, final, diags, logs =
List.fold_left (process_cmd uri) ([],root,[],[]) cmds in
let logs = List.rev logs
and diags =
List.fold_left (fun acc (pos,lvl,msg,goal) ->
match pos with
| None -> acc
| Some pos -> (pos,lvl,msg,goal) :: acc
) [] diags
in
let logs, diags =
match error with
| None -> logs, diags
| Some(error_pos,error_msg) ->
let log_msg = format_error_msg error_msg error_pos in
logs @ [((1, log_msg),Some error_pos)],
diags @ [error_pos,1,error_msg,None]
in
let map = Pure.rangemap cmds in
let doc = { doc with nodes; final=Some(final); map; logs } in
doc, LSP.mk_diagnostics ~uri ~version diags