next prev up top content index

10 Macros

Extensible Grammar

There are three kinds of macros: definition macros, which extend the available set of definitions; statement macros, which extend the available set of statements; and function macros, which syntactically resemble function calls but are more flexible. Named value references and local declarations cannot be macro calls. Only statements, function calls, and definitions are extensible.

Definition Macros

A definition macro extends the definition-macro-call production of the Dylan phrase grammar to recognize additional constructs as valid definitions, by creating a new define-body-word that is recognized by the following grammar line:

definition-macro-call:
define modifiersopt define-body-word body-fragmentopt definition-tail

or by creating a new define-list-word that is recognized by the following grammar line:

definition-macro-call:
define modifiersopt define-list-word list-fragmentopt

This allows programmers to extend Dylan by defining new kinds of definitions. The syntax of the definition must be parseable by one of these two predefined grammar rules. The first handles body-style definitions like define class, define method, and define module, while the second handles list-style definitions like define constant. See Appendix A, "BNF," for the details.

The new define-body-word or define-list-word becomes a partially reserved word in each module where the macro definition is visible. In particular a define-body-word or define-list-word cannot be used as a modifier in a definition. It can still be used as a variable-name.

Statement Macros

A statement macro extends the statement production of the Dylan phrase grammar to recognize additional constructs as valid statements by creating a new begin-word that is recognized by the following grammar line:

statement:

begin-word body-fragmentopt end-clause

The new begin-word becomes a reserved word in each module where the macro definition is visible. It can only be used at the beginning and end of this new statement.

Function Macros

A function macro extends the function-macro-call production of the Dylan phrase grammar to recognize additional constructs by creating a new function-word that is recognized by the following grammar line:

function-macro-call:

function-word ( body-fragmentopt )

In addition, a function macro can be invoked by any of the shorthand syntax constructs available for invoking functions. In this case, the arguments are always parsed expression fragments, as described on page 146.

The new function-word becomes a reserved word in each module where the macro definition is visible. It can only be used at the beginning of a macro call.