Reset lexer correctly when old input was blank

This commit is contained in:
Max Brunsfeld 2015-12-03 10:00:39 -08:00
parent e32fe07432
commit 8a146a9bef
4 changed files with 28 additions and 6 deletions

View file

@ -66,6 +66,20 @@ describe("Document", [&]() {
"(object (string) (array (null) (number)))"));
AssertThat(spy_input->strings_read, Equals(vector<string>({" [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 <EOF>))"));
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)", [&]() {

View file

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

View file

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

View file

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