Move StringInput into its own file
This commit is contained in:
parent
41d26aaceb
commit
03a5a97992
4 changed files with 55 additions and 36 deletions
|
|
@ -3,7 +3,7 @@
|
|||
#include "runtime/tree.h"
|
||||
#include "runtime/node.h"
|
||||
#include "runtime/parser.h"
|
||||
#include <string.h>
|
||||
#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));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue