diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 452320b4..a36cfdee 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -830,12 +830,23 @@ static void parser__halt_parse(Parser *self) { static void parser__recover(Parser *self, StackVersion version, Tree *lookahead) { bool did_recover = false; unsigned previous_version_count = ts_stack_version_count(self->stack); + Length position = ts_stack_top_position(self->stack, version); StackSummary *summary = ts_stack_get_summary(self->stack, version); for (unsigned i = 0; i < summary->size; i++) { StackSummaryEntry entry = summary->contents[i]; if (entry.state == ERROR_STATE) continue; unsigned depth = entry.depth + ts_stack_depth_since_error(self->stack, version); + ErrorStatus status = { + .recovering = false, + .push_count = 0, + .cost = + depth * ERROR_COST_PER_SKIPPED_TREE + + (position.chars - entry.position.chars) * ERROR_COST_PER_SKIPPED_CHAR + + (position.extent.row - entry.position.extent.row) * ERROR_COST_PER_SKIPPED_LINE + }; + if (parser__better_version_exists(self, version, status)) break; + unsigned count = 0; if (ts_language_actions(self->language, entry.state, lookahead->symbol, &count) && count > 0) { LOG("recover state:%u, depth:%u", entry.state, depth); diff --git a/src/runtime/stack.c b/src/runtime/stack.c index ae1e175a..a4204dce 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -492,7 +492,11 @@ inline StackIterateAction summarize_stack_callback(void *payload, const Iterator if (entry.depth < depth) break; if (entry.depth == depth && entry.state == state) return StackIterateNone; } - array_push(session->summary, ((StackSummaryEntry){.depth = depth, .state = state})); + array_push(session->summary, ((StackSummaryEntry){ + .position = iterator->node->position, + .depth = depth, + .state = state, + })); return StackIterateNone; } diff --git a/src/runtime/stack.h b/src/runtime/stack.h index ce0a30ed..9fd925e5 100644 --- a/src/runtime/stack.h +++ b/src/runtime/stack.h @@ -34,6 +34,7 @@ enum { }; typedef struct { + Length position; unsigned depth; TSStateId state; } StackSummaryEntry;