|
22 | 22 | [property-hashes-into-syntax-property-bundle |
23 | 23 | (reducer/c (entry/c syntax-path? immutable-hash?) syntax-property-bundle?)] |
24 | 24 | [syntax-add-all-properties (-> syntax? syntax-property-bundle? syntax?)] |
25 | | - [syntax-immediate-properties (-> syntax? syntax-property-bundle?)] |
26 | | - [syntax-all-properties (-> syntax? syntax-property-bundle?)])) |
| 25 | + [syntax-immediate-properties (->* (syntax?) (#:base-path syntax-path?) syntax-property-bundle?)] |
| 26 | + [syntax-all-properties (->* (syntax?) (#:base-path syntax-path?) syntax-property-bundle?)])) |
27 | 27 |
|
28 | 28 |
|
29 | 29 | (require guard |
|
246 | 246 | (syntax-set stx path new-subform)) |
247 | 247 |
|
248 | 248 |
|
249 | | -(define (syntax-immediate-properties stx) |
| 249 | +(define (syntax-immediate-properties stx #:base-path [base-path empty-syntax-path]) |
250 | 250 | (define keys (syntax-property-symbol-keys stx)) |
251 | | - (sequence->syntax-property-bundle |
252 | | - (for/list ([key (in-list keys)]) |
253 | | - (define value (syntax-property stx key)) |
254 | | - (syntax-property-entry empty-syntax-path key value)))) |
| 251 | + (transduce keys |
| 252 | + (mapping (λ (key) |
| 253 | + (define value (syntax-property stx key)) |
| 254 | + (syntax-property-entry base-path key value))) |
| 255 | + #:into into-syntax-property-bundle)) |
255 | 256 |
|
256 | 257 |
|
257 | | -(define (syntax-all-properties stx) |
258 | | - (sequence->syntax-property-bundle |
259 | | - (for*/list ([path (in-syntax-paths stx)] |
260 | | - #:do [(define subform (syntax-ref stx path))] |
261 | | - [key (in-list (syntax-property-symbol-keys subform))]) |
262 | | - (define value (syntax-property subform key)) |
263 | | - (syntax-property-entry path key value)))) |
| 258 | +(define (syntax-all-properties stx #:base-path [base-path empty-syntax-path]) |
| 259 | + (transduce (in-syntax-paths stx #:base-path base-path) |
| 260 | + (append-mapping |
| 261 | + (λ (path) |
| 262 | + (define subform (syntax-ref stx (syntax-path-remove-prefix path base-path))) |
| 263 | + (define keys (syntax-property-symbol-keys subform)) |
| 264 | + (for/list ([key (in-list keys)]) |
| 265 | + (define value (syntax-property subform key)) |
| 266 | + (syntax-property-entry path key value)))) |
| 267 | + #:into into-syntax-property-bundle)) |
264 | 268 |
|
265 | 269 |
|
266 | 270 | (module+ test |
|
382 | 386 | (check-equal? (hash-ref b-props 'inner-prop) 42) |
383 | 387 |
|
384 | 388 | (define c-props (syntax-property-bundle-get-immediate-properties props (syntax-path (list 1 1)))) |
385 | | - (check-equal? (hash-ref c-props 'inner-prop) 99)))) |
| 389 | + (check-equal? (hash-ref c-props 'inner-prop) 99)) |
| 390 | + |
| 391 | + (test-case "with base-path parameter" |
| 392 | + (define stx (syntax-property #'(a b c) 'root-prop 42)) |
| 393 | + (define base (syntax-path (list 5 10))) |
| 394 | + (define props (syntax-immediate-properties stx #:base-path base)) |
| 395 | + (check-true (syntax-property-bundle? props)) |
| 396 | + (define base-props (syntax-property-bundle-get-immediate-properties props base)) |
| 397 | + (check-equal? (hash-ref base-props 'root-prop) 42)) |
| 398 | + |
| 399 | + (test-case "syntax-all-properties with base-path" |
| 400 | + (define a-stx (syntax-property #'a 'a-prop 1)) |
| 401 | + (define b-stx (syntax-property #'b 'b-prop 2)) |
| 402 | + (define stx (datum->syntax #f (list a-stx b-stx) #f)) |
| 403 | + (define base (syntax-path (list 3 7))) |
| 404 | + (define props (syntax-all-properties stx #:base-path base)) |
| 405 | + |
| 406 | + (define a-props (syntax-property-bundle-get-immediate-properties props (syntax-path (list 3 7 0)))) |
| 407 | + (check-equal? (hash-ref a-props 'a-prop) 1) |
| 408 | + |
| 409 | + (define b-props (syntax-property-bundle-get-immediate-properties props (syntax-path (list 3 7 1)))) |
| 410 | + (check-equal? (hash-ref b-props 'b-prop) 2)))) |
0 commit comments