2014-02-18 09:07:00 -08:00
|
|
|
#include "tree_sitter/runtime.h"
|
2014-07-10 13:14:52 -07:00
|
|
|
#include "tree_sitter/parser.h"
|
2014-07-14 21:11:15 -07:00
|
|
|
#include "runtime/tree.h"
|
2014-07-17 23:29:11 -07:00
|
|
|
#include "runtime/node.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-07-20 20:27:33 -07:00
|
|
|
TSParser *parser;
|
|
|
|
|
const TSTree *tree;
|
|
|
|
|
TSInput input;
|
|
|
|
|
size_t error_count;
|
2014-01-07 21:50:32 -08:00
|
|
|
};
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
TSDocument * ts_document_make() {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSDocument *document = malloc(sizeof(TSDocument));
|
|
|
|
|
*document = (TSDocument) {
|
|
|
|
|
.input = (TSInput) {}
|
|
|
|
|
};
|
|
|
|
|
return document;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
void ts_document_free(TSDocument *document) {
|
2014-07-20 20:27:33 -07:00
|
|
|
if (document->parser)
|
|
|
|
|
ts_parser_free(document->parser);
|
|
|
|
|
if (document->input.release_fn)
|
|
|
|
|
document->input.release_fn(document->input.data);
|
|
|
|
|
free(document);
|
2014-02-20 18:38:31 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-10 13:14:52 -07:00
|
|
|
void ts_document_set_parser(TSDocument *document, TSParser *parser) {
|
2014-07-20 20:27:33 -07:00
|
|
|
if (document->parser)
|
|
|
|
|
ts_parser_free(document->parser);
|
|
|
|
|
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-07-20 20:27:33 -07:00
|
|
|
return document->tree;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
const char * ts_document_string(const TSDocument *document) {
|
2014-07-20 20:27:33 -07:00
|
|
|
return ts_tree_string(document->tree, ts_parser_config(document->parser).symbol_names);
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:56:04 -07:00
|
|
|
void ts_document_set_input(TSDocument *document, TSInput input) {
|
2014-07-20 20:27:33 -07:00
|
|
|
document->input = input;
|
|
|
|
|
document->tree = ts_parser_parse(document->parser, document->input, NULL);
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:56:47 -07:00
|
|
|
void ts_document_edit(TSDocument *document, TSInputEdit edit) {
|
2014-07-20 20:27:33 -07:00
|
|
|
document->tree = ts_parser_parse(document->parser, 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-07-20 20:27:33 -07:00
|
|
|
return ts_parser_config(document->parser).symbol_names[tree->symbol];
|
2014-03-19 19:27:31 -07:00
|
|
|
}
|
|
|
|
|
|
2014-03-01 22:43:25 -08:00
|
|
|
typedef struct {
|
2014-07-20 20:27:33 -07:00
|
|
|
const char *string;
|
|
|
|
|
size_t position;
|
|
|
|
|
size_t length;
|
2014-06-28 19:29:44 -07:00
|
|
|
} TSStringInput;
|
2014-03-01 22:43:25 -08:00
|
|
|
|
2014-03-08 16:51:08 -08:00
|
|
|
const char * ts_string_input_read(void *d, size_t *bytes_read) {
|
2014-07-20 20:27:33 -07:00
|
|
|
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;
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ts_string_input_seek(void *d, size_t position) {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSStringInput *data = (TSStringInput *)d;
|
|
|
|
|
data->position = position;
|
|
|
|
|
return (position < data->length);
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:56:04 -07:00
|
|
|
TSInput ts_string_input_make(const char *string) {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSStringInput *data = malloc(sizeof(TSStringInput));
|
|
|
|
|
data->string = string;
|
|
|
|
|
data->position = 0;
|
|
|
|
|
data->length = strlen(string);
|
|
|
|
|
TSInput input = {
|
|
|
|
|
.data = (void *)data,
|
|
|
|
|
.read_fn = ts_string_input_read,
|
|
|
|
|
.seek_fn = ts_string_input_seek,
|
|
|
|
|
.release_fn = free,
|
|
|
|
|
};
|
|
|
|
|
return input;
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
void ts_document_set_input_string(TSDocument *document, const char *text) {
|
2014-07-20 20:27:33 -07:00
|
|
|
ts_document_set_input(document, ts_string_input_make(text));
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
2014-07-17 23:29:11 -07:00
|
|
|
|
|
|
|
|
TSNode * ts_document_root_node(const TSDocument *document) {
|
2014-07-20 20:27:33 -07:00
|
|
|
return ts_node_make_root(document->tree, document->parser->config.symbol_names);
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSNode * ts_document_get_node(const TSDocument *document, size_t pos) {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSNode *root = ts_document_root_node(document);
|
|
|
|
|
TSNode *result = ts_node_leaf_at_pos(root, pos);
|
|
|
|
|
ts_node_release(root);
|
|
|
|
|
return result;
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|