Assign sizes to error nodes in handle_error

This commit is contained in:
Max Brunsfeld 2014-08-28 18:35:30 -07:00
parent a72b79f7f3
commit 604b149c4b
3 changed files with 25 additions and 0 deletions

View file

@ -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

View file

@ -1,3 +1,4 @@
#include "runtime/lexer.h"
#include "tree_sitter/parser.h"
#include "runtime/tree.h"

View file

@ -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);