2015-07-08 17:34:21 -07:00
|
|
|
#include <assert.h>
|
2015-12-22 14:37:29 -08:00
|
|
|
#include <limits.h>
|
2014-09-01 14:08:07 -07:00
|
|
|
#include <stdbool.h>
|
2014-07-17 23:29:11 -07:00
|
|
|
#include "tree_sitter/parser.h"
|
2016-01-15 15:08:42 -08:00
|
|
|
#include "runtime/alloc.h"
|
2014-06-26 08:52:42 -07:00
|
|
|
#include "runtime/tree.h"
|
2014-09-26 16:15:07 -07:00
|
|
|
#include "runtime/length.h"
|
2014-03-24 00:34:13 -07:00
|
|
|
|
2015-12-22 14:37:29 -08:00
|
|
|
TSStateId TS_TREE_STATE_INDEPENDENT = USHRT_MAX;
|
|
|
|
|
TSStateId TS_TREE_STATE_ERROR = USHRT_MAX - 1;
|
|
|
|
|
|
2015-09-15 16:00:16 -07:00
|
|
|
TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size,
|
2015-11-22 13:32:20 -08:00
|
|
|
TSSymbolMetadata metadata) {
|
2016-01-15 15:08:42 -08:00
|
|
|
TSTree *result = ts_malloc(sizeof(TSTree));
|
2016-01-19 18:07:24 -08:00
|
|
|
if (!result)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2015-07-27 18:29:48 -07:00
|
|
|
*result = (TSTree){
|
|
|
|
|
.ref_count = 1,
|
|
|
|
|
.symbol = sym,
|
|
|
|
|
.size = size,
|
|
|
|
|
.child_count = 0,
|
2015-09-07 21:11:37 -07:00
|
|
|
.visible_child_count = 0,
|
|
|
|
|
.named_child_count = 0,
|
2015-07-27 18:29:48 -07:00
|
|
|
.children = NULL,
|
|
|
|
|
.padding = padding,
|
2015-12-22 14:20:58 -08:00
|
|
|
.visible = metadata.visible,
|
|
|
|
|
.named = metadata.named,
|
2015-12-22 14:37:29 -08:00
|
|
|
.lex_state = TS_TREE_STATE_INDEPENDENT,
|
|
|
|
|
.parse_state = TS_TREE_STATE_INDEPENDENT,
|
2015-07-27 18:29:48 -07:00
|
|
|
};
|
2015-09-08 21:43:37 -07:00
|
|
|
|
|
|
|
|
if (sym == ts_builtin_sym_error) {
|
2015-12-22 14:20:58 -08:00
|
|
|
result->fragile_left = true;
|
|
|
|
|
result->fragile_right = true;
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
return result;
|
2014-03-01 00:25:05 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-02 21:03:55 -08:00
|
|
|
TreeArray ts_tree_array_copy(TreeArray *self) {
|
|
|
|
|
TreeArray result = array_copy(self);
|
|
|
|
|
for (size_t i = 0; i < result.size; i++)
|
|
|
|
|
ts_tree_retain(result.contents[i]);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_tree_array_clear(TreeArray *self) {
|
|
|
|
|
for (size_t i = 0; i < self->size; i++)
|
|
|
|
|
ts_tree_release(self->contents[i]);
|
|
|
|
|
array_clear(self);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) {
|
2015-12-04 20:56:33 -08:00
|
|
|
TSTree *result = ts_tree_make_leaf(ts_builtin_sym_error, padding, size,
|
|
|
|
|
(TSSymbolMetadata){
|
|
|
|
|
.visible = true, .named = true,
|
|
|
|
|
});
|
2016-01-19 18:07:24 -08:00
|
|
|
if (!result)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2014-08-28 13:22:06 -07:00
|
|
|
result->lookahead_char = lookahead_char;
|
2014-07-20 20:27:33 -07:00
|
|
|
return result;
|
2014-02-24 18:42:54 -08:00
|
|
|
}
|
|
|
|
|
|
2015-12-15 22:28:50 -08:00
|
|
|
TSTree *ts_tree_make_copy(TSTree *self) {
|
2016-01-15 15:08:42 -08:00
|
|
|
TSTree *result = ts_malloc(sizeof(TSTree));
|
2016-01-19 18:07:24 -08:00
|
|
|
if (!result)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2015-12-15 22:28:50 -08:00
|
|
|
*result = *self;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 12:21:16 -08:00
|
|
|
void ts_tree_assign_parents(TSTree *self) {
|
2016-02-04 12:59:44 -08:00
|
|
|
TSLength offset;
|
|
|
|
|
|
|
|
|
|
recur:
|
|
|
|
|
offset = ts_length_zero();
|
2015-11-15 12:21:16 -08:00
|
|
|
for (size_t i = 0; i < self->child_count; i++) {
|
|
|
|
|
TSTree *child = self->children[i];
|
|
|
|
|
if (child->context.parent != self) {
|
|
|
|
|
child->context.parent = self;
|
|
|
|
|
child->context.index = i;
|
|
|
|
|
child->context.offset = offset;
|
2016-02-04 12:59:44 -08:00
|
|
|
if (i == self->child_count - 1) {
|
|
|
|
|
self = child;
|
|
|
|
|
goto recur;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 12:21:16 -08:00
|
|
|
ts_tree_assign_parents(child);
|
|
|
|
|
}
|
|
|
|
|
offset = ts_length_add(offset, ts_tree_total_size(child));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
void ts_tree_set_children(TSTree *self, size_t child_count, TSTree **children) {
|
2016-01-29 17:31:43 -08:00
|
|
|
if (self->child_count > 0)
|
|
|
|
|
ts_free(self->children);
|
2015-10-14 21:52:13 -07:00
|
|
|
self->children = children;
|
|
|
|
|
self->child_count = child_count;
|
2015-12-04 15:51:04 -08:00
|
|
|
self->named_child_count = 0;
|
2015-10-24 13:45:42 -07:00
|
|
|
self->visible_child_count = 0;
|
2014-07-20 20:27:33 -07:00
|
|
|
for (size_t i = 0; i < child_count; i++) {
|
|
|
|
|
TSTree *child = children[i];
|
2014-09-02 07:41:29 -07:00
|
|
|
|
|
|
|
|
if (i == 0) {
|
2015-10-14 21:52:13 -07:00
|
|
|
self->padding = child->padding;
|
|
|
|
|
self->size = child->size;
|
2014-09-02 07:41:29 -07:00
|
|
|
} else {
|
2015-12-04 20:20:29 -08:00
|
|
|
self->size = ts_length_add(self->size, ts_tree_total_size(child));
|
2014-09-02 07:41:29 -07:00
|
|
|
}
|
2014-07-16 18:38:06 -07:00
|
|
|
|
2015-12-22 14:20:58 -08:00
|
|
|
if (child->visible) {
|
2015-11-22 13:32:20 -08:00
|
|
|
self->visible_child_count++;
|
2015-12-22 14:20:58 -08:00
|
|
|
if (child->named)
|
2015-10-14 21:52:13 -07:00
|
|
|
self->named_child_count++;
|
2015-11-22 13:32:20 -08:00
|
|
|
} else {
|
|
|
|
|
self->visible_child_count += child->visible_child_count;
|
|
|
|
|
self->named_child_count += child->named_child_count;
|
2015-09-07 21:11:37 -07:00
|
|
|
}
|
2015-12-06 20:31:10 -08:00
|
|
|
|
|
|
|
|
if (child->symbol == ts_builtin_sym_error) {
|
2015-12-22 14:20:58 -08:00
|
|
|
self->fragile_left = self->fragile_right = true;
|
2015-12-22 14:37:29 -08:00
|
|
|
self->parse_state = TS_TREE_STATE_ERROR;
|
2015-12-06 20:31:10 -08:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
|
|
|
|
|
2015-09-08 21:43:37 -07:00
|
|
|
if (child_count > 0) {
|
2015-12-22 14:20:58 -08:00
|
|
|
self->lex_state = children[0]->lex_state;
|
|
|
|
|
if (children[0]->fragile_left)
|
|
|
|
|
self->fragile_left = true;
|
|
|
|
|
if (children[child_count - 1]->fragile_right)
|
|
|
|
|
self->fragile_right = true;
|
2015-06-15 15:24:15 -07:00
|
|
|
}
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
|
2015-09-08 21:43:37 -07:00
|
|
|
TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
|
2015-11-22 13:32:20 -08:00
|
|
|
TSTree **children, TSSymbolMetadata metadata) {
|
2015-09-08 21:43:37 -07:00
|
|
|
TSTree *result =
|
2015-12-04 20:20:29 -08:00
|
|
|
ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), metadata);
|
2016-01-19 18:07:24 -08:00
|
|
|
if (!result)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
ts_tree_set_children(result, child_count, children);
|
2014-07-20 20:27:33 -07:00
|
|
|
return result;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_tree_retain(TSTree *self) {
|
|
|
|
|
assert(self->ref_count > 0);
|
|
|
|
|
self->ref_count++;
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
2014-01-07 21:50:32 -08:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_tree_release(TSTree *self) {
|
2016-01-29 17:31:43 -08:00
|
|
|
if (!self)
|
|
|
|
|
return;
|
2016-02-04 12:59:44 -08:00
|
|
|
|
|
|
|
|
recur:
|
2015-10-14 21:52:13 -07:00
|
|
|
assert(self->ref_count > 0);
|
|
|
|
|
self->ref_count--;
|
2016-02-04 12:59:44 -08:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->ref_count == 0) {
|
2016-02-04 12:59:44 -08:00
|
|
|
if (self->child_count > 0) {
|
|
|
|
|
for (size_t i = 0; i < self->child_count - 1; i++)
|
|
|
|
|
ts_tree_release(self->children[i]);
|
|
|
|
|
TSTree *last_child = self->children[self->child_count - 1];
|
2016-01-15 15:08:42 -08:00
|
|
|
ts_free(self->children);
|
2016-02-04 12:59:44 -08:00
|
|
|
ts_free(self);
|
|
|
|
|
|
|
|
|
|
self = last_child;
|
|
|
|
|
goto recur;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 15:08:42 -08:00
|
|
|
ts_free(self);
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-03-24 00:34:13 -07:00
|
|
|
}
|
|
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
size_t ts_tree_start_column(const TSTree *self) {
|
|
|
|
|
size_t column = self->padding.columns;
|
|
|
|
|
if (self->padding.rows > 0)
|
2015-11-25 13:36:19 -05:00
|
|
|
return column;
|
2015-12-04 20:20:29 -08:00
|
|
|
for (const TSTree *tree = self; tree != NULL; tree = tree->context.parent) {
|
|
|
|
|
column += tree->context.offset.columns;
|
|
|
|
|
if (tree->context.offset.rows > 0)
|
|
|
|
|
break;
|
2015-11-25 13:36:19 -05:00
|
|
|
}
|
2015-11-30 12:56:10 -05:00
|
|
|
return column;
|
2015-11-25 11:08:44 -05:00
|
|
|
}
|
|
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
size_t ts_tree_end_column(const TSTree *self) {
|
|
|
|
|
size_t result = self->size.columns;
|
|
|
|
|
if (self->size.rows == 0)
|
|
|
|
|
result += ts_tree_start_column(self);
|
|
|
|
|
return result;
|
2015-11-18 16:34:50 -08:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
bool ts_tree_eq(const TSTree *self, const TSTree *other) {
|
|
|
|
|
if (self) {
|
|
|
|
|
if (!other)
|
2015-07-27 18:29:48 -07:00
|
|
|
return false;
|
2015-06-03 09:44:13 -07:00
|
|
|
} else {
|
2015-10-14 21:52:13 -07:00
|
|
|
return !other;
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->symbol != other->symbol)
|
2014-10-03 16:06:08 -07:00
|
|
|
return false;
|
2015-12-22 14:20:58 -08:00
|
|
|
if (self->visible != other->visible)
|
2015-11-22 13:32:20 -08:00
|
|
|
return false;
|
2015-12-22 14:20:58 -08:00
|
|
|
if (self->named != other->named)
|
2015-11-22 13:32:20 -08:00
|
|
|
return false;
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->symbol == ts_builtin_sym_error)
|
|
|
|
|
return self->lookahead_char == other->lookahead_char;
|
|
|
|
|
if (self->child_count != other->child_count)
|
2014-10-03 16:06:08 -07:00
|
|
|
return false;
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->visible_child_count != other->visible_child_count)
|
2014-10-03 16:06:08 -07:00
|
|
|
return false;
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->named_child_count != other->named_child_count)
|
2015-09-07 21:11:37 -07:00
|
|
|
return false;
|
2015-10-14 21:52:13 -07:00
|
|
|
for (size_t i = 0; i < self->child_count; i++)
|
|
|
|
|
if (!ts_tree_eq(self->children[i], other->children[i]))
|
2014-10-03 16:06:08 -07:00
|
|
|
return false;
|
|
|
|
|
return true;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2015-11-20 11:53:03 -08:00
|
|
|
int ts_tree_compare(const TSTree *left, const TSTree *right) {
|
|
|
|
|
if (left->symbol < right->symbol)
|
|
|
|
|
return -1;
|
|
|
|
|
if (right->symbol < left->symbol)
|
|
|
|
|
return 1;
|
|
|
|
|
if (left->child_count < right->child_count)
|
|
|
|
|
return -1;
|
|
|
|
|
if (right->child_count < left->child_count)
|
|
|
|
|
return 1;
|
|
|
|
|
for (size_t i = 0; i < left->child_count; i++) {
|
|
|
|
|
TSTree *left_child = left->children[i];
|
|
|
|
|
TSTree *right_child = right->children[i];
|
|
|
|
|
switch (ts_tree_compare(left_child, right_child)) {
|
|
|
|
|
case -1:
|
|
|
|
|
return -1;
|
|
|
|
|
case 1:
|
|
|
|
|
return 1;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-15 16:00:16 -07:00
|
|
|
static inline long min(long a, long b) {
|
|
|
|
|
return a <= b ? a : b;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_tree_edit(TSTree *self, TSInputEdit edit) {
|
2015-09-15 16:00:16 -07:00
|
|
|
size_t start = edit.position;
|
|
|
|
|
size_t new_end = edit.position + edit.chars_inserted;
|
2015-09-22 21:02:25 -07:00
|
|
|
size_t old_end = edit.position + edit.chars_removed;
|
2015-12-04 20:20:29 -08:00
|
|
|
assert(old_end <= ts_tree_total_chars(self));
|
2015-09-15 16:00:16 -07:00
|
|
|
|
2015-12-22 14:20:58 -08:00
|
|
|
self->has_changes = true;
|
2015-09-15 16:00:16 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (start < self->padding.chars) {
|
2015-12-04 20:20:29 -08:00
|
|
|
ts_length_set_unknown(&self->padding);
|
2015-10-14 21:52:13 -07:00
|
|
|
long remaining_padding = self->padding.chars - old_end;
|
2015-09-15 16:00:16 -07:00
|
|
|
if (remaining_padding >= 0) {
|
2015-10-14 21:52:13 -07:00
|
|
|
self->padding.chars = new_end + remaining_padding;
|
2015-09-15 16:00:16 -07:00
|
|
|
} else {
|
2015-10-14 21:52:13 -07:00
|
|
|
self->padding.chars = new_end;
|
|
|
|
|
self->size.chars += remaining_padding;
|
2015-12-04 20:20:29 -08:00
|
|
|
ts_length_set_unknown(&self->size);
|
2015-09-15 16:00:16 -07:00
|
|
|
}
|
2015-10-14 21:52:13 -07:00
|
|
|
} else if (start == self->padding.chars && edit.chars_removed == 0) {
|
|
|
|
|
self->padding.chars += edit.chars_inserted;
|
2015-12-04 20:20:29 -08:00
|
|
|
ts_length_set_unknown(&self->padding);
|
2015-09-15 16:00:16 -07:00
|
|
|
} else {
|
2015-10-14 21:52:13 -07:00
|
|
|
self->size.chars += (edit.chars_inserted - edit.chars_removed);
|
2015-12-04 20:20:29 -08:00
|
|
|
ts_length_set_unknown(&self->size);
|
2015-09-15 16:00:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool found_first_child = false;
|
|
|
|
|
long remainder_to_delete = edit.chars_removed - edit.chars_inserted;
|
|
|
|
|
size_t child_left = 0, child_right = 0;
|
2015-10-14 21:52:13 -07:00
|
|
|
for (size_t i = 0; i < self->child_count; i++) {
|
|
|
|
|
TSTree *child = self->children[i];
|
2015-12-04 20:20:29 -08:00
|
|
|
size_t child_size = ts_tree_total_chars(child);
|
2015-09-15 16:00:16 -07:00
|
|
|
child_left = child_right;
|
|
|
|
|
child_right += child_size;
|
|
|
|
|
|
|
|
|
|
if (!found_first_child) {
|
|
|
|
|
if (child_right >= start) {
|
|
|
|
|
found_first_child = true;
|
|
|
|
|
size_t chars_removed = min(edit.chars_removed, child_right - start);
|
|
|
|
|
remainder_to_delete -= (chars_removed - edit.chars_inserted);
|
|
|
|
|
ts_tree_edit(child, (TSInputEdit){
|
|
|
|
|
.position = start - child_left,
|
|
|
|
|
.chars_inserted = edit.chars_inserted,
|
|
|
|
|
.chars_removed = chars_removed,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (remainder_to_delete > 0) {
|
|
|
|
|
size_t chars_removed = min(remainder_to_delete, child_size);
|
|
|
|
|
remainder_to_delete -= chars_removed;
|
2015-09-13 19:47:45 -07:00
|
|
|
ts_tree_edit(
|
|
|
|
|
child,
|
|
|
|
|
(TSInputEdit){
|
|
|
|
|
.position = 0, .chars_inserted = 0, .chars_removed = chars_removed,
|
|
|
|
|
});
|
2015-09-15 16:00:16 -07:00
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|