tree-sitter/src/runtime/document.c

85 lines
2.2 KiB
C
Raw Normal View History

#include "tree_sitter/parser.h"
2014-07-17 23:29:11 -07:00
#include "runtime/node.h"
2015-08-22 10:48:34 -07:00
#include "runtime/tree.h"
#include "runtime/length.h"
#include "runtime/parser.h"
2014-08-01 12:43:14 -07:00
#include "runtime/string_input.h"
#include "runtime/document.h"
2014-01-07 21:50:32 -08:00
TSDocument *ts_document_make() {
TSDocument *document = calloc(sizeof(TSDocument), 1);
document->parser = ts_parser_make();
return document;
}
2014-01-07 21:50:32 -08:00
2015-10-14 21:52:13 -07:00
void ts_document_free(TSDocument *self) {
ts_parser_destroy(&self->parser);
if (self->tree)
ts_tree_release(self->tree);
free(self);
2014-02-20 18:38:31 -08:00
}
2015-10-14 21:52:13 -07:00
const TSLanguage *ts_document_language(TSDocument *self) {
return self->parser.language;
}
2015-10-14 21:52:13 -07:00
void ts_document_set_language(TSDocument *self, const TSLanguage *language) {
self->parser.language = language;
self->tree = NULL;
}
2015-10-14 21:52:13 -07:00
TSDebugger ts_document_debugger(const TSDocument *self) {
return ts_parser_debugger(&self->parser);
}
2015-10-14 21:52:13 -07:00
void ts_document_set_debugger(TSDocument *self, TSDebugger debugger) {
ts_parser_set_debugger(&self->parser, debugger);
2014-09-06 17:56:00 -07:00
}
2015-10-14 21:52:13 -07:00
TSInput ts_document_input(TSDocument *self) {
return self->input;
}
2015-10-14 21:52:13 -07:00
void ts_document_set_input(TSDocument *self, TSInput input) {
self->input = input;
}
2015-10-14 21:52:13 -07:00
void ts_document_set_input_string(TSDocument *self, const char *text) {
ts_document_set_input(self, ts_string_input_make(text));
}
2015-10-14 21:52:13 -07:00
void ts_document_edit(TSDocument *self, TSInputEdit edit) {
if (!self->tree)
2015-09-20 23:41:40 -07:00
return;
2015-10-14 21:52:13 -07:00
size_t max_chars = ts_tree_total_size(self->tree).chars;
if (edit.position > max_chars)
edit.position = max_chars;
if (edit.chars_removed > max_chars - edit.position)
edit.chars_removed = max_chars - edit.position;
2015-10-14 21:52:13 -07:00
ts_tree_edit(self->tree, edit);
}
2015-10-14 21:52:13 -07:00
void ts_document_parse(TSDocument *self) {
if (self->input.read_fn && self->parser.language) {
TSTree *tree = ts_parser_parse(&self->parser, self->input, self->tree);
if (self->tree)
ts_tree_release(self->tree);
self->tree = tree;
ts_tree_retain(tree);
2015-10-14 21:52:13 -07:00
self->parse_count++;
}
2014-01-07 21:50:32 -08:00
}
2014-07-17 23:29:11 -07:00
2015-10-14 21:52:13 -07:00
TSNode ts_document_root_node(const TSDocument *self) {
TSNode result = ts_node_make(self->tree, ts_length_zero());
while (result.data && !ts_tree_is_visible(result.data))
result = ts_node_named_child(result, 0);
return result;
2014-07-17 23:29:11 -07:00
}
2015-10-14 21:52:13 -07:00
size_t ts_document_parse_count(const TSDocument *self) {
return self->parse_count;
}