-
Notifications
You must be signed in to change notification settings - Fork 11
Parse Tree Specification (ktn_code)
The main function in the ktn_code
module is parse_tree/[1,2]
. This function take Erlang source code and returns a parse tree composed of a root
node and all top level forms as its children. The type for all nodes in this parse tree is the following:
-type tree_node() ::
#{type => tree_node_type(),
attrs => map(),
node_attrs => map(),
content => [tree_node()]}.
As you can see all nodes are simply a map with a fixed number of keys. Some of these keys are present for all nodes (type
and attrs
) while others are optional (node_attrs
and content
). The great advantage of having all nodes maintain the same structure is that it simplifies the mental model needed to manipulate and traverse the tree and its nodes.
Mandatory
-
type
: indicates what does the node represent (e.g.function
,clause
,match
, etc.). For a complete list of possible values for this key please check the section Node Types. -
attrs
: this map holds all associated attributes whose values are not nodes such as:-
location
(i.e.{Line, Column}
) of the node in the original source file. -
text
from the source file. -
name
of the node, which is generally an atom or a string.
-
Optional
-
node_attrs
: this map holds all associated attributes whose values are nodes such as thepatterns
in a (function
orcase
)clause
. -
content
: list of all children nodes.
The node_attrs
was created because of the need of being to traverse all existing nodes, without breaking the parent-child relationship between existing nodes. Previous to the addition of node_attrs
all information related to a node, that wasn't a child or the node's type
, was held in the attrs
map. All attributes turned out to be either a simple value (a tuple, string, integer or atom) or a node, thus it seemed natural to have them aggregated in two different keys. This simplifies a great deal the processing of the nodes in the parse tree, since the difference is explicit.
-type tree_node_type() ::
function | clause | match | tuple
| atom | integer | float | string | char
| binary | binary_element | var
| call | remote
| 'case' | case_expr | case_clauses
| 'fun' | named_fun
| 'query'
| 'try' | try_catch | try_case | try_after
| 'if' | 'catch'
| 'receive' | receive_after | receive_case
| nil | cons
| map | map_field_assoc | map_field_exact
| lc | lc_expr | generate
| bc | bc_expr | b_generate
| op
| record | record_field | record_index
| block
%% Attributes
| module
| type | callback
| export | export_type
| remote_type | type | ann_type | paren_type
| any.