Pattern languageA transformer spec has one of the following forms:
(syntax-rules
({literal} ...) {syntax-rule} ...
)
syntax
(syntax-rules
({ellipsis} {literal} ...) {syntax-rule} ...
)
syntax
_
auxiliary syntax
...
auxiliary syntax
Syntax: It is an error if any of the literals, or the ellipsis in the second form, is not an identifier. It is also an error if syntax rule is not of the form
({pattern} {template})
The pattern in a syntax rule is a list pattern whose first element is an identifier. A pattern is either an identifier, a constant, or one of the following
({pattern} ...)
({pattern} {pattern} ... . {pattern})
({pattern} ... {pattern} {ellipsis} {pattern} ...)
({pattern} ... {pattern} {ellipsis} {pattern} ...
. {pattern})
#({pattern} ...)
#({pattern} ... {pattern} {ellipsis} {pattern} ...)
and a template is either an identifier, a constant, or one of the following
({element} ...)
({element} {element} ... . {template})
({ellipsis} {template})
#({element} ...)
where an element is a template optionally
followed by an ellipsis.
An ellipsis is the identifier specified in the second form
of
Semantics: An instance of
An identifier appearing within a pattern can be an underscore
( The keyword at the beginning of the pattern in a syntax rule is not involved in the matching and is considered neither a pattern variable nor a literal identifier. Pattern variables match arbitrary input elements and are used to refer to elements of the input in the template. It is an error for the same pattern variable to appear more than once in a pattern. Underscores also match arbitrary input elements but are not pattern variables and so cannot be used to refer to those elements. If an underscore appears in the literals list, then that takes precedence and underscores in the pattern match as literals. Multiple underscores can appear in a pattern.
Identifiers that appear in A subpattern followed by ellipsis can match zero or more elements of the input, unless ellipsis appears in the literals, in which case it is matched as a literal. More formally, an input expression E matches a pattern P if and only if:
It is an error to use a macro keyword, within the scope of its binding, in an expression that does not match any of the patterns. When a macro use is transcribed according to the template of the matching syntax rule, pattern variables that occur in the template are replaced by the elements they match in the input. Pattern variables that occur in subpatterns followed by one or more instances of the identifier ellipsis are allowed only in subtemplates that are followed by as many instances of ellipsis. They are replaced in the output by all of the elements they match in the input, distributed as indicated. It is an error if the output cannot be built up as specified.
Identifiers that appear in the template but are not pattern variables
or the identifier
ellipsis are inserted into the output as literal identifiers. If a
literal identifier is inserted as a free identifier then it refers to the
binding of that identifier within whose scope the instance of
A template of the form
(define-syntax be-like-begin
(syntax-rules ()
((be-like-begin name)
(define-syntax name
(syntax-rules ()
((name expr (... ...))
(begin expr (... ...))))))))
(be-like-begin sequence)
(sequence 1 2 3 4) ==> 4
As an example, if
(let ((=> #f))
(cond (#t => 'ok))) ==> ok
The macro transformer for cond recognizes =>
as a local variable, and hence an expression, and not as the
base identifier => , which the macro transformer treats
as a syntactic keyword. Thus the example expands into
(let ((=> #f))
(if #t (begin => 'ok)))
instead of
(let ((=> #f))
(let ((temp #t))
(if temp ('ok temp))))
which would result in an invalid procedure call. |