Merge pull request #59 from tree-sitter/input-string-with-length

Input string with length
This commit is contained in:
Timothy Clem 2017-02-13 09:58:46 -08:00 committed by GitHub
commit 87b8b932e5
5 changed files with 27 additions and 1 deletions

View file

@ -104,6 +104,7 @@ void ts_document_set_language(TSDocument *, const TSLanguage *);
TSInput ts_document_input(TSDocument *);
void ts_document_set_input(TSDocument *, TSInput);
void ts_document_set_input_string(TSDocument *, const char *);
void ts_document_set_input_string_with_length(TSDocument *, const char *, uint32_t);
TSLogger ts_document_logger(const TSDocument *);
void ts_document_set_logger(TSDocument *, TSLogger);
void ts_document_print_debugging_graphs(TSDocument *, bool);

View file

@ -116,6 +116,17 @@ describe("Document", [&]() {
AssertThat(spy_input->strings_read, Equals(vector<string>({" [null, 2" })));
});
it("allows setting input string with length", [&]() {
const char content[] = { '1' };
ts_document_set_input_string_with_length(document, content, 1);
ts_document_parse(document);
TSNode new_root = ts_document_root_node(document);
AssertThat(ts_node_end_char(new_root), Equals<size_t>(1));
assert_node_string_equals(
new_root,
"(number)");
});
it("reads from the new input correctly when the old input was blank", [&]() {
ts_document_set_input_string(document, "");
ts_document_parse(document);

View file

@ -77,6 +77,15 @@ void ts_document_set_input_string(TSDocument *self, const char *text) {
}
}
void ts_document_set_input_string_with_length(TSDocument *self, const char *text, uint32_t length) {
ts_document_invalidate(self);
TSInput input = ts_string_input_make_with_length(text, length);
ts_document_set_input(self, input);
if (input.payload) {
self->owns_input = true;
}
}
void ts_document_edit(TSDocument *self, TSInputEdit edit) {
if (!self->tree)
return;

View file

@ -27,13 +27,17 @@ int ts_string_input_seek(void *payload, uint32_t character, uint32_t byte) {
}
TSInput ts_string_input_make(const char *string) {
return ts_string_input_make_with_length(string, strlen(string));
}
TSInput ts_string_input_make_with_length(const char *string, uint32_t length) {
TSStringInput *input = ts_malloc(sizeof(TSStringInput));
if (!input)
goto error;
input->string = string;
input->position = 0;
input->length = strlen(string);
input->length = length;
return (TSInput){
.payload = input,
.read = ts_string_input_read,

View file

@ -8,6 +8,7 @@ extern "C" {
#include "tree_sitter/runtime.h"
TSInput ts_string_input_make(const char *);
TSInput ts_string_input_make_with_length(const char *, uint32_t);
#ifdef __cplusplus
}