Debian Codegraph @

lib/pp.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 (* The different kinds of blocks are:
12 \begin{description}
13 \item[hbox:] Horizontal block no line breaking;
14 \item[vbox:] Vertical block each break leads to a new line;
15 \item[hvbox:] Horizontal-vertical block: same as vbox, except if
16 this block is small enough to fit on a single line
17 \item[hovbox:] Horizontal or Vertical block: breaks lead to new line
18 only when necessary to print the content of the block
19 \end{description}
20 *)
21
22 type pp_tag = string
23
24 type block_type =
25 | Pp_hbox
26 | Pp_vbox of int
27 | Pp_hvbox of int
28 | Pp_hovbox of int
29
30 type doc_view =
31 | Ppcmd_empty
32 | Ppcmd_string of string
33 | Ppcmd_sized_string of int * string
34 | Ppcmd_glue of doc_view list
35 | Ppcmd_box of block_type * doc_view
36 | Ppcmd_tag of pp_tag * doc_view
37 (* Are those redundant? *)
38 | Ppcmd_print_break of int * int
39 | Ppcmd_force_newline
40 | Ppcmd_comment of string list
41
42 (* Following discussion on #390, we play on the safe side and make the
43 internal representation opaque here. *)
44 type t = doc_view
45
46 let repr x = x
47 let unrepr x = x
48
49 (* Compute length of an UTF-8 encoded string
50 Rem 1 : utf8_length <= String.length (equal if pure ascii)
51 Rem 2 : if used for an iso8859_1 encoded string, the result is
52 wrong in very rare cases. Such a wrong case corresponds to any
53 sequence of a character in range 192..253 immediately followed by a
54 character in range 128..191 (typical case in french is "déçu" which
55 is counted 3 instead of 4); then no real harm to use always
56 utf8_length even if using an iso8859_1 encoding *)
57
58 let utf8_length s =
59 let len = String.length s
60 and cnt = ref 0
61 and nc = ref 0
62 and p = ref 0 in
63 while !p < len do
64 begin
65 match s.[!p] with
66 | '\000'..'\127' -> nc := 0 (* ascii char *)
67 | '\128'..'\191' -> nc := 0 (* cannot start with a continuation byte *)
68 | '\192'..'\223' -> nc := 1 (* expect 1 continuation byte *)
69 | '\224'..'\239' -> nc := 2 (* expect 2 continuation bytes *)
70 | '\240'..'\247' -> nc := 3 (* expect 3 continuation bytes *)
71 | '\248'..'\251' -> nc := 4 (* expect 4 continuation bytes *)
72 | '\252'..'\253' -> nc := 5 (* expect 5 continuation bytes *)
73 | '\254'..'\255' -> nc := 0 (* invalid byte *)
74 end ;
75 incr p ;
76 while !p < len && !nc > 0 do
77 match s.[!p] with
78 | '\128'..'\191' (* next continuation byte *) -> incr p ; decr nc
79 | _ (* not a continuation byte *) -> nc := 0
80 done ;
81 incr cnt
82 done ;
83 !cnt
84
85 let rec app d1 d2 = match d1, d2 with
86 | Ppcmd_empty, d
87 | d, Ppcmd_empty -> d
88
89 (* Optimizations *)
90 | Ppcmd_glue [l1;l2], Ppcmd_glue l3 -> Ppcmd_glue (l1 :: l2 :: l3)
91 | Ppcmd_glue [l1;l2], d2 -> Ppcmd_glue [l1 ; l2 ; d2]
92 | d1, Ppcmd_glue l2 -> Ppcmd_glue (d1 :: l2)
93
94 | Ppcmd_tag(t1,d1), Ppcmd_tag(t2,d2)
95 when t1 = t2 -> Ppcmd_tag(t1,app d1 d2)
96 | d1, d2 -> Ppcmd_glue [d1; d2]
97 (* Optimizations deemed too costly *)
98 (* | Ppcmd_glue l1, Ppcmd_glue l2 -> Ppcmd_glue (l1 @ l2) *)
99 (* | Ppcmd_string s1, Ppcmd_string s2 -> Ppcmd_string (s1 ^ s2) *)
100
101 let seq s = Ppcmd_glue s
102
103 let (++) = app
104
105 (* formatting commands *)
106 let str s = Ppcmd_string s
107 let sized_str n s = Ppcmd_sized_string (n,s)
108 let brk (a,b) = Ppcmd_print_break (a,b)
109 let fnl () = Ppcmd_force_newline
110 let ws n = Ppcmd_print_break (n,0)
111 let comment l = Ppcmd_comment l
112
113 (* derived commands *)
114 let mt () = Ppcmd_empty
115 let spc () = Ppcmd_print_break (1,0)
116 let cut () = Ppcmd_print_break (0,0)
117 let align () = Ppcmd_print_break (0,0)
118 let int n = str (string_of_int n)
119 let int64 n = str (Int64.to_string n)
120 let real r = str (string_of_float r)
121 let bool b = str (string_of_bool b)
122
123 (* XXX: To Remove *)
124 let strbrk s =
125 let rec aux p n =
126 if n < String.length s then
127 if s.[n] = ' ' then
128 if p = n then spc() :: aux (n+1) (n+1)
129 else str (String.sub s p (n-p)) :: spc () :: aux (n+1) (n+1)
130 else aux p (n + 1)
131 else if p = n then [] else [str (String.sub s p (n-p))]
132 in Ppcmd_glue (aux 0 0)
133
134 let ismt = function | Ppcmd_empty -> true | _ -> false
135
136 (* boxing commands *)
137 let h s = Ppcmd_box(Pp_hbox,s)
138 let v n s = Ppcmd_box(Pp_vbox n,s)
139 let hv n s = Ppcmd_box(Pp_hvbox n,s)
140 let hov n s = Ppcmd_box(Pp_hovbox n,s)
141
142 (* Opening and closing of tags *)
143 let tag t s = Ppcmd_tag(t,s)
144
145 let qstring s = str (CString.quote_coq_string s)
146 let qs = qstring
147 let quote s = h (str "\"" ++ s ++ str "\"")
148
149 let rec pr_com ft s =
150 let (s1,os) =
151 try
152 let n = String.index s '\n' in
153 String.sub s 0 n, Some (String.sub s (n+1) (String.length s - n - 1))
154 with Not_found -> s,None in
155 Format.pp_print_as ft (utf8_length s1) s1;
156 match os with
157 Some s2 -> Format.pp_force_newline ft (); pr_com ft s2
158 | None -> ()
159
160 let pr_com ft s =
161 pr_com ft s;
162 Format.pp_print_break ft 0 0
163
164 let start_pfx = "start."
165 let end_pfx = "end."
166
167 let split_pfx pfx str =
168 let (str_len, pfx_len) = (String.length str, String.length pfx) in
169 if str_len >= pfx_len && (String.sub str 0 pfx_len) = pfx then
170 (pfx, String.sub str pfx_len (str_len - pfx_len)) else ("", str);;
171
172 let split_tag tag =
173 let (pfx, ttag) = split_pfx start_pfx tag in
174 if pfx <> "" then (pfx, ttag) else
175 let (pfx, ttag) = split_pfx end_pfx tag in
176 (pfx, ttag);;
177
178 (* pretty printing functions *)
179 let pp_with ft pp =
180 let cpp_open_box = function
181 | Pp_hbox -> Format.pp_open_hbox ft ()
182 | Pp_vbox n -> Format.pp_open_vbox ft n
183 | Pp_hvbox n -> Format.pp_open_hvbox ft n
184 | Pp_hovbox n -> Format.pp_open_box ft n
185 in
186 let rec pp_cmd = let open Format in function
187 | Ppcmd_empty -> ()
188 | Ppcmd_glue sl -> List.iter pp_cmd sl
189 | Ppcmd_string str ->
190 let n = utf8_length str in
191 pp_print_as ft n str
192 | Ppcmd_sized_string (n, str) -> pp_print_as ft n str
193 | Ppcmd_box(bty,ss) ->
194 cpp_open_box bty ;
195 if not (over_max_boxes ()) then pp_cmd ss;
196 pp_close_box ft ()
197 | Ppcmd_print_break(m,n) -> pp_print_break ft m n
198 | Ppcmd_force_newline -> pp_force_newline ft ()
199 | Ppcmd_comment coms -> List.iter (pr_com ft) coms
200 | Ppcmd_tag(tag, s) ->
201 pp_open_stag ft (String_tag tag);
202 pp_cmd s;
203 pp_close_stag ft ()
204 in
205 pp_cmd pp
206
207 (* If mixing some output and a goal display, please use msg_warning,
208 so that interfaces (proofgeneral for example) can easily dispatch
209 them to different windows. *)
210
211 (** Output to a string formatter *)
212 let string_of_ppcmds c =
213 Format.fprintf Format.str_formatter "@[%a@]" pp_with c;
214 Format.flush_str_formatter ()
215
216 (* Copy paste from Util *)
217
218 let pr_comma () = str "," ++ spc ()
219 let pr_semicolon () = str ";" ++ spc ()
220 let pr_bar () = str "|" ++ spc ()
221 let pr_spcbar () = str " |" ++ spc ()
222 let pr_arg pr x = spc () ++ pr x
223 let pr_non_empty_arg pr x = let pp = pr x in if ismt pp then mt () else spc () ++ pr x
224 let pr_opt pr = function None -> mt () | Some x -> pr_arg pr x
225 let pr_opt_no_spc pr = function None -> mt () | Some x -> pr x
226 let pr_opt_default prdf pr = function None -> prdf () | Some x -> pr_arg pr x
227 let pr_opt_no_spc_default prdf pr = function None -> prdf () | Some x -> pr x
228
229 (** TODO: merge with CString.ordinal *)
230 let pr_nth n =
231 let s =
232 if (n / 10) mod 10 = 1 then "th"
233 else match n mod 10 with
234 | 1 -> "st"
235 | 2 -> "nd"
236 | 3 -> "rd"
237 | _ -> "th"
238 in
239 int n ++ str s
240
241 (* [prlist pr [a ; ... ; c]] outputs [pr a ++ ... ++ pr c] *)
242
243 let prlist pr l = Ppcmd_glue (List.map pr l)
244
245 (* unlike all other functions below, [prlist] works lazily.
246 if a strict behavior is needed, use [prlist_strict] instead.
247 evaluation is done from left to right. *)
248
249 let prlist_sep_lastsep no_empty sep_thunk lastsep_thunk elem l =
250 let sep = sep_thunk () in
251 let lastsep = lastsep_thunk () in
252 let elems = List.map elem l in
253 let filtered_elems =
254 if no_empty then
255 List.filter (fun e -> not (ismt e)) elems
256 else
257 elems
258 in
259 let rec insert_seps es =
260 match es with
261 | [] -> mt ()
262 | [e] -> e
263 | h::[e] -> h ++ lastsep ++ e
264 | h::t -> h ++ sep ++ insert_seps t
265 in
266 insert_seps filtered_elems
267
268 let prlist_strict pr l = prlist_sep_lastsep true mt mt pr l
269 (* [prlist_with_sep sep pr [a ; ... ; c]] outputs
270 [pr a ++ sep() ++ ... ++ sep() ++ pr c] *)
271 let prlist_with_sep sep pr l = prlist_sep_lastsep false sep sep pr l
272 (* Print sequence of objects separated by space (unless an element is empty) *)
273 let pr_sequence pr l = prlist_sep_lastsep true spc spc pr l
274 (* [pr_enum pr [a ; b ; ... ; c]] outputs
275 [pr a ++ str "," ++ pr b ++ str "," ++ ... ++ str "and" ++ pr c] *)
276 let pr_enum pr l = prlist_sep_lastsep true pr_comma (fun () -> str " and" ++ spc ()) pr l
277
278 let pr_choice pr l = prlist_sep_lastsep true pr_comma (fun () -> str " or" ++ spc ()) pr l
279
280 let pr_vertical_list pr = function
281 | [] -> str "none" ++ fnl ()
282 | l -> fnl () ++ str " " ++ hov 0 (prlist_with_sep fnl pr l) ++ fnl ()
283
284 (* [prvecti_with_sep sep pr [|a0 ; ... ; an|]] outputs
285 [pr 0 a0 ++ sep() ++ ... ++ sep() ++ pr n an] *)
286
287 let prvecti_with_sep sep elem v =
288 let v = CArray.mapi (fun i x ->
289 let pp = if i = 0 then mt() else sep() in
290 pp ++ elem i x)
291 v
292 in
293 seq (Array.to_list v)
294
295 (* [prvecti pr [|a0 ; ... ; an|]] outputs [pr 0 a0 ++ ... ++ pr n an] *)
296
297 let prvecti elem v = prvecti_with_sep mt elem v
298
299 (* [prvect_with_sep sep pr [|a ; ... ; c|]] outputs
300 [pr a ++ sep() ++ ... ++ sep() ++ pr c] *)
301
302 let prvect_with_sep sep elem v = prvecti_with_sep sep (fun _ -> elem) v
303
304 (* [prvect pr [|a ; ... ; c|]] outputs [pr a ++ ... ++ pr c] *)
305
306 let prvect elem v = prvect_with_sep mt elem v
307
308 let surround p = hov 1 (str"(" ++ p ++ str")")
309
310 (*** DEBUG code ***)
311
312 let db_print_pp fmt pp =
313 let open Format in
314 let block_type fmt btype =
315 let (bt, v) =
316 match btype with
317 | Pp_hbox -> ("Pp_hbox", None)
318 | Pp_vbox v -> ("Pp_vbox", Some v)
319 | Pp_hvbox v -> ("Pp_hvbox", Some v)
320 | Pp_hovbox v -> ("Pp_hovbox", Some v)
321 in
322 match v with
323 | None -> fprintf fmt "%s" bt
324 | Some v -> fprintf fmt "%s %d" bt v
325 in
326 let rec db_print_pp_r indent pp =
327 let ind () = fprintf fmt "%s" (String.make (2 * indent) ' ') in
328 ind();
329 match pp with
330 | Ppcmd_empty ->
331 fprintf fmt "Ppcmd_empty@;"
332 | Ppcmd_string str ->
333 fprintf fmt "Ppcmd_string '%s'@;" str
334 | Ppcmd_sized_string (n, str) ->
335 fprintf fmt "Ppcmd_size_string (%d, '%s')@;" n str
336 | Ppcmd_glue list ->
337 fprintf fmt "Ppcmd_glue@;";
338 List.iter (fun x -> db_print_pp_r (indent + 1) (repr x)) list;
339 | Ppcmd_box (block, pp) ->
340 fprintf fmt "Ppcmd_box %a@;" block_type block;
341 db_print_pp_r (indent + 1) (repr pp);
342 | Ppcmd_tag (tag, pp) ->
343 fprintf fmt "Ppcmd_tag %s@;" tag;
344 db_print_pp_r (indent + 1) (repr pp);
345 | Ppcmd_print_break (i, j) ->
346 fprintf fmt "Ppcmd_print_break %d %d@;" i j
347 | Ppcmd_force_newline ->
348 fprintf fmt "Ppcmd_force_newline@;"
349 | Ppcmd_comment list ->
350 fprintf fmt "Ppcmd_comment@;";
351 List.iter (fun x -> ind(); (fprintf fmt "%s@;" x)) list
352 in
353 pp_open_vbox fmt 0;
354 db_print_pp_r 0 pp;
355 pp_close_box fmt ();
356 pp_print_flush fmt ()
357
358 let db_string_of_pp pp =
359 Format.asprintf "%a" db_print_pp pp
360
361 let has_format_special s =
362 String.exists (function
363 | '@' | '%' | '\\' | '"' -> true
364 | _ -> false)
365 s
366
367 let pp_as_format ?(with_tags=false) pp =
368 let open Format in
369 let fmt, return =
370 let buf = Buffer.create 200 in
371 let fmt = Format.formatter_of_buffer buf in
372 fmt, (fun () -> Format.pp_print_flush fmt (); buf)
373 in
374 let args = ref [] in
375 let open_box bty =
376 fprintf fmt "%s" "@[";
377 match bty with
378 | Pp_hbox -> fprintf fmt "<h>"
379 | Pp_vbox i -> if i = 0 then fprintf fmt "<v>" else fprintf fmt "<v %d>" i
380 | Pp_hvbox i -> if i = 0 then fprintf fmt "<hv>" else fprintf fmt "<hv %d>" i
381 | Pp_hovbox i -> if i = 0 then () else fprintf fmt "<%d>" i
382 in
383 let close_box () = fprintf fmt "%s" "@]" in
384 let rec pprec pp =
385 match pp with
386 | Ppcmd_empty -> ()
387 | Ppcmd_string s ->
388 if has_format_special s then begin
389 fprintf fmt "%%s";
390 args := s :: !args
391 end else fprintf fmt "%s" s
392 | Ppcmd_sized_string (n, s) ->
393 fprintf fmt "@<%d>%%s" n;
394 args := s :: !args
395 | Ppcmd_glue l -> List.iter pprec l
396 | Ppcmd_box (bty, pp) ->
397 open_box bty;
398 pprec pp;
399 close_box ()
400 | Ppcmd_tag (tag,pp) ->
401 if with_tags then begin
402 fprintf fmt "%s<%s>" "@{" tag;
403 pprec pp;
404 fprintf fmt "%s" "@}"
405 end
406 else pprec pp
407 | Ppcmd_print_break (nspaces,offset) -> begin match nspaces, offset with
408 | 0, 0 -> fprintf fmt "%s" "@,"
409 | 1, 0 -> fprintf fmt "%s" "@ "
410 | _ -> fprintf fmt "%s<%d %d>" "@;" nspaces offset
411 end
412 | Ppcmd_force_newline -> fprintf fmt "%s" "@."
413 | Ppcmd_comment [] -> ()
414 | Ppcmd_comment _ -> failwith "not implemented pp_as_format on nonempty Ppcmd_comment"
415 in
416 let () = pprec pp in
417 let buf = return () in
418 let fmt = Buffer.contents buf in
419 let args = List.rev !args in
420 fmt, args
421
422 let rec flatten pp =
423 match pp with
424 | Ppcmd_glue l -> Ppcmd_glue (List.concat (List.map
425 (fun x -> let x = flatten x in
426 match x with
427 | Ppcmd_glue l2 -> l2
428 | p -> [p])
429 l))
430 | Ppcmd_box (block, pp) -> Ppcmd_box (block, flatten pp)
431 | Ppcmd_tag (tag, pp) -> Ppcmd_tag (tag, flatten pp)
432 | p -> p
433
434 type container =
435 | CBox of block_type
436 | CTag of pp_tag
437 | CTop
438
439 let box_of_string s =
440 let indent, bty = CamlinternalFormat.open_box_of_string s in
441 match bty with
442 | Pp_hbox -> Pp_hbox (* check indent? *)
443 | Pp_vbox -> Pp_vbox indent
444 | Pp_hvbox -> Pp_hvbox indent
445 (* NB these 3 are all treated the same *)
446 | Pp_hovbox | Pp_box | Pp_fits -> Pp_hovbox indent
447
448 let of_acc =
449 let open CamlinternalFormat in
450 let push stack elt = match stack with
451 | [] -> assert false
452 | (l,k) :: rest -> (elt :: l, k) :: rest
453 in
454 let seq0 = function
455 | [] -> mt()
456 | [x] -> x
457 | l -> seq l
458 in
459 let init = [[], CTop] in
460 let finish = function
461 | [elts, CTop] -> seq0 (List.rev elts)
462 | _ -> assert false
463 in
464 let rec of_acc stack = function
465 | Acc_string_literal(Acc_formatting_lit (p, Magic_size (_, size)), s)
466 | Acc_data_string (Acc_formatting_lit (p, Magic_size (_, size)), s) ->
467 let stack = of_acc stack p in
468 push stack (sized_str size s)
469 | Acc_char_literal (Acc_formatting_lit (p, Magic_size (_, size)), c)
470 | Acc_data_char (Acc_formatting_lit (p, Magic_size (_, size)), c) ->
471 let stack = of_acc stack p in
472 push stack (sized_str size (String.make 1 c))
473 | Acc_formatting_lit (p, f) ->
474 let stack = of_acc stack p in
475 of_formatting_lit stack f
476 | Acc_formatting_gen (p, Acc_open_tag acc') ->
477 let stack = of_acc stack p in
478 let tag = compute_tag acc' in
479 ([], CTag tag) :: stack
480 | Acc_formatting_gen (p, Acc_open_box acc') ->
481 let stack = of_acc stack p in
482 let tag = compute_tag acc' in
483 let box = box_of_string tag in
484 ([], CBox box) :: stack
485 | Acc_string_literal (p, s)
486 | Acc_data_string (p, s) ->
487 let stack = of_acc stack p in
488 push stack (str s)
489 | Acc_char_literal (p, c)
490 | Acc_data_char (p, c) ->
491 let stack = of_acc stack p in
492 push stack (str @@ String.make 1 c)
493 | Acc_delay (p, f) ->
494 let stack = of_acc stack p in
495 let x = f () in
496 push stack x
497 | Acc_flush p -> of_acc stack p (* ignore flush *)
498 | Acc_invalid_arg (p, msg) -> invalid_arg msg
499 | End_of_acc -> stack
500
501 and compute_tag tag =
502 let tag = finish (of_acc init tag) in
503 let tag = string_of_ppcmds tag in
504 if String.length tag < 2 then tag
505 else String.sub tag 1 (String.length tag - 2)
506
507 and of_formatting_lit stack flit = match stack, flit with
508 | (elts, CBox box) :: rest, Close_box ->
509 push rest (Ppcmd_box (box, seq0 (List.rev elts)))
510 | (elts, CTag tag) :: rest, Close_tag ->
511 push rest (Ppcmd_tag (tag, seq0 (List.rev elts)))
512 | stack, Break (_, width, offset) ->
513 push stack (brk (width, offset))
514 | stack, FFlush -> stack (* flush is ignored, maybe error instead? *)
515 | stack, Force_newline -> push stack (fnl())
516 | stack, Flush_newline -> push stack (fnl()) (* not sure if correct *)
517 | stack, Magic_size _ -> stack (* invalid size? maybe should raise invalid_arg *)
518 | stack, Escaped_at -> push stack (str "@")
519 | stack, Escaped_percent -> push stack (str "%")
520 | stack, Scan_indic c -> push stack (str @@ Printf.sprintf "@%c" c)
521 | _, (Close_box | Close_tag) -> assert false
522 in
523 fun acc -> finish (of_acc init acc)
524
525 let kf k (CamlinternalFormatBasics.Format (fmt, _)) =
526 CamlinternalFormat.make_printf (fun acc -> k (of_acc acc))
527 End_of_acc
528 fmt
529
530 let fmt fmt = kf (fun x -> x) fmt