From 16a45d4aa439e543e7e355c57a7fb15767490efe Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 16 Feb 2018 12:44:30 -0800 Subject: [PATCH] Fix hole in logic for terminating tree balancing It's important that the repetition nodes have a child count of 2, because we assign to their second child. We could maybe generalize this to allow balancing in the presence of 'extra' nodes like comments and errors, but this might be complicated. --- src/runtime/tree.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/runtime/tree.c b/src/runtime/tree.c index d1b551c8..ae35e1f8 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -206,13 +206,21 @@ Tree *ts_tree_make_copy(TreePool *pool, Tree *self) { static void ts_tree__compress(Tree *self, unsigned count, const TSLanguage *language) { Tree *tree = self; for (unsigned i = 0; i < count; i++) { + if (tree->ref_count > 1 || tree->child_count != 2) break; + Tree *child = tree->children[0]; - if (child->symbol != tree->symbol) break; + if ( + child->ref_count > 1 || + child->child_count != 2 || + child->symbol != tree->symbol + ) break; Tree *grandchild = child->children[0]; - if (grandchild->symbol != tree->symbol) break; - if (grandchild->children[0]->symbol != tree->symbol) break; - if (child->ref_count > 1 || grandchild->ref_count > 1) break; + if ( + grandchild->ref_count > 1 || + grandchild->child_count != 2 || + grandchild->symbol != tree->symbol + ) break; tree->children[0] = grandchild; grandchild->context.parent = tree;