1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
type pp_tag = string
type block_type =
| Pp_hbox
| Pp_vbox of int
| Pp_hvbox of int
| Pp_hovbox of int
type doc_view =
| Ppcmd_empty
| Ppcmd_string of string
| Ppcmd_glue of doc_view list
| Ppcmd_box of block_type * doc_view
| Ppcmd_tag of pp_tag * doc_view
| Ppcmd_print_break of int * int
| Ppcmd_force_newline
type t = doc_view
let repr x = x
let unrepr x = x
let utf8_length s =
let len = String.length s
and cnt = ref 0
and nc = ref 0
and p = ref 0 in
while !p < len do
begin
match s.[!p] with
| '\000'..'\127' -> nc := 0
| '\128'..'\191' -> nc := 0
| '\192'..'\223' -> nc := 1
| '\224'..'\239' -> nc := 2
| '\240'..'\247' -> nc := 3
| '\248'..'\251' -> nc := 4
| '\252'..'\253' -> nc := 5
| '\254'..'\255' -> nc := 0
end ;
incr p ;
while !p < len && !nc > 0 do
match s.[!p] with
| '\128'..'\191' -> incr p ; decr nc
| _ -> nc := 0
done ;
incr cnt
done ;
!cnt
let rec app d1 d2 = match d1, d2 with
| Ppcmd_empty, d
| d, Ppcmd_empty -> d
| Ppcmd_glue [l1;l2], Ppcmd_glue l3 -> Ppcmd_glue (l1 :: l2 :: l3)
| Ppcmd_glue [l1;l2], d2 -> Ppcmd_glue [l1 ; l2 ; d2]
| d1, Ppcmd_glue l2 -> Ppcmd_glue (d1 :: l2)
| Ppcmd_tag(t1,d1), Ppcmd_tag(t2,d2)
when t1 = t2 -> Ppcmd_tag(t1,app d1 d2)
| d1, d2 -> Ppcmd_glue [d1; d2]
let seq s = Ppcmd_glue s
let (++) = app
let str s = Ppcmd_string s
let brk (a,b) = Ppcmd_print_break (a,b)
let fnl () = Ppcmd_force_newline
let ws n = Ppcmd_print_break (n,0)
let l = Ppcmd_comment l
let mt () = Ppcmd_empty
let spc () = Ppcmd_print_break (1,0)
let cut () = Ppcmd_print_break (0,0)
let align () = Ppcmd_print_break (0,0)
let int n = str (string_of_int n)
let int64 n = str (Int64.to_string n)
let real r = str (string_of_float r)
let bool b = str (string_of_bool b)
let strbrk s =
let rec aux p n =
if n < String.length s then
if s.[n] = ' ' then
if p = n then spc() :: aux (n+1) (n+1)
else str (String.sub s p (n-p)) :: spc () :: aux (n+1) (n+1)
else aux p (n + 1)
else if p = n then [] else [str (String.sub s p (n-p))]
in Ppcmd_glue (aux 0 0)
let ismt = function | Ppcmd_empty -> true | _ -> false
let h s = Ppcmd_box(Pp_hbox,s)
let v n s = Ppcmd_box(Pp_vbox n,s)
let hv n s = Ppcmd_box(Pp_hvbox n,s)
let hov n s = Ppcmd_box(Pp_hovbox n,s)
let tag t s = Ppcmd_tag(t,s)
let qstring s = str (CString.quote_coq_string s)
let qs = qstring
let quote s = h (str "\"" ++ s ++ str "\"")
let rec pr_com ft s =
let (s1,os) =
try
let n = String.index s '\n' in
String.sub s 0 n, Some (String.sub s (n+1) (String.length s - n - 1))
with Not_found -> s,None in
Format.pp_print_as ft (utf8_length s1) s1;
match os with
Some s2 -> Format.pp_force_newline ft (); pr_com ft s2
| None -> ()
let pr_com ft s =
pr_com ft s;
Format.pp_print_break ft 0 0
let start_pfx = "start."
let end_pfx = "end."
let split_pfx pfx str =
let (str_len, pfx_len) = (String.length str, String.length pfx) in
if str_len >= pfx_len && (String.sub str 0 pfx_len) = pfx then
(pfx, String.sub str pfx_len (str_len - pfx_len)) else ("", str);;
let split_tag tag =
let (pfx, ttag) = split_pfx start_pfx tag in
if pfx <> "" then (pfx, ttag) else
let (pfx, ttag) = split_pfx end_pfx tag in
(pfx, ttag);;
let pp_with ft pp =
let cpp_open_box = function
| Pp_hbox -> Format.pp_open_hbox ft ()
| Pp_vbox n -> Format.pp_open_vbox ft n
| Pp_hvbox n -> Format.pp_open_hvbox ft n
| Pp_hovbox n -> Format.pp_open_box ft n
in
let rec pp_cmd = let open Format in function
| Ppcmd_empty -> ()
| Ppcmd_glue sl -> List.iter pp_cmd sl
| Ppcmd_string str -> let n = utf8_length str in
pp_print_as ft n str
| Ppcmd_box(bty,ss) -> cpp_open_box bty ;
if not (over_max_boxes ()) then pp_cmd ss;
pp_close_box ft ()
| Ppcmd_print_break(m,n) -> pp_print_break ft m n
| Ppcmd_force_newline -> pp_force_newline ft ()
| Ppcmd_comment coms -> List.iter (pr_com ft) coms
| Ppcmd_tag(tag, s) -> pp_open_stag ft (String_tag tag);
pp_cmd s;
pp_close_stag ft ()
in
pp_cmd pp
(** Output to a string formatter *)
let string_of_ppcmds c =
Format.fprintf Format.str_formatter "@[%a@]" pp_with c;
Format.flush_str_formatter ()
let pr_comma () = str "," ++ spc ()
let pr_semicolon () = str ";" ++ spc ()
let pr_bar () = str "|" ++ spc ()
let pr_spcbar () = str " |" ++ spc ()
let pr_arg pr x = spc () ++ pr x
let pr_non_empty_arg pr x = let pp = pr x in if ismt pp then mt () else spc () ++ pr x
let pr_opt pr = function None -> mt () | Some x -> pr_arg pr x
let pr_opt_no_spc pr = function None -> mt () | Some x -> pr x
let pr_opt_default prdf pr = function None -> prdf () | Some x -> pr_arg pr x
let pr_opt_no_spc_default prdf pr = function None -> prdf () | Some x -> pr x
(** TODO: merge with CString.ordinal *)
let pr_nth n =
let s =
if (n / 10) mod 10 = 1 then "th"
else match n mod 10 with
| 1 -> "st"
| 2 -> "nd"
| 3 -> "rd"
| _ -> "th"
in
int n ++ str s
let prlist pr l = Ppcmd_glue (List.map pr l)
let prlist_sep_lastsep no_empty sep_thunk lastsep_thunk elem l =
let sep = sep_thunk () in
let lastsep = lastsep_thunk () in
let elems = List.map elem l in
let filtered_elems =
if no_empty then
List.filter (fun e -> not (ismt e)) elems
else
elems
in
let rec insert_seps es =
match es with
| [] -> mt ()
| [e] -> e
| h::[e] -> h ++ lastsep ++ e
| h::t -> h ++ sep ++ insert_seps t
in
insert_seps filtered_elems
let prlist_strict pr l = prlist_sep_lastsep true mt mt pr l
let prlist_with_sep sep pr l = prlist_sep_lastsep false sep sep pr l
let pr_sequence pr l = prlist_sep_lastsep true spc spc pr l
let pr_enum pr l = prlist_sep_lastsep true pr_comma (fun () -> str " and" ++ spc ()) pr l
let pr_choice pr l = prlist_sep_lastsep true pr_comma (fun () -> str " or" ++ spc ()) pr l
let pr_vertical_list pr = function
| [] -> str "none" ++ fnl ()
| l -> fnl () ++ str " " ++ hov 0 (prlist_with_sep fnl pr l) ++ fnl ()
let prvecti_with_sep sep elem v =
let v = CArray.mapi (fun i x ->
let pp = if i = 0 then mt() else sep() in
pp ++ elem i x)
v
in
seq (Array.to_list v)
let prvecti elem v = prvecti_with_sep mt elem v
let prvect_with_sep sep elem v = prvecti_with_sep sep (fun _ -> elem) v
let prvect elem v = prvect_with_sep mt elem v
let surround p = hov 1 (str"(" ++ p ++ str")")
let db_print_pp fmt pp =
let open Format in
let block_type fmt btype =
let (bt, v) =
match btype with
| Pp_hbox -> ("Pp_hbox", None)
| Pp_vbox v -> ("Pp_vbox", Some v)
| Pp_hvbox v -> ("Pp_hvbox", Some v)
| Pp_hovbox v -> ("Pp_hovbox", Some v)
in
match v with
| None -> fprintf fmt "%s" bt
| Some v -> fprintf fmt "%s %d" bt v
in
let rec db_print_pp_r indent pp =
let ind () = fprintf fmt "%s" (String.make (2 * indent) ' ') in
ind();
match pp with
| Ppcmd_empty ->
fprintf fmt "Ppcmd_empty@;"
| Ppcmd_string str ->
fprintf fmt "Ppcmd_string '%s'@;" str
| Ppcmd_glue list ->
fprintf fmt "Ppcmd_glue@;";
List.iter (fun x -> db_print_pp_r (indent + 1) (repr x)) list;
| Ppcmd_box (block, pp) ->
fprintf fmt "Ppcmd_box %a@;" block_type block;
db_print_pp_r (indent + 1) (repr pp);
| Ppcmd_tag (tag, pp) ->
fprintf fmt "Ppcmd_tag %s@;" tag;
db_print_pp_r (indent + 1) (repr pp);
| Ppcmd_print_break (i, j) ->
fprintf fmt "Ppcmd_print_break %d %d@;" i j
| Ppcmd_force_newline ->
fprintf fmt "Ppcmd_force_newline@;"
| Ppcmd_comment list ->
fprintf fmt "Ppcmd_comment@;";
List.iter (fun x -> ind(); (fprintf fmt "%s@;" x)) list
in
pp_open_vbox fmt 0;
db_print_pp_r 0 pp;
pp_close_box fmt ();
pp_print_flush fmt ()
let db_string_of_pp pp =
Format.asprintf "%a" db_print_pp pp
let has_format_special s =
let rec aux i =
if i = String.length s then false
else match String.unsafe_get s i with
| '@' | '%' | '\\' | '"' -> true
| _ -> aux (i+1)
in
aux 0
let pp_as_format ?(with_tags=false) pp =
let open Format in
let fmt, return =
let buf = Buffer.create 200 in
let fmt = Format.formatter_of_buffer buf in
fmt, (fun () -> Format.pp_print_flush fmt (); buf)
in
let args = ref [] in
let open_box bty =
fprintf fmt "%s" "@[";
match bty with
| Pp_hbox -> fprintf fmt "<h>"
| Pp_vbox i -> if i = 0 then fprintf fmt "<v>" else fprintf fmt "<v %d>" i
| Pp_hvbox i -> if i = 0 then fprintf fmt "<hv>" else fprintf fmt "<hv %d>" i
| Pp_hovbox i -> if i = 0 then () else fprintf fmt "<%d>" i
in
let close_box () = fprintf fmt "%s" "@]" in
let rec pprec pp =
match pp with
| Ppcmd_empty -> ()
| Ppcmd_string s ->
if has_format_special s then begin
fprintf fmt "%s" "%s";
args := s :: !args
end else fprintf fmt "%s" s
| Ppcmd_glue l -> List.iter pprec l
| Ppcmd_box (bty, pp) ->
open_box bty;
pprec pp;
close_box ()
| Ppcmd_tag (tag,pp) ->
if with_tags then begin
fprintf fmt "%s<%s>" "@{" tag;
pprec pp;
fprintf fmt "%s" "@}"
end
else pprec pp
| Ppcmd_print_break (nspaces,offset) -> begin match nspaces, offset with
| 0, 0 -> fprintf fmt "%s" "@,"
| 1, 0 -> fprintf fmt "%s" "@ "
| _ -> fprintf fmt "%s<%d %d>" "@;" nspaces offset
end
| Ppcmd_force_newline -> fprintf fmt "%s" "@."
| Ppcmd_comment [] -> ()
| Ppcmd_comment _ -> failwith "not implemented pp_as_format on nonempty Ppcmd_comment"
in
let () = pprec pp in
let buf = return () in
let fmt = Buffer.contents buf in
let args = List.rev !args in
fmt, args
let rec flatten pp =
match pp with
| Ppcmd_glue l -> Ppcmd_glue (List.concat (List.map
(fun x -> let x = flatten x in
match x with
| Ppcmd_glue l2 -> l2
| p -> [p])
l))
| Ppcmd_box (block, pp) -> Ppcmd_box (block, flatten pp)
| Ppcmd_tag (tag, pp) -> Ppcmd_tag (tag, flatten pp)
| p -> p