Skip to content

Commit

Permalink
Remove last trace of child container assumptions in SyntaxTreeNode.
Browse files Browse the repository at this point in the history
The container is now ready to be replaced with other implementations
for nicer node allocations.

Issues: #1523
  • Loading branch information
hzeller committed Dec 21, 2023
1 parent 4e8133d commit c49846e
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 c49846e

Please sign in to comment.