Files
carbon-lang/parser/parse_tree.cpp
T
Chandler CarruthandJon Meow 3512c2218f Merge parser library from the toolchain repository. (#214)
Only change is to update the path to the fuzzer build extension.

Original main commit message:

> Add an initial parser library. (#30)
>
> This library builds a parse tree, very similar to a concrete syntax
> tree. There are no semantics here, simply introducing the basic
> syntactic structure.
>
> The current focus has been on the APIs and the data structures used to
> represent the parse tree, and not on the actual code doing the
> parsing. The code doing the parsing tries to be reasonably efficient
> and reasonably easy to understand recursive descent parser. But there
> is likely much that can be done to improve this code path. A notable
> area where very little thought has been given yet are emitting good
> diagnostics and doing good recovery in the event of parse errors.
>
> Also, this code does not try to match the current under-discussion
> grammar closely. It is only partial and reflects discussions from some
> time ago. It should be updated incrementally to reflect the current
> expected grammar.
>
> The data structure used for the parse tree is unusual. The first
> constraint is that there is a precise one-to-one correspondence
> between the tokens produced by the lexer and the nodes in the parse
> tree. Every token results in exactly one node. In that way, the parse
> tree can be thought of as merely shaping the token stream into a tree.
>
> Each node is also represented with a fixed set of data that is densely
> packed. Combined with the exact relationship to tokens, this allows us
> to fully allocate the parse tree's storage, and to use a dense array
> rather than a pointer-based tree structure.
>
> The tree structure itself is implicitly defined by tracking the size
> of each subtree rooted at a particular node. See the code comments for
> more details (and I'm happy to add more comments where necessary). The
> goal is to minimize both the allocations (one), the working set size
> of the tree as a whole, and optimize common iteration patterns. The
> tree is stored in postorder. This allows depth-first postorder
> iteration as well as topological iteration by walking in reverse.
>
> Building the parse tree in postorder is a natural consequence of the
> grammar being LR rather than LL, which is a consequence of supporting
> infix operators.
>
> As with the Lexer, the parser supports an API for operating on the
> parse tree, as well as the ability to print the tree in both
> a human-readable and machine-readable format (YAML-based). It includes
> significant unit tests and a fuzz tester. The fuzzer's corpus will be
> in a follow-up commit.
>
> This is the largest chunk of code already written by several of us
> prior to open sourcing. (There are a few more pieces, but they are
> significantly smaller and less interesting.) If there are major things
> that folks would like to see happen here, it may make sense to move
> them into issues for tracking. I have tried to update the code to
> follow the style guidelines, but apologies if I missed anything, just
> let me know. We also have issues #19 and #29 to track things that
> already came up with the lexer.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2020-12-08 01:52:43 -08:00

195 lines
6.4 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "parser/parse_tree.h"
#include <cstdlib>
#include "lexer/token_kind.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/iterator.h"
#include "llvm/Support/raw_ostream.h"
#include "parser/parse_node_kind.h"
#include "parser/parser_impl.h"
namespace Carbon {
auto ParseTree::Parse(TokenizedBuffer& tokens, DiagnosticEmitter& emitter)
-> ParseTree {
// Delegate to the parser.
return Parser::Parse(tokens, emitter);
}
auto ParseTree::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> {
// The postorder ends after this node, the root, and begins at the start of
// its subtree.
int end_index = n.index + 1;
int start_index = end_index - node_impls[n.index].subtree_size;
return {PostorderIterator(Node(start_index)),
PostorderIterator(Node(end_index))};
}
auto ParseTree::Children(Node n) const
-> llvm::iterator_range<SiblingIterator> {
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> {
return {SiblingIterator(*this, Node(static_cast<int>(node_impls.size()) - 1)),
SiblingIterator(*this, Node(-1))};
}
auto ParseTree::HasErrorInNode(Node n) const -> bool {
return node_impls[n.index].has_error;
}
auto ParseTree::GetNodeKind(Node n) const -> ParseNodeKind {
return node_impls[n.index].kind;
}
auto ParseTree::GetNodeToken(Node n) const -> TokenizedBuffer::Token {
return node_impls[n.index].token;
}
auto ParseTree::GetNodeText(Node n) const -> llvm::StringRef {
return tokens->GetTokenText(node_impls[n.index].token);
}
auto ParseTree::Print(llvm::raw_ostream& output) const -> void {
output << "[\n";
// The parse tree is stored in postorder, but the most natural order to
// visualize is preorder. This is a tree, so the preorder can be constructed
// by reversing the order of each level of siblings within an RPO. The sibling
// iterators are directly built around RPO and so can be used with a stack to
// produce preorder.
// The roots, like siblings, are in RPO (so reversed), but we add them in
// order here because we'll pop off the stack effectively reversing then.
llvm::SmallVector<std::pair<Node, int>, 16> node_stack;
for (Node n : Roots())
node_stack.push_back({n, 0});
while (!node_stack.empty()) {
Node n;
int depth;
std::tie(n, depth) = node_stack.pop_back_val();
auto& n_impl = node_impls[n.GetIndex()];
for (int unused_indent : llvm::seq(0, depth)) {
(void)unused_indent;
output << " ";
}
output << "{node_index: " << n.index << ", kind: '" << n_impl.kind.GetName()
<< "', text: '" << tokens->GetTokenText(n_impl.token) << "'";
if (n_impl.has_error)
output << ", has_error: yes";
if (n_impl.subtree_size > 1) {
output << ", subtree_size: " << n_impl.subtree_size;
// Has children, so we descend.
output << ", children: [\n";
// We append the children in order here as well because they will get
// reversed when popped off the stack.
for (Node sibling_n : Children(n))
node_stack.push_back({sibling_n, depth + 1});
continue;
}
// This node is finished, so close it up.
assert(n_impl.subtree_size == 1 &&
"Subtree size must always be a positive integer!");
output << "}";
int next_depth = node_stack.empty() ? 0 : node_stack.back().second;
assert(next_depth <= depth && "Cannot have the next depth increase!");
for (int close_children_count : llvm::seq(0, depth - next_depth)) {
(void)close_children_count;
output << "]}";
}
// We always end with a comma and a new line as we'll move to the next node
// at whatever the current level ends up being.
output << ",\n";
}
output << "]\n";
}
auto ParseTree::Verify() const -> bool {
// Verify basic tree structure invariants.
llvm::SmallVector<ParseTree::Node, 16> ancestors;
for (Node n : llvm::reverse(Postorder())) {
auto& n_impl = node_impls[n.GetIndex()];
if (n_impl.has_error && !has_errors) {
llvm::errs()
<< "Node #" << n.GetIndex()
<< " has errors, but the tree is not marked as having any.\n";
return false;
}
if (n_impl.subtree_size > 1) {
if (!ancestors.empty()) {
auto parent_n = ancestors.back();
auto& parent_n_impl = node_impls[parent_n.GetIndex()];
int end_index = n.GetIndex() - n_impl.subtree_size;
int parent_end_index = parent_n.GetIndex() - parent_n_impl.subtree_size;
if (parent_end_index > end_index) {
llvm::errs() << "Node #" << n.GetIndex() << " has a subtree size of "
<< n_impl.subtree_size
<< " which extends beyond its parent's (node #"
<< parent_n.GetIndex() << ") subtree (size "
<< parent_n_impl.subtree_size << ")\n";
return false;
}
}
// Has children, so we descend.
ancestors.push_back(n);
continue;
}
if (n_impl.subtree_size < 1) {
llvm::errs() << "Node #" << n.GetIndex()
<< " has an invalid subtree size of " << n_impl.subtree_size
<< "!\n";
return false;
}
// We're going to pop off some levels of the tree. Check each ancestor to
// make sure the offsets are correct.
int next_index = n.GetIndex() - 1;
while (!ancestors.empty()) {
ParseTree::Node parent_n = ancestors.back();
if ((parent_n.GetIndex() -
node_impls[parent_n.GetIndex()].subtree_size) != next_index)
break;
ancestors.pop_back();
}
}
if (!ancestors.empty()) {
llvm::errs()
<< "Finished walking the parse tree and there are still ancestors:\n";
for (Node ancestor_n : ancestors)
llvm::errs() << " Node #" << ancestor_n.GetIndex() << "\n";
return false;
}
return true;
}
} // namespace Carbon