This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
format.rkt
256 lines (236 loc) · 5.96 KB
/
format.rkt
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
#lang racket
(require (planet dyoo/while-loop:1:=1)
"etc.rkt"
"read.rkt"
memoize)
(provide format-module)
(define (abbrev-prefix v)
(match v
((list (== quasiquote-symbol) _)
"`")
((list (== quasisyntax-symbol) _)
"#`")
((list (== quote-symbol) _)
"'")
((list (== syntax-symbol) _)
"#'")
((list (== unquote-splicing-symbol) _)
",@")
((list (== unquote-symbol) _)
",")
((list (== unsyntax-splicing-symbol) _)
"#,@")
((list (== unsyntax-symbol) _)
"#,")
(_ #f)))
(define (blank-after-decls lst)
(match lst
((list a (== blank-symbol) b ...)
(list* a blank-symbol (blank-after-decls b)))
((list (? decl*? a) b c ...)
(list* a blank-symbol (blank-after-decls (cons b c))))
((list a b ...)
(cons a (blank-after-decls b)))
(_ '())))
(define (blank-before-comments lst)
(match lst
((list (== blank-symbol) a ...)
(cons blank-symbol (blank-before-comments a)))
((list (list (== line-comment-symbol) a) b ...)
(list* (list line-comment-symbol a) (blank-before-comments b)))
((list a (list (== line-comment-symbol) b) c ...)
(list* a blank-symbol (list line-comment-symbol b) (blank-before-comments c)))
((list a b ...)
(cons a (blank-before-comments b)))
(_ '())))
(define (decl*? x)
(match x
((list (or 'provide
'require)
b ...)
#t)
(_ (decl? x))))
(define (expr col v)
(define col1 (+ col 1))
(define col2
(when (pair? v)
(+ col 2 (width (car v)))))
(define op
(when (pair? v)
(~a (car v))))
(define op2
(when (pair? v)
(format "(~a "
(car v))))
(match v
; atom
((== blank-symbol)
"")
((? atom? _)
(~s v))
; comment
((list (== block-comment-symbol) s)
s)
((list (== expr-comment-symbol) a)
(string-append "#;" (expr (+ col 2) a)))
((list (== line-comment-symbol) s)
s)
; abbrev prefix
((? abbrev-prefix (list _ w))
(define s (abbrev-prefix v))
(string-append s (expr (+ col (string-length s)) w)))
; special form
((list (or 'define
'set!)
a
b)
#:when
(and (symbol? a)
(inline col1 v))
(string-append "(" (inline col1 v) ")"))
((list (or 'begin
'cond
'else)
b ...)
(string-append "(" op "\n" (make-string col1 #\space) (exprs col1 b) ")"))
((list (or 'case
'define
'define-syntax
'define/memo
'define/memo*
'eprintf
'for
'for*
'for*/and
'for*/first
'for*/hash
'for*/hasheq
'for*/hasheqv
'for*/last
'for*/list
'for*/or
'for*/product
'for*/sum
'for/and
'for/first
'for/hash
'for/hasheq
'for/hasheqv
'for/last
'for/list
'for/or
'for/product
'for/sublists
'for/sum
'format
'if
'lambda
'lambda/memo
'lambda/memo*
'match
'parameterize
'printf
'receive
'syntax-rules
'unless
'when
'while
'while/list
'with-handlers
'with-output-to-file)
a
b ...)
(string-append op2
(expr col2 a)
"\n"
(make-string col1 #\space)
(exprs col1 b)
")"))
((list 'let (list a ...) b ...)
(string-append op2
(expr col2 a)
"\n"
(make-string col1 #\space)
(exprs col1 b)
")"))
((list (or 'let
'syntax-case)
id
(list a ...)
b ...)
(string-append op2
(~a id)
" ("
(exprs (+ col2 (width id) 2) a)
")\n"
(make-string col1 #\space)
(exprs col1 b)
")"))
; no args
((list f)
(string-append "(" (expr col1 f) ")"))
; args inline
((list (not (or 'and
'or
'provide
'require))
a ...)
#:when
(and (symbol? (car v))
(not (memq line-comment-symbol (flatten v)))
(inline col1 v))
(string-append "(" (inline col1 v) ")"))
; args aligned
((list (? symbol? f) a ...)
#:when
(for/and ((w a))
(< (+ col2 (width w)) 80))
(string-append op2 (exprs col2 a) ")"))
; args unaligned
((list f a ...)
(string-append "("
(expr col1 f)
"\n"
(make-string col1 #\space)
(exprs col1 a)
")"))))
(define (exprs col lst)
(set! lst (blank-after-decls lst))
(set! lst (blank-before-comments lst))
(set! lst
(let loop ((lst lst))
(match lst
((list a '... c ...)
(cons (string-append (expr col a) " ...") (loop c)))
((list a b ...)
(cons (expr col a) (loop b)))
(_ '()))))
(string-join lst (string-append "\n" (make-string col #\space))))
(define (format-module m)
(trim-lines (exprs 0 m)))
(define (inline col lst)
(set! lst
(let loop ((col col)
(lst lst))
(match lst
((list a b ...)
(cons (expr col a) (loop (+ col (width a) 1) b)))
(_ '()))))
(define s (string-join lst " "))
(and (not (string-contains? s "\n"))
(< (+ col (string-length s)) 80)
s))
(define (max-line-length s)
(define lines (string-split s "\n"))
(if (null? lines)
0
(apply max (map string-length lines))))
(define (trim-lines s)
(define lines (string-split s "\n"))
(set! lines
(for/list ((line lines))
(string-trim line #:left? #f)))
(string-join lines "\n" #:after-last "\n"))
(define/memo* (width v)
(max-line-length (expr 0 v)))
(define blank-symbol (gensym))