2014-01-07 21:50:32 -08:00
|
|
|
#include <string.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-03-24 00:34:13 -07:00
|
|
|
|
2014-07-16 18:38:06 -07:00
|
|
|
static TSTree * ts_tree_make(TSSymbol symbol, size_t size, size_t offset, int is_hidden) {
|
2014-06-28 18:45:22 -07:00
|
|
|
TSTree *result = malloc(sizeof(TSTree));
|
|
|
|
|
*result = (TSTree) {
|
2014-03-19 19:27:31 -07:00
|
|
|
.ref_count = 1,
|
|
|
|
|
.symbol = symbol,
|
|
|
|
|
.size = size,
|
|
|
|
|
.offset = offset,
|
2014-07-16 18:38:06 -07:00
|
|
|
.options = is_hidden ? TSTreeOptionsHidden : 0,
|
2014-03-19 19:27:31 -07:00
|
|
|
};
|
2014-03-01 00:25:05 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 18:38:06 -07:00
|
|
|
TSTree * ts_tree_make_leaf(TSSymbol symbol, size_t size, size_t offset, int is_hidden) {
|
|
|
|
|
TSTree *result = ts_tree_make(symbol, size, offset, is_hidden);
|
|
|
|
|
result->child_count = 0;
|
|
|
|
|
result->children = NULL;
|
2014-02-24 18:42:54 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 18:38:06 -07:00
|
|
|
TSTree * ts_tree_make_node(TSSymbol symbol, size_t child_count, TSTree **children, int is_hidden) {
|
2014-06-09 13:12:28 -07:00
|
|
|
size_t size = 0, offset = 0;
|
2014-07-16 18:38:06 -07:00
|
|
|
for (size_t i = 0; i < child_count; i++) {
|
|
|
|
|
TSTree *child = children[i];
|
2014-06-02 13:32:36 -07:00
|
|
|
ts_tree_retain(child);
|
|
|
|
|
if (i == 0) {
|
2014-07-14 21:11:15 -07:00
|
|
|
offset = child->offset;
|
|
|
|
|
size = child->size;
|
2014-06-02 13:32:36 -07:00
|
|
|
} else {
|
2014-07-14 21:11:15 -07:00
|
|
|
size += child->offset + child->size;
|
2014-06-02 13:32:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
2014-07-16 18:38:06 -07:00
|
|
|
|
|
|
|
|
TSTree *result = ts_tree_make(symbol, size, offset, is_hidden);
|
|
|
|
|
result->child_count = child_count;
|
|
|
|
|
result->children = children;
|
|
|
|
|
|
|
|
|
|
if (child_count == 1 && (ts_tree_is_visible(children[0]) || ts_tree_is_wrapper(children[0])))
|
|
|
|
|
result->options |= (TSTreeOptionsWrapper | TSTreeOptionsHidden);
|
|
|
|
|
|
2013-12-17 13:14:41 -08:00
|
|
|
return result;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:53:32 -07:00
|
|
|
TSTree * ts_tree_make_error(char lookahead_char, size_t expected_input_count, const TSSymbol *expected_inputs, size_t size, size_t offset) {
|
2014-07-16 18:38:06 -07:00
|
|
|
TSTree *result = ts_tree_make(ts_builtin_sym_error, size, offset, 0);
|
|
|
|
|
result->lookahead_char = lookahead_char;
|
|
|
|
|
result->expected_input_count = expected_input_count;
|
|
|
|
|
result->expected_inputs = expected_inputs;
|
2014-02-24 18:42:54 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:45:22 -07:00
|
|
|
void ts_tree_retain(TSTree *tree) {
|
2014-01-07 21:50:32 -08:00
|
|
|
tree->ref_count++;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 21:11:15 -07:00
|
|
|
void ts_tree_release(TSTree *tree) {
|
|
|
|
|
tree->ref_count--;
|
|
|
|
|
if (tree->ref_count == 0) {
|
|
|
|
|
size_t count;
|
2014-07-16 18:38:06 -07:00
|
|
|
TSTree **children = ts_tree_children(tree, &count);
|
2014-07-14 21:11:15 -07:00
|
|
|
for (size_t i = 0; i < count; i++)
|
|
|
|
|
ts_tree_release(children[i]);
|
2014-07-16 18:38:06 -07:00
|
|
|
free(tree->children);
|
2014-07-14 21:11:15 -07:00
|
|
|
free(tree);
|
|
|
|
|
}
|
2014-03-24 00:34:13 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:45:22 -07:00
|
|
|
size_t ts_tree_total_size(const TSTree *tree) {
|
2014-07-14 21:11:15 -07:00
|
|
|
return tree->offset + tree->size;
|
2014-03-24 00:34:13 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:45:22 -07:00
|
|
|
int ts_tree_equals(const TSTree *node1, const TSTree *node2) {
|
2014-02-24 18:42:54 -08:00
|
|
|
if (node1->symbol != node2->symbol) return 0;
|
2014-02-26 19:03:43 -08:00
|
|
|
if (node1->symbol == ts_builtin_sym_error) {
|
2014-02-24 18:42:54 -08:00
|
|
|
// check error equality
|
|
|
|
|
} else {
|
2014-07-16 18:38:06 -07:00
|
|
|
if (node1->child_count != node2->child_count) return 0;
|
|
|
|
|
for (size_t i = 0; i < node1->child_count; i++)
|
|
|
|
|
if (!ts_tree_equals(node1->children[i], node2->children[i])) return 0;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:45:22 -07:00
|
|
|
TSTree ** ts_tree_children(const TSTree *tree, size_t *count) {
|
2014-03-21 12:46:23 -07:00
|
|
|
if (!tree || tree->symbol == ts_builtin_sym_error) {
|
2014-03-18 12:47:26 -07:00
|
|
|
if (count) *count = 0;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-07-16 18:38:06 -07:00
|
|
|
if (count) *count = tree->child_count;
|
|
|
|
|
return tree->children;
|
2014-02-24 18:42:54 -08:00
|
|
|
}
|
|
|
|
|
|
2014-05-26 21:50:01 -07:00
|
|
|
static size_t write_lookahead_to_string(char *string, size_t limit, char lookahead) {
|
|
|
|
|
switch (lookahead) {
|
|
|
|
|
case '\0':
|
|
|
|
|
return snprintf(string, limit, "<EOF>");
|
|
|
|
|
default:
|
|
|
|
|
return snprintf(string, limit, "'%c'", lookahead);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 18:38:06 -07:00
|
|
|
static size_t tree_write_to_string(const TSTree *tree, const char **symbol_names, char *string, size_t limit, int is_root) {
|
2014-03-19 19:27:31 -07:00
|
|
|
char *cursor = string;
|
2014-07-16 18:38:06 -07:00
|
|
|
char **writer = (limit > 0) ? &cursor : &string;
|
|
|
|
|
int visible = ts_tree_is_visible(tree);
|
|
|
|
|
|
|
|
|
|
if (visible && !is_root)
|
|
|
|
|
cursor += snprintf(*writer, limit, " ");
|
2014-03-21 12:46:23 -07:00
|
|
|
|
2014-03-19 22:59:07 -07:00
|
|
|
if (!tree)
|
2014-07-16 18:38:06 -07:00
|
|
|
return snprintf(*writer, limit, "(NULL)");
|
2014-05-26 21:50:01 -07:00
|
|
|
if (tree->symbol == ts_builtin_sym_error) {
|
2014-07-16 18:38:06 -07:00
|
|
|
cursor += snprintf(*writer, limit, "(ERROR ");
|
|
|
|
|
cursor += write_lookahead_to_string(*writer, limit, tree->lookahead_char);
|
|
|
|
|
cursor += snprintf(*writer, limit, ")");
|
2014-05-26 21:50:01 -07:00
|
|
|
return cursor - string;
|
|
|
|
|
}
|
2014-03-19 22:59:07 -07:00
|
|
|
|
2014-07-16 18:38:06 -07:00
|
|
|
if (visible) {
|
|
|
|
|
cursor += snprintf(*writer, limit, "(%s", symbol_names[tree->symbol]);
|
|
|
|
|
is_root = 0;
|
2014-03-19 19:27:31 -07:00
|
|
|
}
|
2014-07-16 18:38:06 -07:00
|
|
|
|
|
|
|
|
for (size_t i = 0; i < tree->child_count; i++)
|
|
|
|
|
cursor += tree_write_to_string(tree->children[i], symbol_names, *writer, limit, is_root);
|
|
|
|
|
|
|
|
|
|
if (visible)
|
|
|
|
|
cursor += snprintf(*writer, limit, ")");
|
2014-03-24 09:14:29 -07:00
|
|
|
|
2014-03-19 22:59:07 -07:00
|
|
|
return cursor - string;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:45:22 -07:00
|
|
|
char * ts_tree_string(const TSTree *tree, const char **symbol_names) {
|
2014-03-24 00:36:47 -07:00
|
|
|
static char SCRATCH_STRING[1];
|
2014-07-16 18:38:06 -07:00
|
|
|
size_t size = tree_write_to_string(tree, symbol_names, SCRATCH_STRING, 0, 1) + 1;
|
2014-03-08 16:30:44 -08:00
|
|
|
char *result = malloc(size * sizeof(char));
|
2014-07-16 18:38:06 -07:00
|
|
|
tree_write_to_string(tree, symbol_names, result, size, 1);
|
2014-02-19 18:58:28 -08:00
|
|
|
return result;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|