On this page:
stx
define-syntax/  case
define-syntax/  parse
λ/  syntax-parse
define-for-syntax/  case-args
λ/  syntax-case
define/  case-args
23.1 Pattern expanders
~either
~lit
~with
~attr
~optkw
~optkw…
~maybe
23.2 Untyped versions of syntax-parse helpers
stx
define-syntax/  case
define-syntax/  parse
7.7

23 syntax-parse helpers

Georges Dupéron <georges.duperon@gmail.com>

 (require phc-toolkit/syntax-parse) package: phc-toolkit

syntax

stx

This identifier can only be used in the body of some forms, like define-syntax. It is an error to use it as an expression elsewhere.

syntax

(define-syntax/case (name . args) (literal-id ...) . body)

This form is roughly equivalent to:

(define-syntax (name stx)
  (syntax-case stx (literal-id ...)
    [(_ . args) (let () . body)]))

Within body, the syntax parameter stx can be used to refer to the whole syntax given as an argument to name.

syntax

(define-syntax/parse (name . syntax-patterns)
  pattern-directive ... . body)
This form is roughly equivalent to:

(define-syntax (name stx)
  (syntax-parse stx
    [(_ . syntax-patterns) pattern-directive ... . body]))

Within the syntax-patterns, the pattern-directive and the body, the syntax parameter stx can be used to refer to the whole syntax given as an argument to name.

syntax

(λ/syntax-parse (name . syntax-patterns)
  pattern-directive ... . body)
This form is roughly equivalent to:

(λ (stx)
  (syntax-parse stx
    [(_ . syntax-patterns) pattern-directive ... . body]))

Within the syntax-patterns, the pattern-directive and the body, the syntax parameter stx can be used to refer to the whole syntax given as an argument to the function.

syntax

(define-for-syntax/case-args (name (pattern ...)) . body)

This form is roughly equivalent to:

(define-for-syntax (name arg ...)
  (with-syntax ([pattern arg] ...)
    . body))

where each arg is a fresh identifier.

syntax

(λ/syntax-case patterns (literal ...) . body)

This form is roughly equivalent to:

(λ (stx)
  (syntax-case stx (literal ...)
    [(_ . patterns) ... . body]))

Within the patterns, and the body, the syntax parameter stx can be used to refer to the whole syntax given as an argument to the function.

syntax

(define/case-args (name (pattern ...)) . body)

This form is roughly equivalent to:

(define (name arg ...)
  (with-syntax ([pattern arg] ...)
    . body))

where each arg is a fresh identifier.

23.1 Pattern expanders

Georges Dupéron <georges.duperon@gmail.com>

pattern expander

(~either alt ...)

Like ~or, but with no special behaviour when present under ellipses. The use case for this is that ({~or {~and 1 x} {~and 2 x}} ...) would match any list of 1s and 2s in any order, but it complains that the attribute is bound twice, since both alternatives within the ~or are understood as separate patterns, not mutually-exclusive choices. On the other hand ({~either {~and 1 x} {~and 2 x}} ...) still matches (2 1 1 1 2 2 1), and successfully binds all the elements to x ....

pattern expander

(~lit alt ...)

Alias for ~literal.

pattern expander

(~with pat val)

Alias for ~parse, can be used semantically when #:with would have been used in a syntax class definition.

pattern expander

(~attr attr-name val)

Alias for (~bind [attr-name val]), can be used semantically when #:attr would have been used in a syntax class definition.

pattern expander

(~optkw kw pattern ...)

 
  kw : keyword?
A shorthand for:

{~optional {~seq {~and name kw} pattern ...}}

where name is derived from the keyword, so that ~optkw #:foo binds the pattern variable foo.

pattern expander

(~optkw… kw pattern ...)

 
  kw : keyword?
A shorthand for:

(~optional {~seq {~and name kw} pattern ...}
           #:name "the kw keyword")

where the occurrence of "kw" within the string is replaced by the actual kw keywords, and where the name is derived from the keyword, so that ~optkw #:foo binds the pattern variable foo, and uses the name "the #:foo keyword".

This form can only be used where an ellipsis-head pattern is allowed.

pattern expander

(~maybe pattern ...)

A shorthand for:

(~optional {~seq pattern ...})

23.2 Untyped versions of syntax-parse helpers

 (require phc-toolkit/untyped/syntax-parse)
  package: phc-toolkit

syntax

stx

Untyped version of stx.

Untyped version of define-syntax/case.
Untyped version of define-syntax/parse.