-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoc.rkt
364 lines (299 loc) · 10.7 KB
/
doc.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
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
355
356
357
358
359
360
361
362
363
364
;; This module defines basic document combinator along with
;; the basic partial evaluation.
#lang racket/base
(#%declare #:unsafe)
(provide doc?
;; pattern expanders
:newline
:fail
:text
:special
:alternatives
:concat
:nest
:align
:reset
:full
:cost
;; constructor
newline
fail
text
special
alternatives
concat
nest
align
reset
full
cost
set-memo-limit!
get-memo-limit)
(module+ private
(provide (struct-out doc)))
(require racket/match
(for-syntax racket/base
syntax/parse/pre))
;; We only memoize every memo-weight-limit node.
;; The value of memo-weight-limit must be a positive integer (initialized below)
(define memo-weight-limit #f)
;; memo-weight-init is an integer in range [0, *memo-weight-limit* - 1]
;; that is used as the initial value given to the leaf nodes.
;; Since we don't want the leaf node to get memoized and want memoization to
;; occur farthest away from the leaf nodes,
;; we set the value to memo-weight-limit - 1 (initialized below).
(define memo-weight-init #f)
(define (set-memo-limit! memo-limit)
(set! memo-weight-limit memo-limit)
(set! memo-weight-init (sub1 memo-limit)))
(define (get-memo-limit)
memo-weight-limit)
(set-memo-limit! 7)
;; - failing/X/Y means the doc will always fail to resolve.
;; X indicates fullness before resolving, and
;; Y indicates fullness after resolving.
;; The value usually starts as #f. The #f value could be mutated to #t.
(struct doc (memo-weight ; memo weight in range of 0 to *memo-weight-limit* - 1
table/no/no ; memo table for not full before and not full after
table/yes/no ; memo table for full before and not full after
table/no/yes ; memo table for not full before and full after
table/yes/yes ; memo table for full before and full after
failing/no/no ; fails when not full before and not full after?
failing/yes/no ; fails when full before and not full after?
failing/no/yes ; fails when not full before and full after?
failing/yes/yes ; fails when full before and full after?
nl-cnt) ; overapproximation of newline count
#:mutable)
(begin-for-syntax
(define-splicing-syntax-class fail-flag
(pattern {~seq #:fail value ...})
(pattern {~seq} #:with (value ...) #'(#f #f #f #f))))
;; calc-weight :: doc -> natural?
;; Calculate the current memo weight in range 0 to memo-weight-limit - 1
(define (calc-weight d)
(define val (doc-memo-weight d))
(cond
[(zero? val) memo-weight-init]
[else (sub1 val)]))
(define-syntax (get-weight stx)
(syntax-parse stx
[(_ d) #'(calc-weight d)]
[(_ a b) #'(min (calc-weight a) (calc-weight b))]))
;; inst-internal-doc creates an internal doc with a constructor ctor.
;; It automatically decides whether memo tables should be allocated based on
;; the memo weight, and initializes the failure flags with #f if
;; #:fail is not given
(define-syntax (inst-internal-doc stx)
(syntax-parse stx
[(_ ctor #:doc [doc ...]
fail:fail-flag #:args arg ...)
#'(let ([weight (get-weight doc ...)])
(cond
[(zero? weight)
(ctor weight
(make-hasheq) (make-hasheq) (make-hasheq) (make-hasheq)
fail.value ...
arg ...)]
[else
(ctor weight
#f #f #f #f
fail.value ...
arg ...)]))]))
;; inst-leaf-doc creates a leaf doc with a constructor ctor.
;; It automatically blanks the memo table fields, initializes the memo weight
;; and initializes the failure flags with #f if #:fail is not given
(define-syntax (inst-leaf-doc stx)
(syntax-parse stx
[(_ ctor fail:fail-flag #:args arg ...)
#'(ctor memo-weight-init
#f #f #f #f
fail.value ...
arg ...)]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A newline consists of:
;; - s :: (or/c #f string?)
(struct :newline doc (s) #:transparent #:constructor-name make-newline)
(define (newline s)
(inst-leaf-doc make-newline #:fail #f #f #t #t #:args 1 s))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A fail is a nullary constructor.
(struct :fail doc () #:transparent #:constructor-name make-fail)
(define fail (inst-leaf-doc make-fail #:fail #t #t #t #t #:args -1))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A text consists of:
;; - s :: (or/c string? (treeof string?))
;; - len :: natural?
;; where len is the length of s
(struct :text doc (s len) #:transparent #:constructor-name make-text*)
(define (make-text s len)
(inst-leaf-doc make-text* #:fail #f #t #t (not (zero? len)) #:args 0 s len))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; special consists of:
;; - s :: any/c
;; - len :: natural?
;; where len is the length of s
(struct :special doc (s len) #:transparent #:constructor-name make-special*)
(define (special s len)
(inst-leaf-doc make-special* #:fail #f #t #t (not (zero? len)) #:args 0 s len))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; An alternatives consists of:
;; - a :: doc?
;; - b :: doc?
(struct :alternatives doc (a b) #:transparent #:constructor-name make-alternatives*)
(define (make-alternatives a b)
(inst-internal-doc make-alternatives*
#:doc [a b]
#:args
(max (doc-nl-cnt a) (doc-nl-cnt b))
a b))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A concat consists of:
;; - a :: doc?
;; - b :: doc?
(struct :concat doc (a b) #:transparent #:constructor-name make-concat*)
(define (make-concat a b)
(inst-internal-doc make-concat*
#:doc [a b]
#:args
(+ (doc-nl-cnt a) (doc-nl-cnt b))
a b))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A nest consists of:
;; - n :: natural?
;; - d :: doc?
(struct :nest doc (n d) #:transparent #:constructor-name make-nest*)
(define (make-nest n d)
(inst-internal-doc make-nest*
#:doc [d]
#:args
(doc-nl-cnt d)
n d))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; An align consists of:
;; - d :: doc?
(struct :align doc (d) #:transparent #:constructor-name make-align*)
(define (make-align d)
(inst-internal-doc make-align*
#:doc [d]
#:args
(doc-nl-cnt d)
d))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A reset consists of:
;; - d :: doc?
(struct :reset doc (d) #:transparent #:constructor-name make-reset*)
(define (make-reset d)
(inst-internal-doc make-reset*
#:doc [d]
#:args
(doc-nl-cnt d)
d))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A full consists of:
;; - d :: doc?
(struct :full doc (d) #:transparent #:constructor-name make-full*)
(define (make-full d)
(inst-internal-doc make-full*
#:doc [d]
#:fail #t #t #f #f
#:args
(doc-nl-cnt d)
d))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A cost consists of:
;; - n :: tau
;; - d :: doc?
(struct :cost doc (n d) #:transparent #:constructor-name make-cost*)
(define (make-cost n d)
(inst-internal-doc make-cost*
#:doc [d]
#:args
(doc-nl-cnt d)
n d))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Set current-debug? to true for the debug mode (disable partial evaluation)
(define-for-syntax current-debug? #f)
;; perform partial evaluation on the "constructor"s
;; For production code, use #:prod clause. For debugging code, use #:dbg clause.
(define-syntax (cond-dbg stx)
(syntax-parse stx
[(_ [#:dbg e ...+]
[#:prod e2 ...+])
#:with out (if current-debug?
#'(let () e ...)
#'(let () e2 ...))
#'out]))
(cond-dbg
[#:dbg
(displayln "==========")
(displayln "debug mode")
(displayln "==========")]
[#:prod (void)])
(define (text s)
(make-text s (string-length s)))
(define (concat a b)
(cond-dbg
[#:dbg (make-concat a b)]
[#:prod (match* (a b)
[((struct* :text ([len 0])) d) d]
[(d (struct* :text ([len 0]))) d]
[((? :full?) (? :text?)) fail] ; the text is non-empty
[((? :fail?) _) fail]
[(_ (? :fail?)) fail]
[((struct* :text ([s sa] [len la]))
(struct* :text ([s sb] [len lb])))
(make-text (cons sa sb) (+ la lb))]
[(_ _) (make-concat a b)])]))
(define (alternatives a b)
(cond-dbg
[#:dbg (make-alternatives a b)]
[#:prod (match* (a b)
[((? :fail?) _) b]
[(_ (? :fail?)) a]
[(_ _)
(cond
[(eq? a b) a]
[else (make-alternatives a b)])])]))
(define (full d)
(cond-dbg
[#:dbg (make-full d)]
[#:prod (match d
[(? :full?) d]
[(? :fail?) fail]
[_ (make-full d)])]))
(define (cost n d)
(cond-dbg
[#:dbg (make-cost n d)]
[#:prod (match d
[(? :fail?) fail]
[_ (make-cost n d)])]))
(define (nest n d)
(cond-dbg
[#:dbg (make-nest n d)]
[#:prod (match d
[(? :fail?) d]
[(? :align?) d]
[(? :reset?) d]
[(? :text?) d]
[(struct* :nest ([n n2] [d d])) (make-nest (+ n n2) d)]
[_ (make-nest n d)])]))
(define (align d)
(cond-dbg
[#:dbg (make-align d)]
[#:prod (match d
[(? :fail?) d]
[(? :align?) d]
[(? :reset?) d]
[(? :text?) d]
[_ (make-align d)])]))
(define (reset d)
(cond-dbg
[#:dbg (make-reset d)]
[#:prod (match d
[(? :fail?) d]
[(? :align?) d]
[(? :reset?) d]
[(? :text?) d]
[_ (make-reset d)])]))