Debian Codegraph @

tools/rocqtex.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 (* coq-tex
12 * JCF, 16/1/98
13 * adapted from caml-tex (perl script written by Xavier Leroy)
14 *
15 * Perl isn't as portable as it pretends to be, and is quite difficult
16 * to read and maintain... Let us rewrite the stuff in Caml! *)
17
18 let linelen = ref 72
19 let output = ref ""
20 let output_specified = ref false
21 let image = ref ""
22 let cut_at_blanks = ref false
23 let verbose = ref false
24 let slanted = ref false
25 let hrule = ref false
26 let small = ref false
27
28 let any_prompt = Str.regexp "[A-Z0-9a-z_\\$']* < "
29
30 (* First pass: extract the Rocq phrases to evaluate from [texfile]
31 * and put them into the file [inputv] *)
32
33 let begin_coq = Str.regexp "\\\\begin{coq_\\(example\\|example\\*\\|example\\#\\|eval\\)}[ \t]*$"
34 let end_coq = Str.regexp "\\\\end{coq_\\(example\\|example\\*\\|example\\#\\|eval\\)}[ \t]*$"
35
36 let extract texfile inputv =
37 let chan_in = open_in texfile in
38 let chan_out = open_out inputv in
39 let rec inside () =
40 let s = input_line chan_in in
41 if Str.string_match end_coq s 0 then
42 outside ()
43 else begin
44 output_string chan_out (s ^ "\n");
45 inside ()
46 end
47 and outside () =
48 let s = input_line chan_in in
49 if Str.string_match begin_coq s 0 then
50 inside ()
51 else
52 outside ()
53 in
54 try
55 output_string chan_out
56 ("Set Printing Width " ^ (string_of_int !linelen) ^".\n");
57 outside ()
58 with End_of_file ->
59 (* a dummy command, just in case the last line was a comment *)
60 output_string chan_out "Set Printing Width 78.\n";
61 close_in chan_in;
62 close_out chan_out
63
64 (* Second pass: insert the answers of Rocq from [coq_output] into the
65 * TeX file [texfile]. The result goes in file [result]. *)
66
67 let tex_escaped s =
68 let delims = Str.regexp "[_{}&%#$\\^~ <>'`]" in
69 let adapt_delim = function
70 | "{" | "}" | "&" | "%" | "#" | "$" as c -> "\\"^c
71 | "_" -> "{\\char`\\_}"
72 | "\\" -> "{\\char'134}"
73 | "^" -> "{\\char'136}"
74 | "~" -> "{\\char'176}"
75 | " " -> "~"
76 | "<" -> "{<}"
77 | ">" -> "{>}"
78 | "'" -> "{\\textquotesingle}"
79 | "`" -> "\\`{}"
80 | _ -> assert false
81 in
82 let adapt = function
83 | Str.Text s -> s
84 | Str.Delim s -> adapt_delim s
85 in
86 String.concat "" (List.map adapt (Str.full_split delims s))
87
88 let encapsule sl c_out s =
89 if sl then
90 Printf.fprintf c_out "\\texttt{\\textit{%s}}\\\\\n" (tex_escaped s)
91 else
92 Printf.fprintf c_out "\\texttt{%s}\\\\\n" (tex_escaped s)
93
94 let print_block c_out bl =
95 List.iter (fun s -> if s="" then () else encapsule !slanted c_out s) bl
96
97 let insert texfile coq_output result =
98 let c_tex = open_in texfile in
99 let c_coq = open_in coq_output in
100 let c_out = open_out result in
101 (* read lines until a prompt is found (starting from the second line),
102 purge prompts on the first line and return their number *)
103 let last_read = ref (input_line c_coq) in
104 let read_output () =
105 let first = !last_read in
106 let nb = ref 0 in
107 (* remove the leading prompts *)
108 let rec skip_prompts pos =
109 if Str.string_match any_prompt first pos then
110 let () = incr nb in
111 skip_prompts (Str.match_end ())
112 else pos in
113 let first =
114 let start = skip_prompts 0 in
115 String.sub first start (String.length first - start) in
116 (* read and return the following lines until a prompt is found *)
117 let rec read_lines () =
118 let s = input_line c_coq in
119 if Str.string_match any_prompt s 0 then begin
120 last_read := s; []
121 end else
122 s :: read_lines () in
123 (first :: read_lines (), !nb) in
124 let unhandled_output = ref None in
125 let read_output () =
126 match !unhandled_output with
127 | Some some -> unhandled_output := None; some
128 | None -> read_output () in
129 (* we are inside a \begin{coq_...} ... \end{coq_...} block
130 * show_... tell what kind of block it is *)
131 let rec inside show_answers show_questions not_first discarded =
132 let s = input_line c_tex in
133 if s = "" then
134 inside show_answers show_questions not_first (discarded + 1)
135 else if not (Str.string_match end_coq s 0) then begin
136 let (bl,n) = read_output () in
137 assert (n > discarded);
138 let n = n - discarded in
139 if not_first then output_string c_out "\\medskip\n";
140 if !verbose then Printf.printf "Rocq < %s\n" s;
141 if show_questions then encapsule false c_out ("Rocq < " ^ s);
142 let rec read_lines k =
143 if k = 0 then []
144 else
145 let s = input_line c_tex in
146 if Str.string_match end_coq s 0 then []
147 else s :: read_lines (k - 1) in
148 let al = read_lines (n - 1) in
149 if !verbose then List.iter (Printf.printf " %s\n") al;
150 if show_questions then
151 List.iter (fun s -> encapsule false c_out (" " ^ s)) al;
152 let la = n - 1 - List.length al in
153 if la <> 0 then
154 (* this happens when the block ends with a comment; the output
155 is for the command at the beginning of the next block *)
156 unhandled_output := Some (bl, la)
157 else begin
158 if !verbose then List.iter print_endline bl;
159 if show_answers then print_block c_out bl;
160 inside show_answers show_questions (show_answers || show_questions) 0
161 end
162 end else if discarded > 0 then begin
163 (* this happens when the block ends with an empty line *)
164 let (bl,n) = read_output () in
165 assert (n > discarded);
166 unhandled_output := Some (bl, n - discarded)
167 end in
168 (* we are outside of a \begin{coq_...} ... \end{coq_...} block *)
169 let rec outside just_after =
170 let start_block () =
171 if !small then output_string c_out "\\begin{small}\n";
172 output_string c_out "\\begin{flushleft}\n";
173 if !hrule then output_string c_out "\\hrulefill\\\\\n" in
174 let end_block () =
175 if !hrule then output_string c_out "\\hrulefill\\\\\n";
176 output_string c_out "\\end{flushleft}\n";
177 if !small then output_string c_out "\\end{small}\n" in
178 let s = input_line c_tex in
179 if Str.string_match begin_coq s 0 then begin
180 let kind = Str.matched_group 1 s in
181 if kind = "eval" then begin
182 if just_after then end_block ();
183 inside false false false 0;
184 outside false
185 end else begin
186 let show_answers = kind <> "example*" in
187 let show_questions = kind <> "example#" in
188 if not just_after then start_block ();
189 inside show_answers show_questions just_after 0;
190 outside true
191 end
192 end else begin
193 if just_after then end_block ();
194 output_string c_out (s ^ "\n");
195 outside false
196 end in
197 try
198 let _ = read_output () in (* to skip the Rocq banner *)
199 let _ = read_output () in (* to skip the Rocq answer to Set Printing Width *)
200 outside false
201 with End_of_file -> begin
202 close_in c_tex;
203 close_in c_coq;
204 close_out c_out
205 end
206
207 (* Process of one TeX file *)
208
209 let rm f = try Sys.remove f with _ -> ()
210
211 let one_file texfile =
212 let inputv = Filename.temp_file "coq_tex" ".v" in
213 let coq_output = Filename.temp_file "coq_tex" ".coq_output"in
214 let result =
215 if !output_specified then
216 !output
217 else if Filename.check_suffix texfile ".tex" then
218 (Filename.chop_suffix texfile ".tex") ^ ".v.tex"
219 else
220 texfile ^ ".v.tex"
221 in
222 try
223 (* 1. extract Rocq phrases *)
224 extract texfile inputv;
225 (* 2. run Rocq on input *)
226 let _ = Sys.command (Printf.sprintf "%s < %s > %s 2>&1" !image inputv
227 coq_output)
228 in
229 (* 3. insert Rocq output into original file *)
230 insert texfile coq_output result;
231 (* 4. clean up *)
232 rm inputv; rm coq_output
233 with reraise -> begin
234 rm inputv; rm coq_output;
235 raise reraise
236 end
237
238 (* Parsing of the command line, check of the Rocq command and process
239 * of all the files in the command line, one by one *)
240
241 let files = ref []
242
243 let parse_cl ~prog args =
244 Arg.parse_argv (Array.of_list (prog :: args))
245 [ "-o", Arg.String (fun s -> output_specified := true; output := s),
246 "output-file Specify the resulting LaTeX file";
247 "-n", Arg.Int (fun n -> linelen := n),
248 "line-width Set the line width";
249 "-image", Arg.String (fun s -> image := s),
250 "rocq-image Use rocq-image as Rocq command";
251 "-w", Arg.Set cut_at_blanks,
252 " Try to cut lines at blanks";
253 "-v", Arg.Set verbose,
254 " Verbose mode (show Rocq answers on stdout)";
255 "-sl", Arg.Set slanted,
256 " Rocq answers in slanted font (only with LaTeX2e)";
257 "-hrule", Arg.Set hrule,
258 " Rocq parts are written between 2 horizontal lines";
259 "-small", Arg.Set small,
260 " Rocq parts are written in small font";
261 ]
262 (fun s -> files := s :: !files)
263 "coq-tex [options] file ..."
264
265 let find_coqtop () =
266 let prog = Sys.executable_name in
267 try
268 let size = String.length prog in
269 let i = Str.search_backward (Str.regexp_string "coq-tex") prog (size-7) in
270 (String.sub prog 0 i)^"coqtop"^(String.sub prog (i+7) (size-i-7))
271 with Not_found -> begin
272 Printf.printf "Warning: preprocessing with default image \"coqtop\"\n";
273 "coqtop"
274 end
275
276 let main ~prog args =
277 parse_cl ~prog args;
278 if !image = "" then image := Filename.quote (find_coqtop ());
279 if Sys.command (!image ^ " -batch -silent") <> 0 then begin
280 Printf.printf "Error: ";
281 let _ = Sys.command (!image ^ " -batch") in
282 exit 1
283 end else begin
284 (*Printf.printf "Your version of coqtop seems OK\n";*)
285 flush stdout
286 end;
287 List.iter one_file (List.rev !files)