type

Macro

src @Js
const class Macro : Obj

Macro provides a way to replace macro expressions within a pattern using a pluggable implementation for the macro resolution. See apply for macro syntax.

constructors

make

Create a macro for the given pattern.

fields

pattern

The unresolved macro pattern

methods

apply

The pattern text is scanned for keys delimited by {{ and }}.

keys

Get a list of all the macro keys in the order they appear in the macro pattern.

Slot Details

apply

src Str apply(|Str->Str| resolve)

The pattern text is scanned for keys delimited by {{ and }}. The text between the delimiters is the key. The supplied callback is invoked to resolve the key and the macro is replaced with that value in the text. Returns the resulting Str after the macro has been applied. Throws ParseErr if the pattern contains invalid macros.

Macro("{{hello}} {{world}}!").apply { it.upper } => HELLO WORLD!
Macro("{{notTerminated").apply { it.upper } => ParseErr

No lexical restriction is placed on the macro keys. The callback is entirely reponsible for validating keys. For example, all the following are perfectly acceptable keys as far as parsing the macro goes:

  • {{}} - empty key
  • {{ }} - all white space
  • {{ foo }} - leading and trailing white space

keys

src Str[] keys()

Get a list of all the macro keys in the order they appear in the macro pattern. Duplicates are not removed.

Macro("{{hello}} {{world}}! Good-bye {{world}}").keys
   => ["hello", "world", "world"]

make

src new make(Str pattern)

Create a macro for the given pattern.

pattern

src const Str pattern

The unresolved macro pattern