2014-07-10 13:14:52 -07:00
|
|
|
#include "tree_sitter/parser.h"
|
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"
|
2014-01-07 21:50:32 -08:00
|
|
|
|
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));
|
|
|
|
|
if (!self)
|
2016-02-04 11:15:46 -08:00
|
|
|
goto error;
|
2016-01-18 10:44:49 -08:00
|
|
|
|
2016-08-29 12:08:58 -07:00
|
|
|
if (!parser_init(&self->parser))
|
2016-02-04 11:15:46 -08:00
|
|
|
goto error;
|
2016-01-18 10:44:49 -08:00
|
|
|
|
|
|
|
|
return self;
|
2016-02-04 11:15:46 -08:00
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if (self)
|
|
|
|
|
ts_free(self);
|
|
|
|
|
return NULL;
|
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) {
|
2016-08-29 12:08:58 -07:00
|
|
|
parser_destroy(&self->parser);
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->tree)
|
|
|
|
|
ts_tree_release(self->tree);
|
2016-02-17 20:41:29 -08: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) {
|
2015-10-18 13:05:40 -07:00
|
|
|
ts_document_invalidate(self);
|
2015-10-14 21:52:13 -07:00
|
|
|
self->parser.language = language;
|
2016-01-29 17:25:07 -08:00
|
|
|
if (self->tree) {
|
|
|
|
|
ts_tree_release(self->tree);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
size_t max_chars = ts_tree_total_chars(self->tree);
|
2015-09-13 19:47:45 -07:00
|
|
|
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);
|
2014-03-19 19:27:31 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-07 17:49:16 -07:00
|
|
|
typedef Array(TSRange) RangeArray;
|
|
|
|
|
|
|
|
|
|
#define NAME(t) ((t) ? (ts_language_symbol_name(doc->parser.language, ((TSTree *)(t))->symbol)) : "<NULL>")
|
|
|
|
|
// #define PRINT(msg, ...) for (size_t k = 0; k < depth; k++) { printf(" "); } printf(msg "\n", __VA_ARGS__);
|
|
|
|
|
#define PRINT(msg, ...)
|
|
|
|
|
|
|
|
|
|
static bool push_diff(RangeArray *results, TSNode *node, bool *extend_last_change) {
|
|
|
|
|
TSPoint start = ts_node_start_point(*node);
|
|
|
|
|
TSPoint end = ts_node_end_point(*node);
|
|
|
|
|
if (*extend_last_change) {
|
|
|
|
|
TSRange *last_range = array_back(results);
|
|
|
|
|
last_range->end = end;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
*extend_last_change = true;
|
|
|
|
|
return array_push(results, ((TSRange){start, end}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool ts_tree_diff(TSDocument *doc, TSTree *old, TSNode *new_node,
|
|
|
|
|
size_t depth, RangeArray *results, bool *extend_last_change) {
|
|
|
|
|
TSTree *new = (TSTree *)(new_node->data);
|
|
|
|
|
|
|
|
|
|
PRINT("At %lu, ('%s', %lu) vs ('%s', %lu) {",
|
|
|
|
|
ts_node_start_char(*new_node),
|
|
|
|
|
NAME(old), old->size.chars,
|
|
|
|
|
NAME(new), new->size.chars);
|
|
|
|
|
|
|
|
|
|
if (old->visible) {
|
|
|
|
|
if (old == new || (old->symbol == new->symbol &&
|
|
|
|
|
old->size.chars == new->size.chars && !old->has_changes)) {
|
|
|
|
|
*extend_last_change = false;
|
|
|
|
|
PRINT("}", NULL);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (old->symbol != new->symbol) {
|
|
|
|
|
PRINT("}", NULL);
|
|
|
|
|
return push_diff(results, new_node, extend_last_change);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSNode child = ts_node_child(*new_node, 0);
|
|
|
|
|
if (child.data) {
|
|
|
|
|
*new_node = child;
|
|
|
|
|
} else {
|
|
|
|
|
PRINT("}", NULL);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
depth++;
|
|
|
|
|
size_t old_child_start;
|
|
|
|
|
size_t old_child_end = ts_node_start_char(*new_node) - old->padding.chars;
|
|
|
|
|
|
|
|
|
|
for (size_t j = 0; j < old->child_count; j++) {
|
|
|
|
|
TSTree *old_child = old->children[j];
|
|
|
|
|
if (old_child->padding.chars == 0 && old_child->size.chars == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
old_child_start = old_child_end + old_child->padding.chars;
|
|
|
|
|
old_child_end = old_child_start + old_child->size.chars;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
size_t new_child_start = ts_node_start_char(*new_node);
|
|
|
|
|
if (new_child_start < old_child_start) {
|
|
|
|
|
PRINT("skip new:('%s', %lu), old:('%s', %lu), old_parent:%s",
|
|
|
|
|
NAME(new_node->data), ts_node_start_char(*new_node), NAME(old_child),
|
|
|
|
|
old_child_start, NAME(old));
|
|
|
|
|
|
|
|
|
|
if (!push_diff(results, new_node, extend_last_change))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
TSNode next = ts_node_next_sibling(*new_node);
|
|
|
|
|
if (next.data) {
|
|
|
|
|
PRINT("advance before diff ('%s', %lu) -> ('%s', %lu)",
|
|
|
|
|
NAME(new_node->data), ts_node_start_char(*new_node), NAME(next.data),
|
|
|
|
|
ts_node_start_char(next));
|
2016-09-08 17:51:34 -07:00
|
|
|
|
2016-09-07 17:49:16 -07:00
|
|
|
*new_node = next;
|
2016-09-08 17:51:34 -07:00
|
|
|
} else {
|
|
|
|
|
break;
|
2016-09-07 17:49:16 -07:00
|
|
|
}
|
|
|
|
|
} else if (new_child_start == old_child_start) {
|
|
|
|
|
if (!ts_tree_diff(doc, old_child, new_node, depth, results, extend_last_change))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (old_child->visible) {
|
|
|
|
|
TSNode next = ts_node_next_sibling(*new_node);
|
|
|
|
|
if (next.data) {
|
|
|
|
|
PRINT("advance after diff ('%s', %lu) -> ('%s', %lu)",
|
|
|
|
|
NAME(new_node->data), ts_node_start_char(*new_node), NAME(next.data),
|
|
|
|
|
ts_node_start_char(next));
|
|
|
|
|
*new_node = next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
depth--;
|
|
|
|
|
if (old->visible) {
|
|
|
|
|
*new_node = ts_node_parent(*new_node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PRINT("}", NULL);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 17:51:34 -07:00
|
|
|
int ts_document_parse_and_get_changed_ranges(TSDocument *self, TSRange **ranges, size_t *range_count) {
|
2016-09-07 17:49:16 -07:00
|
|
|
if (ranges) *ranges = NULL;
|
|
|
|
|
if (range_count) *range_count = 0;
|
|
|
|
|
|
2016-09-06 21:39:10 -07:00
|
|
|
if (!self->input.read || !self->parser.language)
|
2016-02-04 11:15:46 -08:00
|
|
|
return -1;
|
2015-10-18 13:05:40 -07:00
|
|
|
|
|
|
|
|
TSTree *reusable_tree = self->valid ? self->tree : NULL;
|
2015-12-22 14:20:58 -08:00
|
|
|
if (reusable_tree && !reusable_tree->has_changes)
|
2016-01-19 18:07:24 -08:00
|
|
|
return 0;
|
2015-10-18 13:05:40 -07:00
|
|
|
|
2016-08-29 12:08:58 -07:00
|
|
|
TSTree *tree = parser_parse(&self->parser, self->input, reusable_tree);
|
2016-01-19 18:07:24 -08:00
|
|
|
if (!tree)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2016-09-07 17:49:16 -07:00
|
|
|
if (self->tree) {
|
|
|
|
|
TSTree *old_tree = self->tree;
|
|
|
|
|
self->tree = tree;
|
|
|
|
|
TSNode new_root = ts_document_root_node(self);
|
|
|
|
|
|
|
|
|
|
// ts_tree_print_dot_graph(old_tree, self->parser.language, stderr);
|
|
|
|
|
// ts_tree_print_dot_graph(tree, self->parser.language, stderr);
|
|
|
|
|
|
|
|
|
|
if (ranges && range_count) {
|
|
|
|
|
bool extend_last_change = false;
|
|
|
|
|
RangeArray result = {0, 0, 0};
|
|
|
|
|
if (!ts_tree_diff(self, old_tree, &new_root, 0, &result, &extend_last_change))
|
|
|
|
|
return -1;
|
|
|
|
|
*ranges = result.contents;
|
|
|
|
|
*range_count = result.size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ts_tree_release(old_tree);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-18 13:05:40 -07:00
|
|
|
self->tree = tree;
|
|
|
|
|
self->parse_count++;
|
|
|
|
|
self->valid = true;
|
2016-01-19 18:07:24 -08:00
|
|
|
return 0;
|
2015-10-18 13:05:40 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-07 17:49:16 -07:00
|
|
|
int ts_document_parse(TSDocument *self) {
|
2016-09-08 17:51:34 -07:00
|
|
|
return ts_document_parse_and_get_changed_ranges(self, NULL, NULL);
|
2016-09-07 17:49:16 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-18 13:05:40 -07:00
|
|
|
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) {
|
2015-12-04 10:45:30 -08:00
|
|
|
TSNode result = ts_node_make(self->tree, 0, 0, 0);
|
2015-12-22 14:20:58 -08:00
|
|
|
while (result.data && !((TSTree *)result.data)->visible)
|
2015-09-08 23:16:24 -07:00
|
|
|
result = ts_node_named_child(result, 0);
|
2015-09-02 16:36:29 -07:00
|
|
|
return result;
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
2015-09-10 14:23:42 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
size_t ts_document_parse_count(const TSDocument *self) {
|
|
|
|
|
return self->parse_count;
|
2015-09-10 14:23:42 -07:00
|
|
|
}
|