2014-02-18 09:07:00 -08:00
|
|
|
#include "tree_sitter/runtime.h"
|
2014-02-19 18:58:28 -08:00
|
|
|
#include <string>
|
2014-01-07 21:50:32 -08:00
|
|
|
#include <string.h>
|
2013-12-17 13:14:41 -08:00
|
|
|
|
2014-02-19 18:58:28 -08:00
|
|
|
using std::string;
|
2014-02-24 18:42:54 -08:00
|
|
|
using std::to_string;
|
2014-02-19 18:58:28 -08:00
|
|
|
|
2014-02-24 18:42:54 -08:00
|
|
|
const ts_symbol ts_symbol_error = -1;
|
|
|
|
|
|
|
|
|
|
ts_tree * ts_tree_make_leaf(ts_symbol symbol) {
|
2014-02-20 13:30:43 -08:00
|
|
|
ts_tree *result = new ts_tree();
|
2014-02-19 18:58:28 -08:00
|
|
|
result->ref_count = 0;
|
2014-02-24 18:42:54 -08:00
|
|
|
result->symbol = symbol;
|
|
|
|
|
result->data.children = {
|
|
|
|
|
.count = 0,
|
|
|
|
|
.contents = NULL
|
|
|
|
|
};
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ts_tree * ts_tree_make_node(ts_symbol symbol, size_t child_count, ts_tree **children) {
|
|
|
|
|
ts_tree *result = new ts_tree();
|
|
|
|
|
result->ref_count = 0;
|
|
|
|
|
result->symbol = symbol;
|
|
|
|
|
result->data.children = {
|
|
|
|
|
.count = child_count,
|
|
|
|
|
.contents = children
|
|
|
|
|
};
|
2014-01-07 21:50:32 -08:00
|
|
|
for (int i = 0; i < child_count; i++)
|
2014-02-20 13:30:43 -08:00
|
|
|
ts_tree_retain(children[i]);
|
2013-12-17 13:14:41 -08:00
|
|
|
return result;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2014-02-24 18:42:54 -08:00
|
|
|
ts_tree * ts_tree_make_error(char lookahead_char, size_t expected_input_count, const ts_symbol *expected_inputs) {
|
|
|
|
|
ts_tree *result = new ts_tree();
|
2014-02-25 18:32:17 -08:00
|
|
|
result->symbol = ts_symbol_error;
|
2014-02-24 18:42:54 -08:00
|
|
|
result->data.error = {
|
|
|
|
|
.lookahead_char = lookahead_char,
|
|
|
|
|
.expected_input_count = expected_input_count,
|
|
|
|
|
};
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 13:30:43 -08:00
|
|
|
void ts_tree_retain(ts_tree *tree) {
|
2014-01-07 21:50:32 -08:00
|
|
|
tree->ref_count++;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 13:30:43 -08:00
|
|
|
void ts_tree_release(ts_tree *tree) {
|
2014-01-07 21:50:32 -08:00
|
|
|
tree->ref_count--;
|
|
|
|
|
if (tree->ref_count == 0) {
|
2014-02-24 18:42:54 -08:00
|
|
|
ts_tree **children = tree->data.children.contents;
|
|
|
|
|
for (int i = 0; i < ts_tree_child_count(tree); i++)
|
|
|
|
|
ts_tree_release(children[i]);
|
|
|
|
|
// free(children);
|
2014-01-07 21:50:32 -08:00
|
|
|
free(tree);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 13:30:43 -08:00
|
|
|
int ts_tree_equals(const ts_tree *node1, const ts_tree *node2) {
|
2014-02-24 18:42:54 -08:00
|
|
|
if (node1->symbol != node2->symbol) return 0;
|
|
|
|
|
if (node1->symbol == ts_symbol_error) {
|
|
|
|
|
// check error equality
|
|
|
|
|
} else {
|
|
|
|
|
if (node1->data.children.count != node2->data.children.count)
|
|
|
|
|
return 0;
|
|
|
|
|
for (int i = 0; i < node1->data.children.count; i++) {
|
|
|
|
|
ts_tree *child1 = node1->data.children.contents[i];
|
|
|
|
|
ts_tree *child2 = node2->data.children.contents[i];
|
|
|
|
|
if (!ts_tree_equals(child1, child2))
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-24 18:42:54 -08:00
|
|
|
ts_tree ** ts_tree_children(const ts_tree *tree) {
|
|
|
|
|
if (tree->symbol == ts_symbol_error) return NULL;
|
|
|
|
|
return tree->data.children.contents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t ts_tree_child_count(const ts_tree *tree) {
|
|
|
|
|
if (tree->symbol == ts_symbol_error) return 0;
|
|
|
|
|
return tree->data.children.count;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 13:30:43 -08:00
|
|
|
static string __tree_to_string(const ts_tree *tree, const char **symbol_names) {
|
2014-02-19 18:58:28 -08:00
|
|
|
if (!tree) return "#<null-tree>";
|
2014-02-25 18:32:17 -08:00
|
|
|
if (tree->symbol == ts_symbol_error) return "(ERROR)";
|
2014-02-24 18:42:54 -08:00
|
|
|
string result = string("(") + symbol_names[tree->symbol];
|
|
|
|
|
for (int i = 0; i < tree->data.children.count; i++)
|
|
|
|
|
result += " " + __tree_to_string(tree->data.children.contents[i], symbol_names);
|
2014-02-19 18:58:28 -08:00
|
|
|
return result + ")";
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2014-02-20 13:30:43 -08:00
|
|
|
char * ts_tree_string(const ts_tree *tree, const char **symbol_names) {
|
2014-02-19 18:58:28 -08:00
|
|
|
string value(__tree_to_string(tree, symbol_names));
|
|
|
|
|
char *result = (char *)malloc(value.size());
|
|
|
|
|
strcpy(result, value.c_str());
|
|
|
|
|
return result;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
2014-02-24 18:42:54 -08:00
|
|
|
|
|
|
|
|
char * ts_tree_error_string(const ts_tree *tree, const char **symbol_names) {
|
|
|
|
|
string result = string("Unexpected character '") + tree->data.error.lookahead_char + "'. Expected:";
|
|
|
|
|
for (int i = 0; i < tree->data.error.expected_input_count; i++) {
|
|
|
|
|
ts_symbol symbol = tree->data.error.expected_inputs[i];
|
|
|
|
|
result += string(" ") + symbol_names[symbol];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *stuff = (char *)malloc(result.size() * sizeof(char));
|
|
|
|
|
strcpy(stuff, result.c_str());
|
|
|
|
|
return stuff;
|
|
|
|
|
}
|