-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp-util.rkt
385 lines (348 loc) · 10.9 KB
/
app-util.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#lang racket/base
#|
Application-specific utilities shared by both the #lang and the
compiler.
|#
(require "util.rkt" "util/racket-5-compat.rkt"
racket/contract racket/dict racket/function
racket/list racket/pretty
syntax/id-table syntax/stx)
;;;
;;; gensym
;;;
;; Produces interned, deterministically-numbered symbols. They are
;; only unique in a context as indicated by `r`.
(define* (next-gensym r sym)
(define num (hash-ref r sym 0))
(define n-sym
(if (= num 0)
sym
(string->symbol
(string-append (symbol->string sym) "_"
(number->string num)))))
(values (hash-set r sym (+ num 1)) n-sym))
;; Produces interned, deterministically-numbered symbols. They are
;; only unique in a context as indicated by `r`. This variant of
;; `next-gensym` always adds a number to the generated symbol.
(define* (next-gensym1 r sym)
(define num (hash-ref r sym 1))
(define n-sym
(string->symbol
(string-append (symbol->string sym) "_"
(number->string num))))
(values (hash-set r sym (+ num 1)) n-sym))
;;;
;;; identifier tables
;;;
(define* (id-table? x)
(or (free-id-table? x)
(bound-id-table? x)))
(define* (immutable-id-table? x)
(or (immutable-free-id-table? x)
(immutable-bound-id-table? x)))
;;;
;;; debugging utilities
;;;
(define* (show-matches-in-id-table id-stx d)
(writeln
(cons
`(looking-for ,(syntax-e id-stx))
(dict-map
d
(lambda (k v)
(cond
((bound-identifier=? k id-stx) `(bound ,(syntax-e k)))
((free-identifier=? k id-stx) `(free ,(syntax-e k)))
(else #f)))))))
;; E.g.
;; (let* ((x 1)
;; (x-id #'x))
;; (let ((x 1))
;; (syntax->datum/free-id #`(x x #,x-id y))))
;; See also: identifier-binding-symbol
(define* (syntax->datum/free-id stx)
(define n 0)
(define h (make-free-id-table))
(define (f x)
(cond
((null? x)
null)
((pair? x)
(cons (f (car x)) (f (cdr x))))
((syntax? x)
(define uw (syntax-e x))
(cond
((pair? uw)
(f uw))
((symbol? uw)
(define cur-n (dict-ref h x #f))
(unless cur-n
(set!-values (n cur-n) (values (+ n 1) n))
(dict-set! h x cur-n))
(string->symbol (format "~a.~a" (syntax-e x) cur-n)))
(else
;; Composite types other than pairs (vectors, boxes, etc.) not
;; supported at this time.
(syntax->datum x))))
(else
(error 'syntax->datum/free-id "unsupported: ~s" x))))
(f stx))
(define (mpi->datum/split mpi)
(define (mpi->datum mpi [root? #f])
(define-values (m b) (module-path-index-split mpi))
(cond
((and root? (not (or m b))) '(self))
((not m) null)
((not b) (list m))
((module-path-index? b) (append (mpi->datum b) (list m)))
((resolved-module-path? b) (list b m))
(else (raise-result-error
'module-path-index-split "documented result" 0 m b))))
(mpi->datum mpi #t))
(define (mpi->datum/resolve mpi)
(resolved-module-path-name (module-path-index-resolve mpi)))
(define* (id->datum/detailed id-stx
#:conv-mpi mpi->datum
#:phase [ph (syntax-local-phase-level)])
(define b (identifier-binding id-stx ph))
(define name (syntax-e id-stx))
(cond
((not b) `[,name : #f])
((eq? b 'lexical) `[,name : lexical])
((list? b)
(define mpi (first b))
(define sym (second b))
`[,name : ,(mpi->datum mpi)
,@(if (eq? sym name) '() `(RENAMED FROM ,sym))])
(else
(raise-result-error 'identifier-binding
"documented identifier-binding result" b))))
(define* (id->datum/detailed/sym id-stx #:conv-mpi mpi->datum)
(define b (identifier-binding id-stx))
(define name (syntax-e id-stx))
(define v
(cond
((not b) #f)
((eq? b 'lexical) "lexical")
((list? b)
(define mpi (first b))
(define sym (second b))
(format "~a~a"
(if (eq? sym name) "" (format "was:~a " sym))
(mpi->datum mpi)))
(else
(raise-result-error 'identifier-binding
"documented identifier-binding result" b))))
(if (not v)
name
(string->symbol (format "~s«~a»" name v))))
;; Tries to make it easy to see at a glance which IDs have bindings
;; and which do not, and what kind they are.
(define* (id->datum/phase id #:conv-mpi dummy)
(define (pick-glyph)
(define b- (identifier-binding id -1))
(define b0 (identifier-binding id 0))
(define b+ (identifier-binding id 1))
(cond
((not (or b- b0 b+)) "ø")
((eq? b0 'lexical)
(assert (eq? b- 'lexical))
(assert (eq? b+ 'lexical))
"$")
(else
(format "~a~a~a"
(if b- (begin (assert (list? b-)) "¥") "")
(if b0 (begin (assert (list? b0)) "£") "")
(if b+ (begin (assert (list? b+)) "€") "")))))
(string->symbol
(format "~a~a" (syntax-e id) (pick-glyph))))
(define* (id->datum/plain id #:conv-mpi dummy)
(syntax-e id))
(define* (syntax->datum/binding stx
#:conv-id [id->datum id->datum/detailed]
#:conv-mpi [mpi->datum mpi->datum/resolve]
#:pred [p? (lambda (x) #t)])
(define (f x)
(cond
((null? x)
null)
((pair? x)
(cons (f (car x)) (f (cdr x))))
((syntax? x)
(define uw (syntax-e x))
(cond
((pair? uw)
(f uw))
((symbol? uw)
(if (p? uw) (id->datum x #:conv-mpi mpi->datum) uw))
(else
;; Composite types other than pairs (vectors, boxes, etc.) not
;; supported at this time.
(syntax->datum x))))
(else
(error 'syntax->datum/binding "unsupported: ~s" x))))
(f stx))
(define (sym-add-sfx sfx sym)
(string->symbol (string-append (symbol->string sym) sfx)))
(define (sym-add-pfx pfx sym)
(string->symbol (string-append pfx (symbol->string sym))))
(define* (stx->datum/source f stx)
(define src
(or (let-and p (syntax-source stx)
(if (path? p)
(let ()
(define-values (d f d?) (split-path p))
f)
p))
'ø))
(define uw (syntax-e stx))
(cond
[(pair? uw)
(cons (string->symbol (format "~a" src)) (f uw))]
[(symbol? uw)
(string->symbol (format "~a:~a" src uw))]
[else
(syntax->datum stx)]))
(define* (stx->datum/any-loc? f stx)
(define has? (syntax-source stx))
(define sigil (if has? '¤ 'ø))
(define uw (syntax-e stx))
(cond
[(pair? uw)
(cons sigil (f uw))]
[(symbol? uw)
(sym-add-pfx (symbol->string sigil) uw)]
[else
(syntax->datum stx)]))
;; `stx->datum` is invoked as (stx->datum f stx), where `f` can be
;; used to convert any sub-forms.
(define* (syntax->datum/loc stx
#:stx->datum [stx->datum stx->datum/any-loc?]
#:pred [p? (lambda (x) #t)])
(define (f x)
(cond
((null? x)
null)
((pair? x)
(cons (f (car x)) (f (cdr x))))
((syntax? x)
(cond
[(p? x) (stx->datum f x)]
[else
(define uw (syntax-e x))
(cond
[(pair? uw)
(f uw)]
[(symbol? uw)
(f uw)]
[else
;; Composite types other than pairs (vectors, boxes, etc.) not
;; supported at this time.
(syntax->datum x)])]))
(else
(error 'syntax->datum/loc "unsupported: ~s" x))))
(f stx))
(define* (print-with-select-syntax-properties props stx)
(define (props-for stx)
(define lst
(for/list ((prop props))
(define v (syntax-property stx prop))
(and v `(,prop = ,v))))
(filter identity lst))
(define (print-for stx)
(define vs (props-for stx))
(unless (null? vs)
(writeln `(PROPS ,@vs PRESENT IN ,stx))))
(define (f stx)
(define lst (syntax->list stx))
(cond
(lst (print-for stx)
(for-each f lst))
(else (print-for stx))))
(pretty-print (syntax->datum stx))
(f stx))
;;;
;;; error reporting
;;;
(define (port-skip-chars in amt)
(let loop ((k amt))
(when (> k 0)
(define j (min k 2048))
(when (eof-object? (read-string j in))
(error 'eof))
(loop (- k j)))))
(define* (read-original-code stx)
(let-and path (syntax-source stx)
(let-and beg (syntax-position stx)
(let ([amt (or (syntax-span stx) 20)])
(may-fail
(call-with-input-file
path
(lambda (in)
(port-skip-chars in (sub1 beg))
;; It's OK for less to be available than `amt`.
(read-string amt in))))))))
(define-syntax-rule* (unsupported v ...)
(error "unsupported" v ...))
(define-with-contract*
(-> syntax? (or/c symbol? #f))
(form-get-name stx)
(define x (syntax-e stx))
(cond
((symbol? x) x)
((pair? x)
(define y (syntax-e (car x)))
(and (symbol? y) y))
(else #f)))
;; Unlike with exn:fail:syntax, 'exprs' need not contain syntax
;; objects. It is a (listof any/c).
(concrete-struct* exn:fail:language exn:fail (exprs) #:transparent)
(define field/c
(let ([option/c (or/c 'value 'multi 'maybe)])
(or/c string? (cons/c string? (listof option/c)))))
(define-with-contract*
(->* ((or/c symbol? #f) string?)
(any/c any/c (listof any/c)
#:continued (or/c string? (listof string?))
#:fields (listof (list/c field/c any/c)))
any)
;; The 'name' symbol here has different semantics compared to
;; raise-syntax-error, as it is only used as the fallback value if
;; neither 'expr' nor 'sub-expr' have no symbol to extract.
(raise-language-error name message
[expr #f]
[sub-expr #f]
[extra-exprs null]
#:continued [continued null]
#:fields [more-fields null])
(define (get-sym x)
(and (syntax? x)
(if (identifier? x)
(syntax-e x)
(and (stx-pair? x)
(let ((y (car (syntax-e x))))
(and (identifier? y)
(syntax-e y)))))))
(define n (or (get-sym expr) (get-sym sub-expr) name '?))
(define exprs (append
(if sub-expr (list sub-expr) null)
(if expr (list expr) null)
extra-exprs))
(define-syntax-rule
(and-let-list x e t ...)
(if-let x e (list t ...) null))
(define (mk s e)
(if (syntax? e)
`(,s ,(syntax->datum e)
,@(and-let-list txt (read-original-code e)
(string-append s " (source)") txt)
,(string-append s " (syntax)") ,e
,@(and-let-list orig (syntax-property e 'origin)
(string-append s " (origin)") orig))
(list s e)))
(define fields
(apply append (mk "at" sub-expr) (mk "in" expr) more-fields))
(apply raise-misc-error n message fields
#:continued continued
#:constructor (lambda (s cs)
(exn:fail:language s cs exprs))))