4 Commits
Author SHA1 Message Date
josh 66af46a4f6 v5.2.0 2026-09-05 18:17:10 -04:00
josh 429ce88eb5 Add cast-to-bool operators for tree node handles for C++ and D 2026-09-05 17:56:18 -04:00
josh 3b914466b3 v5.1.0 2026-09-03 07:52:07 -04:00
josh dc95fe041d Add node_id() to C++ and D tree node handles 2026-09-02 21:00:47 -04:00
10 changed files with 133 additions and 7 deletions

No files matched your search

+18
View File
@@ -1,3 +1,21 @@
## v5.2.0
### New Features
- Tree node handles can now be used directly as a boolean condition (e.g.
`if (node)`) in C++ (an `explicit operator bool()`) and D (an
`opCast!bool`), equivalent to testing `valid()` / `valid`. C and Rust do not
support a user-defined conversion to a boolean condition; use
`p_node_valid(node)` and `node.valid()` there.
## 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
### New Features
+24
View File
@@ -149,6 +149,18 @@ public struct <%= @grammar.tree_prefix %>Token<%= @grammar.tree_suffix %>
return __id != 0u;
}
/** Support using this handle directly as a boolean condition. */
bool opCast(T : bool)()
{
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). */
@property ref <%= @grammar.prefix %>node_data_t __node()
{
@@ -177,6 +189,18 @@ public struct <%= @grammar.tree_prefix %><%= rule_set.name %><%= @grammar.tree_s
return __id != 0u;
}
/** Support using this handle directly as a boolean condition. */
bool opCast(T : bool)()
{
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. */
@property <%= @grammar.prefix %>position_t position()
{
+18 -3
View File
@@ -349,16 +349,31 @@ accessors on a node handle:
for token payload and user fields), and `p_node_id(node)` (for identity
comparison).
* C++: handle methods called with `()`, e.g. `node.field()`, `node.valid()`,
`node.position()`, `node.token()`, `node.pvalue()`, and `node.data()`. The
C-style functions and macros above are also available.
`node.position()`, `node.token()`, `node.pvalue()`, `node.data()`, and
`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`,
`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()`,
`node.position()`, `node.end_position()`, `node.n_fields()`,
`node.token()`, `node.pvalue()`, `node.data()` (a reference to the node
record, for token user fields), and `node.node_id()` (for identity
comparison).
In C++ and D, a node handle can also be used directly as a boolean condition,
which is equivalent to testing `valid()` (C++) or `valid` (D):
```
if (node)
{
/* node refers to a valid (non-null) tree node. */
}
```
For C, use the `p_node_valid(node)` macro, and for Rust use the `node.valid()`
method; neither language supports a user-defined conversion to a boolean
condition.
The positional position expansions (`${$.position}`, `${N.position}`, etc...)
are not available in tree generation mode; use the position accessors above
instead.
+8 -3
View File
@@ -696,9 +696,10 @@ class Propane
end
# Generate the C++ tree node handle class declarations for the header.
# Only valid() is defined inline; every other method dereferences the
# context, which is still an incomplete type here, so those are declared
# and defined out of line once the context is complete.
# Only valid() and node_id() are defined inline; every other method
# dereferences the context, which is still an incomplete type here, so
# those are declared and defined out of line once the context is
# complete.
def cpp_tree_handle_types_header
p = @grammar.prefix
out = []
@@ -711,6 +712,8 @@ class Propane
out << " #{p}context_t * __context;"
out << " #{p}node_id_t __id;"
out << " bool valid() const { return __id != 0u; }"
out << " explicit operator bool() const { return __id != 0u; }"
out << " #{p}node_id_t node_id() const { return __id; }"
out << " #{p}node_data_t * data() const;"
out << " #{p}position_t position() const;"
out << " #{p}position_t end_position() const;"
@@ -726,6 +729,8 @@ class Propane
out << " #{p}context_t * __context;"
out << " #{p}node_id_t __id;"
out << " bool valid() const { return __id != 0u; }"
out << " explicit operator bool() const { return __id != 0u; }"
out << " #{p}node_id_t node_id() const { return __id; }"
out << " #{p}node_data_t * data() const;"
out << " #{p}position_t position() const;"
out << " #{p}position_t end_position() const;"
+1 -1
View File
@@ -1,3 +1,3 @@
class Propane
VERSION = "5.0.0"
VERSION = "5.2.0"
end
+1
View File
@@ -31,6 +31,7 @@ unittest
assert(start.pR3.valid);
assert(start.pR.valid);
assert(start.pR == start.pR3);
assert_eq(start.pR.node_id, start.pR3.node_id);
assert_eq(TOKEN_c, start.pR.pToken1.token);
p_context_delete(context);
+2
View File
@@ -17,6 +17,8 @@ int main()
assert(p_node_valid(p_tree_walk_Items(items, pItem, pToken1)));
assert_eq(TOKEN_a, p_tree_walk_Items(items, pItem, pToken1, token));
assert_eq(11, p_tree_walk_Items(items, pItem, pToken1, pvalue));
/* A token node handle from an unmatched rule is invalid. */
assert(!p_node_valid(p_tree_walk_Items(items, pItem, pDual, pOne1, pToken1)));
assert(p_node_valid(p_Items_pItemsMore(items)));
ItemsMore itemsmore = p_Items_pItemsMore(items);
assert(p_node_valid(p_ItemsMore_pItem(itemsmore)));
+31
View File
@@ -12,11 +12,35 @@ int main()
Start start = p_result(context);
assert(start.pItems1().valid());
assert(start.pItems().valid());
// A rule node handle is usable directly as a boolean condition.
assert(start);
assert(start.pItems());
if (!start.pItems())
{
return 1;
}
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().pToken1().valid());
assert_eq(TOKEN_a, items.pItem().pToken1().token());
assert_eq(11, items.pItem().pToken1().pvalue());
// A token node handle is usable directly as a boolean condition.
Token token = items.pItem().pToken1();
assert(token);
if (!token)
{
return 1;
}
// An invalid token node handle converts to false.
Token invalid_token = items.pItem().pDual().pOne1().pToken1();
assert(!invalid_token.valid());
assert(!invalid_token);
if (invalid_token)
{
return 1;
}
assert(items.pItemsMore().valid());
ItemsMore itemsmore = items.pItemsMore();
assert(itemsmore.pItem().valid());
@@ -40,6 +64,13 @@ int main()
assert_eq(P_SUCCESS, p_parse(context));
start = p_result(context);
assert(!start.pItems().valid());
// An invalid rule node handle converts to false.
assert(!start.pItems());
if (start.pItems())
{
return 1;
}
assert_eq(0u, start.pItems().node_id());
p_context_delete(context);
+28
View File
@@ -15,11 +15,33 @@ unittest
Start start = p_result(context);
assert(start.pItems1.valid);
assert(start.pItems.valid);
// A rule node handle is usable directly as a boolean condition.
assert(start);
assert(start.pItems);
if (!start.pItems)
{
assert(false);
}
Items items = start.pItems;
assert(items.pItem.valid);
assert(items.pItem.pToken1.valid);
assert_eq(TOKEN_a, items.pItem.pToken1.token);
assert_eq(11, items.pItem.pToken1.pvalue);
// A token node handle is usable directly as a boolean condition.
Token token = items.pItem.pToken1;
assert(token);
if (!token)
{
assert(false);
}
// An invalid token node handle converts to false.
Token invalidToken = items.pItem.pDual.pOne1.pToken1;
assert(!invalidToken.valid);
assert(!invalidToken);
if (invalidToken)
{
assert(false);
}
assert(items.pItemsMore.valid);
ItemsMore itemsmore = items.pItemsMore;
assert(itemsmore.pItem.valid);
@@ -43,6 +65,12 @@ unittest
assert_eq(P_SUCCESS, p_parse(context));
start = p_result(context);
assert(!start.pItems.valid);
// An invalid rule node handle converts to false.
assert(!start.pItems);
if (start.pItems)
{
assert(false);
}
p_context_delete(context);
+2
View File
@@ -13,6 +13,8 @@ fn main() {
assert!(items.pItem().pToken1().valid());
assert_eq!(TOKEN_a, items.pItem().pToken1().token());
assert_eq!(11, items.pItem().pToken1().pvalue());
/* A token node handle from an unmatched rule is invalid. */
assert!(!items.pItem().pDual().pOne1().pToken1().valid());
let itemsmore = items.pItemsMore();
assert!(itemsmore.pItem().pItem().pItem().pToken1().valid());
assert_eq!(TOKEN_b, itemsmore.pItem().pItem().pItem().pToken1().token());