diff --git a/project.gyp b/project.gyp index 4e998048..f348a616 100644 --- a/project.gyp +++ b/project.gyp @@ -102,6 +102,7 @@ 'src/runtime/node.c', 'src/runtime/parser.c', 'src/runtime/stack.c', + 'src/runtime/string_input.c', 'src/runtime/tree.c', ], 'cflags_c': [ diff --git a/src/runtime/document.c b/src/runtime/document.c index a734998f..0f8a95af 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -3,7 +3,7 @@ #include "runtime/tree.h" #include "runtime/node.h" #include "runtime/parser.h" -#include +#include "runtime/string_input.h" struct TSDocument { TSParser parser; @@ -59,41 +59,6 @@ const char *ts_document_symbol_name(const TSDocument *document, return document->parser.language->symbol_names[tree->symbol]; } -typedef struct { - const char *string; - size_t position; - size_t length; -} TSStringInput; - -const char *ts_string_input_read(void *d, size_t *bytes_read) { - TSStringInput *data = (TSStringInput *)d; - if (data->position >= data->length) { - *bytes_read = 0; - return ""; - } - size_t previous_position = data->position; - data->position = data->length; - *bytes_read = data->position - previous_position; - return data->string + previous_position; -} - -int ts_string_input_seek(void *d, size_t position) { - TSStringInput *data = (TSStringInput *)d; - data->position = position; - return (position < data->length); -} - -TSInput ts_string_input_make(const char *string) { - TSStringInput *data = malloc(sizeof(TSStringInput)); - data->string = string; - data->position = 0; - data->length = strlen(string); - return (TSInput) { .data = (void *)data, - .read_fn = ts_string_input_read, - .seek_fn = ts_string_input_seek, - .release_fn = free }; -} - void ts_document_set_input_string(TSDocument *document, const char *text) { ts_document_set_input(document, ts_string_input_make(text)); } diff --git a/src/runtime/string_input.c b/src/runtime/string_input.c new file mode 100644 index 00000000..f55ac36d --- /dev/null +++ b/src/runtime/string_input.c @@ -0,0 +1,37 @@ +#include "runtime/string_input.h" +#include + +typedef struct { + const char *string; + size_t position; + size_t length; +} TSStringInput; + +const char *ts_string_input_read(void *d, size_t *bytes_read) { + TSStringInput *data = (TSStringInput *)d; + if (data->position >= data->length) { + *bytes_read = 0; + return ""; + } + size_t previous_position = data->position; + data->position = data->length; + *bytes_read = data->position - previous_position; + return data->string + previous_position; +} + +int ts_string_input_seek(void *d, size_t position) { + TSStringInput *data = (TSStringInput *)d; + data->position = position; + return (position < data->length); +} + +TSInput ts_string_input_make(const char *string) { + TSStringInput *data = malloc(sizeof(TSStringInput)); + data->string = string; + data->position = 0; + data->length = strlen(string); + return (TSInput) { .data = (void *)data, + .read_fn = ts_string_input_read, + .seek_fn = ts_string_input_seek, + .release_fn = free }; +} diff --git a/src/runtime/string_input.h b/src/runtime/string_input.h new file mode 100644 index 00000000..fe5e4b1f --- /dev/null +++ b/src/runtime/string_input.h @@ -0,0 +1,16 @@ +#ifndef RUNTIME_STRING_INPUT_H_ +#define RUNTIME_STRING_INPUT_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "tree_sitter/runtime.h" + +TSInput ts_string_input_make(const char *); + +#ifdef __cplusplus +} +#endif + +#endif // RUNTIME_STRING_INPUT_H_