Skip to content

Commit 78428df

Browse files
Copilotjackfirth
andcommitted
Use transduce with into-syntax-property-bundle and add base-path parameter
Co-authored-by: jackfirth <[email protected]>
1 parent b3ed43e commit 78428df

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

private/syntax-property-bundle.rkt

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
[property-hashes-into-syntax-property-bundle
2323
(reducer/c (entry/c syntax-path? immutable-hash?) syntax-property-bundle?)]
2424
[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?)]))
2727

2828

2929
(require guard
@@ -246,21 +246,25 @@
246246
(syntax-set stx path new-subform))
247247

248248

249-
(define (syntax-immediate-properties stx)
249+
(define (syntax-immediate-properties stx #:base-path [base-path empty-syntax-path])
250250
(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))
255256

256257

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))
264268

265269

266270
(module+ test
@@ -382,4 +386,25 @@
382386
(check-equal? (hash-ref b-props 'inner-prop) 42)
383387

384388
(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

Comments
 (0)