From ef87ed6130910584d548c7f65961375bdc61b745 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 19 Aug 2019 17:11:39 -0700 Subject: [PATCH] Check once for cancellation at the beginning of a parse --- lib/src/parser.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/src/parser.c b/lib/src/parser.c index 29a1c96b..1570bed1 100644 --- a/lib/src/parser.c +++ b/lib/src/parser.c @@ -1290,15 +1290,18 @@ static bool ts_parser__advance( } for (;;) { + // If a cancellation flag or a timeout was provided, then check every + // time a fixed number of parse actions has been processed. if (++self->operation_count == OP_COUNT_PER_TIMEOUT_CHECK) { self->operation_count = 0; - if ( - (self->cancellation_flag && atomic_load(self->cancellation_flag)) || - (!clock_is_null(self->end_clock) && clock_is_gt(clock_now(), self->end_clock)) - ) { - ts_subtree_release(&self->tree_pool, lookahead); - return false; - } + } + if ( + self->operation_count == 0 && + ((self->cancellation_flag && atomic_load(self->cancellation_flag)) || + (!clock_is_null(self->end_clock) && clock_is_gt(clock_now(), self->end_clock))) + ) { + ts_subtree_release(&self->tree_pool, lookahead); + return false; } StackVersion last_reduction_version = STACK_VERSION_NONE;