-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpretty.ralph
320 lines (242 loc) · 10.2 KB
/
pretty.ralph
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
(define-module ralph/pretty
import: (ralph/stream
ralph/format))
;;;; an implementation of Christian Lindig's "Strictly Pretty",
;;;; a strict variant of Philip Wadler's "A prettier printer"
;;;; document
(define-class <document> (<object>))
;; empty
(define-class <empty-document> (<document>))
(define $empty-document
(make <empty-document>))
(define-method description ((document <empty-document>))
"#{empty-document}")
(define-function empty-document? (document)
(instance? document <empty-document>))
;; combination
(define-class <combined-document> (<document>)
left
right)
(define-method description ((document <combined-document>))
(bind-properties (left right) document
(format-to-string "#{combined-document left=%= right=%=}"
left right)))
(define-function combine-documents (#rest documents)
(select (size documents) ==
((0) $empty-document)
((1) (first documents))
((2) (make <combined-document>
left: (first documents)
right: (second documents)))
(else:
(reduce1 combine-documents documents
from-end?: #t))))
;; text
(define-class <text-document> (<document>)
text)
(define-method description ((document <text-document>))
(format-to-string "#{text-document \"%s\"}"
(get document "text")))
(define-function as-text-document (text)
(make <text-document> text: text))
;; nesting
(define-class <nested-document> (<document>)
level
document)
(define-method description ((nested-document <nested-document>))
(bind-properties (level document) nested-document
(format-to-string "#{nested-document level=%= document=%=}"
level document)))
(define-function nest-document (level document)
(make <nested-document>
level: level
document: document))
;; break
(define-class <break-document> (<document>)
text)
(define-method description ((document <break-document>))
(format-to-string "#{break-document text=\"%s\"}"
(get document "text")))
(define-function break-with (text)
(make <break-document> text: text))
(define $break (break-with " "))
;; group
(define-class <group-document> (<document>)
document)
(define-method description ((document <group-document>))
(format-to-string "#{group-document document=%=}"
(get document "document")))
(define-function group-document (document)
(make <group-document> document: document))
;; connect
;; connects two documents with an optional line break
(define-function connect-documents (left right #key (break $break))
(cond ((empty-document? left) right)
((empty-document? right) left)
(else: (combine-documents left break right))))
;;; other
(define-function binary-document (nesting left operand right)
(group-document
(nest-document nesting
(connect-documents
(group-document (connect-documents left operand))
right))))
;;;; simple document
(define-class <simple-document> (<object>))
;; empty
(define-class <empty-simple-document> (<simple-document>))
(define $empty-simple-document
(make <empty-simple-document>))
(define-method description ((document <empty-simple-document>))
"#{empty-simple-document}")
;; noop
(define-method render-simple-document ((document <empty-simple-document>) stream))
;; text
(define-class <text-simple-document> (<simple-document>)
text
next-document)
(define-method description ((document <text-simple-document>))
(bind-properties (text next-document) document
(format-to-string "#{text-simple-document text=\"%s\" next-document=%=}"
text next-document)))
(define-method render-simple-document ((document <text-simple-document>) stream)
(bind-properties (text next-document) document
(stream-write stream text)
(render-simple-document next-document stream)))
;; line
(define-class <line-simple-document> (<simple-document>)
indentation
document)
(define-method description ((line-document <line-simple-document>))
(bind-properties (indentation document) line-document
(format-to-string "#{line-simple-document indentation=%d document=%=}"
indentation document)))
(define-method render-simple-document ((line-document <line-simple-document>) stream)
(bind-properties (indentation document) line-document
(stream-write stream "\n")
(dotimes (_ indentation)
(stream-write stream " "))
(render-simple-document document stream)))
;;;; transformation
(define-function fits? (width triples)
(when (>= width 0)
(or (empty? triples)
(destructuring-bind ((indent mode current-document) #rest rest)
triples
(select current-document instance?
((<empty-document>)
(fits? width rest))
((<combined-document>)
(bind-properties (left right) current-document
(fits? width (concatenate [[indent mode left]
[indent mode right]]
rest))))
((<nested-document>)
(bind-properties (level document) current-document
(fits? width (cons [(+ indent level) mode document]
rest))))
((<text-document>)
(bind-properties (text) current-document
(fits? (- width (size text)) rest)))
((<group-document>)
(bind-properties (document) current-document
(fits? width (cons [indent 'flat document]
rest))))
((<break-document>)
(bind-properties (text) current-document
(or (== mode 'break)
(fits? (- width (size text)) rest)))))))))
(define-function transform-documents (width consumed documents)
(if (empty? documents)
$empty-simple-document
(destructuring-bind ((indent mode current-document) #rest rest)
documents
(select current-document instance?
((<empty-document>)
(transform-documents width consumed rest))
((<combined-document>)
(bind-properties (left right) current-document
(transform-documents width consumed
(concatenate [[indent mode left]
[indent mode right]]
rest))))
((<nested-document>)
(bind-properties (level document) current-document
(transform-documents width consumed
(cons [(+ indent level) mode document]
rest))))
((<text-document>)
(bind-properties (text) current-document
(make <text-simple-document>
text: text
next-document: (transform-documents
width (+ consumed (size text)) rest))))
((<break-document>)
(bind-properties (text) current-document
(if (== mode 'flat)
(make <text-simple-document>
text: text
next-document: (transform-documents
width (+ consumed (size text)) rest))
(make <line-simple-document>
indentation: indent
document: (transform-documents
width indent rest)))))
((<group-document>)
(bind-properties (document) current-document
(bind ((mode* (if (fits? (- width consumed)
(cons [indent 'flat document]
rest))
'flat
'break)))
(transform-documents width consumed
(cons [indent mode* document]
rest)))))
(else:
$empty-simple-document)))))
(define-function as-simple-document (document width)
(transform-documents width 0 [[0 'flat (group-document document)]]))
(define-function pretty-print (stream document width)
(render-simple-document (as-simple-document document width)
stream))
; (define-function conditional-document (nesting conditional expr1 expr2)
; (group-document
; (connect-documents
; (group-document
; (nest-document nesting
; (connect-documents
; (as-text-document "if")
; conditional)))
; (connect-documents (group-document
; (nest-document nesting
; (connect-documents
; (as-text-document "then")
; expr1)))
; (group-document
; (nest-document nesting
; (connect-documents
; (as-text-document "else")
; expr2)))))))
;
; (bind ((condition (binary-document 2
; (as-text-document "a")
; (as-text-document "==")
; (as-text-document "b")))
; (expr1 (binary-document 2
; (as-text-document "a")
; (as-text-document "<<")
; (as-text-document "2")))
; (expr2 (binary-document 2
; (as-text-document "a")
; (as-text-document "+")
; (as-text-document "b")))
; (document (conditional-document 2 condition expr1 expr2)))
; (for-each ((width [32 15 10 8 7 6]))
; ()
; (bind ((simple-document (as-simple-document document width)))
; ; (format-out "document: %=\n" document)
; ; (format-out "simple document: %=\n" simple-document)
; (format-out "width: %d\n" width)
; (format-out ">>>\n")
; (render-simple-document simple-document *standard-output*)
; (format-out "\n<<<\n======\n"))))