Repository navigation
Expand file tree
/
Copy pathsyntax-traversal.rkt
More file actions
297 lines (256 loc) · 10.3 KB
/
Copy pathsyntax-traversal.rkt
File metadata and controls
297 lines (256 loc) · 10.3 KB
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
#lang racket/base
(require racket/contract/base)
(provide atom
expression-directly-enclosing
syntax-find-first
syntax-search
syntax-search-everything
syntax-traverse)
(require (for-syntax racket/base
resyntax/private/more-syntax-parse-classes)
racket/match
racket/sequence
racket/stream
syntax/parse
syntax/parse/define)
(module+ test
(require racket/syntax-srcloc
rackunit
(submod "..")))
;@----------------------------------------------------------------------------------------------------
(define-syntax-class atom
(pattern (~or :id :number :str :boolean :regexp :keyword)))
(define-syntax-class (expression-directly-enclosing id)
(pattern (part ...)
#:when (for/or ([part-stx (in-list (attribute part))]
#:when (identifier? part-stx))
(free-identifier=? id part-stx)))
(pattern (part ...+ . tail-part)
#:when (for/or ([part-stx (in-list (cons #'tail-part (attribute part)))]
#:when (identifier? part-stx))
(free-identifier=? id part-stx))))
(begin-for-syntax
(define-syntax-class syntax-search-clause
#:attributes (syntax-pattern [directive 1] output-stream)
(pattern [syntax-pattern directive:syntax-parse-pattern-directive ... body:expr ... last-body]
#:declare last-body (expr/c #'stream?)
#:with output-stream #'(stream-lazy (let () body ... last-body.c)))
(pattern [syntax-pattern directive:syntax-parse-pattern-directive ...]
#:with output-stream #'(stream this-syntax))))
(define-syntax-parse-rule
(syntax-search stx-expr
(~optional (~seq #:skip-root? skip-root?) #:defaults ([skip-root? #'#false]))
option:syntax-parse-option ...
clause:syntax-search-clause ...)
#:declare stx-expr (expr/c #'syntax?)
(let ([skip-root-id skip-root?])
(define-syntax-class search-case
#:attributes (output-stream)
(~@ . option) ...
(pattern clause.syntax-pattern (~@ . clause.directive) ...
#:attr output-stream clause.output-stream)
...)
(let loop ([stx stx-expr.c] [root? #true])
(stream-lazy
(syntax-parse stx
[child
#:when (not (and skip-root-id root?))
#:with (~var matched search-case) (attribute child)
(attribute matched.output-stream)]
[(part (... ...))
#:cut
(apply stream-append
(for/list ([part-stx (in-list (attribute part))])
(loop part-stx #false)))]
[(part (... ...+) . tail-part)
#:cut
(stream-append (apply stream-append
(for/list ([part-stx (in-list (attribute part))])
(loop part-stx #false)))
(loop #'tail-part #false))]
[_ (stream)])))))
;; Returns stx and every syntax object nested within it, in depth-first preorder. This traversal is
;; eager, even though it's exposed as a stream, because building it lazily costs far more than the
;; list it saves allocating: every caller consumes nearly all of the traversal anyway, and this runs
;; on every syntax object the expander visits.
(define (syntax-search-everything stx)
(define subforms-in-reverse '())
(let loop ([stx stx])
(set! subforms-in-reverse (cons stx subforms-in-reverse))
(syntax-parse stx
[(part ...)
#:cut
(for ([part-stx (in-list (attribute part))])
(loop part-stx))]
[(part ...+ . tail-part)
#:cut
(for ([part-stx (in-list (attribute part))])
(loop part-stx))
(loop #'tail-part)]
[_ (void)]))
(reverse subforms-in-reverse))
(module+ test
(test-case "syntax-search"
(define stx
#'(define (foo)
(cons x y)
(define (bar)
(cons a b))
(cons c d)))
(define actual
(sequence->list
(syntax-search stx
#:literals (cons)
[(cons _ _) (stream (syntax->datum this-syntax))])))
(define expected '((cons x y) (cons a b) (cons c d)))
(check-equal? actual expected)))
(define-syntax-parse-rule (syntax-find-first stx-expr
option:syntax-parse-option ...
syntax-pattern
directive:syntax-parse-pattern-directive ...)
(let ()
(define results (syntax-search stx-expr (~@ . option) ... [syntax-pattern (~@ . directive) ...]))
(and (not (stream-empty? results)) (stream-first results))))
(module+ test
(test-case "syntax-find-first"
(define stx
#'(define (foo)
(cons x y)
(define (bar)
(cons a b))
(cons c d)))
(define actual (syntax-find-first stx #:literals (cons) #:datum-literals (a) (cons a _)))
(check-equal? (syntax->datum actual) '(cons a b))
(check-false (syntax-find-first stx #:literals (cons) (cons _ _ _)))))
(define (perform-syntax-traversal stx modifier
#:skip-root? [skip-root? #false]
#:parent-context-modifier [context-modifier* #false]
#:parent-srcloc-modifier [srcloc-modifier (λ (_) #'here)]
#:parent-props-modifier [props-modifier (λ (_) #false)])
(define context-modifier
(or context-modifier*
(let ([scope (make-syntax-introducer)])
(λ (stx) (scope stx 'add)))))
(let loop ([stx stx] [root? #true])
(define should-skip-because-root? (and skip-root? root?))
(define modified-stx (and (not should-skip-because-root?) (modifier stx)))
(match modified-stx
[(== stx) stx]
[(? syntax?) modified-stx]
[#false
(syntax-parse stx
[(child ...)
#:cut
(define traversed-children
(for/list ([child-stx (in-list (attribute child))])
(loop child-stx #false)))
(define all-same?
(for/and ([traversed-child (in-list traversed-children)]
[child-stx (in-list (attribute child))])
(equal? traversed-child child-stx)))
(if all-same?
stx
(datum->syntax (context-modifier stx)
traversed-children
(srcloc-modifier stx)
(props-modifier stx)))]
[(child ...+ . tail-child)
#:cut
(define traversed-children
(for/list ([child-stx (in-list (attribute child))])
(loop child-stx #false)))
(define traversed-tail-child (loop (attribute tail-child) #false))
(define all-same?
(and (for/and ([traversed-child (in-list traversed-children)]
[child-stx (in-list (attribute child))])
(equal? traversed-child child-stx))
(equal? traversed-tail-child (attribute tail-child))))
(if all-same?
stx
(datum->syntax (context-modifier stx)
(append traversed-children traversed-tail-child)
(srcloc-modifier stx)
(props-modifier stx)))]
[_ stx])])))
(define-syntax-parse-rule
(syntax-traverse (~var stx-expr (expr/c #'syntax?))
(~optional (~seq #:skip-root? skip-root?))
option:syntax-parse-option ...
[clause-pattern directive:syntax-parse-pattern-directive ... clause-body:expr ...+] ...
(~optional (~seq #:parent-context-modifier context-modifier:expr))
(~optional (~seq #:parent-srcloc-modifier srcloc-modifier:expr))
(~optional (~seq #:parent-props-modifier props-modifier:expr)))
(let ()
(define (traversal-case stx)
(syntax-parse stx
(~@ . option) ...
[clause-pattern (~@ . directive) ... clause-body ...]
...
[_ #false]))
(perform-syntax-traversal stx-expr.c traversal-case
(~? (~@ #:skip-root? skip-root?))
(~? (~@ #:parent-context-modifier context-modifier))
(~? (~@ #:parent-srcloc-modifier srcloc-modifier))
(~? (~@ #:parent-props-modifier props-modifier)))))
(module+ test
(test-case "syntax-traverse"
(define stx
#'(define (foo)
(cons x y)
(define (bar)
(cons a b))
(cons c d)))
(define actual
(syntax-traverse stx
#:literals (cons)
[(cons _ _) #'CONS-EXPRESSION]))
(define expected
'(define (foo)
CONS-EXPRESSION
(define (bar)
CONS-EXPRESSION)
CONS-EXPRESSION))
(check-equal? (syntax->datum actual) expected))
(test-case "syntax-traverse dotted tail syntax"
(define stx #'(a b . c))
(define actual (syntax-traverse stx [:id #'ID]))
(define expected '(ID ID . ID))
(check-equal? (syntax->datum actual) expected))
(test-case "syntax-traverse #:skip-root? true"
(define stx #'(a b (c d) e))
(define actual
(syntax-traverse stx
#:skip-root? #true
[(_ ...) #'LIST]))
(check-equal? (syntax->datum actual) '(a b LIST e)))
(test-case "syntax-traverse #:skip-root? true doesn't execute directives on root"
(define stx #'(a b (c d) e))
(define execution-count 0)
(syntax-traverse stx
#:skip-root? #true
[(_ ...)
#:do [(set! execution-count (add1 execution-count))]
#'LIST])
(check-equal? execution-count 1))
(test-case "syntax-traverse #:skip-root? false"
(define stx #'(a b (c d) e))
(define actual
(syntax-traverse stx
#:skip-root? #false
[(_ ...) #'LIST]))
(check-equal? (syntax->datum actual) 'LIST))
(test-case "syntax-traverse originality"
(define stx (read-syntax #false (open-input-string "(1 2 (a b) 3 4)")))
(check-true (syntax-original? stx))
(define traversed-stx
(syntax-traverse stx
[(_ id:id) (attribute id)]))
(check-equal? (syntax->datum traversed-stx) '(1 2 b 3 4))
(check-false (syntax-original? traversed-stx))
(check-not-equal? (syntax-srcloc traversed-stx) (syntax-srcloc stx))
(define/syntax-parse (1* 2* b* 3* 4*) traversed-stx)
(check-true (syntax-original? #'1*))
(check-true (syntax-original? #'2*))
(check-true (syntax-original? #'b*))
(check-true (syntax-original? #'3*))
(check-true (syntax-original? #'4*))))