Dodo doc > Macros and Templates > Advanced Macro


Advanced macro

Syntax

An advanced macro uses the same syntax as a simple macro. The following patterns have special meaning:

PatternMatchesMeaning
$variable a dodo expressiondeclares a variable which gives the value of the expression when used
$Type a dodo type expressiondeclares a type which has the value of the expression when used
\.&reference a variable reference starting with "."declares a reference which references the specified variable
#identifier a variable namedeclares an enum variable which value is the variable name
~pattern a dodo patterndeclares a pattern variable which can be used for pattern matching
\k'variable' the specified variable converted to a string and used as patternallows back references
(?'variable' ... ) pattern inside the bracketsdeclares a variable capturing the content of the brackets
(?'block_var' ... ) a dodo block enclosed in "{" and "}" or ":" and "."declares "block_var" capturing the content of the block
(?'list_var' ... ) a comma-separated listdeclares "list_var" capturing the elements of the list
_ a dodo expressionthe expression is captured when part of a capture group
(?{}) dodo instructionsthe instructions are captured when part of a capture group
(?#comment) no matchinginserts a comment

Declarations originating from the pattern are valid inside the macro block. If they are part of a named capture group they become attributes of this capture group variable.

Whitespace in the pattern matches any whitespace, for example a newline and a tab can match a blank.

A named capture group with a quantifier such as "*" or "?" gives a list variable. A repeated variable name gives a list variable.

A named capture group containing wilcards "_" or "(?{})" is automatically converted to the captured expression or instruction list when used.

Examples:

wrap
/if \( $condition \) $true_choice
   else $false_choice/
{
   condition ->
      true_choice
   |
      false_choice;
}

wrap /match \( $expr \)
   (?'block_match'
      ((?'list_options' ~pattern) (?'block_instr' (?{})))*)/:

   # use block_match.list_options[i] and block_match.block_instr[i]
.
Rationale: the special meaning patterns (some of which are standard) were added to make it easy to extend the dodo syntax. The variables valid outside the pattern help interfacing with the compiler. Variables names in the pattern are not as restricted as other regular expression processors to match dodo variable names.

^ 5.2. Macro Function

v 5.4. Templates