Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove last trace of child container assumptions in SyntaxTreeNode. #2064

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions common/text/concrete_syntax_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,18 @@ class SyntaxTreeNode final : public Symbol {
const SymbolPtr &back() const { return children_.back(); }
SymbolPtr &back() { return children_.back(); }

void pop_back() { children_.pop_back(); }

size_t size() const { return children_.size(); }
bool empty() const { return children_.empty(); }

ConstRange children() const {
return ConstRange(children_.cbegin(), children_.cend());
}

#if 0 // TODO: to switch, find solution for pop_back() in PruneTreeFromRight()
MutableRange mutable_children() {
return MutableRange(children_.begin(), children_.end());
}
#else
ChildContainer &mutable_children() { return children_; }
#endif

// Compares this node to an arbitrary symbol using the compare_tokens
// function.
Expand Down
18 changes: 11 additions & 7 deletions common/text/tree_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,22 +301,26 @@ bool PruneTreeFromRight(ConcreteSyntaxTree *tree, const char *offset) {
}
case SymbolKind::kNode: {
auto &node = down_cast<SyntaxTreeNode &>(*tree->get());
auto &children = node.mutable_children();
for (auto &child : reversed_view(children)) {
int prune_count = 0;
for (auto &child : const_reversed_view(node.mutable_children())) {
if (child == nullptr) {
children.pop_back(); // pop_back() guaranteed to not realloc
++prune_count;
} else {
if (PruneTreeFromRight(&child, offset)) {
children.pop_back();
++prune_count;
} else {
// Since token locations are monotonic, we can stop checking
// as soon as the above function returns false.
break;
}
}
}
// To avoid iterator invalidation issues in the loop, we erase at end.
while (prune_count--) {
node.pop_back();
}
// If no children remain, tell caller to delete this node.
return children.empty();
return node.empty();
}
}

Expand All @@ -343,8 +347,8 @@ ConcreteSyntaxTree *LeftSubtree(ConcreteSyntaxTree *tree) {
// Leaves don't have subtrees.
return nullptr;
}
auto &children = down_cast<SyntaxTreeNode &>(*tree->get()).mutable_children();
for (auto &child : children) {
SyntaxTreeNode *const tree_node = down_cast<SyntaxTreeNode *>(tree->get());
for (auto &child : tree_node->mutable_children()) {
if (child != nullptr) return &child;
}
return nullptr;
Expand Down
Loading