From c1b6d9f5be4dda2d3306692a2670ca64b4d7d71a Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 1 Sep 2016 11:39:23 -0700 Subject: [PATCH] Improve error comparison criteria Signed-off-by: Nathan Sobo --- spec/fixtures/error_corpus/c_errors.txt | 5 ++- .../error_corpus/javascript_errors.txt | 14 +++---- src/runtime/error_costs.h | 38 ++++++++----------- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/spec/fixtures/error_corpus/c_errors.txt b/spec/fixtures/error_corpus/c_errors.txt index a8a56037..4dfb0894 100644 --- a/spec/fixtures/error_corpus/c_errors.txt +++ b/spec/fixtures/error_corpus/c_errors.txt @@ -122,9 +122,10 @@ int b() { (identifier) (function_declarator (identifier)) (compound_statement (declaration - (ERROR (identifier) (identifier)) (identifier) - (init_declarator (identifier) (number_literal))) + (init_declarator + (ERROR (identifier) (identifier)) + (identifier) (number_literal))) (declaration (ERROR (identifier) (identifier)) (identifier) diff --git a/spec/fixtures/error_corpus/javascript_errors.txt b/spec/fixtures/error_corpus/javascript_errors.txt index d9e88a3e..b8ddc33a 100644 --- a/spec/fixtures/error_corpus/javascript_errors.txt +++ b/spec/fixtures/error_corpus/javascript_errors.txt @@ -14,12 +14,9 @@ e f; (ERROR (identifier)) (identifier) (statement_block - (expression_statement - (ERROR (identifier)) - (identifier)))) - (expression_statement - (ERROR (identifier)) - (identifier))) + (ERROR (identifier)) + (expression_statement (identifier)))) + (expression_statement (ERROR (identifier)) (identifier))) ======================================================= multiple invalid tokens right after the viable prefix @@ -37,9 +34,8 @@ h i j k; (ERROR (identifier) (identifier)) (identifier) (statement_block - (expression_statement - (ERROR (identifier) (identifier) (identifier)) - (identifier)))) + (ERROR (identifier) (identifier) (identifier)) + (expression_statement (identifier)))) (expression_statement (ERROR (identifier) (identifier) (identifier)) (identifier))) diff --git a/src/runtime/error_costs.h b/src/runtime/error_costs.h index c628672e..e7c8ed71 100644 --- a/src/runtime/error_costs.h +++ b/src/runtime/error_costs.h @@ -11,34 +11,28 @@ typedef struct { unsigned push_count; } ErrorStatus; -static double error_threshold(unsigned push_count) { - return 6 * ERROR_COST_PER_SKIPPED_TREE / (1 + push_count / 2); +static inline unsigned error_status_min_cost(ErrorStatus status) { + return status.cost + + ERROR_COST_PER_SKIPPED_TREE * status.count * status.count; +} + +static inline unsigned error_status_max_cost(ErrorStatus status) { + return status.cost + + ERROR_COST_PER_SKIPPED_TREE * status.count * status.count + + (6 * ERROR_COST_PER_SKIPPED_TREE * status.count + 12 * ERROR_COST_PER_SKIPPED_TREE) / + (1 + status.push_count / 2); } static inline int error_status_compare(ErrorStatus a, ErrorStatus b) { - // TODO remove - a.cost += ERROR_COST_PER_SKIPPED_TREE * a.count; - b.cost += ERROR_COST_PER_SKIPPED_TREE * b.count; - - if ((a.count + 1 < b.count) || - (a.count < b.count && a.cost <= b.cost)) { + if ((a.count + 1 < b.count) || (a.count < b.count && a.cost <= b.cost)) return -1; - } - - if ((b.count + 1 < a.count) || - (b.count < a.count && b.cost <= a.cost)) { + if ((a.count > b.count + 1) || (b.count < a.count && b.cost <= a.cost)) return 1; - } - if (a.cost == b.cost) { - if (a.cost + error_threshold(a.push_count) < b.cost) { - return -1; - } - - if (b.cost + error_threshold(b.push_count) < a.cost) { - return 1; - } - } + if (error_status_max_cost(a) < error_status_min_cost(b)) + return -1; + if (error_status_min_cost(a) > error_status_max_cost(b)) + return 1; return 0; }