From 9ffcb16392941c15fdd8bf45c8ea6ea3f1799668 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 23 Jan 2020 10:26:53 -0800 Subject: [PATCH] Fix tree-balancing logic Remove incorrect condition that would prevent balancing of repeating structures containing only tokens (nodes w/ no children). Co-Authored-By: Rob Rix Co-Authored-By: Patrick Thomson --- lib/src/subtree.c | 13 +++---------- lib/src/subtree.h | 4 ++++ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/src/subtree.c b/lib/src/subtree.c index 30144fa1..b98f1723 100644 --- a/lib/src/subtree.c +++ b/lib/src/subtree.c @@ -322,12 +322,9 @@ void ts_subtree_balance(Subtree self, SubtreePool *pool, const TSLanguage *langu if (tree.ptr->repeat_depth > 0) { Subtree child1 = tree.ptr->children[0]; Subtree child2 = tree.ptr->children[tree.ptr->child_count - 1]; - if ( - ts_subtree_child_count(child1) > 0 && - ts_subtree_child_count(child2) > 0 && - child1.ptr->repeat_depth > child2.ptr->repeat_depth - ) { - unsigned n = child1.ptr->repeat_depth - child2.ptr->repeat_depth; + long repeat_delta = (long)ts_subtree_repeat_depth(child1) - (long)ts_subtree_repeat_depth(child2); + if (repeat_delta > 0) { + unsigned n = repeat_delta; for (unsigned i = n / 2; i > 0; i /= 2) { ts_subtree__compress(tree, i, language, &pool->tree_stack); n -= i; @@ -344,10 +341,6 @@ void ts_subtree_balance(Subtree self, SubtreePool *pool, const TSLanguage *langu } } -static inline uint32_t ts_subtree_repeat_depth(Subtree self) { - return ts_subtree_child_count(self) ? self.ptr->repeat_depth : 0; -} - void ts_subtree_set_children( MutableSubtree self, Subtree *children, uint32_t child_count, const TSLanguage *language ) { diff --git a/lib/src/subtree.h b/lib/src/subtree.h index 79ccd923..18c48dcb 100644 --- a/lib/src/subtree.h +++ b/lib/src/subtree.h @@ -206,6 +206,10 @@ static inline uint32_t ts_subtree_child_count(Subtree self) { return self.data.is_inline ? 0 : self.ptr->child_count; } +static inline uint32_t ts_subtree_repeat_depth(Subtree self) { + return self.data.is_inline ? 0 : self.ptr->repeat_depth; +} + static inline uint32_t ts_subtree_node_count(Subtree self) { return (self.data.is_inline || self.ptr->child_count == 0) ? 1 : self.ptr->node_count; }