From 1d9d6f37ad770df8eed4c8ff80dc8863616f6ef3 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 2 Apr 2018 11:52:34 -0700 Subject: [PATCH] Introduce an error cost per error instance to favor fewer errors --- src/runtime/error_costs.h | 1 + src/runtime/stack.c | 10 +++++++--- src/runtime/tree.c | 11 +++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/runtime/error_costs.h b/src/runtime/error_costs.h index f543b3ff..d6420488 100644 --- a/src/runtime/error_costs.h +++ b/src/runtime/error_costs.h @@ -2,6 +2,7 @@ #define RUNTIME_ERROR_COSTS_H_ #define ERROR_STATE 0 +#define ERROR_COST_PER_RECOVERY 500 #define ERROR_COST_PER_MISSING_TREE 110 #define ERROR_COST_PER_SKIPPED_TREE 100 #define ERROR_COST_PER_SKIPPED_LINE 30 diff --git a/src/runtime/stack.c b/src/runtime/stack.c index f993f59f..17a301fe 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -399,7 +399,9 @@ void ts_stack_set_last_external_token(Stack *self, StackVersion version, Tree *t unsigned ts_stack_error_cost(const Stack *self, StackVersion version) { StackHead *head = array_get(&self->heads, version); - return head->node->error_cost; + unsigned result = head->node->error_cost; + if (head->node->state == ERROR_STATE) result += ERROR_COST_PER_RECOVERY; + return result; } unsigned ts_stack_node_count_since_error(const Stack *self, StackVersion version) { @@ -668,8 +670,10 @@ bool ts_stack_print_dot_graph(Stack *self, const char **symbol_names, FILE *f) { fprintf(f, "color=red "); } fprintf(f, - "label=%u, fontcolor=blue, weight=10000, labeltooltip=\"node_count: %u", - i, head->node->node_count - head->node_count_at_last_error + "label=%u, fontcolor=blue, weight=10000, labeltooltip=\"node_count: %u\nerror_cost: %u", + i, + ts_stack_node_count_since_error(self, i), + ts_stack_error_cost(self, i) ); if (head->last_external_token) { diff --git a/src/runtime/tree.c b/src/runtime/tree.c index c58c987a..f88895d2 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -354,11 +354,14 @@ void ts_tree_set_children(Tree *self, uint32_t child_count, Tree **children, } if (self->symbol == ts_builtin_sym_error) { - self->error_cost += ERROR_COST_PER_SKIPPED_CHAR * self->size.bytes + + self->error_cost += ERROR_COST_PER_RECOVERY + + ERROR_COST_PER_SKIPPED_CHAR * self->size.bytes + ERROR_COST_PER_SKIPPED_LINE * self->size.extent.row; - for (uint32_t i = 0; i < child_count; i++) - if (!self->children[i]->extra) + for (uint32_t i = 0; i < child_count; i++) { + if (!self->children[i]->extra) { self->error_cost += ERROR_COST_PER_SKIPPED_TREE; + } + } } if (child_count > 0) { @@ -418,7 +421,7 @@ Tree *ts_tree_make_error_node(TreePool *pool, TreeArray *children, const TSLangu Tree *ts_tree_make_missing_leaf(TreePool *pool, TSSymbol symbol, const TSLanguage *language) { Tree *result = ts_tree_make_leaf(pool, symbol, length_zero(), length_zero(), language); result->is_missing = true; - result->error_cost = ERROR_COST_PER_MISSING_TREE; + result->error_cost = ERROR_COST_PER_MISSING_TREE + ERROR_COST_PER_RECOVERY; return result; }