Store tree nodes in congruent, compact arena array. Define handle types to refer to tree nodes rather than pointers to structure instances. Free tree with context.
2.6 KiB
2.6 KiB
v5.0.0
The generated API for tree generation mode (tree;) has been changed
significantly for this version.
The lexer/parser value APIs for non-tree grammars are unchanged.
Tree memory management
- Remove all calls to
p_tree_delete()/p_tree_delete_XXX(). Tree nodes now live in the parser context and are freed byp_context_delete(). - Tree node handles (returned by
p_result()and the field accessors) are only valid while the context is alive. Do not use them afterp_context_delete().
Tree node field access
Tree nodes are now referenced by handle values instead of pointers, and field access differs per target language:
- C: replace
node->fieldwith the accessor functionp_TYPE_field(node), or use the tree walk macrop_tree_walk_TYPE(node, field1, field2, ...). Replacex != NULL/x == NULLnode checks withp_node_valid(x)/!p_node_valid(x). Read positions withp_node_position(node)/p_node_end_position(node), token payload withp_TYPE_token(node)/p_TYPE_pvalue(node)orp_node_data(node)->field, and compare node identity withp_node_id(a) == p_node_id(b). - C++: replace
node->fieldwith the handle methodnode.field(). Usenode.valid(),node.position(),node.token(),node.pvalue(), andnode.data()->fieldfor user token fields. (The C-style functions and macros above are also available.) - D: replace pointer declarations (
Start * s) with value handles (Start s) and replacex !is null/x is nullwithx.valid/!x.valid. Field access syntax (node.field.field) is otherwise unchanged.
Tree-mode parser rule user code
In tree generation mode $$ and $1, $2, ... now expand to node handles.
Reference child fields through the target-language accessors above (for example
$$->pA->pToken1->pvalue becomes p_tree_walk_Start($$, pA, pToken1, pvalue)
in C, $$.pA().pToken1().pvalue() in C++, and $$.pA.pToken1.pvalue in D).
v4.0.0
API Changes
- Replace any calls to
p_context_init()withp_context_new(). - Replace any references to the address of a statically allocated context
structure with the pointer returned from
p_context_init()(e.g.&context->context). - Add a call to
p_context_delete()(for C or C++) after lexing/parsing to reclaim context memory. - Rename
p_free_tree()calls top_tree_delete(). - Change
free_token_nodestatement calls from taking a function name argument to taking a user code block.
v3.0.0
Grammar Changes
- Rename
ast;statement totree;. - Rename
ast_prefix;statement totree_prefix;. - Rename
ast_suffix;statement totree_suffix;.