From 9538b5b879e7e369712971b2d21edb680b9e7272 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sun, 26 Jun 2016 22:45:19 -0700 Subject: [PATCH] Don't count extra trees toward stack versions' error costs --- .../error_corpus/javascript_errors.txt | 29 +++++++++++++++++-- src/runtime/stack.c | 18 ++++++++---- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/spec/fixtures/error_corpus/javascript_errors.txt b/spec/fixtures/error_corpus/javascript_errors.txt index 5c067d47..cf171f55 100644 --- a/spec/fixtures/error_corpus/javascript_errors.txt +++ b/spec/fixtures/error_corpus/javascript_errors.txt @@ -123,8 +123,7 @@ var (var_assignment (identifier) (number)) (ERROR) (identifier) (var_assignment (identifier) (number)) - (ERROR (identifier)) - (identifier))) + (var_assignment (identifier) (ERROR) (identifier)))) ============================================ Misplaced expressions in var declarations @@ -150,3 +149,29 @@ var x; (var_assignment (identifier) (number)) (var_assignment (identifier) (number))) (var_declaration (identifier))) + +================================================ +Mismatched delimiters in var declarations +================================================ + +var + // keep this + a, + + // skip this + [ + + // keep this + b = 1; + +var c; + +--- + +(program + (var_declaration + (comment) + (identifier) + (ERROR (comment) (comment)) + (var_assignment (identifier) (number))) + (var_declaration (identifier))) diff --git a/src/runtime/stack.c b/src/runtime/stack.c index c3412e08..07eb0676 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -91,8 +91,6 @@ static StackNode *stack_node_new(StackNode *next, TSTree *tree, bool is_pending, else if (!(node = ts_malloc(sizeof(StackNode)))) return NULL; - bool is_error = (state == ts_parse_state_error); - *node = (StackNode){ .ref_count = 1, .link_count = 0, @@ -100,21 +98,29 @@ static StackNode *stack_node_new(StackNode *next, TSTree *tree, bool is_pending, .state = state, .position = position, .error_depth = 0, - .error_cost = is_error ? 1 : 0, + .error_cost = 0, }; if (next) { stack_node_retain(next); - node->links[0] = (StackLink){ next, tree, is_pending }; node->link_count = 1; - node->error_cost += next->error_cost; + node->links[0] = (StackLink){ next, tree, is_pending }; + + node->error_cost = next->error_cost; node->error_depth = next->error_depth; if (tree) { ts_tree_retain(tree); - node->error_cost += tree->error_size; + if (state == ts_parse_state_error) { + if (!tree->extra) { + node->error_cost++; + } + } else { + node->error_cost += tree->error_size; + } } else { + node->error_cost++; node->error_depth++; } }