From 604b149c4b4e21afee41ec80b6e31969205f93d2 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 28 Aug 2014 18:35:30 -0700 Subject: [PATCH] Assign sizes to error nodes in handle_error --- spec/runtime/node_spec.cc | 22 ++++++++++++++++++++++ src/runtime/lexer.c | 1 + src/runtime/parser.c | 2 ++ 3 files changed, 25 insertions(+) diff --git a/spec/runtime/node_spec.cc b/spec/runtime/node_spec.cc index c0fa30d2..bc740515 100644 --- a/spec/runtime/node_spec.cc +++ b/spec/runtime/node_spec.cc @@ -152,6 +152,28 @@ describe("Node", []() { ts_node_release(node); }); }); + + describe("error nodes", [&]() { + before_each([&]() { + ts_document_set_input_string(document, " [123, total nonsense, true]"); + AssertThat(ts_node_string(ts_document_root_node(document)), Equals( + "(DOCUMENT (array (number) (ERROR 'o') (true)))")); + + root = ts_document_root_node(document); + }); + + it("computes their size and position correctly", [&]() { + TSNode *array = ts_node_child(root, 0); + TSNode *error = ts_node_child(array, 1); + + AssertThat(ts_node_name(error), Equals("error")); + AssertThat(ts_node_pos(error), Equals(string(" [123,").length())) + AssertThat(ts_node_size(error), Equals(string(" total nonsense").length())) + + ts_node_release(array); + ts_node_release(error); + }); + }); }); END_TEST diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 080a86a6..d5be2e18 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -1,3 +1,4 @@ +#include "runtime/lexer.h" #include "tree_sitter/parser.h" #include "runtime/tree.h" diff --git a/src/runtime/parser.c b/src/runtime/parser.c index a521c1b0..58365ae9 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -132,6 +132,7 @@ static void lex(TSParser *parser, TSStateId lex_state) { static int handle_error(TSParser *parser) { TSTree *error = parser->lookahead; ts_tree_retain(error); + size_t start_position = ts_lexer_position(&parser->lexer); for (;;) { @@ -166,6 +167,7 @@ static int handle_error(TSParser *parser) { if (action_after_error.type != TSParseActionTypeError) { DEBUG_PARSE("RECOVER %u", state_after_error); + error->size = ts_lexer_position(&parser->lexer) - start_position + 1; ts_stack_shrink(&parser->stack, i + 1); ts_stack_push(&parser->stack, state_after_error, error); ts_tree_release(error);