| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
let error s = |
| 14 |
prerr_endline ("fake_ide: error: "^s); |
| 15 |
exit 1 |
| 16 |
|
| 17 |
let pperr_endline pp = Format.eprintf "@[%a@]\n%!" Pp.pp_with pp |
| 18 |
|
| 19 |
type coqtop = { |
| 20 |
xml_printer : Xml_printer.t; |
| 21 |
xml_parser : Xml_parser.t; |
| 22 |
} |
| 23 |
|
| 24 |
let print_error msg = |
| 25 |
Format.eprintf "fake_ide: error: @[%a@]\n%!" Pp.pp_with msg |
| 26 |
|
| 27 |
let base_eval_call ?(print=true) ?(fail=true) call coqtop = |
| 28 |
if print then prerr_endline (Xmlprotocol.pr_call call); |
| 29 |
let xml_query = Xmlprotocol.of_call call in |
| 30 |
Xml_printer.print coqtop.xml_printer xml_query; |
| 31 |
let rec loop () = |
| 32 |
let xml = Xml_parser.parse coqtop.xml_parser in |
| 33 |
match Xmlprotocol.msg_kind xml with |
| 34 |
| Xmlprotocol.Feedback |
| 35 |
| Xmlprotocol.LtacDebugInfo -> |
| 36 |
loop () |
| 37 |
| Xmlprotocol.Other -> |
| 38 |
Xmlprotocol.to_answer call xml |
| 39 |
in |
| 40 |
let res = loop () in |
| 41 |
if print then prerr_endline (Xmlprotocol.pr_full_value call res); |
| 42 |
match res with |
| 43 |
| Interface.Fail (_,_,s) when fail -> print_error s; exit 1 |
| 44 |
| Interface.Fail (_,_,s) as x -> print_error s; x |
| 45 |
| x -> x |
| 46 |
|
| 47 |
let eval_call c q = ignore(base_eval_call c q) |
| 48 |
|
| 49 |
module Parser = struct |
| 50 |
|
| 51 |
exception Err of string |
| 52 |
exception More |
| 53 |
|
| 54 |
type token = |
| 55 |
| Tok of string * string |
| 56 |
| Top of token list |
| 57 |
|
| 58 |
type grammar = |
| 59 |
| Alt of grammar list |
| 60 |
| Seq of grammar list |
| 61 |
| Opt of grammar |
| 62 |
| Item of (string * (string -> token * int)) |
| 63 |
|
| 64 |
let eat_rex x = x, fun s -> |
| 65 |
if Str.string_match (Str.regexp x) s 0 then begin |
| 66 |
let m = Str.match_end () in |
| 67 |
let w = String.sub s 0 m in |
| 68 |
Tok(x,w), m |
| 69 |
end else raise (Err ("Regexp "^x^" not found in: "^s)) |
| 70 |
|
| 71 |
let eat_balanced c = |
| 72 |
let c' = match c with |
| 73 |
| '{' -> '}' | '(' -> ')' | '[' -> ']' | _ -> assert false in |
| 74 |
let sc, sc' = String.make 1 c, String.make 1 c' in |
| 75 |
let name = sc ^ "..." ^ sc' in |
| 76 |
let unescape s = |
| 77 |
Str.global_replace (Str.regexp ("\\\\"^sc)) sc |
| 78 |
(Str.global_replace (Str.regexp ("\\\\"^sc')) sc' s) in |
| 79 |
name, fun s -> |
| 80 |
if s.[0] = c then |
| 81 |
let rec find n m = |
| 82 |
if String.length s <= m then raise More; |
| 83 |
if s.[m] = c' then |
| 84 |
if n = 0 then Tok (name, unescape (String.sub s 1 (m-1))), m+1 |
| 85 |
else find (n-1) (m+1) |
| 86 |
else if s.[m] = c then find (n+1) (m+1) |
| 87 |
else if s.[m] = '\\' && String.length s > m+1 && s.[m+1] = c then |
| 88 |
find n (m+2) |
| 89 |
else if s.[m] = '\\' && String.length s > m+1 && s.[m+1] = c' then |
| 90 |
find n (m+2) |
| 91 |
else find n (m+1) |
| 92 |
in find ~-1 0 |
| 93 |
else raise (Err ("Balanced "^String.make 1 c^" not found in: "^s)) |
| 94 |
|
| 95 |
let eat_blanks s = snd (eat_rex "[ \r\n\t]*") s |
| 96 |
|
| 97 |
let s = ref "" |
| 98 |
|
| 99 |
let parse g ic = |
| 100 |
let read_more () = s := !s ^ input_line ic ^ "\n" in |
| 101 |
let ensure_non_empty n = if n = String.length !s then read_more () in |
| 102 |
let cut_after s n = String.sub s n (String.length s - n) in |
| 103 |
let rec wrap f n = |
| 104 |
try |
| 105 |
ensure_non_empty n; |
| 106 |
let _, n' = eat_blanks (cut_after !s n) in |
| 107 |
ensure_non_empty n'; |
| 108 |
let t, m = f (cut_after !s (n+n')) in |
| 109 |
let _, m' = eat_blanks (cut_after !s (n+n'+m)) in |
| 110 |
t, n+n'+m+m' |
| 111 |
with More -> read_more (); wrap f n in |
| 112 |
let rec aux n g res : token list * int = |
| 113 |
match g with |
| 114 |
| Item (_,f) -> |
| 115 |
let t, n = wrap f n in |
| 116 |
t :: res, n |
| 117 |
| Opt g -> |
| 118 |
(try let res', n = aux n g [] in Top (List.rev res') :: res, n |
| 119 |
with Err _ -> Top [] :: res, n) |
| 120 |
| Alt gl -> |
| 121 |
let rec fst = function |
| 122 |
| [] -> raise (Err ("No more alternatives for: "^cut_after !s n)) |
| 123 |
| g :: gl -> |
| 124 |
try aux n g res |
| 125 |
with Err s -> fst gl in |
| 126 |
fst gl |
| 127 |
| Seq gl -> |
| 128 |
let rec all (res,n) = function |
| 129 |
| [] -> res, n |
| 130 |
| g :: gl -> all (aux n g res) gl in |
| 131 |
all (res,n) gl in |
| 132 |
let res, n = aux 0 g [] in |
| 133 |
s := cut_after !s n; |
| 134 |
List.rev res |
| 135 |
|
| 136 |
let clean s = Str.global_replace (Str.regexp "\n") "\\n" s |
| 137 |
|
| 138 |
let rec print g = |
| 139 |
match g with |
| 140 |
| Item (s,_) -> Printf.sprintf "%s" (clean s) |
| 141 |
| Opt g -> Printf.sprintf "[%s]" (print g) |
| 142 |
| Alt gs -> Printf.sprintf "( %s )" (String.concat "\n| " (List.map print gs)) |
| 143 |
| Seq gs -> String.concat " " (List.map print gs) |
| 144 |
|
| 145 |
let rec print_toklist = function |
| 146 |
| [] -> "" |
| 147 |
| Tok(k,v) :: rest when k = v -> clean k ^ " " ^ print_toklist rest |
| 148 |
| Tok(k,v) :: rest -> clean k ^ " = \"" ^ clean v ^ "\" " ^ print_toklist rest |
| 149 |
| Top l :: rest -> print_toklist l ^ " " ^ print_toklist rest |
| 150 |
|
| 151 |
end |
| 152 |
|
| 153 |
type sentence = { |
| 154 |
name : string; |
| 155 |
text : string; |
| 156 |
edit_id : int; |
| 157 |
} |
| 158 |
|
| 159 |
let doc : sentence Document.document = Document.create () |
| 160 |
|
| 161 |
let tip_id () = |
| 162 |
try Document.tip doc |
| 163 |
with |
| 164 |
| Document.Empty -> Stateid.initial |
| 165 |
| Invalid_argument _ -> error "add_sentence on top of non assigned tip" |
| 166 |
|
| 167 |
let add_sentence = |
| 168 |
let edit_id = ref 0 in |
| 169 |
fun ?(name="") text -> |
| 170 |
let tip_id = tip_id () in |
| 171 |
decr edit_id; |
| 172 |
Document.push doc { name; text; edit_id = !edit_id }; |
| 173 |
!edit_id, tip_id |
| 174 |
|
| 175 |
let print_document () = |
| 176 |
let ellipsize s = |
| 177 |
Str.global_replace (Str.regexp "^[\n ]*") "" |
| 178 |
(if String.length s > 20 then String.sub s 0 17 ^ "..." |
| 179 |
else s) in |
| 180 |
pperr_endline ( |
| 181 |
(Document.print doc |
| 182 |
(fun b state_id { name; text } -> |
| 183 |
Pp.str (Printf.sprintf "%s[%10s, %3s] %s" |
| 184 |
(if b then "*" else " ") |
| 185 |
name |
| 186 |
(Stateid.to_string (Option.default Stateid.dummy state_id)) |
| 187 |
(ellipsize text))))) |
| 188 |
|
| 189 |
|
| 190 |
module GUILogic = struct |
| 191 |
|
| 192 |
let after_add = function |
| 193 |
| Interface.Fail (_,_,s) -> print_error s; exit 1 |
| 194 |
| Interface.Good (id, Util.Inl ()) -> |
| 195 |
Document.assign_tip_id doc id |
| 196 |
| Interface.Good (id, Util.Inr tip) -> |
| 197 |
Document.assign_tip_id doc id; |
| 198 |
Document.unfocus doc; |
| 199 |
ignore(Document.cut_at doc tip); |
| 200 |
print_document () |
| 201 |
|
| 202 |
let at id id' _ = Stateid.equal id' id |
| 203 |
|
| 204 |
let after_edit_at (id,need_unfocus) = function |
| 205 |
| Interface.Fail (_,_,s) -> print_error s; exit 1 |
| 206 |
| Interface.Good (Util.Inl ()) -> |
| 207 |
if need_unfocus then Document.unfocus doc; |
| 208 |
ignore(Document.cut_at doc id); |
| 209 |
print_document () |
| 210 |
| Interface.Good (Util.Inr (stop_id,(start_id,tip))) -> |
| 211 |
if need_unfocus then Document.unfocus doc; |
| 212 |
ignore(Document.cut_at doc tip); |
| 213 |
Document.focus doc ~cond_top:(at start_id) ~cond_bot:(at stop_id); |
| 214 |
ignore(Document.cut_at doc id); |
| 215 |
print_document () |
| 216 |
|
| 217 |
let get_id_pred pred = |
| 218 |
try Document.find_id doc pred |
| 219 |
with Not_found -> error "No state found" |
| 220 |
|
| 221 |
let get_id id = get_id_pred (fun _ { name } -> name = id) |
| 222 |
|
| 223 |
let after_fail coq = function |
| 224 |
| Interface.Fail (safe_id,_,s) -> |
| 225 |
prerr_endline "The command failed as expected"; |
| 226 |
let to_id, need_unfocus = |
| 227 |
get_id_pred (fun id _ -> Stateid.equal id safe_id) in |
| 228 |
after_edit_at (to_id, need_unfocus) |
| 229 |
(base_eval_call (Xmlprotocol.edit_at to_id) coq) |
| 230 |
| Interface.Good _ -> error "The command was expected to fail but did not" |
| 231 |
|
| 232 |
end |
| 233 |
|
| 234 |
open GUILogic |
| 235 |
|
| 236 |
let eval_print l coq = |
| 237 |
let open Parser in |
| 238 |
let open Xmlprotocol in |
| 239 |
|
| 240 |
match l with |
| 241 |
| [ Tok(_,"ADD"); Top []; Tok(_,phrase) ] -> |
| 242 |
let eid, tip = add_sentence phrase in |
| 243 |
after_add (base_eval_call (add ((((phrase,eid),(tip,true)),0),(0,0))) coq) |
| 244 |
| [ Tok(_,"ADD"); Top [Tok(_,name)]; Tok(_,phrase) ] -> |
| 245 |
let eid, tip = add_sentence ~name phrase in |
| 246 |
after_add (base_eval_call (add ((((phrase,eid),(tip,true)),0),(0,0))) coq) |
| 247 |
| [ Tok(_,"FAILADD"); Tok(_,phrase) ] -> |
| 248 |
let eid, tip = add_sentence phrase in |
| 249 |
after_fail coq (base_eval_call ~fail:false (add ((((phrase,eid),(tip,true)),0),(0,0))) coq) |
| 250 |
| [ Tok(_,"GOALS"); ] -> |
| 251 |
eval_call (goals ()) coq |
| 252 |
| [ Tok(_,"FAILGOALS"); ] -> |
| 253 |
after_fail coq (base_eval_call ~fail:false (goals ()) coq) |
| 254 |
| [ Tok(_,"EDIT_AT"); Tok(_,id) ] -> |
| 255 |
let to_id, need_unfocus = get_id id in |
| 256 |
after_edit_at (to_id, need_unfocus) (base_eval_call (edit_at to_id) coq) |
| 257 |
| [ Tok(_,"QUERY"); Top []; Tok(_,phrase) ] -> |
| 258 |
eval_call (query (0,(phrase,tip_id()))) coq |
| 259 |
| [ Tok(_,"PDIFF"); Tok(_,id) ] -> |
| 260 |
let to_id, _ = get_id id in |
| 261 |
eval_call (proof_diff ("on",to_id)) coq |
| 262 |
| [ Tok(_,"QUERY"); Top [Tok(_,id)]; Tok(_,phrase) ] -> |
| 263 |
let to_id, _ = get_id id in |
| 264 |
eval_call (query (0,(phrase, to_id))) coq |
| 265 |
| [ Tok(_,"WAIT") ] -> |
| 266 |
eval_call (wait ()) coq |
| 267 |
| [ Tok(_,"JOIN") ] -> |
| 268 |
eval_call (status true) coq |
| 269 |
| [ Tok(_,"FAILJOIN") ] -> |
| 270 |
after_fail coq (base_eval_call ~fail:false (status true) coq) |
| 271 |
| [ Tok(_,"ASSERT"); Tok(_,"TIP"); Tok(_,id) ] -> |
| 272 |
let to_id, _ = get_id id in |
| 273 |
if not(Stateid.equal (Document.tip doc) to_id) then error "Wrong tip" |
| 274 |
else prerr_endline "Good tip" |
| 275 |
| [ Tok(_,"ABORT") ] -> |
| 276 |
prerr_endline "Quitting fake_ide"; |
| 277 |
exit 0 |
| 278 |
| Tok("#[^\n]*",_) :: _ -> () |
| 279 |
| Tok(s,_) :: _ -> error ("syntax error at " ^ s) |
| 280 |
| _ -> error ("syntax error") |
| 281 |
|
| 282 |
let grammar = |
| 283 |
let open Parser in |
| 284 |
let eat_id = eat_rex "[a-zA-Z_][a-zA-Z0-9_]*" in |
| 285 |
let eat_phrase = eat_balanced '{' in |
| 286 |
Alt |
| 287 |
[ Seq [Item (eat_rex "ADD"); Opt (Item eat_id); Item eat_phrase] |
| 288 |
; Seq [Item (eat_rex "FAILADD"); Item eat_phrase] |
| 289 |
; Seq [Item (eat_rex "EDIT_AT"); Item eat_id] |
| 290 |
; Seq [Item (eat_rex "QUERY"); Opt (Item eat_id); Item eat_phrase] |
| 291 |
; Seq [Item (eat_rex "PDIFF"); Item eat_id ] |
| 292 |
; Seq [Item (eat_rex "WAIT")] |
| 293 |
; Seq [Item (eat_rex "JOIN")] |
| 294 |
; Seq [Item (eat_rex "GOALS")] |
| 295 |
; Seq [Item (eat_rex "FAILGOALS")] |
| 296 |
; Seq [Item (eat_rex "FAILJOIN")] |
| 297 |
; Seq [Item (eat_rex "ABORT")] |
| 298 |
; Seq [Item (eat_rex "ASSERT"); Item (eat_rex "TIP"); Item eat_id ] |
| 299 |
; Item (eat_rex "#[^\n]*") |
| 300 |
] |
| 301 |
|
| 302 |
let read_command inc = Parser.parse grammar inc |
| 303 |
|
| 304 |
let usage () = |
| 305 |
prerr_endline (Printf.sprintf "Usage: %s COQIDETOP ( file | - ) [ \"<coqtop arguments>\" ]\n\ |
| 306 |
Input syntax is:\n%s\n" |
| 307 |
(Filename.basename Sys.argv.(0)) |
| 308 |
(Parser.print grammar)); |
| 309 |
exit 1 |
| 310 |
|
| 311 |
module Rocqide = Spawn.Sync () |
| 312 |
|
| 313 |
let main = |
| 314 |
if Sys.os_type = "Unix" then Sys.set_signal Sys.sigpipe |
| 315 |
(Sys.Signal_handle |
| 316 |
(fun _ -> prerr_endline "Broken Pipe (coqtop died ?)"; exit 1)); |
| 317 |
let idetop_name, input_file, args = match Sys.argv with |
| 318 |
| [| _; p; f |] -> p, f, [] |
| 319 |
| [| _; p; f; args |] -> |
| 320 |
let args = Str.split (Str.regexp " ") args in |
| 321 |
p, f, args |
| 322 |
| _ -> usage () in |
| 323 |
let def_coqtop_args = ["--xml_format=Ppcmds"] in |
| 324 |
let coqtop_args = Array.of_list(def_coqtop_args @ args) in |
| 325 |
let inc = if input_file = "-" then stdin else open_in input_file in |
| 326 |
prerr_endline ("Running: "^idetop_name^" "^ |
| 327 |
(String.concat " " (Array.to_list coqtop_args))); |
| 328 |
let coq = |
| 329 |
let _p, cin, cout = Rocqide.spawn idetop_name coqtop_args in |
| 330 |
let ip = Xml_parser.make (Xml_parser.SChannel cin) in |
| 331 |
let op = Xml_printer.make (Xml_printer.TChannel cout) in |
| 332 |
Xml_parser.check_eof ip false; |
| 333 |
{ xml_printer = op; xml_parser = ip } in |
| 334 |
let init () = |
| 335 |
match base_eval_call ~print:false (Xmlprotocol.init None) coq with |
| 336 |
| Interface.Good id -> () |
| 337 |
| Interface.Fail _ -> error "init call failed" in |
| 338 |
let finish () = |
| 339 |
match base_eval_call (Xmlprotocol.status true) coq with |
| 340 |
| Interface.Good _ -> exit 0 |
| 341 |
| Interface.Fail (_,_,s) -> print_error s; exit 1 in |
| 342 |
|
| 343 |
init (); |
| 344 |
while true do |
| 345 |
let cmd = try read_command inc with End_of_file -> finish () in |
| 346 |
try eval_print cmd coq |
| 347 |
with e -> error ("Uncaught exception " ^ Printexc.to_string e) |
| 348 |
done |
| 349 |
|
| 350 |
|