From 445be0736a6360154cd101d83554e32641d355de Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 29 Jun 2017 10:43:20 -0700 Subject: [PATCH] Clean up ts_stack_push function --- src/runtime/stack.c | 74 ++++++++++++++++++--------------------------- src/runtime/stack.h | 5 ++- 2 files changed, 31 insertions(+), 48 deletions(-) diff --git a/src/runtime/stack.c b/src/runtime/stack.c index 5926ed78..66df776a 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -90,51 +90,43 @@ static void stack_node_release(StackNode *self, StackNodeArray *pool) { } } -static StackNode *stack_node_new(StackNode *next, Tree *tree, bool is_pending, - TSStateId state, Length position, - StackNodeArray *pool) { - StackNode *node; - if (pool->size > 0) - node = array_pop(pool); - else if (!(node = ts_malloc(sizeof(StackNode)))) - return NULL; +static StackNode *stack_node_new(StackNode *previous_node, Tree *tree, bool is_pending, + TSStateId state, StackNodeArray *pool) { + StackNode *node = pool->size > 0 ? + array_pop(pool) : + ts_malloc(sizeof(StackNode)); + *node = (StackNode){.ref_count = 1, .link_count = 0, .state = state}; - *node = (StackNode){ - .ref_count = 1, - .link_count = 0, - .links = {}, - .state = state, - .position = position, - }; - - if (next) { - stack_node_retain(next); + if (previous_node) { + stack_node_retain(previous_node); node->link_count = 1; node->links[0] = (StackLink){ - .node = next, .tree = tree, .is_pending = is_pending, .push_count = 0, + .node = previous_node, + .tree = tree, + .is_pending = is_pending, + .push_count = 0, }; - node->error_count = next->error_count; - node->error_cost = next->error_cost; + node->position = previous_node->position; + node->error_count = previous_node->error_count; + node->error_cost = previous_node->error_cost; if (tree) { ts_tree_retain(tree); node->error_cost += tree->error_cost; - - if (state == ERROR_STATE) { - if (!tree->extra) { - node->error_cost += ERROR_COST_PER_SKIPPED_TREE + - ERROR_COST_PER_SKIPPED_CHAR * - (tree->padding.chars + tree->size.chars) + - ERROR_COST_PER_SKIPPED_LINE * - (tree->padding.extent.row + tree->size.extent.row); - } + node->position = length_add(node->position, ts_tree_total_size(tree)); + if (state == ERROR_STATE && !tree->extra) { + node->error_cost += + ERROR_COST_PER_SKIPPED_TREE + + ERROR_COST_PER_SKIPPED_CHAR * (tree->padding.chars + tree->size.chars) + + ERROR_COST_PER_SKIPPED_LINE * (tree->padding.extent.row + tree->size.extent.row); } } else { node->error_count++; } } else { + node->position = length_zero(); node->error_count = 0; node->error_cost = 0; } @@ -303,8 +295,7 @@ Stack *ts_stack_new() { array_grow(&self->iterators, 4); array_grow(&self->node_pool, MAX_NODE_POOL_SIZE); - self->base_node = - stack_node_new(NULL, NULL, false, 1, length_zero(), &self->node_pool); + self->base_node = stack_node_new(NULL, NULL, false, 1, &self->node_pool); stack_node_retain(self->base_node); array_push(&self->heads, ((StackHead){ self->base_node, @@ -381,25 +372,18 @@ unsigned ts_stack_error_count(const Stack *self, StackVersion version) { return node->error_count; } -bool ts_stack_push(Stack *self, StackVersion version, Tree *tree, +void ts_stack_push(Stack *self, StackVersion version, Tree *tree, bool is_pending, TSStateId state) { StackHead *head = array_get(&self->heads, version); - StackNode *node = head->node; - Length position = node->position; - if (tree) - position = length_add(position, ts_tree_total_size(tree)); - StackNode *new_node = - stack_node_new(node, tree, is_pending, state, position, &self->node_pool); - if (!new_node) - return false; - stack_node_release(node, &self->node_pool); - head->node = new_node; + StackNode *new_node = stack_node_new(head->node, tree, is_pending, state, &self->node_pool); if (state == ERROR_STATE) { new_node->links[0].push_count = head->push_count; head->push_count = 0; - } else + } else { head->push_count++; - return true; + } + stack_node_release(head->node, &self->node_pool); + head->node = new_node; } StackPopResult ts_stack_iterate(Stack *self, StackVersion version, diff --git a/src/runtime/stack.h b/src/runtime/stack.h index e64332dc..a96e2f97 100644 --- a/src/runtime/stack.h +++ b/src/runtime/stack.h @@ -78,10 +78,9 @@ void ts_stack_set_last_external_token(Stack *, StackVersion, Tree *); Length ts_stack_top_position(const Stack *, StackVersion); /* - * Push a tree and state onto the given head of the stack. This could cause - * the version to merge with an existing version. + * Push a tree and state onto the given head of the stack. */ -bool ts_stack_push(Stack *, StackVersion, Tree *, bool, TSStateId); +void ts_stack_push(Stack *, StackVersion, Tree *, bool, TSStateId); /* * Pop the given number of entries from the given version of the stack. This