Move StringInput into its own file

This commit is contained in:
Max Brunsfeld 2014-08-01 12:43:14 -07:00
parent 41d26aaceb
commit 03a5a97992
4 changed files with 55 additions and 36 deletions

View file

@ -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': [

View file

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

View file

@ -0,0 +1,37 @@
#include "runtime/string_input.h"
#include <string.h>
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 };
}

View file

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