2014-02-18 09:07:00 -08:00
|
|
|
#include "tree_sitter/runtime.h"
|
2014-03-01 22:43:25 -08:00
|
|
|
#include <string.h>
|
2014-01-07 21:50:32 -08:00
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
struct TSDocument {
|
2014-03-13 00:43:23 -07:00
|
|
|
ts_parser parser;
|
2014-06-28 18:45:22 -07:00
|
|
|
const TSTree *tree;
|
2014-03-12 13:39:12 -07:00
|
|
|
ts_input input;
|
2014-02-24 18:42:54 -08:00
|
|
|
size_t error_count;
|
2014-01-07 21:50:32 -08:00
|
|
|
};
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
TSDocument * ts_document_make() {
|
|
|
|
|
return malloc(sizeof(TSDocument));
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
void ts_document_free(TSDocument *document) {
|
2014-06-09 13:02:39 -07:00
|
|
|
if (document->parser.free_fn)
|
|
|
|
|
document->parser.free_fn(document->parser.data);
|
|
|
|
|
if (document->input.release_fn)
|
|
|
|
|
document->input.release_fn(document->input.data);
|
2014-03-08 16:30:44 -08:00
|
|
|
free(document);
|
2014-02-20 18:38:31 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
void ts_document_set_parser(TSDocument *document, ts_parser parser) {
|
2014-03-13 00:43:23 -07:00
|
|
|
document->parser = parser;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:45:22 -07:00
|
|
|
const TSTree * ts_document_tree(const TSDocument *document) {
|
2014-01-07 21:50:32 -08:00
|
|
|
return document->tree;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
const char * ts_document_string(const TSDocument *document) {
|
2014-03-13 00:43:23 -07:00
|
|
|
return ts_tree_string(document->tree, document->parser.symbol_names);
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
void ts_document_set_input(TSDocument *document, ts_input input) {
|
2014-03-12 13:39:12 -07:00
|
|
|
document->input = input;
|
2014-05-09 15:03:29 -07:00
|
|
|
document->tree = document->parser.parse_fn(document->parser.data, input, NULL);
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
void ts_document_edit(TSDocument *document, ts_input_edit edit) {
|
2014-05-09 15:03:29 -07:00
|
|
|
document->tree = document->parser.parse_fn(document->parser.data, document->input, &edit);
|
2014-03-12 13:39:12 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:45:22 -07:00
|
|
|
const char * ts_document_symbol_name(const TSDocument *document, const TSTree *tree) {
|
2014-03-24 00:34:13 -07:00
|
|
|
return document->parser.symbol_names[ts_tree_symbol(tree)];
|
2014-03-19 19:27:31 -07:00
|
|
|
}
|
|
|
|
|
|
2014-03-01 22:43:25 -08:00
|
|
|
typedef struct {
|
|
|
|
|
const char *string;
|
|
|
|
|
size_t position;
|
|
|
|
|
size_t length;
|
|
|
|
|
} ts_string_input_data;
|
|
|
|
|
|
2014-03-08 16:51:08 -08:00
|
|
|
const char * ts_string_input_read(void *d, size_t *bytes_read) {
|
2014-03-01 22:43:25 -08:00
|
|
|
ts_string_input_data *data = (ts_string_input_data *)d;
|
2014-03-10 13:25:31 -07:00
|
|
|
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;
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ts_string_input_seek(void *d, size_t position) {
|
|
|
|
|
ts_string_input_data *data = (ts_string_input_data *)d;
|
|
|
|
|
data->position = position;
|
|
|
|
|
return (position < data->length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ts_input ts_string_input_make(const char *string) {
|
2014-03-08 16:30:44 -08:00
|
|
|
ts_string_input_data *data = malloc(sizeof(ts_string_input_data));
|
2014-03-01 22:43:25 -08:00
|
|
|
data->string = string;
|
|
|
|
|
data->position = 0;
|
|
|
|
|
data->length = strlen(string);
|
|
|
|
|
ts_input input = {
|
|
|
|
|
.data = (void *)data,
|
|
|
|
|
.read_fn = ts_string_input_read,
|
|
|
|
|
.seek_fn = ts_string_input_seek,
|
|
|
|
|
.release_fn = free,
|
|
|
|
|
};
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
void ts_document_set_input_string(TSDocument *document, const char *text) {
|
2014-03-01 22:43:25 -08:00
|
|
|
ts_document_set_input(document, ts_string_input_make(text));
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|