tree-sitter/src/runtime/tree.c

211 lines
6.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
TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength size, TSLength padding,
2014-09-03 18:53:38 -07:00
bool is_hidden) {
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,
.children = NULL,
.padding = padding,
.options = is_hidden ? TSTreeOptionsHidden : 0,
};
2014-07-20 20:27:33 -07:00
return result;
}
TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) {
TSTree *result = ts_tree_make_leaf(ts_builtin_sym_error, size, padding, false);
ts_tree_set_fragile_left(result);
ts_tree_set_fragile_right(result);
result->lookahead_char = lookahead_char;
2014-07-20 20:27:33 -07:00
return result;
}
TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
2014-09-01 14:08:07 -07:00
TSTree **children, bool is_hidden) {
2015-08-14 16:11:37 -07:00
TSTree *result = malloc(sizeof(TSTree));
2014-08-31 16:24:27 -07:00
/*
* Determine the new node's size, padding and visible child count based on
2014-08-31 16:24:27 -07:00
* the given child nodes.
*/
TSLength size = ts_length_zero(), padding = ts_length_zero();
size_t 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);
child->context.parent = result;
child->context.index = i;
if (i == 0) {
padding = child->padding;
size = child->size;
} else {
size = ts_length_add(ts_length_add(size, child->padding), child->size);
}
2014-07-20 20:27:33 -07:00
if (ts_tree_is_visible(child))
visible_child_count++;
else
2014-09-01 14:08:07 -07:00
visible_child_count += child->visible_child_count;
2014-07-20 20:27:33 -07:00
}
/*
2014-08-31 16:24:27 -07:00
* Mark the tree as hidden if it wraps a single child node.
*/
2014-07-20 20:27:33 -07:00
TSTreeOptions options = 0;
2015-06-15 15:24:15 -07:00
if (symbol == ts_builtin_sym_error) {
options |= (TSTreeOptionsFragileLeft | TSTreeOptionsFragileRight);
} else {
if (is_hidden)
options |= TSTreeOptionsHidden;
if (child_count > 0) {
if (ts_tree_is_fragile_left(children[0]))
options |= (TSTreeOptionsFragileLeft);
if (ts_tree_is_fragile_right(children[child_count - 1]))
options |= (TSTreeOptionsFragileRight);
}
}
2014-07-20 20:27:33 -07:00
2015-07-27 18:29:48 -07:00
*result = (TSTree){.ref_count = 1,
2015-08-16 10:27:26 -07:00
.context = {.parent = NULL, .index = 0 },
2015-07-27 18:29:48 -07:00
.symbol = symbol,
.children = children,
.child_count = child_count,
.visible_child_count = visible_child_count,
.size = size,
.padding = padding,
.options = options };
2014-07-20 20:27:33 -07:00
return result;
2014-01-07 21:50:32 -08:00
}
void ts_tree_retain(TSTree *tree) {
assert(tree->ref_count > 0);
tree->ref_count++;
}
2014-01-07 21:50:32 -08:00
void ts_tree_release(TSTree *tree) {
assert(tree->ref_count > 0);
2014-07-20 20:27:33 -07:00
tree->ref_count--;
if (tree->ref_count == 0) {
2015-08-14 16:11:37 -07:00
for (size_t i = 0; i < tree->child_count; i++)
ts_tree_release(tree->children[i]);
if (tree->child_count > 0)
free(tree->children);
2014-07-20 20:27:33 -07:00
free(tree);
}
2014-03-24 00:34:13 -07:00
}
TSLength ts_tree_total_size(const TSTree *tree) {
return ts_length_add(tree->padding, tree->size);
}
2014-10-03 16:06:08 -07:00
bool ts_tree_eq(const TSTree *node1, const TSTree *node2) {
2015-06-03 09:44:13 -07:00
if (node1) {
2015-07-27 18:29:48 -07:00
if (!node2)
return false;
2015-06-03 09:44:13 -07:00
} else {
return !node2;
}
if (node1->symbol != node2->symbol)
2014-10-03 16:06:08 -07:00
return false;
if (node1->symbol == ts_builtin_sym_error)
return node1->lookahead_char == node2->lookahead_char;
if (node1->child_count != node2->child_count)
2014-10-03 16:06:08 -07:00
return false;
if (node1->visible_child_count != node2->visible_child_count)
2014-10-03 16:06:08 -07:00
return false;
for (size_t i = 0; i < node1->child_count; i++)
2014-10-03 16:06:08 -07:00
if (!ts_tree_eq(node1->children[i], node2->children[i]))
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-08-16 19:53:34 -07:00
static size_t ts_tree__write_to_string(const TSTree *tree,
const char **symbol_names, char *string,
size_t limit, int is_root) {
2014-09-01 14:08:07 -07:00
if (!tree)
return snprintf(string, limit, "(NULL)");
2014-07-20 20:27:33 -07:00
char *cursor = string;
char **writer = (limit > 0) ? &cursor : &string;
2014-09-01 14:08:07 -07:00
int visible = ts_tree_is_visible(tree) || 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-06-15 15:24:15 -07:00
if (tree->symbol == ts_builtin_sym_error && tree->child_count == 0) {
cursor += snprintf(*writer, limit, "(UNEXPECTED ");
2014-09-01 14:08:07 -07:00
cursor += write_lookahead_to_string(*writer, limit, tree->lookahead_char);
} else {
cursor += snprintf(*writer, limit, "(%s", symbol_names[tree->symbol]);
}
2014-07-20 20:27:33 -07:00
}
2014-09-01 14:08:07 -07:00
for (size_t i = 0; i < tree->child_count; i++) {
TSTree *child = tree->children[i];
2015-08-16 19:53:34 -07:00
cursor += ts_tree__write_to_string(child, symbol_names, *writer, limit, 0);
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 *tree, const char **symbol_names) {
2014-08-31 16:24:27 -07:00
static char SCRATCH[1];
2015-08-16 19:53:34 -07:00
size_t size = ts_tree__write_to_string(tree, symbol_names, SCRATCH, 0, 1) + 1;
2014-07-20 20:27:33 -07:00
char *result = malloc(size * sizeof(char));
2015-08-16 19:53:34 -07:00
ts_tree__write_to_string(tree, symbol_names, result, size, 1);
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
void ts_tree_prepend_children(TSTree *tree, size_t count, TSTree **children) {
if (count == 0)
return;
tree->size = ts_length_add(tree->size, tree->padding);
size_t visible_count = 0;
for (size_t i = 0; i < count; i++) {
if (i == 0)
tree->padding = children[i]->padding;
else
tree->size = ts_length_add(tree->size, children[i]->padding);
tree->size = ts_length_add(tree->size, children[i]->size);
if (ts_tree_is_visible(children[i]))
visible_count++;
}
size_t new_child_count = count + tree->child_count;
TSTree **new_children = realloc(children, new_child_count * sizeof(TSTree *));
memcpy(new_children + count, tree->children,
tree->child_count * sizeof(TSTree *));
free(tree->children);
tree->children = new_children;
tree->visible_child_count += visible_count;
tree->child_count += count;
}