tree-sitter/src/runtime/document.c

115 lines
3.3 KiB
C
Raw Normal View History

2014-02-18 09:07:00 -08:00
#include "tree_sitter/runtime.h"
#include "tree_sitter/parser.h"
#include "runtime/tree.h"
2014-07-17 23:29:11 -07:00
#include "runtime/node.h"
#include "runtime/parser.h"
#include <string.h>
2014-01-07 21:50:32 -08:00
2014-06-28 18:37:29 -07:00
struct TSDocument {
TSParser parser;
2014-07-20 20:27:33 -07:00
const TSTree *tree;
TSInput input;
2014-01-07 21:50:32 -08:00
};
TSDocument *ts_document_make() {
2014-07-20 20:27:33 -07:00
TSDocument *document = malloc(sizeof(TSDocument));
2014-07-29 12:59:18 -07:00
*document = (TSDocument) {
.input = (TSInput) { .data = NULL, .read_fn = NULL, .seek_fn = NULL },
.tree = NULL
};
2014-07-20 20:27:33 -07:00
return document;
2014-01-07 21:50:32 -08:00
}
2014-06-28 18:37:29 -07:00
void ts_document_free(TSDocument *document) {
ts_parser_destroy(&document->parser);
2014-07-20 20:27:33 -07:00
if (document->input.release_fn)
document->input.release_fn(document->input.data);
free(document);
2014-02-20 18:38:31 -08:00
}
2014-07-31 13:11:39 -07:00
void ts_document_set_language(TSDocument *document,
const TSLanguage *language) {
ts_parser_destroy(&document->parser);
document->parser = ts_parser_make(language);
if (document->input.read_fn)
document->tree = ts_parser_parse(&document->parser, document->input, NULL);
2014-01-07 21:50:32 -08: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
}
const char *ts_document_string(const TSDocument *document) {
2014-07-31 13:11:39 -07:00
return ts_tree_string(document->tree,
document->parser.language->symbol_names);
}
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;
if (document->parser.language)
document->tree = ts_parser_parse(&document->parser, document->input, NULL);
}
void ts_document_edit(TSDocument *document, TSInputEdit edit) {
document->tree = ts_parser_parse(&document->parser, document->input, &edit);
}
const char *ts_document_symbol_name(const TSDocument *document,
const TSTree *tree) {
return document->parser.language->symbol_names[tree->symbol];
}
typedef struct {
2014-07-20 20:27:33 -07:00
const char *string;
size_t position;
size_t length;
} TSStringInput;
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;
}
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-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);
return (TSInput) { .data = (void *)data,
.read_fn = ts_string_input_read,
.seek_fn = ts_string_input_seek,
.release_fn = free };
}
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) {
if (document->tree)
return ts_node_make_root(document->tree,
document->parser.language->symbol_names);
else
return NULL;
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
}