.TH erl_syntax 3erl "syntax_tools 3.0.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 revert/1 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 syntaxTree() data type, and the function type/1\&. .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 erl_parse() 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 annotation_or_location() = erl_anno:anno() | erl_anno:location(): .TP 2 .B encoding() = utf8 | unicode | latin1: .TP 2 .B 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(): .TP 2 .B forms() = syntaxTree() | [syntaxTree()]: .TP 2 .B guard() = none | syntaxTree() | [syntaxTree()] | [[syntaxTree()]]: .TP 2 .B padding() = none | integer(): .TP 2 .B syntaxTree() = tree() | wrapper() | erl_parse(): .TP 2 .B syntaxTreeAttributes() = #attr{pos=term(), ann=[term()], com=none | #com{pre=[syntaxTree()], post=[syntaxTree()]}}: .TP 2 .B tree() = #tree{type=atom(), attr=#attr{pos=term(), ann=[term()], com=none | #com{pre=[syntaxTree()], post=[syntaxTree()]}}, data=term()}: .TP 2 .B wrapper() = #wrapper{type=atom(), attr=#attr{pos=term(), ann=[term()], com=none | #com{pre=[syntaxTree()], post=[syntaxTree()]}}, tree=erl_parse()}: .RE .SH EXPORTS .LP .B abstract(T::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 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\& concrete/1, is_literal/1\&. .RE .LP .B add_ann(A::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\& get_ann/1, set_ann/2\&. .RE .LP .B add_postcomments(Cs::[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\& add_precomments/2, comment/2, get_postcomments/1, join_comments/2, set_postcomments/2\&. .RE .LP .B add_precomments(Cs::[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\& add_postcomments/2, comment/2, get_precomments/1, join_comments/2, set_precomments/2\&. .RE .LP .B annotated_type(Name::syntaxTree(), Type::syntaxTree()) -> syntaxTree() .br .RS .LP Creates an abstract annotated type expression\&. The result represents "\fIName :: Type\fR\&"\&. .LP \fISee also:\fR\& annotated_type_body/1, annotated_type_name/1\&. .RE .LP .B annotated_type_body(Node::syntaxTree()) -> syntaxTree() .br .RS .LP Returns the type subtrees of an \fIannotated_type\fR\& node\&. .LP \fISee also:\fR\& annotated_type/2\&. .RE .LP .B annotated_type_name(Node::syntaxTree()) -> syntaxTree() .br .RS .LP Returns the name subtree of an \fIannotated_type\fR\& node\&. .LP \fISee also:\fR\& annotated_type/2\&. .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\& application/3, application_arguments/1, application_operator/1\&. .RE .LP .B application(Module::none | syntaxTree(), Name::syntaxTree(), Arguments::[syntaxTree()]) -> syntaxTree() .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\& application/2, module_qualifier/2\&. .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\& application/2\&. .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\& application/2, module_qualifier/2\&. .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\& arity_qualifier_argument/1, arity_qualifier_body/1\&. .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\& arity_qualifier/2\&. .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\& arity_qualifier/2\&. .RE .LP .B atom(Name::atom() | string()) -> syntaxTree() .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\& atom_literal/1, atom_literal/2, atom_name/1, atom_value/1, is_atom/2\&. .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\&. Characters beyond 255 will be escaped\&. .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 string/1\&. .LP \fISee also:\fR\& atom/1, string/1\&. .RE .LP .B atom_literal(Node, X2) -> term() .br .RS .LP Returns the literal string represented by an \fIatom\fR\& node\&. This includes surrounding single-quote characters if necessary\&. Depending on the encoding a character beyond 255 will be escaped (\fIlatin1\fR\&) or copied as is (\fIutf8\fR\&)\&. .LP \fISee also:\fR\& atom/1, atom_literal/1, string/1\&. .RE .LP .B atom_name(Node::syntaxTree()) -> string() .br .RS .LP Returns the printname of an \fIatom\fR\& node\&. .LP \fISee also:\fR\& atom/1\&. .RE .LP .B atom_value(Node::syntaxTree()) -> atom() .br .RS .LP Returns the value represented by an \fIatom\fR\& node\&. .LP \fISee also:\fR\& atom/1\&. .RE .LP .B attribute(Name::syntaxTree()) -> syntaxTree() .br .RS .LP Equivalent to attribute(Name, none)\&. .RE .LP .B attribute(Name::syntaxTree(), Args::none | [syntaxTree()]) -> syntaxTree() .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\& attribute/1, attribute_arguments/1, attribute_name/1, is_form/1, text/1\&. .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\& attribute/1\&. .RE .LP .B attribute_name(Node::syntaxTree()) -> syntaxTree() .br .RS .LP Returns the name subtree of an \fIattribute\fR\& node\&. .LP \fISee also:\fR\& attribute/1\&. .RE .LP .B binary(List::[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\& binary_field/2, binary_fields/1\&. .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<