Clean up main parser loop

This commit is contained in:
Max Brunsfeld 2015-12-24 10:21:42 -08:00
parent f0c89f36f7
commit f483178f83

View file

@ -198,7 +198,11 @@ static TSTree *ts_parser__get_next_lookahead(TSParser *self, int head) {
return result;
}
return NULL;
ts_lexer_reset(&self->lexer, position);
TSStateId parse_state = ts_stack_top_state(self->stack, head);
TSStateId lex_state = self->language->lex_states[parse_state];
LOG("lex state:%d", lex_state);
return self->language->lex_fn(&self->lexer, lex_state);
}
static int ts_parser__split(TSParser *self, int head) {
@ -648,29 +652,22 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) {
for (;;) {
TSTree *lookahead = NULL;
TSLength position = ts_length_zero(), last_position;
TSLength last_position = ts_length_zero();
TSLength position = ts_length_zero();
self->is_split = ts_stack_head_count(self->stack) > 1;
for (int head = 0; head < ts_stack_head_count(self->stack);) {
StackEntry *entry = ts_stack_head(self->stack, head);
last_position = position;
position = entry ? entry->position : ts_length_zero();
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 (!ts_parser__can_reuse(self, head, lookahead) ||
position.chars != last_position.chars) {
if (position.chars != last_position.chars ||
!ts_parser__can_reuse(self, head, lookahead))
lookahead = ts_parser__get_next_lookahead(self, head);
if (!lookahead) {
ts_lexer_reset(&self->lexer, position);
TSStateId parse_state = ts_stack_top_state(self->stack, head);
TSStateId lex_state = self->language->lex_states[parse_state];
LOG("lex state:%d", lex_state);
lookahead = self->language->lex_fn(&self->lexer, lex_state);
}
}
LOG("lookahead sym:%s, size:%lu", SYM_NAME(lookahead->symbol),
ts_tree_total_chars(lookahead));