tree-sitter/src/runtime/tree.c

212 lines
6.6 KiB
C
Raw Normal View History

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));
*result = (TSTree) { .ref_count = 1,
2014-09-01 14:08:07 -07:00
.symbol = sym,
.size = size,
.child_count = 0,
.children = NULL,
.lookahead_char = 0,
.padding = padding,
2014-09-01 14:08:07 -07:00
.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) {
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);
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;
if (is_hidden)
options |= TSTreeOptionsHidden;
if (child_count == 1 &&
(ts_tree_is_visible(children[0]) || ts_tree_is_wrapper(children[0])))
2014-07-20 20:27:33 -07:00
options |= (TSTreeOptionsWrapper | TSTreeOptionsHidden);
if (child_count > 0 && ts_tree_is_fragile_left(children[0]))
options |= (TSTreeOptionsFragileLeft);
if (child_count > 0 && ts_tree_is_fragile_right(children[child_count - 1]))
options |= (TSTreeOptionsFragileRight);
2014-07-20 20:27:33 -07:00
2014-08-31 16:24:27 -07:00
/*
* Store the visible child array adjacent to the tree itself. This avoids
* performing a second allocation and storing an additional pointer.
*/
TSTree *result =
malloc(sizeof(TSTree) + (visible_child_count * sizeof(TSTreeChild)));
*result = (TSTree) { .ref_count = 1,
.symbol = symbol,
.children = children,
.child_count = child_count,
.visible_child_count = visible_child_count,
.size = size,
.padding = padding,
.options = options };
/*
2014-08-31 16:24:27 -07:00
* Associate a relative offset with each of the visible child nodes, so that
* their positions can be queried without using the hidden child nodes.
*/
TSTreeChild *visible_children = ts_tree_visible_children(result, NULL);
TSLength offset = ts_length_zero();
for (size_t i = 0, vis_i = 0; i < child_count; i++) {
2014-07-20 20:27:33 -07:00
TSTree *child = children[i];
if (i > 0)
offset = ts_length_add(offset, child->padding);
2014-07-20 20:27:33 -07:00
if (ts_tree_is_visible(child)) {
visible_children[vis_i].tree = child;
visible_children[vis_i].offset = offset;
vis_i++;
2014-07-20 20:27:33 -07:00
} else {
size_t n = 0;
TSTreeChild *grandchildren = ts_tree_visible_children(child, &n);
for (size_t j = 0; j < n; j++) {
visible_children[vis_i].tree = grandchildren[j].tree;
2014-10-03 15:44:21 -07:00
visible_children[vis_i].offset =
ts_length_add(offset, grandchildren[j].offset);
vis_i++;
2014-07-20 20:27:33 -07:00
}
2014-07-18 13:08:14 -07:00
}
offset = ts_length_add(offset, child->size);
2014-07-20 20:27:33 -07:00
}
return result;
2014-01-07 21:50:32 -08:00
}
void ts_tree_retain(TSTree *tree) { tree->ref_count++; }
2014-01-07 21:50:32 -08:00
void ts_tree_release(TSTree *tree) {
2014-07-20 20:27:33 -07:00
tree->ref_count--;
if (tree->ref_count == 0) {
size_t count;
TSTree **children = ts_tree_children(tree, &count);
for (size_t i = 0; i < count; i++)
ts_tree_release(children[i]);
free(tree->children);
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) {
if (node1->symbol != node2->symbol)
2014-10-03 16:06:08 -07:00
return false;
if (node1->lookahead_char != node2->lookahead_char)
2014-10-03 16:06:08 -07:00
return false;
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
}
TSTree **ts_tree_children(const TSTree *tree, size_t *count) {
if (count)
*count = tree->child_count;
2014-07-20 20:27:33 -07:00
return tree->children;
}
2014-09-01 14:08:07 -07:00
TSTreeChild *ts_tree_visible_children(const TSTree *tree, size_t *count) {
if (count)
*count = tree->visible_child_count;
return (TSTreeChild *)(tree + 1);
}
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);
}
}
static size_t 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) {
if (tree->symbol == ts_builtin_sym_error) {
cursor += snprintf(*writer, limit, "(ERROR ");
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];
cursor += tree_write_to_string(child, symbol_names, *writer, limit, 0);
}
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];
size_t size = 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));
tree_write_to_string(tree, symbol_names, result, size, 1);
return result;
2014-01-07 21:50:32 -08:00
}