Source file printer.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
type break_strength = Cut | Space | Newline | Blank_line
let strength = function Cut -> 0 | Space -> 1 | Newline -> 2 | Blank_line -> 3
module Doc = struct
type gkind =
| GBox
| GHov
| GHv
| GV
| GH
type doc =
| Empty
| Text of int * string
| Brk of break_strength
| Cat of doc * doc
| Nest of int * doc
| Group of gkind * doc
| If_broken of doc
type frame = { wrap : doc -> doc; mutable items : doc list }
type state = {
fmt : Format.formatter;
width : int;
mutable stack : frame list;
mutable has_emitted : bool;
mutable pending_eol : (unit -> unit) option;
mutable holding_eol : bool;
}
let create fmt ~width =
{
fmt;
width;
stack = [ { wrap = (fun d -> d); items = [] } ];
has_emitted = false;
pending_eol = None;
holding_eol = false;
}
let top st = List.hd st.stack
let append st d =
let fr = top st in
match (d, fr.items) with
| Brk s2, Brk s1 :: tl ->
fr.items <- Brk (if strength s2 >= strength s1 then s2 else s1) :: tl
| _ -> fr.items <- d :: fr.items
let push st wrap = st.stack <- { wrap; items = [] } :: st.stack
let pop st =
match st.stack with
| fr :: rest ->
let body = List.fold_left (fun acc d -> Cat (d, acc)) Empty fr.items in
st.stack <- rest;
append st (fr.wrap body)
| [] -> assert false
let drop_trailing_break st =
let fr = top st in
match fr.items with
| Brk s :: tl ->
fr.items <- tl;
Some s
| _ -> None
let force_eol st =
match st.pending_eol with
| None -> ()
| Some emit ->
st.pending_eol <- None;
let dropped = drop_trailing_break st in
emit ();
let next_brk =
match dropped with Some Blank_line -> Blank_line | _ -> Newline
in
append st (Brk next_brk);
st.has_emitted <- false
let defer_eol st emit =
force_eol st;
st.pending_eol <- Some emit
let with_held_eol st f =
let prev = st.holding_eol in
st.holding_eol <- true;
f ();
st.holding_eol <- prev
let has_pending_eol st = st.pending_eol <> None
let text st len s =
if not st.holding_eol then force_eol st;
st.has_emitted <- true;
append st (Text (len, s))
let string st s = text st (String.length s) s
let string_as st len s = text st len s
let space st = if st.has_emitted then append st (Brk Space)
let cut st = append st (Brk Cut)
let newline st = append st (Brk Newline)
let blank_line st = append st (Brk Blank_line)
let indent st n f =
push st (fun d -> Nest (n, d));
f ();
pop st
let if_broken st f =
push st (fun d -> If_broken d);
f ();
pop st
let group_wrap kind indent d =
Group (kind, if indent = 0 then d else Nest (indent, d))
let scoped st kind ~skip_space ~indent f =
if not st.holding_eol then force_eol st;
(if skip_space then
let fr = top st in
match fr.items with Brk (Cut | Space) :: tl -> fr.items <- tl | _ -> ());
push st (group_wrap kind indent);
f ();
pop st
let box st ~skip_space ~indent f = scoped st GBox ~skip_space ~indent f
let hvbox st ~skip_space ~indent f = scoped st GHv ~skip_space ~indent f
let hovbox st ~skip_space ~indent f = scoped st GHov ~skip_space ~indent f
let vbox st ~skip_space ~indent f = scoped st GV ~skip_space ~indent f
let hbox st ~skip_space f = scoped st GH ~skip_space ~indent:0 f
type mode = Flat | Brkm | Fill
let rec fits avail items =
if avail < 0 then false
else
match items with
| [] -> true
| (i, m, d) :: rest -> (
match d with
| Empty -> fits avail rest
| Text (w, _) -> fits (avail - w) rest
| Cat (a, b) -> fits avail ((i, m, a) :: (i, m, b) :: rest)
| Nest (n, d) -> fits avail ((i + n, m, d) :: rest)
| Group (_, d) -> fits avail ((i, Flat, d) :: rest)
| If_broken _ -> fits avail rest
| Brk b -> (
match m with
| Brkm | Fill -> true
| Flat -> (
match b with
| Newline | Blank_line -> false
| Space -> fits (avail - 1) rest
| Cut -> fits avail rest)))
let render ~width doc =
let b = Buffer.create 4096 in
let col = ref 0 in
let emitted = ref false in
let max_indent = max 0 (width - 10) in
let pend_line = ref None in
let pend_flat = ref None in
let flush () =
(match !pend_line with
| Some (ind, blank) ->
if !emitted then (
Buffer.add_char b '\n';
if blank then Buffer.add_char b '\n';
Buffer.add_string b (String.make ind ' ');
col := ind)
| None -> (
match !pend_flat with
| Some s when strength s >= strength Space ->
Buffer.add_char b ' ';
incr col
| _ -> ()));
pend_line := None;
pend_flat := None
in
let break_line ind blank =
let ind = min ind max_indent in
(match !pend_line with
| Some (_, b0) -> pend_line := Some (ind, b0 || blank)
| None -> pend_line := Some (ind, blank));
pend_flat := None
in
let flat_sep s =
if !pend_line = None then
pend_flat :=
Some
(match !pend_flat with
| Some s0 -> if strength s >= strength s0 then s else s0
| None -> s)
in
let eff_col () =
match !pend_line with
| Some (ind, _) -> ind
| None -> !col + if !pend_flat <> None then 1 else 0
in
let rec go = function
| [] -> ()
| (i, m, d) :: rest -> (
match d with
| Empty -> go rest
| Text (w, s) ->
flush ();
Buffer.add_string b s;
emitted := true;
col := !col + w;
go rest
| Cat (a, c) -> go ((i, m, a) :: (i, m, c) :: rest)
| Nest (n, d) -> go ((i + n, m, d) :: rest)
| Group (k, d) ->
let base = eff_col () in
let m' =
match k with
| GV -> Brkm
| GH -> Flat
| GHv ->
if fits (width - base) ((base, Flat, d) :: rest) then Flat
else Brkm
| GBox | GHov -> Fill
in
go ((base, m', d) :: rest)
| If_broken d ->
go (match m with Brkm -> (i, m, d) :: rest | _ -> rest)
| Brk str ->
(match (str, m) with
| Newline, _ -> break_line i false
| Blank_line, _ -> break_line i true
| (Cut | Space), Flat -> flat_sep str
| (Cut | Space), Brkm -> break_line i false
| (Cut | Space), Fill ->
let sep = match str with Space -> 1 | _ -> 0 in
if fits (width - eff_col () - sep) rest then flat_sep str
else break_line i false);
go rest)
in
go [ (0, Brkm, doc) ];
Buffer.contents b
let finish st =
force_eol st;
let body =
List.fold_left (fun acc d -> Cat (d, acc)) Empty (top st).items
in
let s = render ~width:st.width body in
match String.split_on_char '\n' s with
| [] -> ()
| first :: rest ->
Format.pp_print_string st.fmt first;
List.iter
(fun line ->
Format.pp_force_newline st.fmt ();
Format.pp_print_string st.fmt line)
rest
end
type t = Doc.state
let indent = Doc.indent
let string = Doc.string
let string_as = Doc.string_as
let space t () = Doc.space t
let cut t () = Doc.cut t
let newline t () = Doc.newline t
let blank_line t () = Doc.blank_line t
let defer_eol = Doc.defer_eol
let with_held_eol = Doc.with_held_eol
let has_pending_eol = Doc.has_pending_eol
let if_broken = Doc.if_broken
let box t ?(skip_space = false) ?(indent = 0) f =
Doc.box t ~skip_space ~indent f
let hvbox t ?(skip_space = false) ?(indent = 0) f =
Doc.hvbox t ~skip_space ~indent f
let hbox t ?(skip_space = false) f = Doc.hbox t ~skip_space f
let hovbox t ?(skip_space = false) ?(indent = 0) f =
Doc.hovbox t ~skip_space ~indent f
let vbox t ?(skip_space = false) ?(indent = 0) f =
Doc.vbox t ~skip_space ~indent f
let run ?(width = 78) fmt f =
let c = Doc.create fmt ~width in
f c;
Doc.finish c