Set up a 'Parse' namespace. (#3161)

Continuing on #3070.

I moved ParseTree::Node to just Parse::Node, versus Parse::Tree::Node.
Other name changes are just removing "Parse" or "Parser" prefixes.

In EnumBase, I'm directly defining operator<< because the ostream.h
approach just isn't working, not for either of Parse::State nor
Parse::NodeKind. Errors look like:

```toolchain/parser/parser_context.cpp:449:34: error: invalid operands to binary expression ('llvm::raw_ostream' and 'const Carbon::Parse::State')
    output << "\t" << i << ".\t" << entry.state;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~
```

The expected template in `Carbon::` is not in the error list; I only see
the:

```
./common/ostream.h:112:6: note: candidate template ignored: requirement 'std::is_base_of_v<std::ostream, llvm::raw_ostream>' was not satisfied [with S = llvm::raw_ostream, T = Carbon::Parse::State]
auto operator<<(S& standard_out, const T& value) -> S& {
     ^
```

I'm still prodding at this, but not seeing an obvious fix.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Jon Ross-PerkinsandRichard Smith authored and GitHub committed 2023-08-28 21:58:38 +00:00
1 parent 64687aa7d1
commit 7157445f97
75 files changed
+1181 -1277

No files matched your search

+30 -34
View File
@@ -13,40 +13,40 @@
#include "toolchain/parser/parse_node_kind.h"
#include "toolchain/parser/parser_context.h"
namespace Carbon {
namespace Carbon::Parse {
auto ParseTree::Parse(TokenizedBuffer& tokens, DiagnosticConsumer& consumer,
llvm::raw_ostream* vlog_stream) -> ParseTree {
auto Tree::Parse(TokenizedBuffer& tokens, DiagnosticConsumer& consumer,
llvm::raw_ostream* vlog_stream) -> Tree {
TokenizedBuffer::TokenLocationTranslator translator(&tokens);
TokenDiagnosticEmitter emitter(translator, consumer);
// Delegate to the parser.
ParseTree tree(tokens);
ParserContext context(tree, tokens, emitter, vlog_stream);
Tree tree(tokens);
Context context(tree, tokens, emitter, vlog_stream);
PrettyStackTraceFunction context_dumper(
[&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
context.PushState(ParserState::DeclarationScopeLoop);
context.PushState(State::DeclarationScopeLoop);
// The package should always be the first token, if it's present. Any other
// use is invalid.
if (context.PositionIs(TokenKind::Package)) {
context.PushState(ParserState::Package);
context.PushState(State::Package);
}
while (!context.state_stack().empty()) {
// clang warns on unhandled enum values; clang-tidy is incorrect here.
// NOLINTNEXTLINE(bugprone-switch-missing-default-case)
switch (context.state_stack().back().state) {
#define CARBON_PARSER_STATE(Name) \
case ParserState::Name: \
ParserHandle##Name(context); \
#define CARBON_PARSE_STATE(Name) \
case State::Name: \
Handle##Name(context); \
break;
#include "toolchain/parser/parser_state.def"
}
}
context.AddLeafNode(ParseNodeKind::FileEnd, *context.position());
context.AddLeafNode(NodeKind::FileEnd, *context.position());
if (auto verify = tree.Verify(); !verify.ok()) {
if (vlog_stream) {
@@ -57,13 +57,12 @@ auto ParseTree::Parse(TokenizedBuffer& tokens, DiagnosticConsumer& consumer,
return tree;
}
auto ParseTree::postorder() const -> llvm::iterator_range<PostorderIterator> {
auto Tree::postorder() const -> llvm::iterator_range<PostorderIterator> {
return {PostorderIterator(Node(0)),
PostorderIterator(Node(node_impls_.size()))};
}
auto ParseTree::postorder(Node n) const
-> llvm::iterator_range<PostorderIterator> {
auto Tree::postorder(Node n) const -> llvm::iterator_range<PostorderIterator> {
CARBON_CHECK(n.is_valid());
// The postorder ends after this node, the root, and begins at the start of
// its subtree.
@@ -73,47 +72,46 @@ auto ParseTree::postorder(Node n) const
PostorderIterator(Node(end_index))};
}
auto ParseTree::children(Node n) const
-> llvm::iterator_range<SiblingIterator> {
auto Tree::children(Node n) const -> llvm::iterator_range<SiblingIterator> {
CARBON_CHECK(n.is_valid());
int end_index = n.index - node_impls_[n.index].subtree_size;
return {SiblingIterator(*this, Node(n.index - 1)),
SiblingIterator(*this, Node(end_index))};
}
auto ParseTree::roots() const -> llvm::iterator_range<SiblingIterator> {
auto Tree::roots() const -> llvm::iterator_range<SiblingIterator> {
return {
SiblingIterator(*this, Node(static_cast<int>(node_impls_.size()) - 1)),
SiblingIterator(*this, Node(-1))};
}
auto ParseTree::node_has_error(Node n) const -> bool {
auto Tree::node_has_error(Node n) const -> bool {
CARBON_CHECK(n.is_valid());
return node_impls_[n.index].has_error;
}
auto ParseTree::node_kind(Node n) const -> ParseNodeKind {
auto Tree::node_kind(Node n) const -> NodeKind {
CARBON_CHECK(n.is_valid());
return node_impls_[n.index].kind;
}
auto ParseTree::node_token(Node n) const -> TokenizedBuffer::Token {
auto Tree::node_token(Node n) const -> TokenizedBuffer::Token {
CARBON_CHECK(n.is_valid());
return node_impls_[n.index].token;
}
auto ParseTree::node_subtree_size(Node n) const -> int32_t {
auto Tree::node_subtree_size(Node n) const -> int32_t {
CARBON_CHECK(n.is_valid());
return node_impls_[n.index].subtree_size;
}
auto ParseTree::GetNodeText(Node n) const -> llvm::StringRef {
auto Tree::GetNodeText(Node n) const -> llvm::StringRef {
CARBON_CHECK(n.is_valid());
return tokens_->GetTokenText(node_impls_[n.index].token);
}
auto ParseTree::PrintNode(llvm::raw_ostream& output, Node n, int depth,
bool preorder) const -> bool {
auto Tree::PrintNode(llvm::raw_ostream& output, Node n, int depth,
bool preorder) const -> bool {
const auto& n_impl = node_impls_[n.index];
output.indent(2 * depth);
output << "{";
@@ -140,7 +138,7 @@ auto ParseTree::PrintNode(llvm::raw_ostream& output, Node n, int depth,
return false;
}
auto ParseTree::Print(llvm::raw_ostream& output) const -> void {
auto Tree::Print(llvm::raw_ostream& output) const -> void {
// Walk the tree just to calculate depths for each node.
llvm::SmallVector<int> indents;
indents.append(size(), 0);
@@ -168,7 +166,7 @@ auto ParseTree::Print(llvm::raw_ostream& output) const -> void {
output << "]\n";
}
auto ParseTree::Print(llvm::raw_ostream& output, bool preorder) const -> void {
auto Tree::Print(llvm::raw_ostream& output, bool preorder) const -> void {
if (!preorder) {
Print(output);
return;
@@ -215,8 +213,8 @@ auto ParseTree::Print(llvm::raw_ostream& output, bool preorder) const -> void {
output << "]\n";
}
auto ParseTree::Verify() const -> ErrorOr<Success> {
llvm::SmallVector<ParseTree::Node> nodes;
auto Tree::Verify() const -> ErrorOr<Success> {
llvm::SmallVector<Node> nodes;
// Traverse the tree in postorder.
for (Node n : postorder()) {
const auto& n_impl = node_impls_[n.index];
@@ -282,7 +280,7 @@ auto ParseTree::Verify() const -> ErrorOr<Success> {
if (!has_errors_ && static_cast<int32_t>(node_impls_.size()) !=
tokens_->expected_parse_tree_size()) {
return Error(
llvm::formatv("ParseTree has {0} nodes and no errors, but "
llvm::formatv("Tree has {0} nodes and no errors, but "
"TokenizedBuffer expected {1} nodes for {2} tokens.",
node_impls_.size(), tokens_->expected_parse_tree_size(),
tokens_->size()));
@@ -290,14 +288,12 @@ auto ParseTree::Verify() const -> ErrorOr<Success> {
return Success();
}
auto ParseTree::PostorderIterator::Print(llvm::raw_ostream& output) const
-> void {
auto Tree::PostorderIterator::Print(llvm::raw_ostream& output) const -> void {
output << node_;
}
auto ParseTree::SiblingIterator::Print(llvm::raw_ostream& output) const
-> void {
auto Tree::SiblingIterator::Print(llvm::raw_ostream& output) const -> void {
output << node_;
}
} // namespace Carbon
} // namespace Carbon::Parse