Next: Macro Primitives, Previous: Defmacro, Up: Syntax [Contents][Index]
SCM supports [R5RS] syntax-rules
macros
See Macros in Revised(5) Scheme.
The pattern language is extended by the syntax (... <obj>)
, which
is identical to <obj>
except that ellipses in <obj>
are
treated as ordinary identifiers in a template, or as literals in a
pattern. In particular, (... ...)
quotes the ellipsis token
...
in a pattern or template.
For example:
(define-syntax check-tree (syntax-rules () ((_ (?pattern (... ...)) ?obj) (let loop ((obj ?obj)) (or (null? obj) (and (pair? obj) (check-tree ?pattern (car obj)) (loop (cdr obj)))))) ((_ (?first . ?rest) ?obj) (let ((obj ?obj)) (and (pair? obj) (check-tree ?first (car obj)) (check-tree ?rest (cdr obj))))) ((_ ?atom ?obj) #t))) (check-tree ((a b) ...) '((1 2) (3 4) (5 6))) ⇒ #t (check-tree ((a b) ...) '((1 2) (3 4) not-a-2list) ⇒ #f
Note that although the ellipsis is matched as a literal token in the
defined macro it is not included in the literals list for
syntax-rules
.
The pattern language is also extended to support identifier macros. A reference to an identifier macro keyword that is not the first identifier in a form may expand into Scheme code, rather than raising a “keyword as variable” error. The pattern for expansion of such a bare macro keyword is a single identifier, as in other syntax rules the identifier is ignored.
For example:
(define-syntax eight (syntax-rules () (_ 8))) (+ 3 eight) ⇒ 11 (eight) ⇒ ERROR (set! eight 9) ⇒ ERROR