expandMacro

Expand a macro, and write the result to an OutputRange.

It's the responsability of the caller to ensure that the lexer contains the beginning of a macro. The front of the input should be either a dollar followed an opening parenthesis, or an opening parenthesis.

If the macro does not have a closing parenthesis, input will be exhausted and a DdocException will be thrown.

  1. void expandMacro(Lexer input, string[string] macros, O output)
  2. string expandMacro(Lexer input, string[string] macros)
    string
    expandMacro
    (,
    in string[string] macros
    )

Parameters

input Lexer

A reference to a lexer with front pointing to the macro.

macros string[string]

Additional macros to use, in addition of DDOC's ones.

Examples

import ddoc.lexer : Lexer;
import std.array : appender;

auto macros = [
	"IDENTITY" : "$0", "HWORLD" : "$(IDENTITY Hello world!)",
	"ARGS" : "$(IDENTITY $1 $+)", "GREETINGS" : "$(IDENTITY $(ARGS Hello,$0))",
];

auto l1 = Lexer(`$(HWORLD)`);
immutable r1 = expandMacro(l1, macros);
assert(r1 == "Hello world!", r1);

auto l2 = Lexer(`$(B $(IDENTITY $(GREETINGS John Malkovich)))`);
immutable r2 = expandMacro(l2, macros);
assert(r2 == "<b>Hello John Malkovich</b>", r2);

A simple example, with recursive macros:

import ddoc.lexer : Lexer;

auto lex = Lexer(`$(MYTEST Un,jour,mon,prince,viendra)`);
auto macros = [`MYTEST` : `$1 $(MYTEST $+)`];
// Note: There's also a version of expand that takes an OutputRange.
immutable result = expand(lex, macros);
assert(result == `Un jour mon prince viendra `, result);

Meta