Module merl

Metaprogramming in Erlang.

Copyright © 2010-2013 Richard Carlsson

Authors: Richard Carlsson (carlsson.richard@gmail.com).

Description

Metaprogramming in Erlang.

Data Types

default_action()

default_action() = fun(() -> any())

env()

env() = [{Key::id(), pattern_or_patterns()}]

guard_test()

guard_test() = fun((env()) -> boolean())

guarded_action()

guarded_action() = switch_action() | {guard_test(), switch_action()}

guarded_actions()

guarded_actions() = guarded_action() | [guarded_action()]

id()

id() = atom() | integer()

location()

location() = erl_scan:location()

pattern()

pattern() = tree() | template()

pattern_or_patterns()

pattern_or_patterns() = pattern() | [pattern()]

switch_action()

switch_action() = fun((env()) -> any())

switch_clause()

switch_clause() = {pattern_or_patterns(), guarded_actions()} | {pattern_or_patterns(), guard_test(), switch_action()} | default_action()

template()

template() = tree() | {id()} | {'*', id()} | {template, atom(), term(), [[template()]]}

template_or_templates()

template_or_templates() = template() | [template()]

text()

text() = string() | binary() | [string()] | [binary()]

tree()

tree() = erl_syntax:syntaxTree()

tree_or_trees()

tree_or_trees() = tree() | [tree()]

Function Index

alpha/2Alpha converts a pattern (renames variables).
compile/1Equivalent to compile(Code, []).
compile/2Compile a syntax tree or list of syntax trees representing a module into a binary BEAM object.
compile_and_load/1Equivalent to compile_and_load(Code, []).
compile_and_load/2Compile a syntax tree or list of syntax trees representing a module and load the resulting module into memory.
match/2Match a pattern against a syntax tree (or patterns against syntax trees) returning an environment mapping variable names to subtrees; the environment is always sorted on keys.
meta_template/1Turn a template into a syntax tree representing the template.
print/1Pretty-print a syntax tree or template to the standard output.
qquote/2Parse text and substitute meta-variables.
qquote/3Parse text and substitute meta-variables.
quote/1Parse text.
quote/2Parse text.
show/1Print the structure of a syntax tree or template to the standard output.
subst/2Substitute metavariables in a pattern or list of patterns, yielding a syntax tree or list of trees as result.
switch/2Match against one or more clauses with patterns and optional guards.
template/1Turn a syntax tree or list of trees into a template or templates.
template_vars/1Return an ordered list of the metavariables in the template.
term/1Create a syntax tree for a constant term.
tree/1Revert a template to a normal syntax tree.
tsubst/2Like subst/2, but does not convert the result from a template back to a tree.
var/1Create a variable.

Function Details

alpha/2

alpha(Trees::pattern_or_patterns(), Env::[{id(), id()}]) -> template_or_templates()

Alpha converts a pattern (renames variables). Similar to tsubst/1, but only renames variables (including globs).

See also: tsubst/2.

compile/1

compile(Code) -> any()

Equivalent to compile(Code, []).

compile/2

compile(Code, Options) -> any()

Compile a syntax tree or list of syntax trees representing a module into a binary BEAM object.

See also: compile/1, compile_and_load/2.

compile_and_load/1

compile_and_load(Code) -> any()

Equivalent to compile_and_load(Code, []).

compile_and_load/2

compile_and_load(Code, Options) -> any()

Compile a syntax tree or list of syntax trees representing a module and load the resulting module into memory.

See also: compile/2, compile_and_load/1.

match/2

match(Patterns::pattern_or_patterns(), Trees::tree_or_trees()) -> {ok, env()} | error

Match a pattern against a syntax tree (or patterns against syntax trees) returning an environment mapping variable names to subtrees; the environment is always sorted on keys. Note that multiple occurrences of metavariables in the pattern is not allowed, but is not checked.

See also: switch/2, template/1.

meta_template/1

meta_template(Templates::template_or_templates()) -> tree_or_trees()

Turn a template into a syntax tree representing the template. Meta-variables in the template are turned into normal Erlang variables if their names (after the metavariable prefix characters) begin with an uppercase character. E.g., _@Foo in the template becomes the variable Foo in the meta-template. Furthermore, variables ending with @ are automatically wrapped in a call to merl:term/1, so e.g. _@Foo@ in the template becomes `merl:term(Foo) in the meta-template.

print/1

print(Ts) -> any()

Pretty-print a syntax tree or template to the standard output. This is a utility function for development and debugging.

qquote/2

qquote(Text::text(), Env::env()) -> tree_or_trees()

Equivalent to qquote(1, Text, Env).

Parse text and substitute meta-variables.

qquote/3

qquote(StartPos::location(), Text::text(), Env::env()) -> tree_or_trees()

Parse text and substitute meta-variables. Takes an initial scanner starting position as first argument.

The macro ?Q(Text, Env) expands to merl:qquote(?LINE, Text, Env).

See also: quote/2.

quote/1

quote(Text::text()) -> tree_or_trees()

Equivalent to quote(1, Text).

Parse text.

quote/2

quote(StartPos::location(), Text::text()) -> tree_or_trees()

Parse text. Takes an initial scanner starting position as first argument.

The macro ?Q(Text) expands to merl:quote(?LINE, Text, Env).

See also: quote/1.

show/1

show(Ts) -> any()

Print the structure of a syntax tree or template to the standard output. This is a utility function for development and debugging.

subst/2

subst(Trees::pattern_or_patterns(), Env::env()) -> tree_or_trees()

Substitute metavariables in a pattern or list of patterns, yielding a syntax tree or list of trees as result. Both for normal metavariables and glob metavariables, the substituted value may be a single element or a list of elements. For example, if a list representing 1, 2, 3 is substituted for var in either of [foo, _@var, bar] or [foo, _@var, bar], the result represents [foo, 1, 2, 3, bar].

switch/2

switch(Trees::tree_or_trees(), Cs::[switch_clause()]) -> any()

Match against one or more clauses with patterns and optional guards.

Note that clauses following a default action will be ignored.

See also: match/2.

template/1

template(Trees::pattern_or_patterns()) -> template_or_templates()

Turn a syntax tree or list of trees into a template or templates. Templates can be instantiated or matched against, and reverted back to normal syntax trees using tree/1. If the input is already a template, it is not modified further.

See also: match/2, subst/2, tree/1.

template_vars/1

template_vars(Template::template_or_templates()) -> [id()]

Return an ordered list of the metavariables in the template.

term/1

term(Term::term()) -> tree()

Create a syntax tree for a constant term.

tree/1

tree(Templates::template_or_templates()) -> tree_or_trees()

Revert a template to a normal syntax tree. Any remaining metavariables are turned into @-prefixed atoms or 909-prefixed integers.

See also: template/1.

tsubst/2

tsubst(Trees::pattern_or_patterns(), Env::env()) -> template_or_templates()

Like subst/2, but does not convert the result from a template back to a tree. Useful if you want to do multiple separate substitutions.

See also: subst/2, tree/2.

var/1

var(Name::atom()) -> tree()

Create a variable.


Generated by EDoc