# `erl_syntax` [🔗](https://github.com/garazdawi/otp/blob/lukas/shell_docs/fix-bugs/lib/syntax_tools/src/erl_syntax.erl#L33) Abstract Erlang syntax trees. 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 `m:erl_parse` (often referred to as "parse trees", which is a bit of a misnomer). This means that all `erl_parse` 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 `erl_parse` 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 `erl_parse` representation. A recommended starting point for the first-time user is the documentation of the [`syntaxTree()`](`t:syntaxTree/0`) data type, and the function `type/1`. > #### Note {: .info } > > This module deals with the composition and decomposition of _syntactic_ 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. With the exception of the [`erl_parse()`](`t:erl_parse/0`) 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, _with the following exceptions_: no syntax tree is represented by a single atom, such as `none`, by a list constructor `[X | Y]`, or by the empty list `[]`. This can be relied on when writing functions that operate on syntax trees. # `annotation_or_location` ```erlang -type annotation_or_location() :: erl_anno:anno() | erl_anno:location(). ``` # `encoding` *not exported* ```erlang -type encoding() :: utf8 | unicode | latin1. ``` # `erl_parse` *not exported* ```erlang -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_generator()] | erl_parse:af_remote_function(). ``` # `forms` ```erlang -type forms() :: syntaxTree() | [syntaxTree()]. ``` # `guard` *not exported* ```erlang -type guard() :: none | syntaxTree() | [syntaxTree()] | [[syntaxTree()]]. ``` # `padding` ```erlang -type padding() :: none | integer(). ``` # `syntaxTree` ```erlang -type syntaxTree() :: tree() | wrapper() | erl_parse(). ``` # `syntaxTreeAttributes` ```erlang -type syntaxTreeAttributes() :: #attr{pos :: term(), ann :: [term()], com :: none | #com{pre :: [syntaxTree()], post :: [syntaxTree()]}}. ``` # `tree` *not exported* ```erlang -type tree() :: #tree{type :: atom(), attr :: #attr{pos :: term(), ann :: [term()], com :: none | #com{pre :: [syntaxTree()], post :: [syntaxTree()]}}, data :: term()}. ``` # `wrapper` *not exported* ```erlang -type wrapper() :: #wrapper{type :: atom(), attr :: #attr{pos :: term(), ann :: [term()], com :: none | #com{pre :: [syntaxTree()], post :: [syntaxTree()]}}, tree :: erl_parse()}. ``` # `` ```erlang -spec nil() -> syntaxTree(). ``` Creates an abstract empty list. The result represents "`[]`". The empty list is traditionally called "nil". _See also: _`is_list_skeleton/1`, `list/2`. # `abstract` ```erlang -spec abstract(term()) -> syntaxTree(). ``` Returns the syntax tree corresponding to an Erlang term. `Term` must be a literal term, meaning one that can be represented as a source code literal. Thus, it must 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 `badarg` if `Term` is not a literal term. _See also: _`concrete/1`, `is_literal/1`. # `add_ann` ```erlang -spec add_ann(term(), syntaxTree()) -> syntaxTree(). ``` Appends the term `Annotation` to the list of user annotations of `Node`. Note: this is equivalent to [`set_ann(Node, [Annotation | get_ann(Node)])`](`set_ann/2`), but potentially more efficient. _See also: _`get_ann/1`, `set_ann/2`. # `add_postcomments` ```erlang -spec add_postcomments([syntaxTree()], syntaxTree()) -> syntaxTree(). ``` Appends `Comments` to the post-comments of `Node`. Note: This is equivalent to [`set_postcomments(Node, get_postcomments(Node) ++ Comments)`](`set_postcomments/2`), but potentially more efficient. _See also: _`add_precomments/2`, `comment/2`, `get_postcomments/1`, `join_comments/2`, `set_postcomments/2`. # `add_precomments` ```erlang -spec add_precomments([syntaxTree()], syntaxTree()) -> syntaxTree(). ``` Appends `Comments` to the pre-comments of `Node`. Note: This is equivalent to [`set_precomments(Node, get_precomments(Node) ++ Comments)`](`set_precomments/2`), but potentially more efficient. _See also: _`add_postcomments/2`, `comment/2`, `get_precomments/1`, `join_comments/2`, `set_precomments/2`. # `annotated_type` ```erlang -spec annotated_type(syntaxTree(), syntaxTree()) -> syntaxTree(). ``` Creates an abstract annotated type expression. The result represents "`Name :: Type`". _See also: _`annotated_type_body/1`, `annotated_type_name/1`. # `annotated_type_body` ```erlang -spec annotated_type_body(syntaxTree()) -> syntaxTree(). ``` Returns the type subtrees of an `annotated_type` node. _See also: _`annotated_type/2`. # `annotated_type_name` ```erlang -spec annotated_type_name(syntaxTree()) -> syntaxTree(). ``` Returns the name subtree of an `annotated_type` node. _See also: _`annotated_type/2`. # `application` ```erlang -spec application(syntaxTree(), [syntaxTree()]) -> syntaxTree(). ``` Creates an abstract function application expression. If `Arguments` is `[A1, ..., An]`, the result represents "`Operator(A1, ..., An)`". _See also: _`application/3`, `application_arguments/1`, `application_operator/1`. # `application` ```erlang -spec application(none | syntaxTree(), syntaxTree(), [syntaxTree()]) -> syntaxTree(). ``` Creates an abstract function application expression. If `Module` is `none`, this is call is equivalent to [`application(Function, Arguments)`](`application/2`), otherwise it is equivalent to [`application(module_qualifier(Module, Function), Arguments)`](`application/2`). (This is a utility function.) _See also: _`application/2`, `module_qualifier/2`. # `application_arguments` ```erlang -spec application_arguments(syntaxTree()) -> [syntaxTree()]. ``` Returns the list of argument subtrees of an `application` node. _See also: _`application/2`. # `application_operator` ```erlang -spec application_operator(syntaxTree()) -> syntaxTree(). ``` Returns the operator subtree of an `application` node. If `Node` represents "`M:F(...)`", then the result is the subtree representing "`M:F`". _See also: _`application/2`, `module_qualifier/2`. # `arity_qualifier` ```erlang -spec arity_qualifier(syntaxTree(), syntaxTree()) -> syntaxTree(). ``` Creates an abstract arity qualifier. The result represents "`Body/Arity`". _See also: _`arity_qualifier_argument/1`, `arity_qualifier_body/1`. # `arity_qualifier_argument` ```erlang -spec arity_qualifier_argument(syntaxTree()) -> syntaxTree(). ``` Returns the argument (the arity) subtree of an `arity_qualifier` node. _See also: _`arity_qualifier/2`. # `arity_qualifier_body` ```erlang -spec arity_qualifier_body(syntaxTree()) -> syntaxTree(). ``` Returns the body subtree of an `arity_qualifier` node. _See also: _`arity_qualifier/2`. # `atom` ```erlang -spec atom(atom() | string()) -> syntaxTree(). ``` Creates an abstract atom literal. The print name of the atom is the character sequence represented by `Name`. _See also: _`atom_literal/1`, `atom_literal/2`, `atom_name/1`, `atom_value/1`, `is_atom/2`. # `atom_literal` ```erlang -spec atom_literal(syntaxTree()) -> string(). ``` Returns the literal string represented by an `atom` node. This includes surrounding single-quote characters if necessary. Characters beyond 255 will be escaped. Note that, for example, the result of [`atom("x\ny")`](`atom/1`) represents any and all of `'x\ny'`, `'x\12y'`, `'x\012y'`, and `'x\^Jy'`; see `string/1`. _See also: _`atom/1`, `string/1`. # `atom_literal` ```erlang -spec atom_literal(syntaxTree(), utf8 | unicode | latin1) -> string(). ``` Returns the literal string represented by an `atom` node. This includes surrounding single-quote characters if necessary. Depending on the encoding a character beyond 255 will be escaped (`latin1`) or copied as is (`utf8`). _See also: _`atom/1`, `atom_literal/1`, `string/1`. # `atom_name` ```erlang -spec atom_name(syntaxTree()) -> string(). ``` Returns the printname of an `atom` node. _See also: _`atom/1`. # `atom_value` ```erlang -spec atom_value(syntaxTree()) -> atom(). ``` Returns the value represented by an `atom` node. _See also: _`atom/1`. # `attribute` ```erlang -spec attribute(syntaxTree()) -> syntaxTree(). ``` # `attribute` ```erlang -spec attribute(syntaxTree(), none | [syntaxTree()]) -> syntaxTree(). ``` Creates an abstract program attribute. If `Arguments` is `[A1, ..., An]`, the result represents "`-Name(A1, ..., An).`". Otherwise, if `Arguments` is `none`, the result represents "`-Name.`". The latter form makes it possible to represent preprocessor directives such as "`-endif.`". Attributes are source code forms. > #### Note {: .info } > > The preprocessor macro definition directive "`-define(Name, Body).`" > has relatively few requirements on the syntactical form of `Body` > (viewed as a sequence of tokens). The `text` node type can be used for > a `Body` that is not a normal Erlang construct. _See also: _`attribute/1`, `attribute_arguments/1`, `attribute_name/1`, `is_form/1`, `text/1`. # `attribute_arguments` ```erlang -spec attribute_arguments(syntaxTree()) -> none | [syntaxTree()]. ``` Returns the list of argument subtrees of an `attribute` node, if any. If `Node` represents "`-Name.`", the result is `none`. Otherwise, if `Node` represents "`-Name(E1, ..., En).`", `[E1, ..., E1]` is returned. _See also: _`attribute/1`. # `attribute_name` ```erlang -spec attribute_name(syntaxTree()) -> syntaxTree(). ``` Returns the name subtree of an `attribute` node. _See also: _`attribute/1`. # `binary` ```erlang -spec binary([syntaxTree()]) -> syntaxTree(). ``` Creates an abstract binary-object template. If `Fields` is `[F1, ..., Fn]`, the result represents "`<>`". _See also: _`binary_field/2`, `binary_fields/1`. # `binary_comp` ```erlang -spec binary_comp(syntaxTree(), [syntaxTree()]) -> syntaxTree(). ``` Creates an abstract binary comprehension. If `Body` is `[E1, ..., En]`, the result represents "`<