| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
open Pp |
| 14 |
open CErrors |
| 15 |
open Util |
| 16 |
open Vernacexpr |
| 17 |
open Vernacprop |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
let checknav { CAst.loc; v = { expr } } = |
| 24 |
if is_navigation_vernac expr && not (is_reset expr) then |
| 25 |
CErrors.user_err ?loc (str "Navigation commands forbidden in files.") |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
let vernac_echo ?loc in_chan = let open Loc in |
| 30 |
Option.iter (fun loc -> |
| 31 |
let len = loc.ep - loc.bp in |
| 32 |
seek_in in_chan loc.bp; |
| 33 |
Feedback.msg_notice @@ str @@ really_input_string in_chan len |
| 34 |
) loc |
| 35 |
|
| 36 |
|
| 37 |
type time_output = |
| 38 |
| ToFeedback |
| 39 |
| ToChannel of Format.formatter |
| 40 |
|
| 41 |
let make_time_output = function |
| 42 |
| Coqargs.ToFeedback -> ToFeedback |
| 43 |
| ToFile f -> |
| 44 |
let ch = open_out f in |
| 45 |
let fch = Format.formatter_of_out_channel ch in |
| 46 |
let close () = |
| 47 |
Format.pp_print_flush fch (); |
| 48 |
close_out ch |
| 49 |
in |
| 50 |
at_exit close; |
| 51 |
ToChannel fch |
| 52 |
|
| 53 |
module State = struct |
| 54 |
|
| 55 |
type t = { |
| 56 |
doc : Stm.doc; |
| 57 |
sid : Stateid.t; |
| 58 |
proof : Proof.t option; |
| 59 |
time : time_output option; |
| 60 |
} |
| 61 |
|
| 62 |
end |
| 63 |
|
| 64 |
let emit_time state com tstart tend = |
| 65 |
match state.State.time with |
| 66 |
| None -> () |
| 67 |
| Some time -> |
| 68 |
let pp = Topfmt.pr_cmd_header com ++ System.fmt_time_difference tstart tend in |
| 69 |
match time with |
| 70 |
| ToFeedback -> Feedback.msg_notice pp |
| 71 |
| ToChannel ch -> Pp.pp_with ch (pp ++ fnl()) |
| 72 |
|
| 73 |
let interp_vernac ~check ~state ({CAst.loc;_} as com) = |
| 74 |
let open State in |
| 75 |
try |
| 76 |
let doc, nsid, ntip = Stm.add ~doc:state.doc ~ontop:state.sid (not !Flags.quiet) com in |
| 77 |
|
| 78 |
|
| 79 |
if ntip <> Stm.NewAddTip then |
| 80 |
anomaly (str "vernac.ml: We got an unfocus operation on the toplevel!"); |
| 81 |
|
| 82 |
|
| 83 |
let () = if check then Stm.observe ~doc nsid in |
| 84 |
let new_proof = Vernacstate.Declare.give_me_the_proof_opt () [@ocaml.warning "-3"] in |
| 85 |
{ state with doc; sid = nsid; proof = new_proof; } |
| 86 |
with reraise -> |
| 87 |
let (reraise, info) = Exninfo.capture reraise in |
| 88 |
let info = |
| 89 |
|
| 90 |
match Loc.get_loc info, loc with |
| 91 |
| None, Some loc -> Loc.add_loc info loc |
| 92 |
| Some _, _ | _, None -> info |
| 93 |
in |
| 94 |
Exninfo.iraise (reraise, info) |
| 95 |
|
| 96 |
|
| 97 |
let load_vernac_core ~echo ~check ~state ?source file = |
| 98 |
|
| 99 |
let in_chan = open_utf8_file_in file in |
| 100 |
let in_echo = if echo then Some (open_utf8_file_in file) else None in |
| 101 |
let input_cleanup () = close_in in_chan; Option.iter close_in in_echo in |
| 102 |
|
| 103 |
let source = Option.default (Loc.InFile {dirpath=None; file}) source in |
| 104 |
let in_pa = Procq.Parsable.make ~loc:Loc.(initial source) |
| 105 |
(Gramlib.Stream.of_channel in_chan) in |
| 106 |
let open State in |
| 107 |
|
| 108 |
|
| 109 |
let rec loop state ids = |
| 110 |
let tstart = System.get_time () in |
| 111 |
match |
| 112 |
NewProfile.profile "parse_command" (fun () -> |
| 113 |
Stm.parse_sentence |
| 114 |
~doc:state.doc ~entry:Pvernac.main_entry state.sid in_pa) |
| 115 |
() |
| 116 |
with |
| 117 |
| None -> |
| 118 |
input_cleanup (); |
| 119 |
state, ids, Procq.Parsable.comments in_pa |
| 120 |
| Some ast -> |
| 121 |
|
| 122 |
Option.iter (vernac_echo ?loc:ast.CAst.loc) in_echo; |
| 123 |
|
| 124 |
checknav ast; |
| 125 |
|
| 126 |
let state = |
| 127 |
try_finally |
| 128 |
(fun () -> |
| 129 |
NewProfile.profile "command" |
| 130 |
~args:(fun () -> |
| 131 |
let lnum = match ast.loc with |
| 132 |
| None -> "unknown" |
| 133 |
| Some loc -> string_of_int loc.line_nb |
| 134 |
in |
| 135 |
[("cmd", `String (Pp.string_of_ppcmds (Topfmt.pr_cmd_header ast))); |
| 136 |
("line", `String lnum)]) |
| 137 |
(fun () -> |
| 138 |
Flags.silently (interp_vernac ~check ~state) ast) ()) |
| 139 |
() |
| 140 |
(fun () -> |
| 141 |
let tend = System.get_time () in |
| 142 |
|
| 143 |
|
| 144 |
emit_time state ast tstart tend) |
| 145 |
() |
| 146 |
in |
| 147 |
|
| 148 |
(loop [@ocaml.tailcall]) state (state.sid :: ids) |
| 149 |
in |
| 150 |
try loop state [] |
| 151 |
with any -> |
| 152 |
let (e, info) = Exninfo.capture any in |
| 153 |
input_cleanup (); |
| 154 |
Exninfo.iraise (e, info) |
| 155 |
|
| 156 |
let process_expr ~state loc_ast = |
| 157 |
try interp_vernac ~check:true ~state loc_ast |
| 158 |
with reraise -> |
| 159 |
let reraise, info = Exninfo.capture reraise in |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
let reraise = UserError (CErrors.iprint (reraise, info)) in |
| 164 |
|
| 165 |
let info = Option.cata (Loc.add_loc Exninfo.null) Exninfo.null (Loc.get_loc info) in |
| 166 |
|
| 167 |
ignore(Stm.edit_at ~doc:state.doc state.sid); |
| 168 |
Exninfo.iraise (reraise, info) |
| 169 |
|
| 170 |
let process_expr ~state loc_ast = |
| 171 |
let tstart = System.get_time () in |
| 172 |
try_finally (fun () -> process_expr ~state loc_ast) |
| 173 |
() |
| 174 |
(fun () -> |
| 175 |
let tend = System.get_time () in |
| 176 |
emit_time state loc_ast tstart tend) |
| 177 |
() |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
let beautify_suffix = ".beautified" |
| 186 |
|
| 187 |
let set_formatter_translator ch = |
| 188 |
let out s b e = output_substring ch s b e in |
| 189 |
let ft = Format.make_formatter out (fun () -> flush ch) in |
| 190 |
Format.pp_set_max_boxes ft max_int; |
| 191 |
ft |
| 192 |
|
| 193 |
let pr_new_syntax ?loc ft_beautify ocom = |
| 194 |
let loc = Option.append loc (Option.bind ocom (fun x -> x.CAst.loc)) in |
| 195 |
let loc = Option.cata Loc.unloc (0,0) loc in |
| 196 |
let before = comment (Pputils.extract_comments (fst loc)) in |
| 197 |
let com = Option.cata (fun com -> Ppvernac.pr_vernac com ++ fnl()) (mt ()) ocom in |
| 198 |
let after = comment (Pputils.extract_comments (snd loc)) in |
| 199 |
if !Flags.beautify_file then |
| 200 |
(Pp.pp_with ft_beautify (hov 0 (before ++ com ++ after)); |
| 201 |
Format.pp_print_flush ft_beautify ()) |
| 202 |
else |
| 203 |
Feedback.msg_info (hov 4 (str"New Syntax:" ++ fnl() ++ (hov 0 com))) |
| 204 |
|
| 205 |
|
| 206 |
let beautify_pass ~doc ~comments ~ids ~filename = |
| 207 |
let ft_beautify, close_beautify = |
| 208 |
if !Flags.beautify_file then |
| 209 |
let chan_beautify = open_out (filename^beautify_suffix) in |
| 210 |
set_formatter_translator chan_beautify, fun () -> close_out chan_beautify; |
| 211 |
else |
| 212 |
!Topfmt.std_ft, fun () -> () |
| 213 |
in |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
Pputils.beautify_comments := comments; |
| 218 |
List.iter (fun id -> pr_new_syntax ft_beautify (Stm.get_ast ~doc id)) ids; |
| 219 |
|
| 220 |
|
| 221 |
pr_new_syntax ~loc:(Loc.make_loc (max_int,max_int)) ft_beautify None; |
| 222 |
close_beautify () |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
let load_vernac ~echo ~check ~state ?source filename = |
| 227 |
let ostate, ids, comments = load_vernac_core ~echo ~check ~state ?source filename in |
| 228 |
|
| 229 |
if !Flags.beautify then beautify_pass ~doc:ostate.State.doc ~comments ~ids:(List.rev ids) ~filename; |
| 230 |
|
| 231 |
ostate |