.TH erl_syntax 3erl "syntax_tools 1.6.8" "" "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 \fIerl_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 \fIerl_parse() = parse_tree() (see module erl_parse)\fR\&: .RS 2 .LP The "parse tree" representation built by the Erlang standard library parser \fIerl_parse\fR\&\&. This is a subset of the \fBsyntaxTree\fR\& type\&. .RE .TP 2 .B \fIforms() = syntaxTree() | [syntaxTree()]\fR\&: .TP 2 .B \fIsyntaxTree()\fR\&: .RS 2 .LP An abstract syntax tree\&. The \fIerl_parse\fR\& "parse tree" representation is a 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 \fIerl_parse\fR\& representation with the \fBrevert/1\fR\& function\&. .RE .TP 2 .B \fIsyntaxTreeAttributes()\fR\&: .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(Term::term()) -> syntaxTree() .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(Annotation::term(), Node::syntaxTree()) -> syntaxTree() .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(Comments::[syntaxTree()], Node::syntaxTree()) -> syntaxTree() .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(Comments::[syntaxTree()], Node::syntaxTree()) -> syntaxTree() .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 application(Operator::syntaxTree(), Arguments::[syntaxTree()]) -> syntaxTree() .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, Function::syntaxTree(), Arguments::[syntaxTree()]) -> syntaxTree() .br .RS .LP Types: .RS 3 Module = none | syntaxTree() .br .RE .RE .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::syntaxTree()) -> [syntaxTree()] .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::syntaxTree()) -> syntaxTree() .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::syntaxTree(), Arity::syntaxTree()) -> syntaxTree() .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::syntaxTree()) -> syntaxTree() .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::syntaxTree()) -> syntaxTree() .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) -> syntaxTree() .br .RS .LP Types: .RS 3 Name = atom() | string() .br .RE .RE .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::syntaxTree()) -> 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 \fI\&'x\\ny\&'\fR\&, \fI\&'x\\12y\&'\fR\&, \fI\&'x\\012y\&'\fR\& and \fI\&'x\\^Jy\\\&'\fR\&; cf\&. \fIstring/1\fR\&\&. .LP \fISee also:\fR\& \fBatom/1\fR\&, \fBstring/1\fR\&\&. .RE .LP .B atom_name(Node::syntaxTree()) -> 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::syntaxTree()) -> 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) -> syntaxTree() .br .RS .LP Equivalent to \fBattribute(Name, none)\fR\&\&. .RE .LP .B attribute(Name::syntaxTree(), Args::Arguments) -> syntaxTree() .br .RS .LP Types: .RS 3 Arguments = none | [syntaxTree()] .br .RE .RE .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::syntaxTree()) -> none | [syntaxTree()] .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::syntaxTree()) -> syntaxTree() .br .RS .LP Returns the name subtree of an \fIattribute\fR\& node\&. .LP \fISee also:\fR\& \fBattribute/1\fR\&\&. .RE .LP .B binary(Fields::[syntaxTree()]) -> syntaxTree() .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::syntaxTree(), Body::[syntaxTree()]) -> syntaxTree() .br .RS .LP Creates an abstract binary comprehension\&. If \fIBody\fR\& is \fI[E1, \&.\&.\&., En]\fR\&, the result represents "\fI<