On this page:
1.12.3Reflection
??
?@
top
up

1.12ExperimentalπŸ”— i

The following facilities are experimental.

1.12.1Contracts for Macro Sub-expressionsπŸ”— i

This module is deprecated; it reprovides expr/c for backward compatibility.

1.12.2Contracts for Syntax ClassesπŸ”— i

syntax

[syntax-class-idsyntax-class-contract]...)
syntax-class-contract = (syntax-class/c (mandatory-arg...))
|
(syntax-class/c (mandatory-arg...)
(optional-arg...))
arg = contract-expr
| keywordcontract-expr
contract-expr : contract?
Provides the syntax class (or splicing syntax class) syntax-class-id with the given contracts imposed on its formal parameters.

Keyword recognized by provide-syntax-class/contract .

1.12.3ReflectionπŸ”— i

A syntax class can be reified into a run-time value, and a reified syntax class can be used in a pattern via the ~reflect and ~splicing-reflect pattern forms.

syntax

( reify-syntax-class syntax-class-id)

Reifies the syntax class named syntax-class-id as a run-time value. The same form also handles splicing syntax classes. Syntax classes with the #:no-delimit-cut option cannot be reified.

Returns #t if x is a reified (normal) syntax class or a reified splicing syntax class, respectively.

Returns the reified syntax class’s attributes.

Returns the reified syntax class’s arity and keywords, respectively. Compare with procedure-arity and procedure-keywords .

Partially applies the reified syntax class to the given arguments. If more arguments are given than the reified syntax class accepts, an error is raised.

S-pattern = ....
| (~reflect var-id(reified-exprarg-expr...)maybe-attrs)
H-pattern = ....
|
(~splicing-reflect var-id(reified-exprarg-expr...)
maybe-attrs)

( ~reflect var-id(reified-exprarg-expr...)maybe-attrs)
maybe-attrs =
| #:attributes(attr-arity-decl...)

Like ~var , except that the syntax class position is an expression evaluating to a reified syntax object, not a syntax class name, and the attributes bound by the reified syntax class (if any) must be specified explicitly.

( ~splicing-reflect var-id(reified-exprarg-expr...)maybe-attrs)

Like ~reflect but for reified splicing syntax classes.

Examples:
#:description(format "natural number greater than ~s"x)
#:attributes(diff)
(pattern n:nat
#:when(> (syntax-e #'n)x)
#:withdiff(- (syntax-e #'n)x)))
> (define-syntax-class (nat/multx)
#:description(format "natural number multiple of ~s"x)
#:attributes(quot)
(pattern n:nat
#:when(zero? (remainder (syntax-e #'n)x))
#:withquot(quotient (syntax-e #'n)x)))
> (define r-nat>(reify-syntax-class nat>))
> (define r-nat/mult(reify-syntax-class nat/mult))
> (define (partition/rstxrn)
[((~alt (~reflect yes(rn))no)... )
#'((yes... )(no... ))]))
> (partition/r#'(12345)r-nat>3)

#<syntax:eval:5:0 ((4 5) (1 2 3))>

> (partition/r#'(12345)r-nat/mult2)

#<syntax:eval:5:0 ((2 4) (1 3 5))>

> (define (bad-attrsr)
[(~reflect x(r3)#:attributes(diff))
#'x.diff]))
> (bad-attrsr-nat>)

#<syntax 3>

> (bad-attrsr-nat/mult)

reflect-syntax-class: reified syntax-class is missing

declared attribute `diff'

1.12.4Procedural Splicing Syntax ClassesπŸ”— i

syntax

#:descriptiondescription-expr
#:attributes(attr-arity-decl...)
parser-expr)
Defines a splicing syntax via a procedural parser.

The parser procedure is given two arguments, the syntax to parse and a failure procedure. To signal a successful parse, the parser procedure returns a list of N+1 elements, where N is the number of attributes declared by the splicing syntax class. The first element is the size of the prefix consumed. The rest of the list contains the values of the attributes.

To indicate failure, the parser calls the failure procedure with an optional message argument.

1.12.5Ellipsis-head Alternative SetsπŸ”— i

Unlike single-term patterns and head patterns, ellipsis-head patterns cannot be encapsulated by syntax classes, since they describe not only sets of terms but also repetition constraints.

This module provides ellipsis-head alternative sets, reusable encapsulations of ellipsis-head patterns.

syntax

( define-eh-alternative-set nameeh-alternative...)

alternative = (pattern EH-pattern)
Defines name as an ellipsis-head alternative set. Using name (via ~eh-var ) in an ellipsis-head pattern is equivalent to including each of the alternatives in the pattern via ~alt , except that the attributes bound by the alternatives are prefixed with the name given to ~eh-var .

Unlike syntax classes, ellipsis-head alternative sets must be defined before they are referenced, and they do not delimit cuts (use ~delimit-cut instead).

EH-pattern = ....
| (~eh-var nameeh-alternative-set-id)

( ~eh-var nameeh-alternative-set-id)

Includes the alternatives of eh-alternative-set-id, prefixing their attributes with name.

Examples:
(pattern (~once (~seq #:aa:expr)#:name"#:a option"))
(pattern (~seq #:bb:expr)))
> (define (parse/optionsstx)
[(_ (~eh-var soptions)... )
#'(s.a(s.b... ))]))
> (parse/options#'(m#:a1#:b2#:b3))

#<syntax:eval:12:0 (1 (2 3))>

> (parse/options#'(m#:a1#:a2))

m: too many occurrences of #:a option

at: ()

within: (m #:a 1 #:a 2)

in: (m #:a 1 #:a 2)

> (define (parse/more-optionsstx)
[(_ (~alt (~eh-var soptions)
(~seq #:cc1:exprc2:expr))
... )
#'(s.a(s.b... )((c1c2)... ))]))
> (parse/more-options#'(m#:a1#:b2#:c34#:c56))

#<syntax:eval:15:0 (1 (2) ((3 4) (5 6)))>

(pattern (~eh-var soptions))
(pattern (~seq #:cc1c2)))
> (syntax-parse #'(m#:a1#:b2#:c34#:c56)
[(_ (~eh-var xext-options)... )
#'(x.s.a(x.s.b... )((x.c1x.c2)... ))])

#<syntax:eval:18:0 (1 (2) ((3 4) (5 6)))>

1.12.6Syntax Class SpecializationπŸ”— i

package: base

syntax

( define-syntax-class/specialize headersyntax-class-use)

header = id
| (id. kw-formals)
syntax-class-use = target-stxclass-id
| (target-stxclass-idarg...)
Defines id as a syntax class with the same attributes, options (eg, #:commit, #:no-delimit-cut), and patterns as target-stxclass-id but with the given args supplied.

Examples:
> (define-syntax-class/specialize nat>10(nat>10))
> (syntax-parse #'(1112)[(n:nat>10... )'ok])

'ok

> (syntax-parse #'(89)[(n:nat>10... )'ok])

?: expected natural number greater than 10

at: 8

in: (8 9)

1.12.7Syntax TemplatesπŸ”— i

syntax

( template tmpl)

syntax

( template/loc loc-exprtmpl)

syntax

( quasitemplate tmpl)

syntax

( quasitemplate/loc loc-exprtmpl)

Equivalent to syntax , syntax/loc , quasisyntax , and quasisyntax/loc , respectively.

syntax

( datum-templatetmpl)

Equivalent to datum .

syntax

??

syntax

?@

Equivalent to ~? and ~@ , respectively.

syntax

( define-template-metafunction metafunction-idexpr)

(define-template-metafunction (metafunction-id. formals)body...+)
Defines metafunction-id as a template metafunction. A metafunction application in a syntax or template expression is evaluated by applying the metafunction to the result of processing the “argument” part of the template.

Examples:
[(join(~optional (~seq #:lctxlctx))a:idb:id... )
(datum->syntax (or (attribute lctx)#'a)
(syntax->datum #'(ab... )))))
stx)]))
> (template (joinabc))

#<syntax:eval:23:0 abc>

> (with-syntax ([(x... )#'(abc)])
(template ((x(jointmp-x))... )))

#<syntax:eval:24:0 ((a tmp-a) (b tmp-b) (c tmp-c))>

Metafunctions are useful for performing transformations in contexts where macro expansion does not occur, such as binding occurrences. For example:

(with-syntax ([name#'posn]
[(field... )#'(xy)])
(template (let-values ([((joinname?)
(join#:lctxnamemake-name)
(joinname- field)... )
__))))

'(let-values (((posn? make-posn posn-x posn-y) (make-struct-type ___))) ___)

If join were defined as a macro, it would not be usable in the context above; instead, let-values would report an invalid binding list.

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /