Source file format.ml
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
let id x = x
module Size : sig
type t
val to_int : t -> int
val of_int : int -> t
val zero : t
val unknown : t
val is_known : t -> bool
end = struct
type t = int
let to_int = id
let of_int = id
let zero = 0
let unknown = -1
let is_known n = n >= 0
end
type box_type = CamlinternalFormatBasics.block_type =
| Pp_hbox | Pp_vbox | Pp_hvbox | Pp_hovbox | Pp_box | Pp_fits
type pp_token =
| Pp_text of string
| Pp_substring of { source:string; pos:int; len:int}
| Pp_break of {
fits: string * int * string;
breaks: string * int * string;
}
| Pp_tbreak of int * int
| Pp_stab
| Pp_begin of int * box_type
| Pp_end
| Pp_tbegin of tbox
| Pp_tend
| Pp_newline
| Pp_if_newline
| Pp_open_tag of stag
| Pp_close_tag
and stag = ..
and tbox = Pp_tbox of int list ref
type tag = string
type stag += String_tag of tag
type pp_queue_elem = {
mutable size : Size.t;
token : pp_token;
length : int;
}
type pp_queue = pp_queue_elem Queue.t
type pp_scan_elem = {
left_total : int;
queue_elem : pp_queue_elem
}
type pp_format_elem = { box_type : box_type; width : int }
type formatter = {
pp_scan_stack : pp_scan_elem Stack.t;
pp_format_stack : pp_format_elem Stack.t;
pp_tbox_stack : tbox Stack.t;
pp_tag_stack : stag Stack.t;
pp_mark_stack : stag Stack.t;
mutable pp_margin : int;
mutable pp_min_space_left : int;
mutable pp_max_indent : int;
mutable pp_space_left : int;
mutable pp_current_indent : int;
mutable pp_is_new_line : bool;
mutable pp_left_total : int;
mutable pp_right_total : int;
mutable pp_curr_depth : int;
mutable pp_max_boxes : int;
mutable pp_ellipsis : string;
mutable pp_out_string : string -> int -> int -> unit;
mutable pp_out_flush : unit -> unit;
mutable pp_out_newline : unit -> unit;
mutable pp_out_spaces : int -> unit;
mutable pp_out_indent : int -> unit;
mutable pp_print_tags : bool;
mutable pp_mark_tags : bool;
mutable pp_mark_open_tag : stag -> string;
mutable pp_mark_close_tag : stag -> string;
mutable pp_print_open_tag : stag -> unit;
mutable pp_print_close_tag : stag -> unit;
pp_queue : pp_queue;
}
type formatter_stag_functions = {
mark_open_stag : stag -> string;
mark_close_stag : stag -> string;
print_open_stag : stag -> unit;
print_close_stag : stag -> unit;
}
type formatter_out_functions = {
out_string : string -> int -> int -> unit;
out_flush : unit -> unit;
out_newline : unit -> unit;
out_spaces : int -> unit;
out_indent : int -> unit;
}
let pp_enqueue state token =
state.pp_right_total <- state.pp_right_total + token.length;
Queue.add token state.pp_queue
let pp_clear_queue state =
state.pp_left_total <- 1; state.pp_right_total <- 1;
Queue.clear state.pp_queue
let pp_infinity = 1000000010
let pp_output_string state s = state.pp_out_string s 0 (String.length s)
and pp_output_substring state ~pos ~len s =
state.pp_out_string s pos len
and pp_output_newline state = state.pp_out_newline ()
and pp_output_spaces state n = state.pp_out_spaces n
and pp_output_indent state n = state.pp_out_indent n
let format_pp_text state size text =
state.pp_space_left <- state.pp_space_left - size;
pp_output_string state text;
state.pp_is_new_line <- false
let format_pp_substring state size ~pos ~len source =
state.pp_space_left <- state.pp_space_left - size;
pp_output_substring state ~pos ~len source;
state.pp_is_new_line <- false
let format_string state s =
if s <> "" then format_pp_text state (String.length s) s
let break_new_line state (before, offset, after) width =
format_string state before;
pp_output_newline state;
state.pp_is_new_line <- true;
let indent = state.pp_margin - width + offset in
let real_indent = Int.min state.pp_max_indent indent in
state.pp_current_indent <- real_indent;
state.pp_space_left <- state.pp_margin - state.pp_current_indent;
pp_output_indent state state.pp_current_indent;
format_string state after
let break_line state width = break_new_line state ("", 0, "") width
let break_same_line state (before, width, after) =
format_string state before;
state.pp_space_left <- state.pp_space_left - width;
pp_output_spaces state width;
format_string state after
let pp_force_break_line state =
match Stack.top_opt state.pp_format_stack with
| None -> pp_output_newline state
| Some { box_type; width } ->
if width > state.pp_space_left then
match box_type with
| Pp_fits | Pp_hbox -> ()
| Pp_vbox | Pp_hvbox | Pp_hovbox | Pp_box -> break_line state width
let pp_skip_token state =
match Queue.take_opt state.pp_queue with
| None -> ()
| Some { size; length; _ } ->
state.pp_left_total <- state.pp_left_total - length;
state.pp_space_left <- state.pp_space_left + Size.to_int size
let format_pp_token state size = function
| Pp_text s ->
format_pp_text state size s
| Pp_substring {source;pos;len} ->
format_pp_substring state size ~pos ~len source
| Pp_begin (off, ty) ->
let insertion_point = state.pp_margin - state.pp_space_left in
if insertion_point > state.pp_max_indent then
begin pp_force_break_line state end;
let width = state.pp_space_left - off in
let box_type =
match ty with
| Pp_vbox -> Pp_vbox
| Pp_hbox | Pp_hvbox | Pp_hovbox | Pp_box | Pp_fits ->
if size > state.pp_space_left then ty else Pp_fits in
Stack.push { box_type; width } state.pp_format_stack
| Pp_end ->
Stack.pop_opt state.pp_format_stack |> ignore
| Pp_tbegin (Pp_tbox _ as tbox) ->
Stack.push tbox state.pp_tbox_stack
| Pp_tend ->
Stack.pop_opt state.pp_tbox_stack |> ignore
| Pp_stab ->
begin match Stack.top_opt state.pp_tbox_stack with
| None -> ()
| Some (Pp_tbox tabs) ->
let rec add_tab n = function
| [] -> [n]
| x :: l as ls -> if n < x then n :: ls else x :: add_tab n l in
tabs := add_tab (state.pp_margin - state.pp_space_left) !tabs
end
| Pp_tbreak (n, off) ->
let insertion_point = state.pp_margin - state.pp_space_left in
begin match Stack.top_opt state.pp_tbox_stack with
| None -> ()
| Some (Pp_tbox tabs) ->
let tab =
match !tabs with
| [] -> insertion_point
| first :: _ ->
let rec find = function
| head :: tail ->
if head >= insertion_point then head else find tail
| [] -> first in
find !tabs in
let offset = tab - insertion_point in
if offset >= 0
then break_same_line state ("", offset + n, "")
else break_new_line state ("", tab + off, "") state.pp_margin
end
| Pp_newline ->
begin match Stack.top_opt state.pp_format_stack with
| None -> pp_output_newline state
| Some { width; _} -> break_line state width
end
| Pp_if_newline ->
if state.pp_current_indent != state.pp_margin - state.pp_space_left
then pp_skip_token state
| Pp_break { fits; breaks } ->
let before, off, _ = breaks in
begin match Stack.top_opt state.pp_format_stack with
| None -> ()
| Some { box_type; width } ->
begin match box_type with
| Pp_hovbox ->
if size + String.length before > state.pp_space_left
then break_new_line state breaks width
else break_same_line state fits
| Pp_box ->
if state.pp_is_new_line then break_same_line state fits else
if size + String.length before > state.pp_space_left
then break_new_line state breaks width else
if state.pp_current_indent > state.pp_margin - width + off
then break_new_line state breaks width
else break_same_line state fits
| Pp_hvbox -> break_new_line state breaks width
| Pp_fits -> break_same_line state fits
| Pp_vbox -> break_new_line state breaks width
| Pp_hbox -> break_same_line state fits
end
end
| Pp_open_tag tag_name ->
let marker = state.pp_mark_open_tag tag_name in
pp_output_string state marker;
Stack.push tag_name state.pp_mark_stack
| Pp_close_tag ->
begin match Stack.pop_opt state.pp_mark_stack with
| None -> ()
| Some tag_name ->
let marker = state.pp_mark_close_tag tag_name in
pp_output_string state marker
end
let rec advance_left state =
match Queue.peek_opt state.pp_queue with
| None -> ()
| Some { size; token; length } ->
let pending_count = state.pp_right_total - state.pp_left_total in
if Size.is_known size || pending_count >= state.pp_space_left then begin
Queue.take state.pp_queue |> ignore;
let size = if Size.is_known size then Size.to_int size else pp_infinity in
format_pp_token state size token;
state.pp_left_total <- length + state.pp_left_total;
(advance_left [@tailcall]) state
end
let enqueue_advance state tok = pp_enqueue state tok; advance_left state
let enqueue_string_as state size s =
enqueue_advance state { size; token = Pp_text s; length = Size.to_int size }
let enqueue_substring_as ~pos ~len state size source =
let token = Pp_substring {source;pos;len} in
enqueue_advance state { size; token; length = Size.to_int size }
let enqueue_string state s =
enqueue_string_as state (Size.of_int (String.length s)) s
let initialize_scan_stack stack =
Stack.clear stack;
let queue_elem = { size = Size.unknown; token = Pp_text ""; length = 0 } in
Stack.push { left_total = -1; queue_elem } stack
let set_size state ty =
match Stack.top_opt state.pp_scan_stack with
| None -> ()
| Some { left_total; queue_elem } ->
let size = Size.to_int queue_elem.size in
if left_total < state.pp_left_total then
initialize_scan_stack state.pp_scan_stack
else
match queue_elem.token with
| Pp_break _ | Pp_tbreak (_, _) ->
if ty then begin
queue_elem.size <- Size.of_int (state.pp_right_total + size);
Stack.pop_opt state.pp_scan_stack |> ignore
end
| Pp_begin (_, _) ->
if not ty then begin
queue_elem.size <- Size.of_int (state.pp_right_total + size);
Stack.pop_opt state.pp_scan_stack |> ignore
end
| Pp_text _ | Pp_substring _ | Pp_stab | Pp_tbegin _ | Pp_tend | Pp_end
| Pp_newline | Pp_if_newline | Pp_open_tag _ | Pp_close_tag ->
()
let scan_push state b token =
pp_enqueue state token;
if b then set_size state true;
let elem = { left_total = state.pp_right_total; queue_elem = token } in
Stack.push elem state.pp_scan_stack
let pp_open_box_gen state indent br_ty =
state.pp_curr_depth <- state.pp_curr_depth + 1;
if state.pp_curr_depth < state.pp_max_boxes then
let size = Size.of_int (- state.pp_right_total) in
let elem = { size; token = Pp_begin (indent, br_ty); length = 0 } in
scan_push state false elem else
if state.pp_curr_depth = state.pp_max_boxes
then enqueue_string state state.pp_ellipsis
let pp_open_sys_box state = pp_open_box_gen state 0 Pp_hovbox
let pp_close_box state () =
if state.pp_curr_depth > 1 then
begin
if state.pp_curr_depth < state.pp_max_boxes then
begin
pp_enqueue state { size = Size.zero; token = Pp_end; length = 0 };
set_size state true; set_size state false
end;
state.pp_curr_depth <- state.pp_curr_depth - 1;
end
let pp_open_stag state tag_name =
if state.pp_print_tags then
begin
Stack.push tag_name state.pp_tag_stack;
state.pp_print_open_tag tag_name
end;
if state.pp_mark_tags then
let token = Pp_open_tag tag_name in
pp_enqueue state { size = Size.zero; token; length = 0 }
let pp_close_stag state () =
if state.pp_mark_tags then
pp_enqueue state { size = Size.zero; token = Pp_close_tag; length = 0 };
if state.pp_print_tags then
match Stack.pop_opt state.pp_tag_stack with
| None -> ()
| Some tag_name ->
state.pp_print_close_tag tag_name
let pp_set_print_tags state b = state.pp_print_tags <- b
let pp_set_mark_tags state b = state.pp_mark_tags <- b
let pp_get_print_tags state () = state.pp_print_tags
let pp_get_mark_tags state () = state.pp_mark_tags
let pp_set_tags state b =
pp_set_print_tags state b; pp_set_mark_tags state b
let pp_get_formatter_stag_functions state () = {
mark_open_stag = state.pp_mark_open_tag;
mark_close_stag = state.pp_mark_close_tag;
print_open_stag = state.pp_print_open_tag;
print_close_stag = state.pp_print_close_tag;
}
let pp_set_formatter_stag_functions state {
mark_open_stag = mot;
mark_close_stag = mct;
print_open_stag = pot;
print_close_stag = pct;
} =
state.pp_mark_open_tag <- mot;
state.pp_mark_close_tag <- mct;
state.pp_print_open_tag <- pot;
state.pp_print_close_tag <- pct
let pp_rinit state =
pp_clear_queue state;
initialize_scan_stack state.pp_scan_stack;
Stack.clear state.pp_format_stack;
Stack.clear state.pp_tbox_stack;
Stack.clear state.pp_tag_stack;
Stack.clear state.pp_mark_stack;
state.pp_current_indent <- 0;
state.pp_curr_depth <- 0;
state.pp_space_left <- state.pp_margin;
pp_open_sys_box state
let clear_tag_stack state =
Stack.iter (fun _ -> pp_close_stag state ()) state.pp_tag_stack
let pp_flush_queue state ~end_with_newline =
clear_tag_stack state;
while state.pp_curr_depth > 1 do
pp_close_box state ()
done;
state.pp_right_total <- pp_infinity;
advance_left state;
if end_with_newline then pp_output_newline state;
pp_rinit state
let pp_print_as_size state size s =
if state.pp_curr_depth < state.pp_max_boxes
then enqueue_string_as state size s
let pp_print_as state isize s =
pp_print_as_size state (Size.of_int isize) s
let pp_print_string state s =
pp_print_as state (String.length s) s
let pp_print_substring_as ~pos ~len state size s =
if state.pp_curr_depth < state.pp_max_boxes
then enqueue_substring_as ~pos ~len state (Size.of_int size) s
let pp_print_substring ~pos ~len state s =
pp_print_substring_as ~pos ~len state len s
let pp_print_bytes state s =
pp_print_as state (Bytes.length s) (Bytes.to_string s)
let pp_print_int state i = pp_print_string state (Int.to_string i)
let pp_print_float state f = pp_print_string state (string_of_float f)
let pp_print_bool state b = pp_print_string state (string_of_bool b)
let pp_print_char state c =
pp_print_as state 1 (String.make 1 c)
let pp_print_nothing _state () = ()
let pp_open_hbox state () = pp_open_box_gen state 0 Pp_hbox
and pp_open_vbox state indent = pp_open_box_gen state indent Pp_vbox
and pp_open_hvbox state indent = pp_open_box_gen state indent Pp_hvbox
and pp_open_hovbox state indent = pp_open_box_gen state indent Pp_hovbox
and pp_open_box state indent = pp_open_box_gen state indent Pp_box
let pp_print_newline state () =
pp_flush_queue state ~end_with_newline:true; state.pp_out_flush ()
and pp_print_flush state () =
pp_flush_queue state ~end_with_newline:false; state.pp_out_flush ()
let pp_force_newline state () =
if state.pp_curr_depth < state.pp_max_boxes then
enqueue_advance state { size = Size.zero; token = Pp_newline; length = 0 }
let pp_print_if_newline state () =
if state.pp_curr_depth < state.pp_max_boxes then
enqueue_advance state
{ size = Size.zero; token = Pp_if_newline; length = 0 }
let pp_print_custom_break state ~fits ~breaks =
let before, width, after = fits in
if state.pp_curr_depth < state.pp_max_boxes then
let size = Size.of_int (- state.pp_right_total) in
let token = Pp_break { fits; breaks } in
let length = String.length before + width + String.length after in
let elem = { size; token; length } in
scan_push state true elem
let pp_print_break state width offset =
pp_print_custom_break state
~fits:("", width, "") ~breaks:("", offset, "")
let pp_print_space state () = pp_print_break state 1 0
and pp_print_cut state () = pp_print_break state 0 0
let pp_open_tbox state () =
state.pp_curr_depth <- state.pp_curr_depth + 1;
if state.pp_curr_depth < state.pp_max_boxes then
let size = Size.zero in
let elem = { size; token = Pp_tbegin (Pp_tbox (ref [])); length = 0 } in
enqueue_advance state elem
let pp_close_tbox state () =
if state.pp_curr_depth > 1 then
begin
if state.pp_curr_depth < state.pp_max_boxes then
let elem = { size = Size.zero; token = Pp_tend; length = 0 } in
enqueue_advance state elem;
state.pp_curr_depth <- state.pp_curr_depth - 1
end
let pp_print_tbreak state width offset =
if state.pp_curr_depth < state.pp_max_boxes then
let size = Size.of_int (- state.pp_right_total) in
let elem = { size; token = Pp_tbreak (width, offset); length = width } in
scan_push state true elem
let pp_print_tab state () = pp_print_tbreak state 0 0
let pp_set_tab state () =
if state.pp_curr_depth < state.pp_max_boxes then
let elem = { size = Size.zero; token = Pp_stab; length = 0 } in
enqueue_advance state elem
let pp_set_max_boxes state n = if n > 1 then state.pp_max_boxes <- n
let pp_get_max_boxes state () = state.pp_max_boxes
let pp_over_max_boxes state () = state.pp_curr_depth = state.pp_max_boxes
let pp_set_ellipsis_text state s = state.pp_ellipsis <- s
and pp_get_ellipsis_text state () = state.pp_ellipsis
let pp_limit n =
if n < pp_infinity then n else pred pp_infinity
let pp_set_min_space_left state n =
if n >= 1 then
let n = pp_limit n in
state.pp_min_space_left <- n;
state.pp_max_indent <- state.pp_margin - state.pp_min_space_left;
pp_rinit state
let pp_set_max_indent state n =
if n > 1 then
pp_set_min_space_left state (state.pp_margin - n)
let pp_get_max_indent state () = state.pp_max_indent
let pp_set_margin state n =
if n >= 1 then
let n = pp_limit n in
state.pp_margin <- n;
let new_max_indent =
if state.pp_max_indent <= state.pp_margin
then state.pp_max_indent else
Int.max (Int.max (state.pp_margin - state.pp_min_space_left)
(state.pp_margin / 2)) 1 in
pp_set_max_indent state new_max_indent
(** Geometry functions and types *)
type geometry = { max_indent:int; margin: int}
let validate_geometry {margin; max_indent} =
if max_indent < 2 then
Error "max_indent < 2"
else if margin <= max_indent then
Error "margin <= max_indent"
else if margin >= pp_infinity then
Error "margin >= pp_infinity"
else Ok ()
let check_geometry geometry =
match validate_geometry geometry with
| Ok () -> true
| Error _ -> false
let pp_get_margin state () = state.pp_margin
let pp_set_full_geometry state {margin; max_indent} =
pp_set_margin state margin;
pp_set_max_indent state max_indent;
()
let pp_set_geometry state ~max_indent ~margin =
let geometry = { max_indent; margin } in
match validate_geometry geometry with
| Error msg ->
raise (Invalid_argument ("Format.pp_set_geometry: " ^ msg))
| Ok () ->
pp_set_full_geometry state geometry
let pp_safe_set_geometry state ~max_indent ~margin =
let geometry = { max_indent; margin } in
match validate_geometry geometry with
| Error _msg ->
()
| Ok () ->
pp_set_full_geometry state geometry
let pp_get_geometry state () =
{ margin = pp_get_margin state (); max_indent = pp_get_max_indent state () }
let pp_update_geometry state update =
let geometry = pp_get_geometry state () in
pp_set_full_geometry state (update geometry)
let pp_set_formatter_out_functions state {
out_string = f;
out_flush = g;
out_newline = h;
out_spaces = i;
out_indent = j;
} =
state.pp_out_string <- f;
state.pp_out_flush <- g;
state.pp_out_newline <- h;
state.pp_out_spaces <- i;
state.pp_out_indent <- j
let pp_get_formatter_out_functions state () = {
out_string = state.pp_out_string;
out_flush = state.pp_out_flush;
out_newline = state.pp_out_newline;
out_spaces = state.pp_out_spaces;
out_indent = state.pp_out_indent;
}
let pp_set_formatter_output_functions state f g =
state.pp_out_string <- f; state.pp_out_flush <- g
let pp_get_formatter_output_functions state () =
(state.pp_out_string, state.pp_out_flush)
let display_newline state () = state.pp_out_string "\n" 0 1
let blank_line = String.make 80 ' '
let rec display_blanks state n =
if n > 0 then
if n <= 80 then state.pp_out_string blank_line 0 n else
begin
state.pp_out_string blank_line 0 80;
display_blanks state (n - 80)
end
let display_indent = display_blanks
let pp_set_formatter_out_channel state oc =
state.pp_out_string <- output_substring oc;
state.pp_out_flush <- (fun () -> flush oc);
state.pp_out_newline <- display_newline state;
state.pp_out_spaces <- display_blanks state;
state.pp_out_indent <- display_indent state
let default_pp_mark_open_tag = function
| String_tag s -> "<" ^ s ^ ">"
| _ -> ""
let default_pp_mark_close_tag = function
| String_tag s -> "</" ^ s ^ ">"
| _ -> ""
let default_pp_print_open_tag = ignore
let default_pp_print_close_tag = ignore
let pp_make_formatter f g h i j =
let pp_queue = Queue.create () in
let sys_tok =
{ size = Size.unknown; token = Pp_begin (0, Pp_hovbox); length = 0 } in
Queue.add sys_tok pp_queue;
let scan_stack = Stack.create () in
initialize_scan_stack scan_stack;
Stack.push { left_total = 1; queue_elem = sys_tok } scan_stack;
let pp_margin = 78
and pp_min_space_left = 10 in
{
pp_scan_stack = scan_stack;
pp_format_stack = Stack.create ();
pp_tbox_stack = Stack.create ();
pp_tag_stack = Stack.create ();
pp_mark_stack = Stack.create ();
pp_margin = pp_margin;
pp_min_space_left = pp_min_space_left;
pp_max_indent = pp_margin - pp_min_space_left;
pp_space_left = pp_margin;
pp_current_indent = 0;
pp_is_new_line = true;
pp_left_total = 1;
pp_right_total = 1;
pp_curr_depth = 1;
pp_max_boxes = max_int;
pp_ellipsis = ".";
pp_out_string = f;
pp_out_flush = g;
pp_out_newline = h;
pp_out_spaces = i;
pp_out_indent = j;
pp_print_tags = false;
pp_mark_tags = false;
pp_mark_open_tag = default_pp_mark_open_tag;
pp_mark_close_tag = default_pp_mark_close_tag;
pp_print_open_tag = default_pp_print_open_tag;
pp_print_close_tag = default_pp_print_close_tag;
pp_queue = pp_queue;
}
let formatter_of_out_functions out_funs =
pp_make_formatter
out_funs.out_string
out_funs.out_flush
out_funs.out_newline
out_funs.out_spaces
out_funs.out_indent
let make_formatter output flush =
let ppf = pp_make_formatter output flush ignore ignore ignore in
ppf.pp_out_newline <- display_newline ppf;
ppf.pp_out_spaces <- display_blanks ppf;
ppf.pp_out_indent <- display_indent ppf;
ppf
let formatter_of_out_channel oc =
make_formatter (output_substring oc) (fun () -> flush oc)
let formatter_of_buffer b =
make_formatter (Buffer.add_substring b) ignore
let pp_buffer_size = 512
let pp_make_buffer () = Buffer.create pp_buffer_size
let stdbuf = pp_make_buffer ()
let std_formatter = formatter_of_out_channel Stdlib.stdout
and err_formatter = formatter_of_out_channel Stdlib.stderr
and str_formatter = formatter_of_buffer stdbuf
module DLS = Domain.DLS
let stdbuf_key = DLS.new_key pp_make_buffer
let _ = DLS.set stdbuf_key stdbuf
let str_formatter_key = DLS.new_key (fun () ->
formatter_of_buffer (DLS.get stdbuf_key))
let _ = DLS.set str_formatter_key str_formatter
let buffered_out_string key str ofs len =
Buffer.add_substring (Domain.DLS.get key) str ofs len
let buffered_out_flush oc key () =
let buf = Domain.DLS.get key in
let len = Buffer.length buf in
let str = Buffer.contents buf in
output_substring oc str 0 len ;
Stdlib.flush oc;
Buffer.clear buf
let std_buf_key = Domain.DLS.new_key (fun () -> Buffer.create pp_buffer_size)
let err_buf_key = Domain.DLS.new_key (fun () -> Buffer.create pp_buffer_size)
let std_formatter_key = DLS.new_key (fun () ->
let ppf =
pp_make_formatter (buffered_out_string std_buf_key)
(buffered_out_flush Stdlib.stdout std_buf_key) ignore ignore ignore
in
ppf.pp_out_newline <- display_newline ppf;
ppf.pp_out_spaces <- display_blanks ppf;
ppf.pp_out_indent <- display_indent ppf;
Domain.at_exit (pp_print_flush ppf);
ppf)
let _ = DLS.set std_formatter_key std_formatter
let err_formatter_key = DLS.new_key (fun () ->
let ppf =
pp_make_formatter (buffered_out_string err_buf_key)
(buffered_out_flush Stdlib.stderr err_buf_key) ignore ignore ignore
in
ppf.pp_out_newline <- display_newline ppf;
ppf.pp_out_spaces <- display_blanks ppf;
ppf.pp_out_indent <- display_indent ppf;
Domain.at_exit (pp_print_flush ppf);
ppf)
let _ = DLS.set err_formatter_key err_formatter
let get_std_formatter () = DLS.get std_formatter_key
let get_err_formatter () = DLS.get err_formatter_key
let get_str_formatter () = DLS.get str_formatter_key
let get_stdbuf () = DLS.get stdbuf_key
let flush_buffer_formatter buf ppf =
pp_flush_queue ppf ~end_with_newline:false;
let s = Buffer.contents buf in
Buffer.reset buf;
s
let flush_str_formatter () =
let stdbuf = DLS.get stdbuf_key in
let str_formatter = DLS.get str_formatter_key in
flush_buffer_formatter stdbuf str_formatter
let make_synchronized_formatter output flush =
DLS.new_key (fun () ->
let buf = Buffer.create pp_buffer_size in
let output' = Buffer.add_substring buf in
let flush' () =
output (Buffer.contents buf) 0 (Buffer.length buf);
Buffer.clear buf;
flush ()
in
make_formatter output' flush')
let synchronized_formatter_of_out_channel oc =
make_synchronized_formatter (output_substring oc) (fun () -> flush oc)
type symbolic_output_item =
| Output_flush
| Output_newline
| Output_string of string
| Output_spaces of int
| Output_indent of int
type symbolic_output_buffer = {
mutable symbolic_output_contents : symbolic_output_item list;
}
let make_symbolic_output_buffer () =
{ symbolic_output_contents = [] }
let clear_symbolic_output_buffer sob =
sob.symbolic_output_contents <- []
let get_symbolic_output_buffer sob =
List.rev sob.symbolic_output_contents
let flush_symbolic_output_buffer sob =
let items = get_symbolic_output_buffer sob in
clear_symbolic_output_buffer sob;
items
let add_symbolic_output_item sob item =
sob.symbolic_output_contents <- item :: sob.symbolic_output_contents
let formatter_of_symbolic_output_buffer sob =
let symbolic_flush sob () =
add_symbolic_output_item sob Output_flush
and symbolic_newline sob () =
add_symbolic_output_item sob Output_newline
and symbolic_string sob s i n =
add_symbolic_output_item sob (Output_string (String.sub s i n))
and symbolic_spaces sob n =
add_symbolic_output_item sob (Output_spaces n)
and symbolic_indent sob n =
add_symbolic_output_item sob (Output_indent n) in
let f = symbolic_string sob
and g = symbolic_flush sob
and h = symbolic_newline sob
and i = symbolic_spaces sob
and j = symbolic_indent sob in
pp_make_formatter f g h i j
let open_hbox v = pp_open_hbox (DLS.get std_formatter_key) v
and open_vbox v = pp_open_vbox (DLS.get std_formatter_key) v
and open_hvbox v = pp_open_hvbox (DLS.get std_formatter_key) v
and open_hovbox v = pp_open_hovbox (DLS.get std_formatter_key) v
and open_box v = pp_open_box (DLS.get std_formatter_key) v
and close_box v = pp_close_box (DLS.get std_formatter_key) v
and open_stag v = pp_open_stag (DLS.get std_formatter_key) v
and close_stag v = pp_close_stag (DLS.get std_formatter_key) v
and print_as v w = pp_print_as (DLS.get std_formatter_key) v w
and print_string v = pp_print_string (DLS.get std_formatter_key) v
and print_substring ~pos ~len v =
pp_print_substring ~pos ~len (DLS.get std_formatter_key) v
and print_substring_as ~pos ~len as_len v =
pp_print_substring_as ~pos ~len (DLS.get std_formatter_key) as_len v
and print_bytes v = pp_print_bytes (DLS.get std_formatter_key) v
and print_int v = pp_print_int (DLS.get std_formatter_key) v
and print_float v = pp_print_float (DLS.get std_formatter_key) v
and print_char v = pp_print_char (DLS.get std_formatter_key) v
and print_bool v = pp_print_bool (DLS.get std_formatter_key) v
and print_break v w = pp_print_break (DLS.get std_formatter_key) v w
and print_cut v = pp_print_cut (DLS.get std_formatter_key) v
and print_space v = pp_print_space (DLS.get std_formatter_key) v
and force_newline v = pp_force_newline (DLS.get std_formatter_key) v
and print_flush v = pp_print_flush (DLS.get std_formatter_key) v
and print_newline v = pp_print_newline (DLS.get std_formatter_key) v
and print_if_newline v = pp_print_if_newline (DLS.get std_formatter_key) v
and open_tbox v = pp_open_tbox (DLS.get std_formatter_key) v
and close_tbox v = pp_close_tbox (DLS.get std_formatter_key) v
and print_tbreak v w = pp_print_tbreak (DLS.get std_formatter_key) v w
and set_tab v = pp_set_tab (DLS.get std_formatter_key) v
and print_tab v = pp_print_tab (DLS.get std_formatter_key) v
and set_margin v = pp_set_margin (DLS.get std_formatter_key) v
and get_margin v = pp_get_margin (DLS.get std_formatter_key) v
and set_max_indent v = pp_set_max_indent (DLS.get std_formatter_key) v
and get_max_indent v = pp_get_max_indent (DLS.get std_formatter_key) v
and set_geometry ~max_indent ~margin =
pp_set_geometry (DLS.get std_formatter_key) ~max_indent ~margin
and safe_set_geometry ~max_indent ~margin =
pp_safe_set_geometry (DLS.get std_formatter_key) ~max_indent ~margin
and get_geometry v = pp_get_geometry (DLS.get std_formatter_key) v
and update_geometry v = pp_update_geometry (DLS.get std_formatter_key) v
and set_max_boxes v = pp_set_max_boxes (DLS.get std_formatter_key) v
and get_max_boxes v = pp_get_max_boxes (DLS.get std_formatter_key) v
and over_max_boxes v = pp_over_max_boxes (DLS.get std_formatter_key) v
and set_ellipsis_text v = pp_set_ellipsis_text (DLS.get std_formatter_key) v
and get_ellipsis_text v = pp_get_ellipsis_text (DLS.get std_formatter_key) v
and set_formatter_out_channel v =
pp_set_formatter_out_channel (DLS.get std_formatter_key) v
and set_formatter_out_functions v =
pp_set_formatter_out_functions (DLS.get std_formatter_key) v
and get_formatter_out_functions v =
pp_get_formatter_out_functions (DLS.get std_formatter_key) v
and set_formatter_output_functions v w =
pp_set_formatter_output_functions (DLS.get std_formatter_key) v w
and get_formatter_output_functions v =
pp_get_formatter_output_functions (DLS.get std_formatter_key) v
and set_formatter_stag_functions v =
pp_set_formatter_stag_functions (DLS.get std_formatter_key) v
and get_formatter_stag_functions v =
pp_get_formatter_stag_functions (DLS.get std_formatter_key) v
and set_print_tags v =
pp_set_print_tags (DLS.get std_formatter_key) v
and get_print_tags v =
pp_get_print_tags (DLS.get std_formatter_key) v
and set_mark_tags v =
pp_set_mark_tags (DLS.get std_formatter_key) v
and get_mark_tags v =
pp_get_mark_tags (DLS.get std_formatter_key) v
and set_tags v =
pp_set_tags (DLS.get std_formatter_key) v
let pp_print_iter ?(pp_sep = pp_print_cut) iter pp_v ppf v =
let is_first = ref true in
let pp_v v =
if !is_first then is_first := false else pp_sep ppf ();
pp_v ppf v
in
iter pp_v v
let pp_print_list ?(pp_sep = pp_print_cut) pp_v ppf v =
pp_print_iter ~pp_sep List.iter pp_v ppf v
let pp_print_array ?(pp_sep = pp_print_cut) pp_v ppf v =
pp_print_iter ~pp_sep Array.iter pp_v ppf v
let pp_print_seq ?(pp_sep = pp_print_cut) pp_v ppf seq =
pp_print_iter ~pp_sep Seq.iter pp_v ppf seq
let pp_print_text ppf s =
let len = String.length s in
let left = ref 0 in
let right = ref 0 in
let flush () =
pp_print_substring ppf s ~pos:!left ~len:(!right - !left);
incr right; left := !right;
in
while (!right <> len) do
match s.[!right] with
| '\n' ->
flush ();
pp_force_newline ppf ()
| ' ' ->
flush (); pp_print_space ppf ()
| _ -> incr right
done;
if !left <> len then flush ()
let pp_print_option ?(none = fun _ () -> ()) pp_v ppf = function
| None -> none ppf ()
| Some v -> pp_v ppf v
let pp_print_result ~ok ~error ppf = function
| Ok v -> ok ppf v
| Error e -> error ppf e
let pp_print_either ~left ~right ppf = function
| Either.Left l -> left ppf l
| Either.Right r -> right ppf r
let compute_tag output tag_acc =
let buf = Buffer.create 16 in
let ppf = formatter_of_buffer buf in
output ppf tag_acc;
pp_print_flush ppf ();
let len = Buffer.length buf in
if len < 2 then Buffer.contents buf
else Buffer.sub buf 1 (len - 2)
open CamlinternalFormatBasics
open CamlinternalFormat
let output_formatting_lit ppf fmting_lit = match fmting_lit with
| Close_box -> pp_close_box ppf ()
| Close_tag -> pp_close_stag ppf ()
| Break (_, width, offset) -> pp_print_break ppf width offset
| FFlush -> pp_print_flush ppf ()
| Force_newline -> pp_force_newline ppf ()
| Flush_newline -> pp_print_newline ppf ()
| Magic_size (_, _) -> ()
| Escaped_at -> pp_print_char ppf '@'
| Escaped_percent -> pp_print_char ppf '%'
| Scan_indic c -> pp_print_char ppf '@'; pp_print_char ppf c
let rec output_acc ppf acc = match acc with
| Acc_string_literal (Acc_formatting_lit (p, Magic_size (_, size)), s)
| Acc_data_string (Acc_formatting_lit (p, Magic_size (_, size)), s) ->
output_acc ppf p;
pp_print_as_size ppf (Size.of_int size) s;
| Acc_char_literal (Acc_formatting_lit (p, Magic_size (_, size)), c)
| Acc_data_char (Acc_formatting_lit (p, Magic_size (_, size)), c) ->
output_acc ppf p;
pp_print_as_size ppf (Size.of_int size) (String.make 1 c);
| Acc_formatting_lit (p, f) ->
output_acc ppf p;
output_formatting_lit ppf f;
| Acc_formatting_gen (p, Acc_open_tag acc') ->
output_acc ppf p;
pp_open_stag ppf (String_tag (compute_tag output_acc acc'))
| Acc_formatting_gen (p, Acc_open_box acc') ->
output_acc ppf p;
let (indent, bty) = open_box_of_string (compute_tag output_acc acc') in
pp_open_box_gen ppf indent bty
| Acc_string_literal (p, s)
| Acc_data_string (p, s) -> output_acc ppf p; pp_print_string ppf s;
| Acc_char_literal (p, c)
| Acc_data_char (p, c) -> output_acc ppf p; pp_print_char ppf c;
| Acc_delay (p, f) -> output_acc ppf p; f ppf;
| Acc_flush p -> output_acc ppf p; pp_print_flush ppf ();
| Acc_invalid_arg (p, msg) -> output_acc ppf p; invalid_arg msg;
| End_of_acc -> ()
let rec strput_acc ppf acc = match acc with
| Acc_string_literal (Acc_formatting_lit (p, Magic_size (_, size)), s)
| Acc_data_string (Acc_formatting_lit (p, Magic_size (_, size)), s) ->
strput_acc ppf p;
pp_print_as_size ppf (Size.of_int size) s;
| Acc_char_literal (Acc_formatting_lit (p, Magic_size (_, size)), c)
| Acc_data_char (Acc_formatting_lit (p, Magic_size (_, size)), c) ->
strput_acc ppf p;
pp_print_as_size ppf (Size.of_int size) (String.make 1 c);
| Acc_delay (Acc_formatting_lit (p, Magic_size (_, size)), f) ->
strput_acc ppf p;
pp_print_as_size ppf (Size.of_int size) (f ());
| Acc_formatting_lit (p, f) ->
strput_acc ppf p;
output_formatting_lit ppf f;
| Acc_formatting_gen (p, Acc_open_tag acc') ->
strput_acc ppf p;
pp_open_stag ppf (String_tag (compute_tag strput_acc acc'))
| Acc_formatting_gen (p, Acc_open_box acc') ->
strput_acc ppf p;
let (indent, bty) = open_box_of_string (compute_tag strput_acc acc') in
pp_open_box_gen ppf indent bty
| Acc_string_literal (p, s)
| Acc_data_string (p, s) -> strput_acc ppf p; pp_print_string ppf s;
| Acc_char_literal (p, c)
| Acc_data_char (p, c) -> strput_acc ppf p; pp_print_char ppf c;
| Acc_delay (p, f) -> strput_acc ppf p; pp_print_string ppf (f ());
| Acc_flush p -> strput_acc ppf p; pp_print_flush ppf ();
| Acc_invalid_arg (p, msg) -> strput_acc ppf p; invalid_arg msg;
| End_of_acc -> ()
let kfprintf k ppf (Format (fmt, _)) =
make_printf
(fun acc -> output_acc ppf acc; k ppf)
End_of_acc fmt
and ikfprintf k ppf (Format (fmt, _)) =
make_iprintf k ppf fmt
let ifprintf _ppf (Format (fmt, _)) =
make_iprintf ignore () fmt
let fprintf ppf = kfprintf ignore ppf
let printf (Format (fmt, _)) =
make_printf
(fun acc -> output_acc (DLS.get std_formatter_key) acc)
End_of_acc fmt
let eprintf (Format (fmt, _)) =
make_printf
(fun acc -> output_acc (DLS.get err_formatter_key) acc)
End_of_acc fmt
let kdprintf k (Format (fmt, _)) =
make_printf
(fun acc -> k (fun ppf -> output_acc ppf acc))
End_of_acc fmt
let dprintf fmt = kdprintf (fun i -> i) fmt
let ksprintf k (Format (fmt, _)) =
let b = pp_make_buffer () in
let ppf = formatter_of_buffer b in
let k acc =
strput_acc ppf acc;
k (flush_buffer_formatter b ppf) in
make_printf k End_of_acc fmt
let sprintf fmt = ksprintf id fmt
let kasprintf k (Format (fmt, _)) =
let b = pp_make_buffer () in
let ppf = formatter_of_buffer b in
let k acc =
output_acc ppf acc;
k (flush_buffer_formatter b ppf) in
make_printf k End_of_acc fmt
let asprintf fmt = kasprintf id fmt
let flush_standard_formatters () =
pp_print_flush (DLS.get std_formatter_key) ();
pp_print_flush (DLS.get err_formatter_key) ()
let () = at_exit flush_standard_formatters
let () = Domain.before_first_spawn (fun () ->
flush_standard_formatters ();
let fs = pp_get_formatter_out_functions std_formatter () in
pp_set_formatter_out_functions std_formatter
{fs with out_string = buffered_out_string std_buf_key;
out_flush = buffered_out_flush Stdlib.stdout std_buf_key};
let fs = pp_get_formatter_out_functions err_formatter () in
pp_set_formatter_out_functions err_formatter
{fs with out_string = buffered_out_string err_buf_key;
out_flush = buffered_out_flush Stdlib.stderr err_buf_key};
)