diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 9403fbc5..1985487f 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -662,6 +662,7 @@ void ts_parser_set_debugger(TSParser *self, TSDebugger debugger) { TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) { ts_parser__start(self, input, previous_tree); + size_t max_position = 0; for (;;) { TSTree *lookahead = NULL; @@ -671,22 +672,35 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) { self->is_split = ts_stack_head_count(self->stack) > 1; for (int head = 0; head < ts_stack_head_count(self->stack);) { - last_position = position; - position = ts_stack_top_position(self->stack, head); + for (;;) { + last_position = position; + position = ts_stack_top_position(self->stack, head); - LOG("process head:%d, head_count:%d, state:%d, pos:%lu", head, - ts_stack_head_count(self->stack), - ts_stack_top_state(self->stack, head), position.chars); + if (position.chars > max_position) { + max_position = position.chars; + head++; + break; + } - if (position.chars != last_position.chars || - !ts_parser__can_reuse(self, head, lookahead)) - lookahead = ts_parser__get_next_lookahead(self, head); + if (position.chars == max_position && head > 0) { + head++; + break; + } - LOG("lookahead sym:%s, size:%lu", SYM_NAME(lookahead->symbol), - ts_tree_total_chars(lookahead)); + LOG("process head:%d, head_count:%d, state:%d, pos:%lu", head, + ts_stack_head_count(self->stack), + ts_stack_top_state(self->stack, head), position.chars); - if (ts_parser__consume_lookahead(self, head, lookahead)) - head++; + if (position.chars != last_position.chars || + !ts_parser__can_reuse(self, head, lookahead)) + lookahead = ts_parser__get_next_lookahead(self, head); + + LOG("lookahead sym:%s, size:%lu", SYM_NAME(lookahead->symbol), + ts_tree_total_chars(lookahead)); + + if (!ts_parser__consume_lookahead(self, head, lookahead)) + break; + } } if (ts_stack_head_count(self->stack) == 0) {