Compare commits

...

2 Commits

Author SHA1 Message Date
3b914466b3 v5.1.0 2026-09-03 07:52:07 -04:00
dc95fe041d Add node_id() to C++ and D tree node handles 2026-09-02 21:00:47 -04:00
7 changed files with 35 additions and 7 deletions

View File

@ -1,3 +1,11 @@
## v5.1.0
### New Features
- Add a `node_id()` accessor to the C++ and D tree node handle types, for node
identity comparison. This matches the existing `p_node_id()` macro (C) and
`node_id()` method (Rust).
## v5.0.0 ## v5.0.0
### New Features ### New Features

View File

@ -149,6 +149,12 @@ public struct <%= @grammar.tree_prefix %>Token<%= @grammar.tree_suffix %>
return __id != 0u; return __id != 0u;
} }
/** Return the node ID (for identity comparison). */
@property <%= @grammar.prefix %>node_id_t node_id()
{
return __id;
}
/** Access the underlying node record (token, pvalue, and user fields). */ /** Access the underlying node record (token, pvalue, and user fields). */
@property ref <%= @grammar.prefix %>node_data_t __node() @property ref <%= @grammar.prefix %>node_data_t __node()
{ {
@ -177,6 +183,12 @@ public struct <%= @grammar.tree_prefix %><%= rule_set.name %><%= @grammar.tree_s
return __id != 0u; return __id != 0u;
} }
/** Return the node ID (for identity comparison). */
@property <%= @grammar.prefix %>node_id_t node_id()
{
return __id;
}
/** Text position of the first code point spanned by this node. */ /** Text position of the first code point spanned by this node. */
@property <%= @grammar.prefix %>position_t position() @property <%= @grammar.prefix %>position_t position()
{ {

View File

@ -349,10 +349,11 @@ accessors on a node handle:
for token payload and user fields), and `p_node_id(node)` (for identity for token payload and user fields), and `p_node_id(node)` (for identity
comparison). comparison).
* C++: handle methods called with `()`, e.g. `node.field()`, `node.valid()`, * C++: handle methods called with `()`, e.g. `node.field()`, `node.valid()`,
`node.position()`, `node.token()`, `node.pvalue()`, and `node.data()`. The `node.position()`, `node.token()`, `node.pvalue()`, `node.data()`, and
C-style functions and macros above are also available. `node.node_id()` (for identity comparison). The C-style functions and
macros above are also available.
* D: `@property` accessors, e.g. `node.field`, `node.valid`, `node.position`, * D: `@property` accessors, e.g. `node.field`, `node.valid`, `node.position`,
`node.token`, `node.pvalue`. `node.token`, `node.pvalue`, and `node.node_id` (for identity comparison).
* Rust: handle methods called with `()`, e.g. `node.field()`, `node.valid()`, * Rust: handle methods called with `()`, e.g. `node.field()`, `node.valid()`,
`node.position()`, `node.end_position()`, `node.n_fields()`, `node.position()`, `node.end_position()`, `node.n_fields()`,
`node.token()`, `node.pvalue()`, `node.data()` (a reference to the node `node.token()`, `node.pvalue()`, `node.data()` (a reference to the node

View File

@ -696,9 +696,10 @@ class Propane
end end
# Generate the C++ tree node handle class declarations for the header. # Generate the C++ tree node handle class declarations for the header.
# Only valid() is defined inline; every other method dereferences the # Only valid() and node_id() are defined inline; every other method
# context, which is still an incomplete type here, so those are declared # dereferences the context, which is still an incomplete type here, so
# and defined out of line once the context is complete. # those are declared and defined out of line once the context is
# complete.
def cpp_tree_handle_types_header def cpp_tree_handle_types_header
p = @grammar.prefix p = @grammar.prefix
out = [] out = []
@ -711,6 +712,7 @@ class Propane
out << " #{p}context_t * __context;" out << " #{p}context_t * __context;"
out << " #{p}node_id_t __id;" out << " #{p}node_id_t __id;"
out << " bool valid() const { return __id != 0u; }" out << " bool valid() const { return __id != 0u; }"
out << " #{p}node_id_t node_id() const { return __id; }"
out << " #{p}node_data_t * data() const;" out << " #{p}node_data_t * data() const;"
out << " #{p}position_t position() const;" out << " #{p}position_t position() const;"
out << " #{p}position_t end_position() const;" out << " #{p}position_t end_position() const;"
@ -726,6 +728,7 @@ class Propane
out << " #{p}context_t * __context;" out << " #{p}context_t * __context;"
out << " #{p}node_id_t __id;" out << " #{p}node_id_t __id;"
out << " bool valid() const { return __id != 0u; }" out << " bool valid() const { return __id != 0u; }"
out << " #{p}node_id_t node_id() const { return __id; }"
out << " #{p}node_data_t * data() const;" out << " #{p}node_data_t * data() const;"
out << " #{p}position_t position() const;" out << " #{p}position_t position() const;"
out << " #{p}position_t end_position() const;" out << " #{p}position_t end_position() const;"

View File

@ -1,3 +1,3 @@
class Propane class Propane
VERSION = "5.0.0" VERSION = "5.1.0"
end end

View File

@ -31,6 +31,7 @@ unittest
assert(start.pR3.valid); assert(start.pR3.valid);
assert(start.pR.valid); assert(start.pR.valid);
assert(start.pR == start.pR3); assert(start.pR == start.pR3);
assert_eq(start.pR.node_id, start.pR3.node_id);
assert_eq(TOKEN_c, start.pR.pToken1.token); assert_eq(TOKEN_c, start.pR.pToken1.token);
p_context_delete(context); p_context_delete(context);

View File

@ -13,6 +13,8 @@ int main()
assert(start.pItems1().valid()); assert(start.pItems1().valid());
assert(start.pItems().valid()); assert(start.pItems().valid());
Items items = start.pItems(); Items items = start.pItems();
assert_ne(0u, items.node_id());
assert_eq(start.pItems().node_id(), items.node_id());
assert(items.pItem().valid()); assert(items.pItem().valid());
assert(items.pItem().pToken1().valid()); assert(items.pItem().pToken1().valid());
assert_eq(TOKEN_a, items.pItem().pToken1().token()); assert_eq(TOKEN_a, items.pItem().pToken1().token());
@ -40,6 +42,7 @@ int main()
assert_eq(P_SUCCESS, p_parse(context)); assert_eq(P_SUCCESS, p_parse(context));
start = p_result(context); start = p_result(context);
assert(!start.pItems().valid()); assert(!start.pItems().valid());
assert_eq(0u, start.pItems().node_id());
p_context_delete(context); p_context_delete(context);