Macro Lambda Lists (original) (raw)

ANSI Common Lisp 3 Evaluation and Compilation 3.4 Lambda Lists

3.4.4 Macro Lambda Lists

A macro lambda list is used in describing _macros_defined by the operators in the next figure.

Operators that use Macro Lambda Lists

define-compiler-macro defmacro macrolet
define-setf-expander

With the additional restriction that an environment parameter may appear only once (at any of the positions indicated), a macro lambda list has the following syntax:

reqvars::= {var | pattern}
optvars::= [&optional {var | ({var pattern} _[_init-form [supplied-p-parameter _]_])}*]
restvar::= [{&rest | &body} ]{var pattern}
keyvars::= [&key {var | ({var (keyword-name{var pattern} )} _[_init-form [supplied-p-parameter _]_])}*
[&allow-other-keys]]
auxvars::= [&aux {var | (var [ init-form ])}*]
envvar::= [&environment _var_]

A macro lambda list can contain the lambda list keywords shown in the next figure.

Lambda List Keywords used by Macro Lambda Lists

&allow-other-keys &environment &rest
&aux &key &whole
&body &optional

Optional parameters (introduced by &optional) and_keyword parameters_ (introduced by &key) can be supplied in a macro lambda list, just as in an ordinary lambda list. Both may contain default initialization forms and supplied-p parameters.

&bodyis identical in function to &rest, but it can be used to inform certain output-formatting and editing functions that the remainder of the form is treated as a body, and should be indented accordingly. Only one of &body or &rest can be used at any particular level; see Section 3.4.4.1 Destructuring by Lambda Lists.&body can appear at any level of a macro lambda list; for details, see Section 3.4.4.1 Destructuring by Lambda Lists.

&wholeis followed by a single variable that is bound to the entire macro-call form; this is the value that the _macro function_receives as its first argument. If &whole and a following variable appear, they must appear first in lambda-list, before any other parameter or lambda list keyword.&whole can appear at any level of a macro lambda list. At inner levels, the &whole variable is bound to the corresponding part of the argument, as with &rest, but unlike &rest, other arguments are also allowed. The use of &whole does not affect the pattern of arguments specified.

&environmentis followed by a single variable that is bound to an environment representing the lexical environment in which the macro call is to be interpreted. This environmentshould be used withmacro-function,get-setf-expansion,compiler-macro-function, andmacroexpand(for example) in computing the expansion of the macro, to ensure that anylexical bindings or definitions established in the compilation environment are taken into account.&environment can only appear at the top level of a_macro lambda list, and can only appear once, but can appear anywhere in that list; the &environment parameter is bound along with &wholebefore any other variables in the lambda list, regardless of where&environment appears in the lambda list. The object that is bound to theenvironment parameter_ has dynamic extent.

Destructuring allows a macro lambda list to express the structure of a macro call syntax. If no lambda list keywords appear, then the macro lambda list is a _tree_containing parameter names at the leaves. The pattern and the macro form must have compatible tree structure; that is, their tree structure must be equivalent, or it must differ only in that some leaves of the pattern match non-atomic objects of the macro form. For information about error detection in this situation, see Section 3.5.1.7 Destructuring Mismatch.

A destructuring lambda list(whether at top level or embedded) can be dotted, ending in a parameter name. This situation is treated exactly as if the parameter name that ends the list had appeared preceded by &rest.

It is permissible for a macro form (or a subexpression of a_macro_ form) to be a _dotted list_only when (... &rest var) or (... . var) is used to match it. It is the responsibility of the macro to recognize and deal with such situations.

3.4.4.1 Destructuring by Lambda Lists