Dodo doc > Macros and Templates > Simple Macro


Simple Macro

Syntax

A simple macro consists of the keyword "wrap" followed by a regular expression and a block for the body of the macro. The regular expression defines the macro and the body contains the substitution. Existing macros are applied to the substitution.

Examples:

wrap /foo/:
   bar.

wrap /kk+/
{
   kk
}
Rationale: macros are used for syntactic sugar and the "wrap" keyword conveys that notion. A regular expression allows to describe pretty complex patterns and can be extended, that will be discussed in the Advanced Macro chapter.

Declaration and use

A macro can be declared in any declaration block, but it is normally declared at the top level of a module.

The scoping rules are the same as other declarations.

In the module itself the macro can be used directly. In other modules it needs to be preceded with the name of the module, unless a "use" statement for this module is in scope.

A macro cannot occur in the middle of an identifier or a literal.

Examples:

use foo_module
def b = foo  # replaced with bar

kk_module kkkkk  #replaced with kk

Note: if instead of a module the macro is declared in a type and this type is polymorphic, the macro only applies to an object of this type if the compiler can tell the object type statically.

Rationale: macros coming from other modules need an explicit module name in front to prevent clashes, and to avoid the compiler going through all macros in all the modules all the time. Since macros are applied at compile time they can rely only on static type, not dynamic type.

^ 5. Macros and Templates

v 5.2. Macro Function