On this page:
3.2.2.1 Shorthand Macros
@
syntax-matches?
syntax-matcher
syntax-class/  c
syntax-parse/  c
3.2.2.2 Syntax Classes
3.2.2.2.1 Formal Arguments
formals
kw-formals
3.2.2.2.2 For Loops
for-clauses
for-body
3.2.2.2.3 Definition and Expression Bodies
block-body
3.2.2.2.4 Literal Data
self-quoting
module-path
literal
datum-literal
3.2.2.2.5 Identifier Bindings
temp-id
bound-id
static-id
static-binding
struct-binding
struct-binding/  known
struct-binding/  check
3.2.2.3 Literal Sets
define-literals/  ids
require/  define-literals/  ids
8.12
3.2.2 mischief/parse: Tools for syntax-parse🔗ℹ

 (require mischief/parse) package: mischief-dev

3.2.2.1 Shorthand Macros🔗ℹ

syntax

(@ attribute-id)

An alias for attribute.

syntax

(syntax-matches? expr pat ...)

Returns #true if expr produces syntax matched by any of the patterns pat, or #false otherwise.

syntax

(syntax-matcher pat ...)

Produces a predicate that recognizes syntax matched by any of the patterns pat.

syntax

(syntax-class/c class-id)

Produces a contract that accepts syntax belonging to the syntax class named by class-id.

syntax

(syntax-parse/c pat ...)

Produces a contract that accepts syntax matched by any of the patterns pat.

3.2.2.2 Syntax Classes🔗ℹ
3.2.2.2.1 Formal Arguments🔗ℹ

syntax

formals

A syntax class that parses formal arguments as used by #%plain-lambda and case-lambda. The class has 6 attributes: arg-id, rest, rest-id?, rest-id, formal-id, and call.

The attribute arg-id has a depth of 1 and contains all the positional, non-rest formal argument identifiers.

The attribute rest has a depth of 0 and contains the tail of the (possibly improper) list of arguments: either a rest argument or ().

The attribute rest-id? has a depth of 0 and contains the rest argument identifier if one is present, or #false otherwise.

The attribute rest-id has a depth of 1. If there is a rest argument, the attribute contains just that identifier. Otherwise the attribute is empty.

The attribute formal-id has a depth of 1 and contains the result of appending arg-id with rest-id.

The attribute call has a depth of 0. It is bound to #%app if rest-id is empty and apply otherwise.

Examples:
> (define (recursive stx)
    (syntax-parse stx
      [({~literal define} (name:id . args:formals) . _)
       #'(args.call name args.formal-id ...)]))
> (recursive #'(define (print x port) ---etc---))

#<syntax:eval:1:0 (#%app print x port)>

> (recursive #'(define (printf fmt . args) ---etc---))

#<syntax:eval:1:0 (apply printf fmt args)>

A syntax class that parses formal arguments including keyword arguments and optional arguments as used by lambda and define. The class has 13 attributes: req-id, opt-id, opt-expr, req-kw, req-kw-id, opt-kw, opt-kw-id, opt-kw-expr, rest, rest-id?, rest-id, formal-id, and call.

Example:
> (syntax-parse
    '{a b [c "one"] [d "two"] #:w e #:x f #:y [g "three"] #:z [h "four"] . i}
    [args:kw-formals
     (stylish-println
       (list
         (@ args.req-id)
         (@ args.opt-id)
         (@ args.opt-expr)
         (@ args.req-kw)
         (@ args.req-kw-id)
         (@ args.opt-kw)
         (@ args.opt-kw-id)
         (@ args.opt-kw-expr)
         (@ args.rest)
         (@ args.rest-id?)
         (@ args.rest-id)
         (@ args.formal-id)
         (@ args.call)))])

(list

 (list #'a #'b)

 (list #'c #'d)

 (list #'"one" #'"two")

 (list #'#:w #'#:x)

 (list #'e #'f)

 (list #'#:y #'#:z)

 (list #'g #'h)

 (list #'"three" #'"four")

 #'i

 #'i

 (list #'i)

 (list #'a #'b #'e #'f #'c #'d #'g #'h #'i)

 #'keyword-apply #| parse.rkt:161:14 |#)

3.2.2.2.2 For Loops🔗ℹ

A syntax class that parses a sequence of clauses for a for-like macro. Has no attributes.

Examples:
> (define (f x)
    (syntax-parse x
      [c:for-clauses #'c]))
> (f #'{[(k v) (in-dict (hash))] #:when (eq? k v)})

#<syntax:eval:6:0 (((k v) (in-dict (hash))) #:when (eq? k v))>

> (f #'{[(k v) (in-dict (hash))] [k (in-naturals)]})

eval:7:0: ?: duplicate identifier

  at: k

  in: (((k v) (in-dict (hash))) (k (in-naturals)))

  parsing context:

   while parsing for-clauses

    term: (((k v) (in-dict (hash))) (k (in-naturals)))

    location: eval:7:0

> (f #'{[(k v) (in-dict (hash))] #:else "something"})

eval:8:0: ?: expected for-clause

  at: (#:else "something")

  within: (((k v) (in-dict (hash))) #:else "something")

  in: (((k v) (in-dict (hash))) #:else "something")

  parsing context:

   while parsing for-clauses

    term: (((k v) (in-dict (hash))) #:else "something")

    location: eval:8:0

See for-body for a more practical example.

syntax

for-body

A syntax class that parses the body of a for-like macro. Has two attributes: head and tail. The attribute head has a depth of 1 and contains the interleaved definitions, expressions, and break clauses that form most of the body. The attribute tail has a depth of 0 and contains the final expression of the body.

Examples:
> (define-syntax (for/string-set! stx)
    (syntax-parse stx
      [(_ target:expr clauses:for-clauses . body:for-body)
       #'(let {[s target]}
           (for clauses
             body.head ...
             (define-values {i c} body.tail)
             (string-set! s i c)))]))
> (define s (string-copy "fox"))
> (for/string-set! s
      {[i (in-naturals)]
       [c (in-string "abcdefghijklmnopqrstuvwxyz")]}
    #:break (>= i (string-length s))
    (values i c))
> s

"abc"

3.2.2.2.3 Definition and Expression Bodies🔗ℹ

A syntax class that parses the body of a form like lambda or let that accepts definitions an expressions. Equivalent to the pattern (:expr ...+).

3.2.2.2.4 Literal Data🔗ℹ

A syntax class that recognizes self-quoting values. Has no attributes.

A syntax class that recognizes values whose content, produced via syntax->datum, satisfies module-path?. The attribute value stores the content of the parsed syntax.

procedure

(literal x)  syntax-class

  x : any/c
A parametric syntax class that recognizes syntax whose content, produced via syntax->datum, is equal? to x. The attribute value stores the content of the parsed syntax.

procedure

(datum-literal pred desc)  syntax-class

  pred : predicate/c
  desc : string?
A parametric syntax class that recognizes syntax whose content, produced via syntax->datum, satisfies pred. The attribute value stores the content of the parsed syntax.

3.2.2.2.5 Identifier Bindings🔗ℹ

syntax

temp-id

A syntax class that matches anything, and binds the attribute temp to a fresh identifier.

syntax

bound-id

A syntax class that matches identifiers with a module or lexical binding; i.e., identifier-binding does not return #false.

syntax

static-id

A syntax class that matches identifiers bound as syntax; i.e., syntax-local-value does not fail.

procedure

(static-binding pred desc)  syntax-class

  pred : predicate/c
  desc : string?
A syntax class that matches identifiers bound as syntax to values that satisfy the result of pred. Uses scope-static-value in place of syntax-local-value, so this syntax class is sensitive to current-scope. However, it should still work in any context where syntax-local-value would. Has two attributes: value and delta.

The attribute value contains the value that the matched identifier is bound to.

The attribute delta contains a delta syntax introducer based on the parsed identifier. This value is a function that operates on syntax, applying any marks that are present on the parsed identifier that were not present on the original identifier that defined it.

Examples:
> (define-syntax (write-static-string stx)
    (syntax-parse stx
      [(_ (~var s (static-binding string? "a static string variable")))
       (displayln (@ s.value))
       #'(begin)]))
> (define-syntax x "x")
> (write-static-string x)

x

> (define-syntax y 'y)
> (write-static-string y)

eval:17:0: write-static-string: expected a static string

variable, but found an identifier bound as syntax to 'y

  at: y

  in: (write-static-string y)

  parsing context:

   while parsing a static string variable

    term: y

    location: eval:17:0

A syntax class that parses identifiers bound to static information about a structure, i.e., values satisfying struct-info?. Has 8 attributes: value, descriptor-id, constructor-id, predicate-id, accessor-id, mutator-id, super-id, and known-fields?.

The attribute value contains the struct-info? value that the parsed identifier is bound to.

The attributes descriptor-id, constructor-id, predicate-id, accessor-id, mutator-id, and super-id correspond to the fields of the list produced by extract-struct-info. The attributes accessor-id and mutator-id have depth 1, and unlike in extract-struct-info they are proper lists and are not reversed.

The attribute known-fields? contains a boolean that is #true if the attributes accessor-id and mutator-id represent every field of the structure, and #false if there may be missing fields.

A syntax class that parses identifiers bound to static information about a structure, like struct-binding; it further requires that all the information about the structure must be known—none of the attributes may be #false.

procedure

(struct-binding/check [#:all all 
  #:descriptor known-descriptor? 
  #:constructor known-constructor? 
  #:predicate known-predicate? 
  #:fields known-fields? 
  #:super known-super? 
  #:mutable known-mutators?]) 
  syntax-class
  all : boolean? = #false
  known-descriptor? : boolean? = all
  known-constructor? : boolean? = all
  known-predicate? : boolean? = all
  known-fields? : boolean? = all
  known-super? : boolean? = all
  known-mutators? : boolean? = all
A syntax class that parses identifiers bound to static information about a structure, like struct-binding; it further requires that some or all of the information about the structure must be known.

3.2.2.3 Literal Sets🔗ℹ

syntax

(define-literals/ids base-id opt ... [literal-id ...])

Simultaneously defines a literal set named base-id-literals and a list of identifiers named base-id-ids based on the given literal-ids. Passes the given options on to define-literal-set.

syntax

(require/define-literals/ids base-id require-spec)

Requires require-spec and defines both a literal set named base-id-literals and a list of identifiers named base-id-ids based on the imported bindings.