Move information on the token associated with a parse node from the .def file into the typed node. (#4001)

Instead of tracking the token associated with a parse node in the `.def`
file macro, track it on the typed node instead. List the token as a
field inside the node structure to show the order of the token relative
to the other components of the grammar production, and to allow the
token index to be accessed when the node is extracted.

Remove the corresponding information from the `.def` file, leaving
behind just a list of parse node kinds in the majority of cases.

This also removes the checking of the token kind associated with a parse
node in the case where the parse node has errors. Previously we had a
flag on the node kind to indicate whether we should check this, but per
[discord
discussion](https://discord.com/channels/655572317891461132/655578254970716160/1246214418979881052),
we have decided to remove this.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
Richard Smith
2024-05-31 23:11:51 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent 5bb318cae6
commit 3c01ee69ed
14 changed files with 921 additions and 927 deletions
+7 -7
View File
@@ -170,12 +170,12 @@ auto Tree::Print(llvm::raw_ostream& output, bool preorder) const -> void {
output << " ]\n";
}
static auto TestExtract(const Tree* tree, NodeId node_id, NodeKind kind,
ErrorBuilder* trace) -> bool {
auto Tree::VerifyExtract(NodeId node_id, NodeKind kind,
ErrorBuilder* trace) const -> bool {
switch (kind) {
#define CARBON_PARSE_NODE_KIND(Name) \
case NodeKind::Name: \
return tree->VerifyExtractAs<Name>(node_id, trace).has_value();
return VerifyExtractAs<Name>(node_id, trace).has_value();
#include "toolchain/parse/node_kind.def"
}
}
@@ -200,12 +200,12 @@ auto Tree::Verify() const -> ErrorOr<Success> {
// Without this code, a 10 mloc test case of lex & parse takes
// 4.129 s ± 0.041 s. With this additional verification, it takes
// 5.768 s ± 0.036 s.
if (!n_impl.has_error && !TestExtract(this, n, n_impl.kind, nullptr)) {
if (!n_impl.has_error && !VerifyExtract(n, n_impl.kind, nullptr)) {
ErrorBuilder trace;
trace << llvm::formatv(
"NodeId #{0} couldn't be extracted as a {1}. Trace:\n", n,
n_impl.kind);
TestExtract(this, n, n_impl.kind, &trace);
VerifyExtract(n, n_impl.kind, &trace);
return trace;
}
@@ -273,10 +273,10 @@ auto Tree::Verify() const -> ErrorOr<Success> {
}
// Validate the roots, ensures Tree::ExtractFile() doesn't CHECK-fail.
if (!TryExtractNodeFromChildren<File>(roots(), nullptr)) {
if (!TryExtractNodeFromChildren<File>(NodeId::Invalid, roots(), nullptr)) {
ErrorBuilder trace;
trace << "Roots of tree couldn't be extracted as a `File`. Trace:\n";
TryExtractNodeFromChildren<File>(roots(), &trace);
TryExtractNodeFromChildren<File>(NodeId::Invalid, roots(), &trace);
return trace;
}