Skip to content

Commit

Permalink
Merge pull request #2064 from hzeller/20231221-remove-container-assum…
Browse files Browse the repository at this point in the history
…ptions

Remove last trace of child container assumptions in SyntaxTreeNode.
  • Loading branch information
hzeller authored Jan 6, 2024
2 parents c517d61 + c49846e commit a4d61b1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
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

0 comments on commit a4d61b1

Please sign in to comment.