Syntax classes provide an abstraction mechanism for syntax patterns. Built-in syntax classes are supplied that recognize basic classes such as identifier and keyword. Programmers can compose basic syntax classes to build specifications of more complex syntax, such as lists of distinct identifiers and formal arguments with keywords. Macros that manipulate the same syntactic structures can share syntax class definitions.
(define-syntax-class name-id stxclass-option ... stxclass-variant ...+)
(define-syntax-class (name-id . kw-formals) stxclass-option ... stxclass-variant ...+)
stxclass-option = #:attributes (attr-arity-decl ...) | #:auto-nested-attributes | #:description description-expr | #:opaque | #:commit | #:no-delimit-cut | #:literals (literal-entry ...) | #:datum-literals (datum-literal-entry ...) | #:literal-sets (literal-set ...) | #:conventions (convention-id ...) | #:local-conventions (convention-rule ...) | #:disable-colon-notation attr-arity-decl = attr-name-id | (attr-name-id depth) stxclass-variant = (pattern syntax-pattern pattern-directive ...)
description-expr : (or/c string? #f)
A syntax class may have formal parameters, in which case they are bound as variables in the body. Syntax classes support optional arguments and keyword arguments using the same syntax as lambda. The body of the syntax-class definition contains a non-empty sequence of pattern variants.
The following options are supported:
#:attributes (attr-arity-decl ...)
attr-arity-decl = attr-id | (attr-id depth)
Declares the attributes of the syntax class. An attribute arity declaration consists of the attribute name and optionally its ellipsis depth (zero if not explicitly specified).
If the attributes are not explicitly listed, they are inferred as the set of all pattern variables occurring in every variant of the syntax class. Pattern variables that occur at different ellipsis depths are not included, nor are nested attributes from annotated pattern variables.
Deprecated. This option cannot be combined with #:attributes.
Declares the attributes of the syntax class as the set of all pattern variables and nested attributes from annotated pattern variables occurring in every variant of the syntax class. Only syntax classes defined strictly before the enclosing syntax class are used to compute the nested attributes; pattern variables annotated with not-yet-defined syntax classes contribute no nested attributes for export. Note that with this option, reordering syntax-class definitions may change the attributes they export.
The description argument is evaluated in a scope containing the syntax class’s parameters. If the result is a string, it is used in error messages involving the syntax class. For example, if a term is rejected by the syntax class, an error of the form "expected description" may be synthesized. If the result is #f, the syntax class is skipped in the search for a description to report.
If the option is not given, the name of the syntax class is used instead.
Indicates that errors should not be reported with respect to the internal structure of the syntax class.
Directs the syntax class to “commit” to the first successful match. When a variant succeeds, all choice points within the syntax class are discarded. See also ~commit.
By default, a cut (~!) within a syntax class only discards choice points within the syntax class. That is, the body of the syntax class acts as though it is wrapped in a ~delimit-cut form. If #:no-delimit-cut is specified, a cut may affect choice points of the syntax class’s calling context (another syntax class’s patterns or a syntax-parse form).
It is an error to use both #:commit and #:no-delimit-cut.
#:literals (literal-entry ...)
#:datum-literals (datum-literal-entry ...)
#:literal-sets (literal-set ...)
#:conventions (convention-id ...)
Declares the literals and conventions that apply to the syntax class’s variant patterns and their immediate #:with clauses. Patterns occurring within subexpressions of the syntax class (for example, on the right-hand side of a #:fail-when clause) are not affected.
These options have the same meaning as in syntax-parse.
(define-splicing-syntax-class name-id stxclass-option ... stxclass-variant ...+)
(define-splicing-syntax-class (name-id . kw-formals) stxclass-option ... stxclass-variant ...+)
The options are the same as for define-syntax-class.
(pattern syntax-pattern pattern-directive ...)
The attributes of the variant are the attributes of the pattern together with all attributes bound by #:with clauses, including nested attributes produced by syntax classes associated with the pattern variables.
When a transformer is bound to an instance of a struct with this property, then it may be used as a syntax class or splicing syntax class in the same way as the bindings created by define-syntax-class or define-splicing-syntax-class. If the value of the property is an identifier, then it should be bound to a syntax class or splicing syntax class, and the binding will be treated as an alias for the referenced syntax class. If the value of the property is a procedure, then it will be applied to the value with the prop:syntax-class property to obtain an identifier, which will then be used as in the former case.
> (begin-for-syntax (struct expr-and-stxclass (expr-id stxclass-id) #:property prop:procedure (lambda (this stx) ((set!-transformer-procedure (make-variable-like-transformer (expr-and-stxclass-expr-id this))) stx)) #:property prop:syntax-class (lambda (this) (expr-and-stxclass-stxclass-id this)))) > (define-syntax is-id? (expr-and-stxclass #'identifier? #'id)) > (is-id? #'x)
> (syntax-parse #'x [x:is-id? #t] [_ #f])
Added in version 126.96.36.199 of package base.
|pattern-directive||=||#:declare pattern-id stxclass maybe-role|
||||#:with syntax-pattern expr|
||||#:attr attr-arity-decl expr|
||||#:fail-when condition-expr message-expr|
||||#:fail-unless condition-expr message-expr|
||||#:do [def-or-expr ...]|
||||#:undo [def-or-expr ...]|
#:declare pvar-id stxclass maybe-role
stxclass = syntax-class-id | (syntax-class-id arg ...) maybe-role =
| #:role role-expr
Associates pvar-id with a syntax class and possibly a role, equivalent to replacing each occurrence of pvar-id in the pattern with (~var pvar-id stxclass maybe-role). The second form of stxclass allows the use of parameterized syntax classes, which cannot be expressed using the “colon” notation. The args are evaluated in the scope where the pvar-id occurs in the pattern. Keyword arguments are supported, using the same syntax as in #%app.Examples:
> (syntax-parse #'P [x #:declare x id #'x])
> (syntax-parse #'L [x #:with y #'x #:declare x id #'x])
syntax-parse: identifier in #:declare clause does not appear
this #:declare clause affects only the preceding #:with
in: (syntax-parse (syntax L) (x #:with y (syntax x)
#:declare x id (syntax x)))
> (syntax-parse #'T [x #:with y #'x #:declare y id #'x])
Executes the given action pattern as a “post-traversal check” after matching the main pattern. That is, the following are equivalent:
Like #:post except that no ~post wrapper is added. That is, the following are equivalent:
main-pattern #:and action-pattern (~and main-pattern action-pattern)
Evaluates the stx-expr in the context of all previous attribute bindings and matches it against the pattern. If the match succeeds, the pattern’s attributes are added to environment for the evaluation of subsequent side conditions. If the #:with match fails, the matching process backtracks. Since a syntax object may match a pattern in several ways, backtracking may cause the same clause to be tried multiple times before the next clause is reached.
If the value of stx-expr is not a syntax object, it is implicitly converted to a syntax object. If the the conversion would produce 3D syntax—
that is, syntax that contains unwritable values such as procedures, non-prefab structures, etc— then an exception is raised instead.
Equivalent to #:post (~parse syntax-pattern stx-expr).Examples:
Evaluates the expr in the context of all previous attribute bindings and binds it to the given attribute. The value of expr need not be, or even contain, syntax—
see attribute for details.
Equivalent to #:and (~bind attr-arity-decl expr).Examples:
> (syntax-parse #'("do" "mi") [(a b) #:attr rev #'(b a) #'rev])
#<syntax:eval:10:0 ("mi" "do")>
> (syntax-parse #'(1 2) [(a:number b:number) #:attr sum (+ (syntax-e #'a) (syntax-e #'b)) (attribute sum)])
The #:attr directive is often used in syntax classes:Examples:
Evaluates the condition-expr in the context of all previous attribute bindings. If the value is any true value (not #f), the matching process backtracks (with the given message); otherwise, it continues. If the value of the condition expression is a syntax object, it is indicated as the cause of the error.
If the message-expr produces a string it is used as the failure message; otherwise the failure is reported in terms of the enclosing descriptions.
Equivalent to #:post (~fail #:when condition-expr message-expr).Examples:
> (syntax-parse #'(m 4) [(m x:number) #:fail-when (even? (syntax-e #'x)) "expected an odd number" #'x])
m: expected an odd number
at: (m 4)
in: (m 4)
> (syntax-parse #'(m 4) [(m x:number) #:fail-when (and (even? (syntax-e #'x)) #'x) "expected an odd number" #'x])
m: expected an odd number
in: (m 4)
Like #:fail-when with the condition negated.
Equivalent to #:post (~fail #:unless condition-expr message-expr).Example:
Evaluates the condition-expr in the context of all previous attribute bindings. If the value is #f, the matching process backtracks. In other words, #:when is like #:fail-unless without the message argument.
Equivalent to #:post (~fail #:unless condition-expr #f).Example:
Takes a sequence of definitions and expressions, which may be intermixed, and evaluates them in the scope of all previous attribute bindings. The names bound by the definitions are in scope in the expressions of subsequent patterns and clauses.
There is currently no way to bind attributes using a #:do block. It is an error to shadow an attribute binding with a definition in a #:do block.
Has no effect when initially matched, but if backtracking returns to a point before the #:undo directive, the defn-or-exprs are executed. See ~undo for an example.
Eliminates backtracking choice points and commits parsing to the current branch at the current point.
Equivalent to #:and ~!.
An attribute is a name bound by a syntax pattern. An attribute can be a pattern variable itself, or it can be a nested attribute bound by an annotated pattern variable. The name of a nested attribute is computed by concatenating the pattern variable name with the syntax class’s exported attribute’s name, separated by a dot (see the example below).
Attributes can be used in two ways: with the attribute form, and inside syntax templates via syntax, quasisyntax, etc. Attribute names cannot be used directly as expressions; that is, attributes are not variables.
A syntax-valued attribute is an attribute whose value is a syntax object, a syntax list of the appropriate ellipsis depth, or a tree containing promises that when completely forced produces a suitable syntax object or syntax list. Syntax-valued attributes can be used within syntax, quasisyntax, etc as part of a syntax template. If an attribute is used inside a syntax template but it is not syntax-valued, an error is signaled.
The value of an attribute is not required to be syntax. Non-syntax-valued attributes can be used to return a parsed representation of a subterm or the results of an analysis on the subterm. A non-syntax-valued attribute should be bound using the #:attr directive or a ~bind pattern; #:with and ~parse will convert the right-hand side to a (possibly 3D) syntax object.
> (define-syntax-class table (pattern ((key value) ...) #:attr hashtable (for/hash ([k (syntax->datum #'(key ...))] [v (syntax->datum #'(value ...))]) (values k v)) #:attr [sorted-kv 1] (delay (printf "sorting!\n") (sort (syntax->list #'((key value) ...)) < #:key (lambda (kv) (cadr (syntax->datum kv)))))))
The table syntax class provides four attributes: key, value, hashtable, and sorted-kv. The hashtable attribute has ellipsis depth 0 and the rest have depth 1; all but hashtable are syntax-valued. The sorted-kv attribute’s value is a promise; it will be automatically forced if used in a syntax template.
Syntax-valued attributes can be used in syntax templates:
> (syntax-parse #'((a 3) (b 2) (c 1)) [t:table #'(t.key ...)])
#<syntax:eval:19:0 (a b c)>
> (syntax-parse #'((a 3) (b 2) (c 1)) [t:table #'(t.sorted-kv ...)])
#<syntax:eval:20:0 ((c 1) (b 2) (a 3))>
But non-syntax-valued attributes cannot:
> (syntax-parse #'((a 3) (b 2) (c 1)) [t:table #'t.hashtable])
t.hashtable: attribute contains non-syntax value
value: '#hash((a . 3) (b . 2) (c . 1))
Use the attribute form to get the value of an attribute (syntax-valued or not).
> (syntax-parse #'((a 1) (b 2) (c 3)) [t:table (attribute t.hashtable)])
'#hash((a . 1) (c . 3) (b . 2))
> (syntax-parse #'((a 3) (b 2) (c 1)) [t:table (attribute t.sorted-kv)])
Every attribute has an associated ellipsis depth that determines how it can be used in a syntax template (see the discussion of ellipses in syntax). For a pattern variable, the ellipsis depth is the number of ellipses the pattern variable “occurs under” in the pattern. An attribute bound by #:attr has depth 0 unless declared otherwise. For a nested attribute the depth is the sum of the annotated pattern variable’s depth and the depth of the attribute exported by the syntax class.
Consider the following code:
(define-syntax-class quark (pattern (a b ...))) (syntax-parse some-term [(x (y:quark ...) ... z:quark) some-code])
The syntax class quark exports two attributes: a at depth 0 and b at depth 1. The syntax-parse pattern has three pattern variables: x at depth 0, y at depth 2, and z at depth 0. Since y and z are annotated with the quark syntax class, the pattern also binds the following nested attributes: y.a at depth 2, y.b at depth 3, z.a at depth 0, and z.b at depth 1.
An attribute’s ellipsis nesting depth is not a guarantee that it is syntax-valued. In particular, ~or* and ~optional patterns may result in attributes with fewer than expected levels of list nesting, and #:attr and ~bind can be used to bind attributes to arbitrary values.
> (define-syntax-class one (pattern _ #:attr s this-syntax)) > (syntax-parse #'(1 2 3) [(1 o:one _) (attribute o.s)])
> (syntax-parse #'(1 2 3) [(1 . o:one) (attribute o.s)])
'(#<syntax:eval:27:0 2> #<syntax:eval:27:0 3>)
> (define-splicing-syntax-class two (pattern (~seq _ _) #:attr s this-syntax)) > (syntax-parse #'(1 2 3) [(t:two 3) (attribute t.s)])
#<syntax:eval:29:0 (1 2 3)>
> (syntax-parse #'(1 2 3) [(1 t:two) (attribute t.s)])
'(#<syntax:eval:30:0 2> #<syntax:eval:30:0 3>)
Raises an error when used as an expression outside of a syntax-class definition or syntax-parse expression.