chickadee » scheme » define-syntax

(define-syntax <keyword> <transformer spec>)syntax

<Keyword> is an identifier, and the <transformer spec> should be an instance of syntax-rules. Note that CHICKEN also supports er-macro-transformer and ir-macro-transformer here. For more information see the (chicken syntax) module.

The top-level syntactic environment is extended by binding the <keyword> to the specified transformer.

In standard Scheme, there is no define-syntax analogue of internal definitions in, but CHICKEN allows these as an extension to the standard. This means define-syntax may be used to define local macros that are visible throughout the rest of the body in which the definition occurred, i.e.

 (let ()
   ...
   (define-syntax foo ...)
   (define-syntax bar ...)
   ...)

is expanded into

 (let ()
   ...
   (letrec-syntax ((foo ...) (bar ...))
     ...) )

syntax-rules supports SRFI-46 in allowing the ellipsis identifier to be user-defined by passing it as the first argument to the syntax-rules form. Also, "tail" patterns of the form

 (syntax-rules ()
   ((_ (a b ... c) 
     ...

are supported.

The effect of destructively modifying the s-expression passed to a transformer procedure is undefined.

Although macros may expand into definitions and syntax definitions in any context that permits them, it is an error for a definition or syntax definition to shadow a syntactic keyword whose meaning is needed to determine whether some form in the group of forms that contains the shadowing definition is in fact a definition, or, for internal definitions, is needed to determine the boundary between the group and the expressions that follow the group. For example, the following are errors:

(define define 3)
(begin (define begin list))
(let-syntax
  ((foo (syntax-rules ()
          ((foo (proc args ...) body ...)
           (define proc
             (lambda (args ...)
               body ...))))))
  (let ((x 3))
    (foo (plus x y) (+ x y))
    (define foo x)
    (plus foo x)))