mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
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:
co-authored by
Jon Ross-Perkins
Carbon Infra Bot
parent
512583d744
commit
419d2e39d8
@@ -39,55 +39,17 @@ CARBON_DEFINE_ENUM_CLASS_NAMES(NodeKind) = {
|
||||
#include "toolchain/parse/node_kind.def"
|
||||
};
|
||||
|
||||
auto NodeKind::has_bracket() const -> bool {
|
||||
static constexpr bool HasBracket[] = {
|
||||
#define CARBON_PARSE_NODE_KIND_BRACKET(...) true,
|
||||
#define CARBON_PARSE_NODE_KIND_CHILD_COUNT(...) false,
|
||||
#include "toolchain/parse/node_kind.def"
|
||||
};
|
||||
return HasBracket[AsInt()];
|
||||
}
|
||||
|
||||
auto NodeKind::bracket() const -> NodeKind {
|
||||
// Nodes are never self-bracketed, so we use that for nodes that instead set
|
||||
// child_count.
|
||||
static constexpr NodeKind Bracket[] = {
|
||||
#define CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName, ...) \
|
||||
NodeKind::BracketName,
|
||||
#define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) NodeKind::Name,
|
||||
#include "toolchain/parse/node_kind.def"
|
||||
};
|
||||
auto bracket = Bracket[AsInt()];
|
||||
CARBON_CHECK(bracket != *this) << *this;
|
||||
return bracket;
|
||||
}
|
||||
|
||||
auto NodeKind::child_count() const -> int32_t {
|
||||
static constexpr int32_t ChildCount[] = {
|
||||
#define CARBON_PARSE_NODE_KIND_BRACKET(...) -1,
|
||||
#define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size, ...) Size,
|
||||
#include "toolchain/parse/node_kind.def"
|
||||
};
|
||||
auto child_count = ChildCount[AsInt()];
|
||||
CARBON_CHECK(child_count >= 0) << *this;
|
||||
return child_count;
|
||||
}
|
||||
|
||||
auto NodeKind::CheckMatchesTokenKind(Lex::TokenKind token_kind, bool has_error)
|
||||
-> void {
|
||||
static constexpr Lex::TokenKind TokenIfValid[] = {
|
||||
#define CARBON_IF_VALID(LexTokenKind) LexTokenKind
|
||||
#define CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName, LexTokenKind) \
|
||||
Lex::TokenKind::LexTokenKind,
|
||||
#define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size, LexTokenKind) \
|
||||
#define CARBON_PARSE_NODE_KIND_WITH_TOKEN(Name, LexTokenKind) \
|
||||
Lex::TokenKind::LexTokenKind,
|
||||
#include "toolchain/parse/node_kind.def"
|
||||
};
|
||||
static constexpr Lex::TokenKind TokenIfError[] = {
|
||||
#define CARBON_IF_VALID(LexTokenKind) Error
|
||||
#define CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName, LexTokenKind) \
|
||||
Lex::TokenKind::LexTokenKind,
|
||||
#define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size, LexTokenKind) \
|
||||
#define CARBON_PARSE_NODE_KIND_WITH_TOKEN(Name, LexTokenKind) \
|
||||
Lex::TokenKind::LexTokenKind,
|
||||
#include "toolchain/parse/node_kind.def"
|
||||
};
|
||||
@@ -102,6 +64,20 @@ auto NodeKind::CheckMatchesTokenKind(Lex::TokenKind token_kind, bool has_error)
|
||||
<< ", but expected token kind " << expected_token_kind;
|
||||
}
|
||||
|
||||
auto NodeKind::has_bracket() const -> bool {
|
||||
return definition().has_bracket();
|
||||
}
|
||||
|
||||
auto NodeKind::bracket() const -> NodeKind { return definition().bracket(); }
|
||||
|
||||
auto NodeKind::has_child_count() const -> bool {
|
||||
return definition().has_child_count();
|
||||
}
|
||||
|
||||
auto NodeKind::child_count() const -> int32_t {
|
||||
return definition().child_count();
|
||||
}
|
||||
|
||||
auto NodeKind::category() const -> NodeCategory {
|
||||
return definition().category();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user