From 464a0e8dadd58764d42a9c5436607aa2a0daf707 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sat, 15 Mar 2014 15:15:59 -0700 Subject: [PATCH] Clean up parser header further --- include/tree_sitter/parser.h | 23 +++++++++++++---------- include/tree_sitter/runtime.h | 2 +- spec/runtime/parser_spec.cc | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/include/tree_sitter/parser.h b/include/tree_sitter/parser.h index b979e2ee..b9d13915 100644 --- a/include/tree_sitter/parser.h +++ b/include/tree_sitter/parser.h @@ -254,7 +254,7 @@ typedef struct { ts_lexer lexer; ts_stack stack; ts_tree *lookahead; - ts_tree *previous_lookahead; + ts_tree *next_lookahead; } ts_lr_parser; static ts_lr_parser * ts_lr_parser_make() { @@ -267,7 +267,7 @@ static ts_lr_parser * ts_lr_parser_make() { static void ts_lr_parser_reset(ts_lr_parser *parser) { ts_stack_shrink(&parser->stack, 0); parser->lookahead = NULL; - parser->previous_lookahead = NULL; + parser->next_lookahead = NULL; parser->lexer = ts_lexer_make(); } @@ -278,13 +278,13 @@ static ts_symbol ts_lr_parser_lookahead_sym(const ts_lr_parser *parser) { static void ts_lr_parser_shift(ts_lr_parser *parser, state_id parse_state) { ts_stack_push(&parser->stack, parse_state, parser->lookahead); - parser->lookahead = parser->previous_lookahead; - parser->previous_lookahead = NULL; + parser->lookahead = parser->next_lookahead; + parser->next_lookahead = NULL; } static void ts_lr_parser_reduce(ts_lr_parser *parser, ts_symbol symbol, int immediate_child_count, const int *collapse_flags) { ts_tree *lookahead = ts_stack_reduce(&parser->stack, symbol, immediate_child_count, collapse_flags); - parser->previous_lookahead = parser->lookahead; + parser->next_lookahead = parser->lookahead; parser->lookahead = lookahead; } @@ -294,7 +294,15 @@ static int ts_lr_parser_handle_error(ts_lr_parser *parser, size_t count, const t for (;;) { ts_tree_release(parser->lookahead); parser->lookahead = ts_lex(&parser->lexer, ts_lex_state_error); + if (parser->lookahead->symbol == ts_builtin_sym_end) { + parser->stack.entries[0].node = error; + return 0; + } + /* + * Unwind the stack, looking for a state in which this token + * may appear after an error. + */ for (long i = parser->stack.size - 1; i >= 0; i--) { size_t count; state_id to_state; @@ -307,11 +315,6 @@ static int ts_lr_parser_handle_error(ts_lr_parser *parser, size_t count, const t } } } - - if (!ts_lexer_lookahead_char(&parser->lexer)) { - parser->stack.entries[0].node = error; - return 0; - } } } diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index c81dc9a0..21492e52 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -53,7 +53,7 @@ typedef struct { const char **symbol_names; void *data; } ts_parser; - + const ts_tree * ts_parser_parse(ts_parser *, ts_input); void ts_parser_free(ts_parser *); diff --git a/spec/runtime/parser_spec.cc b/spec/runtime/parser_spec.cc index 32f2b09a..3b48bf11 100644 --- a/spec/runtime/parser_spec.cc +++ b/spec/runtime/parser_spec.cc @@ -2,7 +2,7 @@ #include "helpers/spy_reader.h" extern "C" ts_parser ts_parser_json(); - + START_TEST describe("parsing", [&]() {