.TH erl_syntax 3erl "syntax_tools 2.1.1" "" "Erlang Module Definition" .SH NAME erl_syntax \- Abstract Erlang syntax trees. .SH DESCRIPTION .LP Abstract Erlang syntax trees\&. .LP This module defines an abstract data type for representing Erlang source code as syntax trees, in a way that is backwards compatible with the data structures created by the Erlang standard library parser module \fIerl_parse\fR\& (often referred to as "parse trees", which is a bit of a misnomer)\&. This means that all \fIerl_parse\fR\& trees are valid abstract syntax trees, but the reverse is not true: abstract syntax trees can in general not be used as input to functions expecting an \fIerl_parse\fR\& tree\&. However, as long as an abstract syntax tree represents a correct Erlang program, the function \fBrevert/1\fR\& should be able to transform it to the corresponding \fIerl_parse\fR\& representation\&. .LP A recommended starting point for the first-time user is the documentation of the \fBsyntaxTree()\fR\& data type, and the function \fBtype/1\fR\&\&. .LP \fINOTES:\fR\& .LP This module deals with the composition and decomposition of \fIsyntactic\fR\& entities (as opposed to semantic ones); its purpose is to hide all direct references to the data structures used to represent these entities\&. With few exceptions, the functions in this module perform no semantic interpretation of their inputs, and in general, the user is assumed to pass type-correct arguments - if this is not done, the effects are not defined\&. .LP With the exception of the \fBerl_parse()\fR\& data structures, the internal representations of abstract syntax trees are subject to change without notice, and should not be documented outside this module\&. Furthermore, we do not give any guarantees on how an abstract syntax tree may or may not be represented, \fIwith the following exceptions\fR\&: no syntax tree is represented by a single atom, such as \fInone\fR\&, by a list constructor \fI[X | Y]\fR\&, or by the empty list \fI[]\fR\&\&. This can be relied on when writing functions that operate on syntax trees\&. .SH "DATA TYPES" .RS 2 .TP 2 .B encoding() = utf8 | unicode | latin1: .TP 2 .B erl_parse() = \fBerl_parse:abstract_clause()\fR\& | \fBerl_parse:abstract_expr()\fR\& | \fBerl_parse:abstract_form()\fR\& | \fBerl_parse:abstract_type()\fR\& | \fBerl_parse:form_info()\fR\& | {bin_element, term(), term(), term(), term()}: .TP 2 .B forms() = \fBsyntaxTree()\fR\& | [\fBsyntaxTree()\fR\&]: .TP 2 .B guard() = none | \fBsyntaxTree()\fR\& | [\fBsyntaxTree()\fR\&] | [[\fBsyntaxTree()\fR\&]]: .TP 2 .B padding() = none | integer(): .TP 2 .B syntaxTree(): .RS 2 .LP An abstract syntax tree\&. The \fBerl_parse()\fR\& "parse tree" representation is a proper subset of the \fIsyntaxTree()\fR\& representation\&. .RE .RS 2 .LP Every abstract syntax tree node has a \fItype\fR\&, given by the function \fBtype/1\fR\&\&. Each node also has associated \fIattributes\fR\&; see \fBget_attrs/1\fR\& for details\&. The functions \fBmake_tree/2\fR\& and \fBsubtrees/1\fR\& are generic constructor/decomposition functions for abstract syntax trees\&. The functions \fBabstract/1\fR\& and \fBconcrete/1\fR\& convert between constant Erlang terms and their syntactic representations\&. The set of syntax tree nodes is extensible through the \fBtree/2\fR\& function\&. .RE .RS 2 .LP A syntax tree can be transformed to the \fBerl_parse()\fR\& representation with the \fBrevert/1\fR\& function\&. .RE .TP 2 .B syntaxTreeAttributes(): .RS 2 .LP This is an abstract representation of syntax tree node attributes; see the function \fBget_attrs/1\fR\&\&. .RE .RE .SH EXPORTS .LP .B abstract(T::term()) -> \fBsyntaxTree()\fR\& .br .RS .LP Returns the syntax tree corresponding to an Erlang term\&. \fITerm\fR\& must be a literal term, i\&.e\&., one that can be represented as a source code literal\&. Thus, it may not contain a process identifier, port, reference, binary or function value as a subterm\&. The function recognises printable strings, in order to get a compact and readable representation\&. Evaluation fails with reason \fIbadarg\fR\& if \fITerm\fR\& is not a literal term\&. .LP \fISee also:\fR\& \fBconcrete/1\fR\&, \fBis_literal/1\fR\&\&. .RE .LP .B add_ann(A::term(), Node::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Appends the term \fIAnnotation\fR\& to the list of user annotations of \fINode\fR\&\&. .LP Note: this is equivalent to \fIset_ann(Node, [Annotation | get_ann(Node)])\fR\&, but potentially more efficient\&. .LP \fISee also:\fR\& \fBget_ann/1\fR\&, \fBset_ann/2\fR\&\&. .RE .LP .B add_postcomments(Cs::[\fBsyntaxTree()\fR\&], Node::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Appends \fIComments\fR\& to the post-comments of \fINode\fR\&\&. .LP Note: This is equivalent to \fIset_postcomments(Node, get_postcomments(Node) ++ Comments)\fR\&, but potentially more efficient\&. .LP \fISee also:\fR\& \fBadd_precomments/2\fR\&, \fBcomment/2\fR\&, \fBget_postcomments/1\fR\&, \fBjoin_comments/2\fR\&, \fBset_postcomments/2\fR\&\&. .RE .LP .B add_precomments(Cs::[\fBsyntaxTree()\fR\&], Node::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Appends \fIComments\fR\& to the pre-comments of \fINode\fR\&\&. .LP Note: This is equivalent to \fIset_precomments(Node, get_precomments(Node) ++ Comments)\fR\&, but potentially more efficient\&. .LP \fISee also:\fR\& \fBadd_postcomments/2\fR\&, \fBcomment/2\fR\&, \fBget_precomments/1\fR\&, \fBjoin_comments/2\fR\&, \fBset_precomments/2\fR\&\&. .RE .LP .B annotated_type(Name::\fBsyntaxTree()\fR\&, Type::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Creates an abstract annotated type expression\&. The result represents "\fIName :: Type\fR\&"\&. .LP \fISee also:\fR\& \fBannotated_type_body/1\fR\&, \fBannotated_type_name/1\fR\&\&. .RE .LP .B annotated_type_body(Node::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Returns the type subtrees of an \fIannotated_type\fR\& node\&. .LP \fISee also:\fR\& \fBannotated_type/2\fR\&\&. .RE .LP .B annotated_type_name(Node::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Returns the name subtree of an \fIannotated_type\fR\& node\&. .LP \fISee also:\fR\& \fBannotated_type/2\fR\&\&. .RE .LP .B application(Operator::\fBsyntaxTree()\fR\&, Arguments::[\fBsyntaxTree()\fR\&]) -> \fBsyntaxTree()\fR\& .br .RS .LP Creates an abstract function application expression\&. If \fIArguments\fR\& is \fI[A1, \&.\&.\&., An]\fR\&, the result represents "\fIOperator(A1, \&.\&.\&., An)\fR\&"\&. .LP \fISee also:\fR\& \fBapplication/3\fR\&, \fBapplication_arguments/1\fR\&, \fBapplication_operator/1\fR\&\&. .RE .LP .B application(Module::none | \fBsyntaxTree()\fR\&, Name::\fBsyntaxTree()\fR\&, Arguments::[\fBsyntaxTree()\fR\&]) -> \fBsyntaxTree()\fR\& .br .RS .LP Creates an abstract function application expression\&. If \fIModule\fR\& is \fInone\fR\&, this is call is equivalent to \fIapplication(Function, Arguments)\fR\&, otherwise it is equivalent to \fIapplication(module_qualifier(Module, Function), Arguments)\fR\&\&. .LP (This is a utility function\&.) .LP \fISee also:\fR\& \fBapplication/2\fR\&, \fBmodule_qualifier/2\fR\&\&. .RE .LP .B application_arguments(Node::\fBsyntaxTree()\fR\&) -> [\fBsyntaxTree()\fR\&] .br .RS .LP Returns the list of argument subtrees of an \fIapplication\fR\& node\&. .LP \fISee also:\fR\& \fBapplication/2\fR\&\&. .RE .LP .B application_operator(Node::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Returns the operator subtree of an \fIapplication\fR\& node\&. .LP Note: if \fINode\fR\& represents "\fIM:F(\&.\&.\&.)\fR\&", then the result is the subtree representing "\fIM:F\fR\&"\&. .LP \fISee also:\fR\& \fBapplication/2\fR\&, \fBmodule_qualifier/2\fR\&\&. .RE .LP .B arity_qualifier(Body::\fBsyntaxTree()\fR\&, Arity::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Creates an abstract arity qualifier\&. The result represents "\fIBody/Arity\fR\&"\&. .LP \fISee also:\fR\& \fBarity_qualifier_argument/1\fR\&, \fBarity_qualifier_body/1\fR\&\&. .RE .LP .B arity_qualifier_argument(Node::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Returns the argument (the arity) subtree of an \fIarity_qualifier\fR\& node\&. .LP \fISee also:\fR\& \fBarity_qualifier/2\fR\&\&. .RE .LP .B arity_qualifier_body(Node::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Returns the body subtree of an \fIarity_qualifier\fR\& node\&. .LP \fISee also:\fR\& \fBarity_qualifier/2\fR\&\&. .RE .LP .B atom(Name::atom() | string()) -> \fBsyntaxTree()\fR\& .br .RS .LP Creates an abstract atom literal\&. The print name of the atom is the character sequence represented by \fIName\fR\&\&. .LP \fISee also:\fR\& \fBatom_literal/1\fR\&, \fBatom_name/1\fR\&, \fBatom_value/1\fR\&, \fBis_atom/2\fR\&\&. .RE .LP .B atom_literal(Node::\fBsyntaxTree()\fR\&) -> string() .br .RS .LP Returns the literal string represented by an \fIatom\fR\& node\&. This includes surrounding single-quote characters if necessary\&. .LP Note that e\&.g\&. the result of \fIatom("x\\ny")\fR\& represents any and all of `x\\ny\&'\&', `x\\12y\&'\&', `x\\012y\&'\&' and `x\\^Jy\\\&'\&'; see \fBstring/1\fR\&\&. .LP \fISee also:\fR\& \fBatom/1\fR\&, \fBstring/1\fR\&\&. .RE .LP .B atom_name(Node::\fBsyntaxTree()\fR\&) -> string() .br .RS .LP Returns the printname of an \fIatom\fR\& node\&. .LP \fISee also:\fR\& \fBatom/1\fR\&\&. .RE .LP .B atom_value(Node::\fBsyntaxTree()\fR\&) -> atom() .br .RS .LP Returns the value represented by an \fIatom\fR\& node\&. .LP \fISee also:\fR\& \fBatom/1\fR\&\&. .RE .LP .B attribute(Name::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Equivalent to \fBattribute(Name, none)\fR\&\&. .RE .LP .B attribute(Name::\fBsyntaxTree()\fR\&, Args::none | [\fBsyntaxTree()\fR\&]) -> \fBsyntaxTree()\fR\& .br .RS .LP Creates an abstract program attribute\&. If \fIArguments\fR\& is \fI[A1, \&.\&.\&., An]\fR\&, the result represents "\fI-Name(A1, \&.\&.\&., An)\&.\fR\&"\&. Otherwise, if \fIArguments\fR\& is \fInone\fR\&, the result represents "\fI-Name\&.\fR\&"\&. The latter form makes it possible to represent preprocessor directives such as "\fI-endif\&.\fR\&"\&. Attributes are source code forms\&. .LP Note: The preprocessor macro definition directive "\fI-define(Name, Body)\&.\fR\&" has relatively few requirements on the syntactical form of \fIBody\fR\& (viewed as a sequence of tokens)\&. The \fItext\fR\& node type can be used for a \fIBody\fR\& that is not a normal Erlang construct\&. .LP \fISee also:\fR\& \fBattribute/1\fR\&, \fBattribute_arguments/1\fR\&, \fBattribute_name/1\fR\&, \fBis_form/1\fR\&, \fBtext/1\fR\&\&. .RE .LP .B attribute_arguments(Node::\fBsyntaxTree()\fR\&) -> none | [\fBsyntaxTree()\fR\&] .br .RS .LP Returns the list of argument subtrees of an \fIattribute\fR\& node, if any\&. If \fINode\fR\& represents "\fI-Name\&.\fR\&", the result is \fInone\fR\&\&. Otherwise, if \fINode\fR\& represents "\fI-Name(E1, \&.\&.\&., En)\&.\fR\&", \fI[E1, \&.\&.\&., E1]\fR\& is returned\&. .LP \fISee also:\fR\& \fBattribute/1\fR\&\&. .RE .LP .B attribute_name(Node::\fBsyntaxTree()\fR\&) -> \fBsyntaxTree()\fR\& .br .RS .LP Returns the name subtree of an \fIattribute\fR\& node\&. .LP \fISee also:\fR\& \fBattribute/1\fR\&\&. .RE .LP .B binary(List::[\fBsyntaxTree()\fR\&]) -> \fBsyntaxTree()\fR\& .br .RS .LP Creates an abstract binary-object template\&. If \fIFields\fR\& is \fI[F1, \&.\&.\&., Fn]\fR\&, the result represents "\fI<>\fR\&"\&. .LP \fISee also:\fR\& \fBbinary_field/2\fR\&, \fBbinary_fields/1\fR\&\&. .RE .LP .B binary_comp(Template::\fBsyntaxTree()\fR\&, Body::[\fBsyntaxTree()\fR\&]) -> \fBsyntaxTree()\fR\& .br .RS .LP Creates an abstract binary comprehension\&. If \fIBody\fR\& is \fI[E1, \&.\&.\&., En]\fR\&, the result represents "\fI<