Move child count and bracketing information for parse nodes into the node kind definition. (#4000)

Instead of tracking the bracketing and child count information in the
kind macro in `node_kind.def`, provide it to `NodeKind::Define` in
`typed_nodes.h`. If a node is both bracketed and has a fixed child
count, track both facts and check them both in tree verification, since
it's easy to do so now.

The overall goal here is to reduce `node_kind.def` down to a simple list
of names. I have a slightly different approach in mind for the token
kinds.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Carbon Infra Bot <carbon-external-infra@google.com>
This commit is contained in:
Richard Smith
2024-05-29 20:57:51 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins Carbon Infra Bot
parent 512583d744
commit 419d2e39d8
5 changed files with 475 additions and 385 deletions
+11
View File
@@ -211,6 +211,7 @@ auto Tree::Verify() const -> ErrorOr<Success> {
int subtree_size = 1;
if (n_impl.kind.has_bracket()) {
int child_count = 0;
while (true) {
if (nodes.empty()) {
return Error(
@@ -220,7 +221,17 @@ auto Tree::Verify() const -> ErrorOr<Success> {
}
auto child_impl = node_impls_[nodes.pop_back_val().index];
subtree_size += child_impl.subtree_size;
++child_count;
if (n_impl.kind.bracket() == child_impl.kind) {
// If there's a bracketing node and a child count, verify the child
// count too.
if (n_impl.kind.has_child_count() &&
child_count != n_impl.kind.child_count()) {
return Error(llvm::formatv(
"NodeId #{0} is a {1} with child_count {2}, but encountered "
"{3} nodes before we reached the bracketing node.",
n, n_impl.kind, n_impl.kind.child_count(), child_count));
}
break;
}
}