auto-syntax-e
(require auto-syntax-e) | package: auto-syntax-e |
This package allows using syntax pattern variables outside of syntax templates: when x is bound as a syntax pattern variable, writing x then becomes roughly equivalent to (syntax->datum #'x-ddd), where x-ddd is x wrapped under the appropriate number of ellipses. If the pattern variable is bound by syntax-parse and contains non-syntax parts (e.g. it was bound within an ~optional clause, or using #:attr), they are left unchanged.
syntax
(auto-with-syntax ([patᵢ eᵢ] ...) body ...)
> (auto-with-syntax ([x #'123]) (list (add1 x) #'x)) '(124 #<syntax:eval:1:0 123>)
syntax
(auto-syntax (pvarᵢ ...) body ...)
> (syntax-parse #'(1 2 3) [(x:nat y:nat ...) (auto-syntax (x y) (list (map add1 (cons x y)) #'(x y ...)))]) '((2 3 4) #<syntax:eval:1:0 (1 2 3)>)
> (syntax-parse #'(1 2 3) [({~seq x:nat {~optional y:nat}} ...) (auto-syntax (x y) (list (map cons x y) (attribute x) (attribute y)))])
'(((1 . 2) (3 . #f))
(#<syntax:eval:2:0 1> #<syntax:eval:2:0 3>)
(#<syntax:eval:2:0 2> #f))
When one of the pvarᵢ ... is not a syntax pattern variable, it is ignored and the existing binding, if any, is left untouched.
Note that it is not necessary to specify the ellipsis-depth of each pvarᵢ.
syntax
(auto-syntax-case stx-expression (literal ...) [patᵢ maybe-guardᵢ bodyᵢ] ...)
maybe-guardᵢ =
| guard-expression