Debian Codegraph @

lib/srfi-159/internal/pretty.scm

raw
1 ;; pretty.scm -- pretty printing format combinator
2 ;; Copyright (c) 2006-2018 Alex Shinn. All rights reserved.
3 ;; BSD-style license: http://synthcode.com/license.txt
4
5 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6 ;; utilities
7
8 (define (take* ls n) ; handles dotted lists and n > length
9 (cond ((zero? n) '())
10 ((pair? ls) (cons (car ls) (take* (cdr ls) (- n 1))))
11 (else '())))
12
13 (define (drop* ls n) ; may return the dot
14 (cond ((zero? n) ls)
15 ((pair? ls) (drop* (cdr ls) (- n 1)))
16 (else ls)))
17
18 (define (make-space n) (make-string n #\space))
19 (define (make-nl-space n) (string-append "\n" (make-string n #\space)))
20
21 (define (joined/shares fmt ls shares . o)
22 (let ((sep (displayed (if (pair? o) (car o) " "))))
23 (fn ()
24 (if (null? ls)
25 nothing
26 (let lp ((ls ls))
27 (each
28 (fmt (car ls))
29 (let ((rest (cdr ls)))
30 (cond
31 ((null? rest) nothing)
32 ((pair? rest)
33 (call-with-shared-ref/cdr rest
34 shares
35 (fn () (lp rest))
36 sep))
37 (else (each sep ". " (fmt rest)))))))))))
38
39 (define (string-find/index str pred i)
40 (string-cursor->index
41 str
42 (string-index str pred (string-index->cursor str i))))
43
44 (define (try-fitted2 proc fail)
45 (fn (width output)
46 (let ((out (open-output-string)))
47 (call-with-current-continuation
48 (lambda (abort)
49 ;; Modify output to accumulate to an output string port,
50 ;; and escape immediately with failure if we exceed the
51 ;; column width.
52 (define (output* str)
53 (fn (col)
54 (let lp ((i 0) (col col))
55 (let ((nli (string-find/index str #\newline i))
56 (len (string-length str)))
57 (if (< nli len)
58 (if (> (+ (- nli i) col) width)
59 (abort fail)
60 (lp (+ nli 1) 0))
61 (let ((col (+ (- len i) col)))
62 (cond
63 ((> col width)
64 (abort fail))
65 (else
66 (output-default str)))))))))
67 (forked
68 (with ((output output*)
69 (port out))
70 proc)
71 ;; fitted successfully
72 (output (get-output-string out))))))))
73
74 (define (try-fitted proc . fail)
75 (if (null? fail)
76 proc
77 (try-fitted2 proc (apply try-fitted fail))))
78
79 (define (fits-in-width width proc)
80 (call-with-current-continuation
81 (lambda (abort)
82 (show
83 #f
84 (fn (output)
85 (define (output* str)
86 (each (output str)
87 (fn (col)
88 (if (>= col width)
89 (abort #f)
90 nothing))))
91 (with ((output output*))
92 proc))))))
93
94 (define (fits-in-columns width ls writer)
95 (let ((max-w (quotient width 2)))
96 (let lp ((ls ls) (res '()) (widest 0))
97 (cond
98 ((pair? ls)
99 (let ((str (fits-in-width max-w (writer (car ls)))))
100 (and str
101 (lp (cdr ls)
102 (cons str res)
103 (max (string-length str) widest)))))
104 ((null? ls) (cons widest (reverse res)))
105 (else #f)))))
106
107 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
108 ;; style
109
110 (define syntax-abbrevs
111 '((quote . "'") (quasiquote . "`")
112 (unquote . ",") (unquote-splicing . ",@")
113 ))
114
115 (define (pp-let ls pp shares)
116 (if (and (pair? (cdr ls)) (symbol? (cadr ls)))
117 (pp-with-indent 2 ls pp shares)
118 (pp-with-indent 1 ls pp shares)))
119
120 (define indent-rules
121 `((lambda . 1) (define . 1)
122 (let . ,pp-let) (loop . ,pp-let)
123 (let* . 1) (letrec . 1) (letrec* . 1) (and-let* . 1) (let1 . 2)
124 (let-values . 1) (let*-values . 1) (receive . 2) (parameterize . 1)
125 (let-syntax . 1) (letrec-syntax . 1) (syntax-rules . 1) (syntax-case . 2)
126 (match . 1) (match-let . 1) (match-let* . 1)
127 (if . 3) (when . 1) (unless . 1) (case . 1) (while . 1) (until . 1)
128 (do . 2) (dotimes . 1) (dolist . 1) (test . 1)
129 (condition-case . 1) (guard . 1) (rec . 1)
130 (call-with-current-continuation . 0)
131 ))
132
133 (define indent-prefix-rules
134 `(("with-" . -1) ("call-with-" . -1) ("define-" . 1))
135 )
136
137 (define indent-suffix-rules
138 `(("-case" . 1))
139 )
140
141 (define (pp-indentation form)
142 (let ((indent
143 (cond
144 ((assq (car form) indent-rules) => cdr)
145 ((and (symbol? (car form))
146 (let ((str (symbol->string (car form))))
147 (or (find (lambda (rx) (string-prefix? (car rx) str))
148 indent-prefix-rules)
149 (find (lambda (rx) (string-suffix? (car rx) str))
150 indent-suffix-rules))))
151 => cdr)
152 (else #f))))
153 (if (and (number? indent) (negative? indent))
154 (max 0 (- (+ (or (length+ form) +inf.0) indent) 1))
155 indent)))
156
157 (define (with-reset-shares shares proc)
158 (let ((orig-count (cdr shares)))
159 (fn ()
160 (let ((new-count (cdr shares)))
161 (cond
162 ((> new-count orig-count)
163 (hash-table-walk
164 (car shares)
165 (lambda (k v)
166 (if (and (cdr v) (>= (car v) orig-count))
167 (set-cdr! v #f))))
168 (set-cdr! shares orig-count)))
169 proc))))
170
171 (define (pp-with-indent indent-rule ls pp shares)
172 (fn ((col1 col))
173 (each
174 "("
175 (pp (car ls))
176 (fn ((col2 col) width string-width)
177 (let ((fixed (take* (cdr ls) (or indent-rule 1)))
178 (tail (drop* (cdr ls) (or indent-rule 1)))
179 (default
180 (let ((sep (make-nl-space (+ col1 1))))
181 (each sep (joined/shares pp (cdr ls) shares sep))))
182 ;; reset in case we don't fit on the first line
183 (reset-shares (with-reset-shares shares nothing)))
184 (call-with-output
185 (trimmed/lazy (- width col2)
186 (each " "
187 (joined/shares
188 (lambda (x) (pp-flat x pp shares)) fixed shares " "))
189 )
190 (lambda (first-line)
191 (cond
192 ((< (+ col2 (string-width first-line)) width)
193 ;; fixed values on first line
194 (let ((sep (make-nl-space
195 (if indent-rule (+ col1 2) (+ col2 1)))))
196 (each first-line
197 (cond
198 ((not (or (null? tail) (pair? tail)))
199 (each ". " (pp tail)))
200 ((> (or (length+ (cdr ls)) +inf.0) (or indent-rule 1))
201 (each sep (joined/shares pp tail shares sep)))
202 (else
203 nothing)))))
204 (indent-rule
205 ;; fixed values lined up, body indented two spaces
206 (try-fitted
207 (each
208 reset-shares
209 " "
210 (joined/shares pp fixed shares (make-nl-space (+ col2 1)))
211 (if (pair? tail)
212 (let ((sep (make-nl-space (+ col1 2))))
213 (each sep (joined/shares pp tail shares sep)))
214 nothing))
215 (each reset-shares default)))
216 (else
217 ;; all on separate lines
218 (each reset-shares default)))))))
219 ")")))
220
221 (define (pp-app ls pp shares)
222 (let ((indent-rule (pp-indentation ls)))
223 (if (procedure? indent-rule)
224 (indent-rule ls pp shares)
225 (pp-with-indent indent-rule ls pp shares))))
226
227 ;; the elements may be shared, just checking the top level list
228 ;; structure
229 (define (proper-non-shared-list? ls shares)
230 (let ((tab (car shares)))
231 (let lp ((ls ls))
232 (or (null? ls)
233 (and (pair? ls)
234 (not (hash-table-ref/default tab ls #f))
235 (lp (cdr ls)))))))
236
237 (define (non-app? x)
238 (if (pair? x)
239 (or (not (or (null? (cdr x)) (pair? (cdr x))))
240 (non-app? (car x)))
241 (not (symbol? x))))
242
243 (define (pp-data-list ls pp shares)
244 (each
245 "("
246 (fn (col width string-width)
247 (let ((avail (- width col)))
248 (cond
249 ((and (pair? (cdr ls)) (pair? (cddr ls)) (pair? (cdr (cddr ls)))
250 (fits-in-columns width ls (lambda (x) (pp-flat x pp shares))))
251 => (lambda (ls)
252 ;; at least four elements which can be broken into columns
253 (let* ((prefix (make-nl-space col))
254 (widest (+ 1 (car ls)))
255 (columns (quotient width widest))) ; always >= 2
256 (let lp ((ls (cdr ls)) (i 1))
257 (cond
258 ((null? ls)
259 nothing)
260 ((null? (cdr ls))
261 (displayed (car ls)))
262 ((>= i columns)
263 (each (car ls)
264 prefix
265 (fn () (lp (cdr ls) 1))))
266 (else
267 (let ((pad (- widest (string-width (car ls)))))
268 (each (car ls)
269 (make-space pad)
270 (lp (cdr ls) (+ i 1))))))))))
271 (else
272 ;; no room, print one per line
273 (joined/shares pp ls shares (make-nl-space col))))))
274 ")"))
275
276 (define (pp-flat x pp shares)
277 (cond
278 ((pair? x)
279 (cond
280 ((and (pair? (cdr x)) (null? (cddr x))
281 (assq (car x) syntax-abbrevs))
282 => (lambda (abbrev)
283 (each (cdr abbrev)
284 (call-with-shared-ref
285 (cadr x)
286 shares
287 (pp-flat (cadr x) pp shares)))))
288 (else
289 (each "("
290 (joined/shares (lambda (x) (pp-flat x pp shares)) x shares " ")
291 ")"))))
292 ((vector? x)
293 (each "#("
294 (joined/shares
295 (lambda (x) (pp-flat x pp shares)) (vector->list x) shares " ")
296 ")"))
297 (else
298 (pp x))))
299
300 (define (pp-pair ls pp shares)
301 (cond
302 ;; one element list, no lines to break
303 ((null? (cdr ls))
304 (each "(" (pp (car ls)) ")"))
305 ;; quote or other abbrev
306 ((and (pair? (cdr ls)) (null? (cddr ls))
307 (assq (car ls) syntax-abbrevs))
308 => (lambda (abbrev)
309 (each (cdr abbrev) (pp (cadr ls)))))
310 (else
311 (try-fitted
312 (fn () (pp-flat ls pp shares))
313 (with-reset-shares
314 shares
315 (fn ()
316 (if (and (non-app? ls)
317 (proper-non-shared-list? ls shares))
318 (pp-data-list ls pp shares)
319 (pp-app ls pp shares))))))))
320
321 (define (pp-vector vec pp shares)
322 (each "#" (pp-data-list (vector->list vec) pp shares)))
323
324 ;; adapted from `write-with-shares'
325 (define (pp obj shares)
326 (fn (radix precision)
327 (let ((write-number
328 (cond
329 ((and (not precision)
330 (assv radix '((16 . "#x") (10 . "") (8 . "#o") (2 . "#b"))))
331 => (lambda (cell)
332 (lambda (n)
333 (if (or (exact? n) (eqv? radix 10))
334 (each (cdr cell) (number->string n (car cell)))
335 (with ((radix 10)) (numeric n))))))
336 (else (lambda (n) (with ((radix 10)) (numeric n)))))))
337 (let pp ((obj obj))
338 (call-with-shared-ref
339 obj shares
340 (fn ()
341 (cond
342 ((pair? obj)
343 (pp-pair obj pp shares))
344 ((vector? obj)
345 (pp-vector obj pp shares))
346 ((number? obj)
347 (write-number obj))
348 (else
349 (write-with-shares obj shares)))))))))
350
351 (define (pretty obj)
352 (fn ()
353 (call-with-output
354 (each (pp obj (extract-shared-objects obj #t))
355 fl)
356 displayed)))
357
358 (define (pretty-shared obj)
359 (fn ()
360 (call-with-output
361 (each (pp obj (extract-shared-objects obj #f))
362 fl)
363 displayed)))
364
365 (define (pretty-simply obj)
366 (fn ()
367 (each (pp obj (extract-shared-objects #f #f))
368 fl)))