2016-01-15 15:08:42 -08:00
|
|
|
#include "runtime/alloc.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"
|
2014-07-30 23:40:02 -07:00
|
|
|
#include "runtime/parser.h"
|
2014-08-01 12:43:14 -07:00
|
|
|
#include "runtime/string_input.h"
|
2015-07-31 15:47:48 -07:00
|
|
|
#include "runtime/document.h"
|
2017-08-04 11:03:14 -07:00
|
|
|
#include "runtime/get_changed_ranges.h"
|
2014-01-07 21:50:32 -08:00
|
|
|
|
2018-01-26 15:40:07 -08:00
|
|
|
#define LOG(...) \
|
|
|
|
|
snprintf(self->parser.lexer.debug_buffer, TREE_SITTER_SERIALIZATION_BUFFER_SIZE, __VA_ARGS__); \
|
|
|
|
|
self->parser.lexer.logger.log(self->parser.lexer.logger.payload, TSLogTypeLex, self->parser.lexer.debug_buffer); \
|
|
|
|
|
|
2016-09-06 17:26:18 -07:00
|
|
|
TSDocument *ts_document_new() {
|
2016-01-18 10:44:49 -08:00
|
|
|
TSDocument *self = ts_calloc(1, sizeof(TSDocument));
|
2017-10-05 17:32:21 -07:00
|
|
|
parser_init(&self->parser);
|
|
|
|
|
array_init(&self->tree_path1);
|
|
|
|
|
array_init(&self->tree_path2);
|
2016-01-18 10:44:49 -08:00
|
|
|
return self;
|
2014-10-13 01:02:12 -07:00
|
|
|
}
|
2014-01-07 21:50:32 -08:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_document_free(TSDocument *self) {
|
2017-10-05 17:32:21 -07:00
|
|
|
if (self->tree) ts_tree_release(&self->parser.tree_pool, self->tree);
|
|
|
|
|
if (self->tree_path1.contents) array_delete(&self->tree_path1);
|
|
|
|
|
if (self->tree_path2.contents) array_delete(&self->tree_path2);
|
2016-08-29 12:08:58 -07:00
|
|
|
parser_destroy(&self->parser);
|
2017-06-15 16:35:34 -07:00
|
|
|
ts_document_set_input(self, (TSInput){
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
TSInputEncodingUTF8,
|
|
|
|
|
});
|
2016-01-15 15:08:42 -08:00
|
|
|
ts_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-09-08 23:33:43 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_document_set_language(TSDocument *self, const TSLanguage *language) {
|
2017-01-31 10:21:47 -08:00
|
|
|
if (language->version != TREE_SITTER_LANGUAGE_VERSION) return;
|
2015-10-18 13:05:40 -07:00
|
|
|
ts_document_invalidate(self);
|
2016-12-02 22:03:48 -08:00
|
|
|
parser_set_language(&self->parser, language);
|
2016-01-29 17:25:07 -08:00
|
|
|
if (self->tree) {
|
2017-10-05 17:32:21 -07:00
|
|
|
ts_tree_release(&self->parser.tree_pool, self->tree);
|
2016-01-29 17:25:07 -08:00
|
|
|
self->tree = NULL;
|
|
|
|
|
}
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 17:27:50 -07:00
|
|
|
TSLogger ts_document_logger(const TSDocument *self) {
|
|
|
|
|
return self->parser.lexer.logger;
|
2014-10-13 01:02:12 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 17:27:50 -07:00
|
|
|
void ts_document_set_logger(TSDocument *self, TSLogger logger) {
|
|
|
|
|
self->parser.lexer.logger = logger;
|
2014-09-06 17:56:00 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-23 11:16:50 -08:00
|
|
|
void ts_document_print_debugging_graphs(TSDocument *self, bool should_print) {
|
|
|
|
|
self->parser.print_debugging_graphs = should_print;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSInput ts_document_input(TSDocument *self) {
|
|
|
|
|
return self->input;
|
2015-09-08 23:33:43 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_document_set_input(TSDocument *self, TSInput input) {
|
2016-01-29 16:40:38 -08:00
|
|
|
if (self->owns_input)
|
|
|
|
|
ts_free(self->input.payload);
|
2015-10-14 21:52:13 -07:00
|
|
|
self->input = input;
|
2016-01-29 16:40:38 -08:00
|
|
|
self->owns_input = false;
|
2015-09-18 23:20:06 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_document_set_input_string(TSDocument *self, const char *text) {
|
2015-10-18 13:05:40 -07:00
|
|
|
ts_document_invalidate(self);
|
2016-02-04 11:15:46 -08:00
|
|
|
TSInput input = ts_string_input_make(text);
|
|
|
|
|
ts_document_set_input(self, input);
|
|
|
|
|
if (input.payload) {
|
|
|
|
|
self->owns_input = true;
|
|
|
|
|
}
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2017-02-10 09:10:31 -05:00
|
|
|
void ts_document_set_input_string_with_length(TSDocument *self, const char *text, uint32_t length) {
|
|
|
|
|
ts_document_invalidate(self);
|
|
|
|
|
TSInput input = ts_string_input_make_with_length(text, length);
|
|
|
|
|
ts_document_set_input(self, input);
|
|
|
|
|
if (input.payload) {
|
|
|
|
|
self->owns_input = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-09-20 13:39:18 -07:00
|
|
|
|
2016-11-14 12:15:24 -08:00
|
|
|
uint32_t max_bytes = ts_tree_total_bytes(self->tree);
|
2016-09-13 13:08:52 -07:00
|
|
|
if (edit.start_byte > max_bytes)
|
2016-09-19 13:35:08 -07:00
|
|
|
return;
|
2016-09-13 13:08:52 -07:00
|
|
|
if (edit.bytes_removed > max_bytes - edit.start_byte)
|
|
|
|
|
edit.bytes_removed = max_bytes - edit.start_byte;
|
2015-09-13 19:47:45 -07:00
|
|
|
|
2016-09-13 13:08:52 -07:00
|
|
|
ts_tree_edit(self->tree, &edit);
|
2017-12-29 18:02:06 -08:00
|
|
|
|
|
|
|
|
if (self->parser.print_debugging_graphs) {
|
|
|
|
|
ts_tree_print_dot_graph(self->tree, self->parser.language, stderr);
|
|
|
|
|
}
|
2014-03-19 19:27:31 -07:00
|
|
|
}
|
|
|
|
|
|
2017-05-01 13:04:06 -07:00
|
|
|
void ts_document_parse(TSDocument *self) {
|
2017-08-08 10:47:59 -07:00
|
|
|
ts_document_parse_with_options(self, (TSParseOptions){
|
2017-05-01 13:04:06 -07:00
|
|
|
.halt_on_error = false,
|
|
|
|
|
.changed_ranges = NULL,
|
|
|
|
|
.changed_range_count = NULL,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 09:18:38 -07:00
|
|
|
void ts_document_parse_and_get_changed_ranges(TSDocument *self, TSRange **ranges,
|
2016-11-14 12:15:24 -08:00
|
|
|
uint32_t *range_count) {
|
2017-08-08 10:47:59 -07:00
|
|
|
ts_document_parse_with_options(self, (TSParseOptions){
|
2017-05-01 13:04:06 -07:00
|
|
|
.halt_on_error = false,
|
|
|
|
|
.changed_ranges = ranges,
|
|
|
|
|
.changed_range_count = range_count,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_document_parse_with_options(TSDocument *self, TSParseOptions options) {
|
|
|
|
|
if (options.changed_ranges && options.changed_range_count) {
|
|
|
|
|
*options.changed_ranges = NULL;
|
|
|
|
|
*options.changed_range_count = 0;
|
|
|
|
|
}
|
2016-09-07 17:49:16 -07:00
|
|
|
|
2016-09-06 21:39:10 -07:00
|
|
|
if (!self->input.read || !self->parser.language)
|
2016-11-04 09:18:38 -07:00
|
|
|
return;
|
2015-10-18 13:05:40 -07:00
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
Tree *reusable_tree = self->valid ? self->tree : NULL;
|
2015-12-22 14:20:58 -08:00
|
|
|
if (reusable_tree && !reusable_tree->has_changes)
|
2016-11-04 09:18:38 -07:00
|
|
|
return;
|
2015-10-18 13:05:40 -07:00
|
|
|
|
2017-05-01 13:04:06 -07:00
|
|
|
Tree *tree = parser_parse(&self->parser, self->input, reusable_tree, options.halt_on_error);
|
2016-01-19 18:07:24 -08:00
|
|
|
|
2016-09-07 17:49:16 -07:00
|
|
|
if (self->tree) {
|
2016-11-09 20:59:05 -08:00
|
|
|
Tree *old_tree = self->tree;
|
2016-09-07 17:49:16 -07:00
|
|
|
self->tree = tree;
|
|
|
|
|
|
2017-05-01 13:04:06 -07:00
|
|
|
if (options.changed_ranges && options.changed_range_count) {
|
2017-08-04 11:03:14 -07:00
|
|
|
*options.changed_range_count = ts_tree_get_changed_ranges(
|
2017-10-05 17:32:21 -07:00
|
|
|
old_tree, tree, &self->tree_path1, &self->tree_path2,
|
2017-08-04 11:03:14 -07:00
|
|
|
self->parser.language, options.changed_ranges
|
|
|
|
|
);
|
2018-01-26 15:40:07 -08:00
|
|
|
|
|
|
|
|
if (self->parser.lexer.logger.log) {
|
|
|
|
|
for (unsigned i = 0; i < *options.changed_range_count; i++) {
|
|
|
|
|
TSRange range = (*options.changed_ranges)[i];
|
|
|
|
|
LOG(
|
|
|
|
|
"changed_range start:[%u %u], end:[%u %u]",
|
|
|
|
|
range.start.row, range.start.column,
|
|
|
|
|
range.end.row, range.end.column
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-07 17:49:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-05 17:32:21 -07:00
|
|
|
ts_tree_release(&self->parser.tree_pool, old_tree);
|
2016-09-07 17:49:16 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-18 13:05:40 -07:00
|
|
|
self->tree = tree;
|
|
|
|
|
self->parse_count++;
|
|
|
|
|
self->valid = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_document_invalidate(TSDocument *self) {
|
|
|
|
|
self->valid = false;
|
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) {
|
2017-12-20 16:26:38 -08:00
|
|
|
return ts_node_make(self->tree, 0, 0);
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
2015-09-10 14:23:42 -07:00
|
|
|
|
2016-11-14 12:15:24 -08:00
|
|
|
uint32_t ts_document_parse_count(const TSDocument *self) {
|
2015-10-14 21:52:13 -07:00
|
|
|
return self->parse_count;
|
2015-09-10 14:23:42 -07:00
|
|
|
}
|