2015-07-08 17:34:21 -07:00
|
|
|
#include <assert.h>
|
2014-01-07 21:50:32 -08:00
|
|
|
#include <string.h>
|
2014-09-01 14:08:07 -07:00
|
|
|
#include <stdbool.h>
|
2014-03-08 16:30:44 -08:00
|
|
|
#include <stdio.h>
|
2014-07-17 23:29:11 -07:00
|
|
|
#include "tree_sitter/parser.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-09-15 16:00:16 -07:00
|
|
|
TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size,
|
2015-09-05 22:29:17 -07:00
|
|
|
TSNodeType node_type) {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSTree *result = malloc(sizeof(TSTree));
|
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-09-07 21:11:37 -07:00
|
|
|
.options = {.type = node_type },
|
2015-07-27 18:29:48 -07:00
|
|
|
};
|
2015-09-08 21:43:37 -07:00
|
|
|
|
|
|
|
|
if (sym == ts_builtin_sym_error) {
|
|
|
|
|
result->options.fragile_left = true;
|
|
|
|
|
result->options.fragile_right = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
return result;
|
2014-03-01 00:25:05 -08:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) {
|
2015-09-07 21:11:37 -07:00
|
|
|
TSTree *result =
|
2015-09-15 16:00:16 -07:00
|
|
|
ts_tree_make_leaf(ts_builtin_sym_error, padding, size, TSNodeTypeNamed);
|
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-10-14 21:52:13 -07:00
|
|
|
static void ts_tree__set_children(TSTree *self, TSTree **children,
|
2015-09-08 21:43:37 -07:00
|
|
|
size_t child_count) {
|
2015-10-14 21:52:13 -07:00
|
|
|
self->children = children;
|
|
|
|
|
self->child_count = child_count;
|
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];
|
|
|
|
|
ts_tree_retain(child);
|
2015-10-14 21:52:13 -07:00
|
|
|
child->context.parent = self;
|
2015-08-15 23:35:20 -07:00
|
|
|
child->context.index = i;
|
2015-10-14 21:52:13 -07:00
|
|
|
child->context.offset = ts_tree_total_size(self);
|
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-10-14 21:52:13 -07:00
|
|
|
self->size =
|
|
|
|
|
ts_length_add(ts_length_add(self->size, child->padding), child->size);
|
2014-09-02 07:41:29 -07:00
|
|
|
}
|
2014-07-16 18:38:06 -07:00
|
|
|
|
2015-09-07 21:11:37 -07:00
|
|
|
switch (child->options.type) {
|
2015-09-08 23:16:24 -07:00
|
|
|
case TSNodeTypeNamed:
|
2015-10-14 21:52:13 -07:00
|
|
|
self->visible_child_count++;
|
|
|
|
|
self->named_child_count++;
|
2015-09-07 21:11:37 -07:00
|
|
|
break;
|
2015-09-08 23:16:24 -07:00
|
|
|
case TSNodeTypeAnonymous:
|
2015-10-14 21:52:13 -07:00
|
|
|
self->visible_child_count++;
|
2015-09-07 21:11:37 -07:00
|
|
|
break;
|
|
|
|
|
case TSNodeTypeHidden:
|
2015-10-14 21:52:13 -07:00
|
|
|
self->visible_child_count += child->visible_child_count;
|
|
|
|
|
self->named_child_count += child->named_child_count;
|
2015-09-07 21:11:37 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
|
|
|
|
|
2015-09-08 21:43:37 -07:00
|
|
|
if (child_count > 0) {
|
|
|
|
|
if (children[0]->options.fragile_left)
|
2015-10-14 21:52:13 -07:00
|
|
|
self->options.fragile_left = true;
|
2015-09-08 21:43:37 -07:00
|
|
|
if (children[child_count - 1]->options.fragile_right)
|
2015-10-14 21:52:13 -07:00
|
|
|
self->options.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,
|
|
|
|
|
TSTree **children, TSNodeType node_type) {
|
|
|
|
|
TSTree *result =
|
|
|
|
|
ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), node_type);
|
|
|
|
|
ts_tree__set_children(result, children, child_count);
|
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) {
|
|
|
|
|
assert(self->ref_count > 0);
|
|
|
|
|
self->ref_count--;
|
|
|
|
|
if (self->ref_count == 0) {
|
|
|
|
|
for (size_t i = 0; i < self->child_count; i++)
|
|
|
|
|
ts_tree_release(self->children[i]);
|
|
|
|
|
if (self->child_count > 0)
|
|
|
|
|
free(self->children);
|
|
|
|
|
free(self);
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-03-24 00:34:13 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSLength ts_tree_total_size(const TSTree *self) {
|
|
|
|
|
return ts_length_add(self->padding, self->size);
|
2014-09-02 07:41:29 -07: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-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
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
static size_t write_lookahead_to_string(char *string, size_t limit,
|
|
|
|
|
char lookahead) {
|
2014-07-20 20:27:33 -07:00
|
|
|
switch (lookahead) {
|
|
|
|
|
case '\0':
|
|
|
|
|
return snprintf(string, limit, "<EOF>");
|
|
|
|
|
default:
|
|
|
|
|
return snprintf(string, limit, "'%c'", lookahead);
|
|
|
|
|
}
|
2014-05-26 21:50:01 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static size_t ts_tree__write_to_string(const TSTree *self,
|
2015-08-16 19:53:34 -07:00
|
|
|
const char **symbol_names, char *string,
|
2015-10-28 12:09:28 -07:00
|
|
|
size_t limit, int is_root,
|
|
|
|
|
bool include_anonymous) {
|
2015-10-14 21:52:13 -07:00
|
|
|
if (!self)
|
2014-09-01 14:08:07 -07:00
|
|
|
return snprintf(string, limit, "(NULL)");
|
|
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
char *cursor = string;
|
|
|
|
|
char **writer = (limit > 0) ? &cursor : &string;
|
2015-10-28 12:09:28 -07:00
|
|
|
TSNodeType min_node_type =
|
|
|
|
|
include_anonymous ? TSNodeTypeAnonymous : TSNodeTypeNamed;
|
|
|
|
|
int visible = self->options.type >= min_node_type || is_root;
|
2014-07-20 20:27:33 -07:00
|
|
|
|
|
|
|
|
if (visible && !is_root)
|
|
|
|
|
cursor += snprintf(*writer, limit, " ");
|
|
|
|
|
|
2014-09-01 14:08:07 -07:00
|
|
|
if (visible) {
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->symbol == ts_builtin_sym_error && self->child_count == 0) {
|
2015-06-15 15:24:15 -07:00
|
|
|
cursor += snprintf(*writer, limit, "(UNEXPECTED ");
|
2015-10-14 21:52:13 -07:00
|
|
|
cursor += write_lookahead_to_string(*writer, limit, self->lookahead_char);
|
2014-09-01 14:08:07 -07:00
|
|
|
} else {
|
2015-10-14 21:52:13 -07:00
|
|
|
cursor += snprintf(*writer, limit, "(%s", symbol_names[self->symbol]);
|
2014-08-27 12:56:36 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-16 18:38:06 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
for (size_t i = 0; i < self->child_count; i++) {
|
|
|
|
|
TSTree *child = self->children[i];
|
2015-10-28 12:09:28 -07:00
|
|
|
cursor += ts_tree__write_to_string(child, symbol_names, *writer, limit, 0,
|
|
|
|
|
include_anonymous);
|
2014-09-01 14:08:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (visible)
|
|
|
|
|
cursor += snprintf(*writer, limit, ")");
|
|
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
return cursor - string;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2015-10-28 12:09:28 -07:00
|
|
|
char *ts_tree_string(const TSTree *self, const char **symbol_names,
|
|
|
|
|
bool include_anonymous) {
|
2014-08-31 16:24:27 -07:00
|
|
|
static char SCRATCH[1];
|
2015-10-28 12:09:28 -07:00
|
|
|
size_t size = 1 + ts_tree__write_to_string(self, symbol_names, SCRATCH, 0, 1,
|
|
|
|
|
include_anonymous);
|
2014-07-20 20:27:33 -07:00
|
|
|
char *result = malloc(size * sizeof(char));
|
2015-10-28 12:09:28 -07:00
|
|
|
ts_tree__write_to_string(self, symbol_names, result, size, 1,
|
|
|
|
|
include_anonymous);
|
2014-07-20 20:27:33 -07:00
|
|
|
return result;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
2015-08-22 10:48:34 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_tree_prepend_children(TSTree *self, size_t count, TSTree **children) {
|
2015-08-22 10:48:34 -07:00
|
|
|
if (count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
size_t new_child_count = count + self->child_count;
|
2015-08-22 10:48:34 -07:00
|
|
|
TSTree **new_children = realloc(children, new_child_count * sizeof(TSTree *));
|
2015-10-14 21:52:13 -07:00
|
|
|
memcpy(new_children + count, self->children,
|
|
|
|
|
self->child_count * sizeof(TSTree *));
|
|
|
|
|
free(self->children);
|
2015-08-22 10:48:34 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_tree__set_children(self, new_children, new_child_count);
|
2015-08-22 10:48:34 -07:00
|
|
|
}
|
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-10-14 21:52:13 -07:00
|
|
|
assert(old_end <= ts_tree_total_size(self).chars);
|
2015-09-15 16:00:16 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
self->options.has_changes = true;
|
2015-09-15 16:00:16 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (start < self->padding.chars) {
|
|
|
|
|
self->padding.bytes = 0;
|
|
|
|
|
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;
|
|
|
|
|
self->size.bytes = 0;
|
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.bytes = 0;
|
|
|
|
|
self->padding.chars += edit.chars_inserted;
|
2015-09-15 16:00:16 -07:00
|
|
|
} else {
|
2015-10-14 21:52:13 -07:00
|
|
|
self->size.bytes = 0;
|
|
|
|
|
self->size.chars += (edit.chars_inserted - edit.chars_removed);
|
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-09-15 16:00:16 -07:00
|
|
|
size_t child_size = ts_tree_total_size(child).chars;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|