Debian Codegraph @

toplevel/coqtop.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Pp
12 open Coqargs
13
14 (** This file provides generic support for Rocq executables + specific
15 support for the rocq repl executable *)
16
17 let () = at_exit flush_all
18
19 let get_version ~boot =
20 (* XXX assert false on None? *)
21 match Boot.Env.initialized () with
22 | Some Boot | None -> Coq_config.version
23 | Some (Env env) ->
24 try
25 let revision = Boot.Env.revision env |> Boot.Path.to_string in
26 let ch = open_in revision in
27 let ver = input_line ch in
28 let rev = input_line ch in
29 let () = close_in ch in
30 Printf.sprintf "%s (%s)" ver rev
31 with Sys_error _ | End_of_file -> Coq_config.version
32
33 let print_header ~boot () =
34 Feedback.msg_info (str "Welcome to Rocq " ++ str (get_version ~boot));
35 flush_all ()
36
37
38 (******************************************************************************)
39 (* Fatal Errors *)
40 (******************************************************************************)
41
42 (** Prints info which is either an error or an anomaly and then exits
43 with the appropriate error code *)
44 let fatal_error_exn exn =
45 Topfmt.(in_phase ~phase:Initialization print_err_exn exn);
46 flush_all ();
47 let exit_code =
48 CErrors.exit_code exn
49 in
50 exit exit_code
51
52 type ('a,'b) custom_toplevel =
53 { parse_extra : Coqargs.t -> string list -> 'a * string list
54 ; usage : Boot.Usage.specific_usage
55 ; init_extra : 'a -> Coqargs.injection_command list -> opts:Coqargs.t -> 'b
56 ; initial_args : Coqargs.t
57 ; run : 'a -> opts:Coqargs.t -> 'b -> unit
58 }
59
60 (** Main init routine *)
61 let init_toplevel { parse_extra; init_extra; usage; initial_args } args =
62 Coqinit.init_ocaml ();
63 let opts, customopts = Coqinit.parse_arguments ~parse_extra ~initial_args args in
64 Stm.init_process (snd customopts);
65 let () = Coqinit.init_runtime ~usage opts in
66 let () = Coqinit.init_document opts in
67 (* This state will be shared by all the documents *)
68 Stm.init_core ();
69 let customstate = init_extra ~opts customopts (Coqargs.injection_commands opts) in
70 opts, customopts, customstate
71
72 let start_coq custom args =
73 let init_feeder = Feedback.add_feeder Coqloop.coqloop_feed in
74 (* Init phase *)
75 let opts, custom_opts, state =
76 try init_toplevel custom args
77 with any ->
78 flush_all();
79 fatal_error_exn any in
80 Feedback.del_feeder init_feeder;
81 (* Run phase *)
82 custom.run ~opts custom_opts state
83
84 (** ****************************************)
85 (** Specific support for rocq repl executable *)
86
87 let ltac_debug_answer = let open DebugHook.Answer in function
88 | Prompt prompt ->
89 (* No newline *)
90 Format.fprintf !Topfmt.err_ft "@[%a@]%!" Pp.pp_with prompt
91 | Goal g ->
92 Format.fprintf !Topfmt.err_ft "@[%a@]@\n%!" Pp.pp_with g
93 | Output o ->
94 Format.fprintf !Topfmt.err_ft "@[%a@]@\n%!" Pp.pp_with o
95 | Init ->
96 Format.fprintf !Topfmt.err_ft "@[%a@]@\n%!" Pp.pp_with (str "Init")
97 | Stack _
98 | Vars _ -> CErrors.anomaly (str "ltac_debug_answer: unsupported Answer type")
99
100 let ltac_debug_parse () =
101 let open DebugHook in
102 let act =
103 try Action.parse (read_line ())
104 with End_of_file -> Ok Action.Interrupt
105 in
106 match act with
107 | Ok act -> act
108 | Error error -> ltac_debug_answer (Answer.Output (str error)); Action.Failed
109
110 type query = PrintTags | PrintModUid of string list
111 type run_mode = Interactive | Batch | Query of query
112
113 type toplevel_options = {
114 run_mode : run_mode;
115 color_mode : Colors.color;
116 }
117
118 let init_document opts stm_options injections =
119 (* Rocq init process, phase 3: Stm initialization, backtracking state.
120
121 It is essential that the module system is in a consistent
122 state before we take the first snapshot. This was not
123 guaranteed in the past, but now is thanks to the STM API.
124 *)
125 (* Next line allows loading .vos files when in interactive mode *)
126 Loadpath.load_vos_libraries := true;
127 let open Vernac.State in
128 let doc, sid =
129 Stm.(new_doc
130 { doc_type = Interactive opts.config.logic.toplevel_name;
131 injections;
132 }) in
133 { doc; sid; proof = None; time = Option.map Vernac.make_time_output opts.config.time }
134
135 let init_toploop opts stm_opts injections =
136 let state = init_document opts stm_opts injections in
137 let state = Load.load_init_vernaculars opts ~state in
138 state
139
140 let coqtop_init ({ run_mode; color_mode }, async_opts) injections ~opts =
141 if run_mode != Interactive then Flags.quiet := true;
142 Colors.init_color color_mode;
143 Flags.if_verbose (print_header ~boot:opts.pre.boot) ();
144 DebugHook.Intf.(set
145 { read_cmd = ltac_debug_parse
146 ; submit_answer = ltac_debug_answer
147 ; isTerminal = true
148 });
149 init_toploop opts async_opts injections
150
151 let coqtop_parse_extra opts extras =
152 let rec parse_extra run_mode = function
153 | "-batch" :: rest -> parse_extra Batch rest
154 | "-list-tags" :: rest -> Query PrintTags, []
155 | "-print-mod-uid" :: rest -> Query (PrintModUid rest), []
156 | x :: rest ->
157 let run_mode, rest = parse_extra run_mode rest in run_mode, x :: rest
158 | [] -> run_mode, [] in
159 let run_mode, extras = parse_extra Interactive extras in
160 let color_mode, extras = Colors.parse_extra_colors ~emacs:opts.config.print_emacs extras in
161 let async_opts, extras = Stmargs.parse_args opts extras in
162 ({ run_mode; color_mode}, async_opts), extras
163
164 let fix_windows_dirsep s =
165 if Sys.win32 then Str.(global_replace (regexp "\\(.\\)\\") "\\1/" s)
166 else s
167
168 let get_native_name s =
169 (* We ignore even critical errors because this mode has to be super silent *)
170 try
171 fix_windows_dirsep @@
172 Filename.(List.fold_left concat (dirname s)
173 [ !Nativelib.output_dir
174 ; Library.native_name_from_filename s
175 ])
176 with _ -> ""
177
178 let coqtop_run ({ run_mode; color_mode },_) ~opts state =
179 match run_mode with
180 | Interactive -> Coqloop.run ~opts ~state;
181 | Query PrintTags -> Colors.print_style_tags color_mode; exit 0
182 | Query (PrintModUid sl) ->
183 let s = String.concat " " (List.map get_native_name sl) in
184 print_endline s;
185 exit 0
186 | Batch -> exit 0
187
188 let coqtop_specific_usage = Boot.Usage.{
189 executable_name = "rocq repl";
190 extra_args = "";
191 extra_options = "\n\
192 rocq repl specific options:\n\
193 \n -batch batch mode (exits after interpretation of command line)\
194 \n"
195 }
196
197 let coqtop_toplevel =
198 { parse_extra = coqtop_parse_extra
199 ; usage = coqtop_specific_usage
200 ; init_extra = coqtop_init
201 ; run = coqtop_run
202 ; initial_args = Coqargs.default
203 }