tree-sitter/src/runtime/tree.c

305 lines
9.2 KiB
C
Raw Normal View History

#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>
#include <stdio.h>
2014-07-17 23:29:11 -07:00
#include "tree_sitter/parser.h"
#include "runtime/tree.h"
#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,
TSPoint padding_point,
TSPoint size_point,
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,
.visible_child_count = 0,
.named_child_count = 0,
2015-07-27 18:29:48 -07:00
.children = NULL,
.padding = padding,
.padding_point = padding_point,
.size_point = size_point,
.options = {.type = node_type },
2015-07-27 18:29:48 -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;
}
2015-11-12 15:32:53 -05:00
TSTree *ts_tree_make_error(TSLength size, TSLength padding,
TSPoint size_point,
TSPoint padding_point,
2015-11-12 15:32:53 -05:00
char lookahead_char) {
TSTree *result =
ts_tree_make_leaf(ts_builtin_sym_error, padding, size, padding_point,
size_point, TSNodeTypeNamed);
result->lookahead_char = lookahead_char;
2014-07-20 20:27:33 -07:00
return result;
}
2015-10-14 21:52:13 -07:00
static void ts_tree__set_children(TSTree *self, TSTree **children,
size_t child_count) {
2015-10-14 21:52:13 -07:00
self->children = children;
self->child_count = child_count;
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;
child->context.index = i;
2015-10-14 21:52:13 -07:00
child->context.offset = ts_tree_total_size(self);
child->context.offset_point = ts_tree_offset_point(self);
if (i == 0) {
2015-10-14 21:52:13 -07:00
self->padding = child->padding;
self->size = child->size;
self->padding_point = child->padding_point;
self->size_point = child->size_point;
} else {
2015-10-14 21:52:13 -07:00
self->size =
ts_length_add(ts_length_add(self->size, child->padding), child->size);
self->size_point = ts_point_add(ts_point_add(self->size_point, child->padding_point), child->size_point);
}
switch (child->options.type) {
case TSNodeTypeNamed:
2015-10-14 21:52:13 -07:00
self->visible_child_count++;
self->named_child_count++;
break;
case TSNodeTypeAnonymous:
2015-10-14 21:52:13 -07:00
self->visible_child_count++;
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;
break;
}
2014-07-20 20:27:33 -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;
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
}
}
2014-07-20 20:27:33 -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(), ts_point_zero(), ts_point_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++;
}
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
}
size_t ts_tree_offset_column(const TSTree *self) {
2015-11-25 11:08:44 -05:00
const TSTree *parent = self;
size_t column = self->padding_point.column;
if (self->padding_point.row > 0) {
return column;
}
2015-11-25 11:08:44 -05:00
do {
parent = parent->context.parent;
if (!parent) break;
column += parent->context.offset_point.column;
} while (parent->context.offset_point.row == 0);
2015-11-25 11:08:44 -05:00
return column;
2015-11-25 11:08:44 -05: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);
}
TSPoint ts_tree_offset_point(const TSTree *self) {
return ts_point_add(self->padding_point, self->size_point);
}
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)
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
}
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);
}
}
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,
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;
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-07-20 20:27:33 -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];
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
}
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];
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));
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;
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;
ts_tree_edit(
child,
(TSInputEdit){
.position = 0, .chars_inserted = 0, .chars_removed = chars_removed,
});
2015-09-15 16:00:16 -07:00
} else {
break;
}
}
}
}