diff --git a/spec/runtime/document_spec.cc b/spec/runtime/document_spec.cc index eeef1dfd..aa840743 100644 --- a/spec/runtime/document_spec.cc +++ b/spec/runtime/document_spec.cc @@ -66,6 +66,20 @@ describe("Document", [&]() { "(object (string) (array (null) (number)))")); AssertThat(spy_input->strings_read, Equals(vector({" [null, 2", ""}))); }); + + it("reads from the new input correctly when the old input was blank", [&]() { + ts_document_set_input_string(doc, ""); + ts_document_parse(doc); + TSNode new_root = ts_document_root_node(doc); + AssertThat(ts_node_string(new_root, doc), Equals( + "(ERROR (UNEXPECTED ))")); + + ts_document_set_input_string(doc, "1"); + ts_document_parse(doc); + new_root = ts_document_root_node(doc); + AssertThat(ts_node_string(new_root, doc), Equals( + "(number)")); + }); }); describe("set_language(language)", [&]() { diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 36157b25..bc6b5bd9 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -123,10 +123,7 @@ TSLexer ts_lexer_make() { return result; } -void ts_lexer_reset(TSLexer *self, TSLength position, TSPoint point) { - if (ts_length_eq(position, self->current_position)) - return; - +static inline void ts_lexer__reset(TSLexer *self, TSLength position, TSPoint point) { self->token_start_position = position; self->token_end_position = position; self->current_position = position; @@ -141,3 +138,14 @@ void ts_lexer_reset(TSLexer *self, TSLength position, TSPoint point) { self->lookahead_size = 0; self->lookahead = 0; } + +void ts_lexer_set_input(TSLexer *self, TSInput input) { + self->input = input; + ts_lexer__reset(self, ts_length_zero(), ts_point_zero()); +} + +void ts_lexer_reset(TSLexer *self, TSLength position, TSPoint point) { + if (!ts_length_eq(position, self->current_position)) + ts_lexer__reset(self, position, point); + return; +} diff --git a/src/runtime/lexer.h b/src/runtime/lexer.h index 2c4b5812..3dfe4ff5 100644 --- a/src/runtime/lexer.h +++ b/src/runtime/lexer.h @@ -8,6 +8,7 @@ extern "C" { #include "tree_sitter/parser.h" TSLexer ts_lexer_make(); +void ts_lexer_set_input(TSLexer *, TSInput); void ts_lexer_reset(TSLexer *, TSLength, TSPoint); #ifdef __cplusplus diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 485fd2f3..50e7fb80 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -342,8 +342,7 @@ static void ts_parser__start(TSParser *self, TSInput input, LOG("new_parse"); } - self->lexer.input = input; - ts_lexer_reset(&self->lexer, ts_length_zero(), ts_point_zero()); + ts_lexer_set_input(&self->lexer, input); ts_stack_clear(self->stack); LookaheadState head_state = {