From c88e9044d5fb1d395aa986e6a109f468cf1355b8 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 20 Nov 2015 00:01:53 -0800 Subject: [PATCH] Make stack popping more robust --- spec/runtime/stack_spec.cc | 217 ++++++++++++++++++++++++++++--------- src/runtime/parser.c | 111 +++++++++++++------ src/runtime/parser.h | 1 + src/runtime/stack.c | 129 ++++++++++++---------- src/runtime/stack.h | 12 +- src/runtime/vector.h | 9 ++ 6 files changed, 328 insertions(+), 151 deletions(-) diff --git a/spec/runtime/stack_spec.cc b/spec/runtime/stack_spec.cc index b2d12208..7c3cc1c7 100644 --- a/spec/runtime/stack_spec.cc +++ b/spec/runtime/stack_spec.cc @@ -5,12 +5,12 @@ #include "runtime/length.h" enum { - stateA, stateB, stateC, stateD, stateE, stateF, stateG, stateH + stateA, stateB, stateC, stateD, stateE, stateF, stateG, stateH, stateI, stateJ }; enum { symbol0 = ts_builtin_sym_start, - symbol1, symbol2, symbol3, symbol4, symbol5, symbol6, symbol7 + symbol1, symbol2, symbol3, symbol4, symbol5, symbol6, symbol7, symbol8 }; struct TreeSelectionSpy { @@ -32,7 +32,7 @@ START_TEST describe("Stack", [&]() { Stack *stack; - const size_t tree_count = 8; + const size_t tree_count = 10; TSTree *trees[tree_count]; TreeSelectionSpy tree_selection_spy{0, NULL, {NULL, NULL}}; @@ -88,8 +88,6 @@ describe("Stack", [&]() { }); describe("popping nodes from the stack", [&]() { - StackPopResultList pop; - before_each([&]() { /* * A0__B1__C2. @@ -103,43 +101,47 @@ describe("Stack", [&]() { /* * A0. */ - pop = ts_stack_pop(stack, 0, 2, false); - AssertThat(pop.size, Equals(1)); - AssertThat(pop.contents[0].tree_count, Equals(2)); - AssertThat(pop.contents[0].trees[0], Equals(trees[1])); - AssertThat(pop.contents[0].trees[1], Equals(trees[2])); + Vector pop = ts_stack_pop(stack, 0, 2, false); + StackPopResult pop1 = *(StackPopResult *)vector_get(&pop, 0); + AssertThat(pop.size, Equals(1)); + AssertThat(pop1.tree_count, Equals(2)); + AssertThat(pop1.trees[0], Equals(trees[1])); + AssertThat(pop1.trees[1], Equals(trees[2])); AssertThat(*ts_stack_head(stack, 0), Equals({trees[0], stateA})); /* * . */ pop = ts_stack_pop(stack, 0, 1, false); - AssertThat(pop.size, Equals(1)); - AssertThat(pop.contents[0].tree_count, Equals(1)); - AssertThat(pop.contents[0].trees[0], Equals(trees[0])); + pop1 = *(StackPopResult *)vector_get(&pop, 0); + AssertThat(pop.size, Equals(1)); + AssertThat(pop1.tree_count, Equals(1)); + AssertThat(pop1.trees[0], Equals(trees[0])); AssertThat(ts_stack_head(stack, 0), Equals(nullptr)); }); it("does not count 'extra' trees toward the count", [&]() { ts_tree_set_extra(trees[1]); - pop = ts_stack_pop(stack, 0, 2, false); - AssertThat(pop.size, Equals(1)); - AssertThat(pop.contents[0].tree_count, Equals(3)); - AssertThat(pop.contents[0].trees[0], Equals(trees[0])); - AssertThat(pop.contents[0].trees[1], Equals(trees[1])); - AssertThat(pop.contents[0].trees[2], Equals(trees[2])); + Vector pop = ts_stack_pop(stack, 0, 2, false); + StackPopResult pop1 = *(StackPopResult *)vector_get(&pop, 0); + AssertThat(pop.size, Equals(1)); + AssertThat(pop1.tree_count, Equals(3)); + AssertThat(pop1.trees[0], Equals(trees[0])); + AssertThat(pop1.trees[1], Equals(trees[1])); + AssertThat(pop1.trees[2], Equals(trees[2])); AssertThat(ts_stack_head(stack, 0), Equals(nullptr)); }); it("pops the entire stack when given a negative count", [&]() { - pop = ts_stack_pop(stack, 0, -1, false); + Vector pop = ts_stack_pop(stack, 0, -1, false); - AssertThat(pop.size, Equals(1)); - AssertThat(pop.contents[0].tree_count, Equals(3)); - AssertThat(pop.contents[0].trees[0], Equals(trees[0])); - AssertThat(pop.contents[0].trees[1], Equals(trees[1])); - AssertThat(pop.contents[0].trees[2], Equals(trees[2])); + AssertThat(pop.size, Equals(1)); + StackPopResult pop1 = *(StackPopResult *)vector_get(&pop, 0); + AssertThat(pop1.tree_count, Equals(3)); + AssertThat(pop1.trees[0], Equals(trees[0])); + AssertThat(pop1.trees[1], Equals(trees[1])); + AssertThat(pop1.trees[2], Equals(trees[2])); }); }); @@ -317,6 +319,9 @@ describe("Stack", [&]() { ts_stack_push(stack, 1, stateE, trees[4]); ts_stack_push(stack, 1, stateF, trees[5]); ts_stack_push(stack, 1, stateG, trees[6]); + + AssertThat(ts_stack_head_count(stack), Equals(1)); + AssertThat(ts_stack_entry_next_count(ts_stack_head(stack, 0)), Equals(2)); }); describe("when there are two paths that lead to two different heads", [&]() { @@ -325,18 +330,18 @@ describe("Stack", [&]() { * A0__B1__C2. * \__E4. */ - StackPopResultList pop = ts_stack_pop(stack, 0, 2, false); + Vector pop = ts_stack_pop(stack, 0, 2, false); - AssertThat(pop.size, Equals(2)); - StackPopResult pop1 = pop.contents[0]; - AssertThat(pop1.index, Equals(0)); - AssertThat(pop1.tree_count, Equals(2)); + AssertThat(pop.size, Equals(2)); + StackPopResult pop1 = *(StackPopResult *)vector_get(&pop, 0); + AssertThat(pop1.head_index, Equals(0)); + AssertThat(pop1.tree_count, Equals(2)); AssertThat(pop1.trees[0], Equals(trees[3])); AssertThat(pop1.trees[1], Equals(trees[6])); - StackPopResult pop2 = pop.contents[1]; - AssertThat(pop2.index, Equals(1)); - AssertThat(pop2.tree_count, Equals(2)); + StackPopResult pop2 = *(StackPopResult *)vector_get(&pop, 1); + AssertThat(pop2.head_index, Equals(1)); + AssertThat(pop2.tree_count, Equals(2)); AssertThat(pop2.trees[0], Equals(trees[5])); AssertThat(pop2.trees[1], Equals(trees[6])); @@ -360,9 +365,9 @@ describe("Stack", [&]() { * A0__B1__C2__D3__G6. * \__E4__F5__/ */ - StackPopResultList pop = ts_stack_pop(stack, 0, 1, false); + Vector pop = ts_stack_pop(stack, 0, 1, false); - AssertThat(pop.size, Equals(1)); + AssertThat(pop.size, Equals(1)); AssertThat(ts_stack_head_count(stack), Equals(1)); }); }); @@ -380,19 +385,21 @@ describe("Stack", [&]() { * A0__B1__C2__D3. * \__E4__F5. */ - StackPopResultList pop = ts_stack_pop(stack, 0, 2, false); + Vector pop = ts_stack_pop(stack, 0, 2, false); AssertThat(ts_stack_head_count(stack), Equals(2)); - AssertThat(pop.size, Equals(2)); - AssertThat(pop.contents[0].index, Equals(0)); - AssertThat(pop.contents[0].tree_count, Equals(2)); - AssertThat(pop.contents[0].trees[0], Equals(trees[6])); - AssertThat(pop.contents[0].trees[1], Equals(trees[7])); + AssertThat(pop.size, Equals(2)); + StackPopResult pop1 = *(StackPopResult *)vector_get(&pop, 0); + AssertThat(pop1.head_index, Equals(0)); + AssertThat(pop1.tree_count, Equals(2)); + AssertThat(pop1.trees[0], Equals(trees[6])); + AssertThat(pop1.trees[1], Equals(trees[7])); - AssertThat(pop.contents[1].index, Equals(1)); - AssertThat(pop.contents[1].tree_count, Equals(2)); - AssertThat(pop.contents[1].trees[0], Equals(trees[6])); - AssertThat(pop.contents[1].trees[1], Equals(trees[7])); + StackPopResult pop2 = *(StackPopResult *)vector_get(&pop, 1); + AssertThat(pop2.head_index, Equals(1)); + AssertThat(pop2.tree_count, Equals(2)); + AssertThat(pop2.trees[0], Equals(trees[6])); + AssertThat(pop2.trees[1], Equals(trees[7])); }); }); @@ -401,17 +408,121 @@ describe("Stack", [&]() { /* * A0__B1. */ - StackPopResultList pop = ts_stack_pop(stack, 0, 3, false); + Vector pop = ts_stack_pop(stack, 0, 3, false); AssertThat(ts_stack_head_count(stack), Equals(1)); AssertThat(*ts_stack_head(stack, 0), Equals({trees[1], stateB})); - AssertThat(pop.size, Equals(2)); - AssertThat(pop.contents[0].tree_count, Equals(3)); - AssertThat(pop.contents[0].index, Equals(0)); - AssertThat(pop.contents[0].trees[0], Equals(trees[2])); - AssertThat(pop.contents[1].tree_count, Equals(3)); - AssertThat(pop.contents[1].index, Equals(0)); - AssertThat(pop.contents[1].trees[0], Equals(trees[4])); + AssertThat(pop.size, Equals(2)); + StackPopResult pop1 = *(StackPopResult *)vector_get(&pop, 0); + AssertThat(pop1.tree_count, Equals(3)); + AssertThat(pop1.head_index, Equals(0)); + AssertThat(pop1.trees[0], Equals(trees[2])); + + StackPopResult pop2 = *(StackPopResult *)vector_get(&pop, 1); + AssertThat(pop2.tree_count, Equals(3)); + AssertThat(pop2.head_index, Equals(0)); + AssertThat(pop2.trees[0], Equals(trees[4])); + }); + }); + }); + + describe("popping from a stack head that has been 3-way merged", [&]() { + before_each([&]() { + /* + * A0__B1__C2__D3__I8__J9. + * \__E4__F5__/ + * \__G6__H7__/ + */ + ts_stack_clear(stack); + ts_stack_push(stack, 0, stateA, trees[0]); + ts_stack_push(stack, 0, stateB, trees[1]); + ts_stack_split(stack, 0); + ts_stack_split(stack, 1); + ts_stack_push(stack, 0, stateC, trees[2]); + ts_stack_push(stack, 1, stateE, trees[4]); + ts_stack_push(stack, 2, stateG, trees[6]); + ts_stack_push(stack, 0, stateD, trees[3]); + ts_stack_push(stack, 1, stateF, trees[5]); + ts_stack_push(stack, 2, stateH, trees[7]); + ts_stack_push(stack, 0, stateI, trees[8]); + ts_stack_push(stack, 1, stateI, trees[8]); + ts_stack_push(stack, 1, stateI, trees[8]); + ts_stack_push(stack, 0, stateJ, trees[9]); + + AssertThat(ts_stack_head_count(stack), Equals(1)); + StackEntry *head = ts_stack_head(stack, 0); + AssertThat(ts_stack_entry_next_count(head), Equals(1)); + AssertThat(ts_stack_entry_next_count(ts_stack_entry_next(head, 0)), Equals(3)); + }); + + describe("when there is one path that leads to three different heads", [&]() { + it("returns three entries with the same array of trees", [&]() { + /* + * A0__B1__C2__D3. + * \__E4__F5. + * \__G6__H7. + */ + Vector pop = ts_stack_pop(stack, 0, 2, false); + AssertThat(ts_stack_head_count(stack), Equals(3)); + + AssertThat(pop.size, Equals(3)); + + StackPopResult pop1 = *(StackPopResult *)vector_get(&pop, 0); + AssertThat(ts_stack_top_tree(stack, 0), Equals(trees[3])); + AssertThat(pop1.head_index, Equals(0)); + AssertThat(pop1.tree_count, Equals(2)); + AssertThat(pop1.trees[0], Equals(trees[8])); + AssertThat(pop1.trees[1], Equals(trees[9])); + + StackPopResult pop2 = *(StackPopResult *)vector_get(&pop, 1); + AssertThat(ts_stack_top_tree(stack, 1), Equals(trees[5])); + AssertThat(pop2.head_index, Equals(1)); + AssertThat(pop2.tree_count, Equals(2)); + AssertThat(pop2.trees, Equals(pop1.trees)); + + StackPopResult pop3 = *(StackPopResult *)vector_get(&pop, 2); + AssertThat(ts_stack_top_tree(stack, 2), Equals(trees[7])); + AssertThat(pop3.head_index, Equals(2)); + AssertThat(pop3.tree_count, Equals(2)); + AssertThat(pop3.trees, Equals(pop1.trees)); + }); + }); + + describe("when there are three different paths that lead to three different heads", [&]() { + it("returns three entries with different arrays of trees", [&]() { + /* + * A0__B1__C2. + * \__E4. + * \__G6. + */ + Vector pop = ts_stack_pop(stack, 0, 3, false); + AssertThat(ts_stack_head_count(stack), Equals(3)); + + AssertThat(pop.size, Equals(3)); + + StackPopResult pop1 = *(StackPopResult *)vector_get(&pop, 0); + AssertThat(ts_stack_top_tree(stack, 0), Equals(trees[2])); + AssertThat(pop1.head_index, Equals(0)); + AssertThat(pop1.tree_count, Equals(3)); + AssertThat(pop1.trees[0], Equals(trees[3])); + AssertThat(pop1.trees[1], Equals(trees[8])); + AssertThat(pop1.trees[2], Equals(trees[9])); + + StackPopResult pop2 = *(StackPopResult *)vector_get(&pop, 1); + AssertThat(ts_stack_top_tree(stack, 1), Equals(trees[4])); + AssertThat(pop2.head_index, Equals(1)); + AssertThat(pop2.tree_count, Equals(3)); + AssertThat(pop2.trees[0], Equals(trees[5])); + AssertThat(pop2.trees[1], Equals(trees[8])); + AssertThat(pop2.trees[2], Equals(trees[9])); + + StackPopResult pop3 = *(StackPopResult *)vector_get(&pop, 2); + AssertThat(ts_stack_top_tree(stack, 2), Equals(trees[6])); + AssertThat(pop3.head_index, Equals(2)); + AssertThat(pop3.tree_count, Equals(3)); + AssertThat(pop3.trees[0], Equals(trees[7])); + AssertThat(pop3.trees[1], Equals(trees[8])); + AssertThat(pop3.trees[2], Equals(trees[9])); }); }); }); diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 8321a2dc..1b9cf0f6 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -181,52 +181,91 @@ static bool ts_parser__shift_extra(TSParser *self, int head, TSStateId state) { static TSTree *ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, size_t child_count, bool extra, bool count_extra) { + vector_clear(&self->reduce_parents); TSNodeType node_type = self->language->node_types[symbol]; - StackPopResultList pop_results = - ts_stack_pop(self->stack, head, child_count, count_extra); + Vector pop_results = ts_stack_pop(self->stack, head, child_count, count_extra); - TSTree *parent = NULL; - TSTree **last_children = NULL; - int last_index = -1; + int last_head_index = -1; + int removed_heads = 0; - for (int i = 0; i < pop_results.size; i++) { - StackPopResult pop_result = pop_results.contents[i]; + for (size_t i = 0; i < pop_results.size; i++) { + StackPopResult *pop_result = vector_get(&pop_results, i); + /* + * If the same set of trees led to a previous stack head, reuse the parent + * tree that was added to that head. + */ + TSTree *parent = NULL; + for (size_t j = 0; j < i; j++) { + StackPopResult *prior_result = vector_get(&pop_results, j); + if (pop_result->trees == prior_result->trees) { + TSTree **existing_parent = vector_get(&self->reduce_parents, j); + parent = *existing_parent; + break; + } + } + + /* + * Otherwise, create a new parent node for this set of trees. + */ + if (!parent) + parent = ts_tree_make_node(symbol, pop_result->tree_count, pop_result->trees, node_type); + vector_push(&self->reduce_parents, &parent); + + /* + * If another path led to the same stack head, add this new parent tree + * as an alternative for that stack head. + */ + int new_head = pop_result->head_index - removed_heads; + if (pop_result->head_index == last_head_index) { + ts_stack_add_alternative(self->stack, new_head, parent); + continue; + } + + /* + * If the stack has split in the process of popping, create a duplicate of + * the lookahead state for this head, for the new head. + */ if (i > 0) { - assert(pop_result.index == self->head_states.size); - DEBUG("split_during_reduce new_head:%d", pop_result.index); + DEBUG("split_during_reduce new_head:%d", new_head); HeadState *head_state = vector_get(&self->head_states, head); vector_push(&self->head_states, head_state); } - if (pop_result.trees != last_children) { - parent = ts_tree_make_node(symbol, pop_result.tree_count, - pop_result.trees, node_type); - } - - if (pop_result.index == last_index) { - ts_stack_add_alternative(self->stack, pop_result.index, parent); + /* + * If the parent node is extra, then do not change the state when pushing + * it. Otherwise, proceed to the state given in the parse table for the + * new parent symbol. + */ + TSStateId state; + TSStateId top_state = ts_stack_top_state(self->stack, new_head); + if (extra) { + ts_tree_set_extra(parent); + state = top_state; } else { - TSStateId top_state = ts_stack_top_state(self->stack, pop_result.index); - TSStateId state; - - if (extra) { - ts_tree_set_extra(parent); - state = top_state; + TSParseAction action = ts_language__last_action(self->language, top_state, symbol); + if (child_count == -1) { + state = 0; } else { - state = ts_language__last_action(self->language, top_state, symbol) - .data.to_state; + assert(action.type == TSParseActionTypeShift); + state = action.data.to_state; } - - if (ts_stack_push(self->stack, pop_result.index, state, parent)) - vector_erase(&self->head_states, pop_result.index); } - last_index = pop_result.index; - last_children = pop_result.trees; + /* + * If the given state already existed at a different head of the stack, + * then remove the lookahead state for the head. + */ + if (ts_stack_push(self->stack, new_head, state, parent)) { + vector_erase(&self->head_states, new_head); + removed_heads++; + } + + last_head_index = pop_result->head_index; } - return parent; + TSTree **last_parent = vector_back(&self->reduce_parents); + return *last_parent; } static void ts_parser__reduce_fragile(TSParser *self, int head, TSSymbol symbol, @@ -298,7 +337,7 @@ static bool ts_parser__handle_error(TSParser *self, int head) { */ if (self->lookahead->symbol == ts_builtin_sym_end) { DEBUG("fail_to_recover"); - ts_parser__reduce_error(self, head, error_token_count - 1); + ts_parser__reduce_error(self, head, -1); return false; } } @@ -328,10 +367,11 @@ static void ts_parser__start(TSParser *self, TSInput input, } static TSTree *ts_parser__finish(TSParser *self) { - StackPopResult pop_result = ts_stack_pop(self->stack, 0, -1, true).contents[0]; + Vector pop_results = ts_stack_pop(self->stack, 0, -1, true); + StackPopResult *pop_result = vector_get(&pop_results, 0); - TSTree **trees = pop_result.trees; - size_t extra_count = pop_result.tree_count - 1; + TSTree **trees = pop_result->trees; + size_t extra_count = pop_result->tree_count - 1; TSTree *root = trees[extra_count]; ts_tree_prepend_children(root, extra_count, trees); @@ -463,7 +503,8 @@ TSParser ts_parser_make() { .stack = ts_stack_new((TreeSelectionCallback){ NULL, ts_parser__select_tree, }), - .head_states = vector_new(sizeof(HeadState), 3), + .head_states = vector_new(sizeof(HeadState), 4), + .reduce_parents = vector_new(sizeof(TSTree *), 4), .lookahead = NULL, }; } diff --git a/src/runtime/parser.h b/src/runtime/parser.h index 53cb6a0f..b15c06f2 100644 --- a/src/runtime/parser.h +++ b/src/runtime/parser.h @@ -14,6 +14,7 @@ typedef struct { TSTree *lookahead; const TSLanguage *language; Vector head_states; + Vector reduce_parents; } TSParser; TSParser ts_parser_make(); diff --git a/src/runtime/stack.c b/src/runtime/stack.c index 89efb4b8..3236b763 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -5,13 +5,13 @@ #include "runtime/length.h" #include -#define MAX_POP_PATH_COUNT 8 +#define MAX_SUCCESSOR_COUNT 8 #define INITIAL_HEAD_CAPACITY 3 #define STARTING_TREE_CAPACITY 10 typedef struct StackNode { StackEntry entry; - struct StackNode *successors[MAX_POP_PATH_COUNT]; + struct StackNode *successors[MAX_SUCCESSOR_COUNT]; short unsigned int successor_count; short unsigned int ref_count; } StackNode; @@ -20,10 +20,18 @@ struct Stack { StackNode **heads; int head_count; int head_capacity; - StackPopResult last_pop_results[MAX_POP_PATH_COUNT]; + Vector pop_results; + Vector pop_paths; TreeSelectionCallback tree_selection_callback; }; +typedef struct { + size_t goal_tree_count; + StackNode *node; + Vector trees; + bool is_shared; +} PopPath; + /* * Section: Stack lifecycle */ @@ -35,11 +43,15 @@ Stack *ts_stack_new(TreeSelectionCallback tree_selection_callback) { .head_count = 1, .head_capacity = INITIAL_HEAD_CAPACITY, .tree_selection_callback = tree_selection_callback, + .pop_results = vector_new(sizeof(StackPopResult), 4), + .pop_paths = vector_new(sizeof(PopPath), 4), }; return self; } void ts_stack_delete(Stack *self) { + vector_delete(&self->pop_results); + vector_delete(&self->pop_paths); free(self->heads); free(self); } @@ -164,12 +176,12 @@ static int ts_stack__add_head(Stack *self, StackNode *node) { return new_index; } -static int ts_stack__find_or_add_head(Stack *self, StackNode *node) { +static int ts_stack__find_head(Stack *self, StackNode *node) { for (int i = 0; i < self->head_count; i++) if (self->heads[i] == node) { return i; } - return ts_stack__add_head(self, node); + return -1; } void ts_stack_remove_head(Stack *self, int head_index) { @@ -221,17 +233,24 @@ int ts_stack_split(Stack *self, int head_index) { return ts_stack__add_head(self, self->heads[head_index]); } -StackPopResultList ts_stack_pop(Stack *self, int head_index, int child_count, - bool count_extra) { - StackNode *previous_head = self->heads[head_index]; +const char *symbol_names[] = { + "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", + "ten", "eleven", "twelve" +}; - int path_count = 1; +Vector ts_stack_pop(Stack *self, int head_index, int child_count, bool count_extra) { + StackNode *previous_head = self->heads[head_index]; int capacity = (child_count == -1) ? STARTING_TREE_CAPACITY : child_count; - size_t tree_counts_by_path[MAX_POP_PATH_COUNT] = { child_count }; - StackNode *nodes_by_path[MAX_POP_PATH_COUNT] = { previous_head }; - Vector trees_by_path[MAX_POP_PATH_COUNT] = { vector_new(sizeof(TSTree *), - capacity) }; - bool is_shared_by_path[MAX_POP_PATH_COUNT] = { false }; + PopPath initial_path = { + .goal_tree_count = child_count, + .node = previous_head, + .trees = vector_new(sizeof(TSTree *), capacity), + .is_shared = false, + }; + + vector_clear(&self->pop_results); + vector_clear(&self->pop_paths); + vector_push(&self->pop_paths, &initial_path); /* * Reduce along every possible path in parallel. Stop when the given number @@ -240,72 +259,72 @@ StackPopResultList ts_stack_pop(Stack *self, int head_index, int child_count, bool all_paths_done = false; while (!all_paths_done) { all_paths_done = true; - int current_path_count = path_count; - for (int path = 0; path < current_path_count; path++) { - StackNode *node = nodes_by_path[path]; - if (!node || (trees_by_path[path].size == tree_counts_by_path[path])) + + for (size_t i = 0; i < self->pop_paths.size; i++) { + PopPath *path = vector_get(&self->pop_paths, i); + StackNode *node = path->node; + + if (!node || path->trees.size == path->goal_tree_count) continue; + all_paths_done = false; /* * Children that are 'extra' do not count towards the total child count. */ if (ts_tree_is_extra(node->entry.tree) && !count_extra) - tree_counts_by_path[path]++; + path->goal_tree_count++; /* * If a node has more than one successor, create new paths for each of * the additional successors. */ - if (is_shared_by_path[path]) { - trees_by_path[path] = vector_copy(&trees_by_path[path]); - is_shared_by_path[path] = false; + if (path->is_shared) { + path->trees = vector_copy(&path->trees); + path->is_shared = false; } + ts_tree_retain(node->entry.tree); - vector_push(&trees_by_path[path], &node->entry.tree); + vector_push(&path->trees, &node->entry.tree); - for (int i = 0; i < node->successor_count; i++) { - int next_path; - if (i > 0) { - if (path_count == MAX_POP_PATH_COUNT) - break; - next_path = path_count; - tree_counts_by_path[next_path] = tree_counts_by_path[path]; - trees_by_path[next_path] = trees_by_path[path]; - is_shared_by_path[next_path] = true; - path_count++; - } else { - next_path = path; - } - - nodes_by_path[next_path] = node->successors[i]; + path->node = path->node->successors[0]; + for (int j = 1; j < node->successor_count; j++) { + PopPath path_copy = *path; + vector_push(&self->pop_paths, &path_copy); + PopPath *next_path = vector_back(&self->pop_paths); + next_path->node = node->successors[j]; + next_path->is_shared = true; } } } - for (int path = 0; path < path_count; path++) { - if (!is_shared_by_path[path]) - vector_reverse(&trees_by_path[path]); - int index = -1; - if (path == 0) { - stack_node_retain(nodes_by_path[path]); - self->heads[head_index] = nodes_by_path[path]; - index = head_index; + for (size_t i = 0; i < self->pop_paths.size; i++) { + PopPath *path = vector_get(&self->pop_paths, i); + + if (!path->is_shared) + vector_reverse(&path->trees); + + StackPopResult result = { + .trees = path->trees.contents, + .tree_count = path->trees.size, + .head_index = -1, + }; + + if (i == 0) { + stack_node_retain(path->node); + self->heads[head_index] = path->node; + result.head_index = head_index; } else { - index = ts_stack__find_or_add_head(self, nodes_by_path[path]); + result.head_index = ts_stack__find_head(self, path->node); + if (result.head_index == -1) + result.head_index = ts_stack__add_head(self, path->node); } - self->last_pop_results[path] = (StackPopResult){ - .index = index, - .tree_count = trees_by_path[path].size, - .trees = trees_by_path[path].contents, - }; + vector_push(&self->pop_results, &result); } stack_node_release(previous_head); - return (StackPopResultList){ - .size = path_count, .contents = self->last_pop_results, - }; + return self->pop_results; } void ts_stack_shrink(Stack *self, int head_index, int count) { diff --git a/src/runtime/stack.h b/src/runtime/stack.h index 2aab77e9..3b44f351 100644 --- a/src/runtime/stack.h +++ b/src/runtime/stack.h @@ -6,6 +6,7 @@ extern "C" { #endif #include "tree_sitter/parser.h" +#include "runtime/vector.h" typedef struct Stack Stack; @@ -15,16 +16,11 @@ typedef struct { } StackEntry; typedef struct { - int index; - int tree_count; TSTree **trees; + size_t tree_count; + int head_index; } StackPopResult; -typedef struct { - int size; - StackPopResult *contents; -} StackPopResultList; - typedef struct { void *data; TSTree *(*callback)(void *data, TSTree *, TSTree *); @@ -90,7 +86,7 @@ void ts_stack_add_alternative(Stack *, int head, TSTree *); * which had previously been merged. It returns a struct that indicates the * index of each revealed head and the trees removed from that head. */ -StackPopResultList ts_stack_pop(Stack *, int head, int count, bool count_extra); +Vector ts_stack_pop(Stack *, int head, int count, bool count_extra); /* * Remove the given number of entries from the given head of the stack. diff --git a/src/runtime/vector.h b/src/runtime/vector.h index 28437764..434ddefe 100644 --- a/src/runtime/vector.h +++ b/src/runtime/vector.h @@ -25,11 +25,20 @@ static inline Vector vector_new(size_t element_size, size_t capacity) { }; } +static inline void vector_delete(Vector *self) { + free(self->contents); +} + static inline void *vector_get(Vector *self, size_t index) { assert(index < self->size); return (void *)((char *)self->contents + index * self->element_size); } +static inline void *vector_back(Vector *self) { + assert(self->size > 0); + return vector_get(self, self->size - 1); +} + static inline void vector_clear(Vector *self) { self->size = 0; }