View Source merlin_lib (merlin v2.1.0)
erl_syntax:syntaxTree/0
. Similar to erl_syntax_lib
, but with a different set of helpers, and a preference for returning maps over proplists.
Link to this section Summary
Types
env
- Bindings from the surrounding scope, i.e. a
fun
's closure. bound
- Bindings with value.
free
- Bindings without a value. It's a compile time error to try to access them.
Represents a list of annotations.
erl_syntax:syntaxTree/0
also includes the vanilla AST and dialyser doesn't always approve of that.set
data structures in OTP.Functions
See also: add_bindings/2.
Adds the given bindings to the existing ones. Accepts the same input as get_bindings/1
.
add_new_variable/3
with default prefix and suffix.See also: new_variable/3.
add_new_variable/3
with the given prefix and default suffix.See also: new_variable/3.
new_variable/3
and adds it to the given form or bindings.See also: new_variable/3.
new_variables/3
with default prefix and suffix.See also: new_variable/3.
new_variable/3
with the given prefix and default suffix.See also: new_variable/3.
Total
number of new variables using new_variables/4
and adds it to the given form or bindings.See also: new_variable/3.
Annotates the given form or forms using erl_syntax_lib:annotate_bindings/2
.
Returns the given form with an -export
attribute for the given functions.
-file
attribute in Forms
, or ""
if not found.undefined
if it can't be found.See also: erl_parse:format_error/1.
Callback for formatting error messages from this module.
{badkey, Annotation}
if not found.Form
as a map.Returns the arguments to all attributes with the given name in the given list of forms or subtrees of the given form.
See also: erl_syntax_lib:annotate_bindings/2.
get_annotations/1
, or merlin:ast()
form from which those annotations are taken.Type
Get all bindings associated with the given Form
.
-module
attribute in Forms
, or ''
if not found.-module
attribute in Forms
, or undefined
if not found.new_variable/3
with default prefix and suffix.new_variable/3
with the given prefix and default suffix.Returns a new variable guaranteed not to be in the given bindings, or the bindings associated with the given form.
new_variables/4
with default prefix and suffix.new_variables/4
with the given prefix and default suffix.new_variables/4
with the given prefix and suffix.Total
number of new variables like new_variable/3
.See also: erl_syntax_lib:new_variable_names/3.
Returns the given form without the given annotation.
Returns the given form with the given annotations.
erl_anno
annotations from user once, which means if you set line
or file
, you update the position/location of the form, else you are setting an erl_syntax user annotation.See also: erl_anno, erl_syntax:get_ann/1, erl_syntax:get_pos/1.
Updates the given form using the given groups or subtrees of another form.
Returns the value of the given literal node as an Erlang term.
Link to this section Types
-type binding_type() :: env | bound | free.
env
- Bindings from the surrounding scope, i.e. a
fun
's closure. bound
- Bindings with value.
free
- Bindings without a value. It's a compile time error to try to access them.
-type bindings() :: #{env := ordsets:ordset(variable()), bound := ordsets:ordset(variable()), free := ordsets:ordset(variable())}.
-type bindings_or_form() :: bindings() | merlin:ast().
-type erl_annotation() :: {line, erl_anno:line()} | {column, erl_anno:column()} | {file, file:filename_all()} | {generated, boolean()} | {location, erl_anno:location()} | {record, boolean()} | {text, string()}.
-type erl_annotation_key() :: line | column | file | generated | location | record | text.
-type erl_annotations() :: [erl_annotation()].
Represents a list of annotations.
This is also the internal format oferl_anno
once more then erl_anno:line()
and/or erl_anno:line()
has been set.
-type erl_parse() :: erl_parse:abstract_clause() | erl_parse:abstract_expr() | erl_parse:abstract_form() | erl_parse:abstract_type() | erl_parse:form_info() | erl_parse:af_binelement(term()) | erl_parse:af_generator() | erl_parse:af_remote_function().
-type erl_syntax_ast() :: {tree, any(), any(), any()} | {wrapper, any(), any(), any()}.
erl_syntax:syntaxTree/0
also includes the vanilla AST and dialyser doesn't always approve of that.
set()
of variables.
-type set(T) :: sets:set(T) | ordsets:ordset(T).
set
data structures in OTP.
-type variable() :: atom() | merlin:ast().
Link to this section Functions
-spec add_binding(bindings(), variable()) -> bindings(); (merlin:ast(), variable()) -> erl_syntax_ast().
See also: add_bindings/2.
-spec add_bindings(bindings(), set()) -> bindings(); (merlin:ast(), set()) -> erl_syntax_ast().
Adds the given bindings to the existing ones. Accepts the same input as get_bindings/1
.
When given a form, it updates the bindings on that form, see merlin:annotate/2
for more info.
get_bindings_with_type/1
, it updates the free
and bound
fields as appropriate.
-spec add_new_variable(BindingsOrForm) -> {variable(), BindingsOrForm} when BindingsOrForm :: bindings_or_form().
add_new_variable/3
with default prefix and suffix.See also: new_variable/3.
-spec add_new_variable(BindingsOrForm, string()) -> {variable(), BindingsOrForm} when BindingsOrForm :: bindings_or_form().
add_new_variable/3
with the given prefix and default suffix.See also: new_variable/3.
-spec add_new_variable(BindingsOrForm, string(), string()) -> {variable(), BindingsOrForm} when BindingsOrForm :: bindings_or_form().
new_variable/3
and adds it to the given form or bindings.See also: new_variable/3.
-spec add_new_variables(BindingsOrForm, pos_integer()) -> {[variable()], BindingsOrForm} when BindingsOrForm :: bindings_or_form().
new_variables/3
with default prefix and suffix.See also: new_variable/3.
-spec add_new_variables(BindingsOrForm, pos_integer(), string()) -> {[variable()], BindingsOrForm} when BindingsOrForm :: bindings_or_form().
new_variable/3
with the given prefix and default suffix.See also: new_variable/3.
-spec add_new_variables(BindingsOrForm, pos_integer(), string(), string()) -> {[variable()], BindingsOrForm} when BindingsOrForm :: bindings_or_form().
Total
number of new variables using new_variables/4
and adds it to the given form or bindings.See also: new_variable/3.
-spec annotate_bindings(merlin:ast() | [merlin:ast()]) -> merlin:ast().
Annotates the given form or forms using erl_syntax_lib:annotate_bindings/2
.
If given a form, it returns the same with the annotated bindings. If given a list of forms, a form list is returned instead.
It tries to find theenv
variables from the given form, or first form if given a list of forms. If none can be found it assumes there's no env
variables.
-spec export([merlin:ast()], [{atom(), arity()}]) -> [merlin:ast()].
Returns the given form with an -export
attribute for the given functions.
If the -module
attribute is missing, the -export
is prepended to the given forms.
-export
is inserted just after the -module
. In addition, if the -module
attribute form as an analysis
annotation, i.e. from merlin:annotate/2
, then it is used to avoid re-exporting any functions already exported.See also: annotate_bindings/1, get_bindings/1.
-spec file([merlin:ast()]) -> string().
-file
attribute in Forms
, or ""
if not found.
-spec find_source(module()) -> file:filename() | undefined.
undefined
if it can't be found.
-spec format_error(term()) -> string().
See also: erl_parse:format_error/1.
-spec format_error(Reason, erlang:stacktrace()) -> ErrorInfo when Reason :: term(), ErrorInfo :: #{pos_integer() | general | reason => string()}.
Callback for formatting error messages from this module.
See EEP 54-spec get_annotation(merlin:ast(), atom()) -> term().
{badkey, Annotation}
if not found.
-spec get_annotation(merlin:ast(), atom(), Default) -> Default.
-spec get_annotations(merlin:ast()) -> #{atom() := term()}.
Form
as a map.
-spec get_attribute(merlin:ast() | [merlin:ast()], atom(), term()) -> term().
-spec get_attribute_forms(merlin:ast() | [merlin:ast()], atom()) -> merlin:ast().
-spec get_attributes(merlin:ast() | [merlin:ast()], atom()) -> term().
Returns the arguments to all attributes with the given name in the given list of forms or subtrees of the given form.
Returns the empty list if no such attributes are found.-spec get_binding_type(bindings_or_form(), atom()) -> bound | env | free | unknown.
See also: erl_syntax_lib:annotate_bindings/2.
-spec get_bindings(bindings_or_form()) -> ordsets:ordset(atom()).
get_annotations/1
, or merlin:ast()
form from which those annotations are taken.
-spec get_bindings_by_type(bindings_or_form(), binding_type()) -> ordsets:ordset(atom()).
Type
-spec get_bindings_with_type(bindings_or_form()) -> #{variable() := binding_type()}.
Get all bindings associated with the given Form
.
See also: erl_syntax_lib:annotate_bindings/2.
-spec into_error_marker(Reason, Stacktrace | Node) -> merlin:error_marker() when Reason :: term(), Stacktrace :: [{module(), atom(), arity(), [{atom(), term()}]}], Node :: merlin:ast().
-spec module([merlin:ast()]) -> module() | ''.
-module
attribute in Forms
, or ''
if not found.
-spec module_form([merlin:ast()]) -> merlin:ast() | undefined.
-module
attribute in Forms
, or undefined
if not found.
-spec new_variable(bindings_or_form()) -> variable().
new_variable/3
with default prefix and suffix.
-spec new_variable(bindings_or_form(), string()) -> variable().
new_variable/3
with the given prefix and default suffix.
-spec new_variable(bindings_or_form(), string(), string()) -> variable().
Returns a new variable guaranteed not to be in the given bindings, or the bindings associated with the given form.
If given a set of existing bindings, it will return an atom, if given a form it will return a new variable. That variable will have the generated flag set.
The resulting variable will be on the format Prefix<N>Suffix
, where N
is some small number. Prefix defaults to __Var
, and suffix to __
.
TEST
is set during compilation, the numbers will be deterministically increment from 1, otherwise they are random.See also: erl_syntax_lib:new_variable_name/1.
-spec new_variables(pos_integer()) -> ordsets:ordset(variable()).
new_variables/4
with default prefix and suffix.
-spec new_variables(bindings_or_form(), pos_integer()) -> ordsets:ordset(variable()).
new_variables/4
with the given prefix and default suffix.
-spec new_variables(bindings_or_form(), pos_integer(), string()) -> ordsets:ordset(variable()).
new_variables/4
with the given prefix and suffix.
-spec new_variables(bindings_or_form(), pos_integer(), string(), string()) -> ordsets:ordset(variable()).
Total
number of new variables like new_variable/3
.See also: erl_syntax_lib:new_variable_names/3.
-spec remove_annotation(merlin:ast(), atom()) -> merlin:ast().
Returns the given form without the given annotation.
You may not remove theline
, as it must always be present. You may remove annotations that are not present, if which case the original form is returned.
-spec set_annotation(merlin:ast(), atom(), term()) -> merlin:ast().
-spec set_annotations(merlin:ast(), #{atom() := term()}) -> merlin:ast().
Returns the given form with the given annotations.
These may beerl_parse
annotations, user annotations, or a mix of both. The given annotations overwrite any already present. To preseve existing ones use update_annotations/2
instead.
-spec update_annotations(merlin:ast(), #{atom() := term()}) -> erl_syntax_ast().
erl_anno
annotations from user once, which means if you set line
or file
, you update the position/location of the form, else you are setting an erl_syntax user annotation.See also: erl_anno, erl_syntax:get_ann/1, erl_syntax:get_pos/1.
-spec update_tree(merlin:ast(), [[merlin:ast()]] | merlin:ast()) -> merlin:ast().
Updates the given form using the given groups or subtrees of another form.
This is a generalisation oferl_syntax:update_tree/2
.
-spec value(merlin:ast()) -> term().
Returns the value of the given literal node as an Erlang term.
Raises{badvalue, Node}
if the given Node
is not an literal node.