-
Notifications
You must be signed in to change notification settings - Fork 2
/
fpretty.ml
319 lines (301 loc) · 8.01 KB
/
fpretty.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
module List = struct
include List
let zip xs ys =
let rec go acc = function
| [], _ | _, [] -> List.rev acc
| x::xs, y::ys -> go ((x,y)::acc) (xs,ys)
in
go [] (xs,ys)
let rec take n xs =
match n, xs with
| 0, _ | _, [] -> []
| n, x::xs -> x :: take (n-1) xs
let rec drop n xs =
match n, xs with
| 0, _ | _, [] -> xs
| n, _::xs -> drop (n-1) xs
let rec last = function
| [] -> failwith "last: empty list"
| [x] -> x
| x::xs -> last xs
let split_at k xs =
let rec go l k = function
| xs when k = 0 ->
List.rev l, xs
| [] ->
List.rev l, []
| x::xs ->
go (x::l) (k-1) xs
in
go [] k xs
let iota n =
let rec go acc n =
if n = 0 then
acc
else
go ((n-1)::acc) (n-1)
in
go [] n
let map_accuml_rev f acc xs =
let acc, ys = List.fold_left (fun (acc,ys) x ->
let acc, y = f acc x in
acc, y::ys
) (acc,[]) xs
in
acc, ys
let map_accuml f acc xs =
let acc, ys = map_accuml_rev f acc xs in
acc, List.rev ys
end
(* deque *)
type 'a deque = 'a list * 'a list
let emptyq = [], []
let pushl x (xs,ys) = x::xs, ys
let pushr x (xs,ys) = xs, x::ys
let viewl = function
| [], [] ->
None
| [], ys ->
let n = List.length ys in
let ys, xs = List.split_at (n/2) ys in
(match List.rev xs with
| x::xs ->
Some (x, (xs,ys))
| _ -> assert false
)
| x::xs, ys ->
Some (x, (xs,ys))
let viewr = function
| [], [] ->
None
| xs, [] ->
let n = List.length xs in
let xs, ys = List.split_at (n/2) xs in
(match List.rev ys with
| y::ys ->
Some (y, (xs,ys))
| _ -> assert false
)
| xs, y::ys ->
Some (y, (xs,ys))
type doc =
| Nil
| Line of int
| Char of char
| Text of string
| Cat of doc * doc
| Group of doc
| Nest of int * doc
| Align of int * doc
let empty = Nil
let line = Line 1
let linebreak = Line 0
let char c = Char c
let text t = Text t
let (<.>) x y = Cat (x, y)
let group x = Group x
let nest i x = Nest (i, x)
let align x = Align (0, x)
let space = Char ' '
let softline = group line
let softbreak = group linebreak
let lbrace = Char '{'
let rbrace = Char '}'
let lbracket = Char '['
let rbracket = Char ']'
let langle = Char '<'
let rangle = Char '>'
let lparen = Char '('
let rparen = Char ')'
let semicolon = Char ';'
let comma = Char ','
let int i = Text (string_of_int i)
let (<+>) x y = x <.> text " " <.> y
let (<$>) x y = x <.> line <.> y
let (<$$>) x y = x <.> linebreak <.> y
let (</>) x y = x <.> softline <.> y
let (<//>) x y = x <.> softbreak <.> y
let enclose l r x = l <.> x <.> r
let fill_sep = List.fold_left (</>) empty
let rec sep_by sep = function
| [] -> empty
| x::xs ->
let rec go acc = function
| [] -> acc
| x::xs -> go (acc <.> sep <.> x) xs
in
align (go x xs)
let rec enclose_sep l r sep = function
| [] -> l <.> r
| x::xs ->
let rec go acc = function
| [] -> acc
| x::xs -> go (acc <.> sep <.> x) xs
in
align (go (l <.> x) xs <.> r) <.> Align (-10, empty)
let rec enclose_sep_a l r sep a =
let n = Array.length a in
if n = 0 then
l <.> r
else
let rec go acc i =
if i = n then
acc
else
go (acc <.> sep <.> a.(i)) (i+1)
in
align (go (l <.> a.(0)) 1 <.> r)
let rec len = function
| Nil -> 0
| Char _ -> 1
| Text t -> String.length t
| Cat (x, y) -> len x + len y
| _ -> assert false
let normalize x =
(* ensure y and (fst result) only built from text,nil and Cat *)
let rec go x y =
match x with
| Nil -> y, Nil
| Line _ -> Nil, x <.> y
| Char _ | Text _ -> x <.> y, Nil
| Cat (u,v) ->
let l2,r2 = go v y in
let l1,r1 = go u l2 in
l1, r1 <.> r2
| Group u ->
let l,r = go u y in
l, Group r
| Nest (i,u) ->
let l,r = go u y in
l, Nest (i, r)
| Align (i,u) ->
let l,r = go u y in
l, Align (i-len l, r)
in
let l, r = go x Nil in
l <.> r
type pos = int
type remaining = int
type indentation = int list
type horizontal = bool
type out = remaining -> indentation -> unit
type outg = horizontal -> out -> out
type dq = (pos*outg) deque
type tree_cont = pos -> dq -> out
let render w x =
let buf = Buffer.create 0 in
let rec interpret doc (w:int) (tc:tree_cont) (p:pos) (dq:dq) : out =
let rec prune (tc:tree_cont) (p:pos) (dq:dq) (r:remaining) =
match viewl dq with
| None -> tc p dq r
| Some ((s,outg),dq') ->
if s+r < p then
outg false (prune tc p dq') r
else
tc p dq r
in
let extend cont1 cont2 outg (tc:tree_cont) (p:pos) (dq:dq) =
(match viewr dq with
| None ->
outg false (cont1 tc p dq)
| Some ((s,outg2),dq') ->
let outg' h c r = outg2 h (outg h c) r in
cont2 tc p (pushr (s, outg') dq')
)
in
let leave (tc:tree_cont) (p:pos) (dq:dq) =
match viewr dq with
| None ->
tc p dq
| Some ((s1,outg1),dq1) ->
(match viewr dq1 with
| None ->
outg1 true (tc p emptyq)
| Some ((s2,outg2),dq2) ->
let outg' h c = outg2 h (fun r1 -> outg1 (p <= s2+r1) c r1) in
tc p (pushr (s2,outg') dq2)
)
in
let id x = x in
match doc with
| Nil ->
tc p dq
| Line l ->
let outg h c r i =
if h then (
Buffer.add_string buf @@ String.make l ' ';
c (r-l) i
) else (
Buffer.add_char buf '\n';
Buffer.add_string buf @@ String.make (List.hd i) ' ';
c (w-List.hd i) i
)
in
extend id prune outg tc (p+l) dq
| Char ch ->
let outg h c r i =
Buffer.add_char buf ch;
c (r-1) i
in
extend id prune outg tc (p+1) dq
| Text t ->
let l = String.length t in
let outg h c r i =
Buffer.add_string buf t;
c (r-l) i
in
extend id prune outg tc (p+l) dq
| Cat (x,y) ->
interpret x w (interpret y w tc) p dq
| Group x ->
interpret x w (leave tc) p (pushr (p, fun h c -> c) dq)
| Nest (j,x) ->
let outg h c r i = c r ((List.hd i+j)::i) in
let undo h c r i = c r (List.tl i) in
let f tc = interpret x w (extend id id undo tc) in
extend f f outg tc p dq
| Align (j,x) ->
let outg h c r i = c r ((w-r+j)::i) in
let undo h c r i = c r (List.tl i) in
let f tc = interpret x w (extend id id undo tc) in
extend f f outg tc p dq
in
interpret (normalize x) w (fun p dq r i -> ()) 0 emptyq w [0];
Buffer.contents buf
let pretty = render
let take n s = String.sub s 0 n
let prop0 = pretty 6 (group (text "Hi" <.> line <.> text "you") <.> text "!") =
"Hi\nyou!"
let prop1 = pretty 4 (group (text "hi" <.> line <.> text "world")) =
"hi\nworld"
let prop2 =
pretty 8 (group (text "hi" <.> line <.> text "world") <.> text "liness") =
"hi\nworldliness"
let prop3 =
take 6 (pretty 4 (group (text "hi" <.> line <.> text "you" <.> empty))) =
"hi\nyou"
let prop4 =
take 6 (pretty 4 (group (text "hi" <.> line) <.>
group (text "you" <.> line) <.> empty)) =
"hi\nyou"
let prop5 =
take 6 (pretty 4 (group (text "hi" <.>
group (line <.> text "you" <.> empty)))) =
"hi\nyou"
let prop6 =
take 7 (pretty 3 (group (text "hi" <.> line <.>
group (line <.> text "you" <.> empty)))) =
"hi\n\nyou"
let prop7 =
pretty 10 (group (text "what" <.>
align (group (text "do" <.> line <.> text "you" <.> line <.>
text "do" <.> align (line <.> text "now?"))))) =
"whatdo\n you\n do\n now?"
let prop8 =
pretty 10 (group (text "one " <.> (align (line <.> text "two" <.>
align (line <.> text "three"))))) =
"one \n two\n three"
let prop9 =
pretty 10 (group (text "one " <.> (nest 2 (line <.> text "two" <.>
nest 3 (line <.> text "three"))))) =
"one \n two\n three"