auto-syntax-e
auto-with-syntax
auto-syntax
auto-syntax-case
8.12

auto-syntax-e🔗ℹ

Suzanne Soy <racket@suzanne.soy>

 (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 ...)

Like (with-syntax ([patᵢ eᵢ] ...) body ...), but the syntax pattern variables bound by the patᵢ ... can be used outside of syntax patterns (they are implicitly transformed using syntax->datum):

Example:
> (auto-with-syntax ([x #'123])
    (list (add1 x) #'x))

'(124 #<syntax:eval:1:0 123>)

syntax

(auto-syntax (pvarᵢ ...) body ...)

Re-binds the syntax pattern variables pvarᵢ ..., so that can be used outside of syntax patterns like in auto-with-syntax:

Examples:
> (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
Like syntax-case, but the syntax pattern variables bound by the patᵢ ... can be used outside of templates like in auto-with-syntax.