2014-01-07 21:50:32 -08:00
|
|
|
#include <string.h>
|
2014-09-01 14:08:07 -07:00
|
|
|
#include <stdbool.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-09-26 16:15:07 -07:00
|
|
|
#include "runtime/length.h"
|
2014-03-24 00:34:13 -07:00
|
|
|
|
2014-09-26 16:15:07 -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));
|
2014-07-20 21:43:27 -07:00
|
|
|
*result = (TSTree) { .ref_count = 1,
|
2014-09-01 14:08:07 -07:00
|
|
|
.symbol = sym,
|
2014-07-20 21:43:27 -07:00
|
|
|
.size = size,
|
2014-08-31 16:39:16 -07:00
|
|
|
.child_count = 0,
|
|
|
|
|
.children = NULL,
|
2014-09-02 07:41:29 -07:00
|
|
|
.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;
|
2014-03-01 00:25:05 -08:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) {
|
2014-09-09 13:15:40 -07:00
|
|
|
TSTree *result = ts_tree_make_leaf(ts_builtin_sym_error, size, padding, false);
|
2015-02-21 10:39:58 -08:00
|
|
|
ts_tree_set_fragile_left(result);
|
|
|
|
|
ts_tree_set_fragile_right(result);
|
2014-08-28 13:22:06 -07:00
|
|
|
result->lookahead_char = lookahead_char;
|
2014-07-20 20:27:33 -07:00
|
|
|
return result;
|
2014-02-24 18:42:54 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
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
|
|
|
|
2014-08-28 13:22:06 -07:00
|
|
|
/*
|
2014-09-02 07:41:29 -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.
|
2014-08-28 13:22:06 -07:00
|
|
|
*/
|
2014-09-26 16:15:07 -07:00
|
|
|
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);
|
2014-09-02 07:41:29 -07:00
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
padding = child->padding;
|
|
|
|
|
size = child->size;
|
|
|
|
|
} else {
|
2014-09-26 16:15:07 -07:00
|
|
|
size = ts_length_add(ts_length_add(size, child->padding), child->size);
|
2014-09-02 07:41:29 -07:00
|
|
|
}
|
2014-07-16 18:38:06 -07:00
|
|
|
|
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-28 13:22:06 -07:00
|
|
|
/*
|
2014-08-31 16:24:27 -07:00
|
|
|
* Mark the tree as hidden if it wraps a single child node.
|
2014-08-28 13:22:06 -07:00
|
|
|
*/
|
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 == 1 &&
|
|
|
|
|
(ts_tree_is_visible(children[0]) || ts_tree_is_wrapper(children[0])))
|
|
|
|
|
options |= (TSTreeOptionsWrapper | 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
|
|
|
|
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.
|
|
|
|
|
*/
|
2014-08-28 13:22:06 -07:00
|
|
|
TSTree *result =
|
|
|
|
|
malloc(sizeof(TSTree) + (visible_child_count * sizeof(TSTreeChild)));
|
2014-07-20 21:43:27 -07:00
|
|
|
*result = (TSTree) { .ref_count = 1,
|
|
|
|
|
.symbol = symbol,
|
2014-08-31 16:39:16 -07:00
|
|
|
.children = children,
|
|
|
|
|
.child_count = child_count,
|
|
|
|
|
.visible_child_count = visible_child_count,
|
2014-07-20 21:43:27 -07:00
|
|
|
.size = size,
|
2014-09-02 07:41:29 -07:00
|
|
|
.padding = padding,
|
2014-08-27 22:06:27 -07:00
|
|
|
.options = options };
|
2014-08-27 22:23:45 -07:00
|
|
|
|
2014-08-28 13:22:06 -07:00
|
|
|
/*
|
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.
|
2014-08-28 13:22:06 -07:00
|
|
|
*/
|
|
|
|
|
TSTreeChild *visible_children = ts_tree_visible_children(result, NULL);
|
2014-09-26 16:15:07 -07:00
|
|
|
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];
|
2014-08-28 13:22:06 -07:00
|
|
|
|
2014-09-02 07:41:29 -07:00
|
|
|
if (i > 0)
|
2014-09-26 16:15:07 -07:00
|
|
|
offset = ts_length_add(offset, child->padding);
|
2014-09-02 07:41:29 -07:00
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
if (ts_tree_is_visible(child)) {
|
2014-08-28 13:22:06 -07:00
|
|
|
visible_children[vis_i].tree = child;
|
|
|
|
|
visible_children[vis_i].offset = offset;
|
|
|
|
|
vis_i++;
|
2014-07-20 20:27:33 -07:00
|
|
|
} else {
|
2014-08-28 13:22:06 -07:00
|
|
|
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);
|
2014-08-28 13:22:06 -07:00
|
|
|
vis_i++;
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-18 13:08:14 -07:00
|
|
|
}
|
2014-07-16 18:38:06 -07:00
|
|
|
|
2014-09-26 16:15:07 -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
|
|
|
}
|
|
|
|
|
|
2015-05-28 15:06:39 -07:00
|
|
|
TSTree *ts_tree_make_ambiguity(size_t alternative_count, TSTree **alternatives) {
|
|
|
|
|
TSTree *result = malloc(sizeof(TSTree));
|
|
|
|
|
*result = (TSTree) { .ref_count = 1,
|
|
|
|
|
.symbol = ts_builtin_sym_ambiguity,
|
|
|
|
|
.size = alternatives[0]->size,
|
|
|
|
|
.padding = alternatives[0]->padding,
|
|
|
|
|
.child_count = alternative_count,
|
|
|
|
|
.children = alternatives,
|
|
|
|
|
.options = 0 };
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
void ts_tree_retain(TSTree *tree) { tree->ref_count++; }
|
2014-01-07 21:50:32 -08:00
|
|
|
|
2014-07-14 21:11:15 -07: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);
|
2015-05-24 14:43:54 -07:00
|
|
|
if (children) {
|
|
|
|
|
for (size_t i = 0; i < count; i++)
|
|
|
|
|
ts_tree_release(children[i]);
|
|
|
|
|
free(tree->children);
|
|
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
free(tree);
|
|
|
|
|
}
|
2014-03-24 00:34:13 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
TSLength ts_tree_total_size(const TSTree *tree) {
|
|
|
|
|
return ts_length_add(tree->padding, tree->size);
|
2014-09-02 07:41:29 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-03 16:06:08 -07:00
|
|
|
bool ts_tree_eq(const TSTree *node1, const TSTree *node2) {
|
2014-07-20 21:43:27 -07:00
|
|
|
if (node1->symbol != node2->symbol)
|
2014-10-03 16:06:08 -07:00
|
|
|
return false;
|
2015-05-24 14:43:54 -07:00
|
|
|
if (node1->symbol == ts_builtin_sym_error)
|
|
|
|
|
return node1->lookahead_char == node2->lookahead_char;
|
2014-08-31 16:39:16 -07:00
|
|
|
if (node1->child_count != node2->child_count)
|
2014-10-03 16:06:08 -07:00
|
|
|
return false;
|
2014-08-31 16:39:16 -07:00
|
|
|
if (node1->visible_child_count != node2->visible_child_count)
|
2014-10-03 16:06:08 -07:00
|
|
|
return false;
|
2014-08-31 16:39:16 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSTree **ts_tree_children(const TSTree *tree, size_t *count) {
|
2015-05-24 14:43:54 -07:00
|
|
|
if (tree->symbol == ts_builtin_sym_error) {
|
|
|
|
|
if (count)
|
|
|
|
|
*count = 0;
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
if (count)
|
|
|
|
|
*count = tree->child_count;
|
|
|
|
|
return tree->children;
|
|
|
|
|
}
|
2014-02-24 18:42:54 -08:00
|
|
|
}
|
|
|
|
|
|
2014-09-01 14:08:07 -07:00
|
|
|
TSTreeChild *ts_tree_visible_children(const TSTree *tree, size_t *count) {
|
2015-06-15 15:24:15 -07:00
|
|
|
if (tree->child_count == 0) {
|
2015-05-24 14:43:54 -07:00
|
|
|
if (count)
|
|
|
|
|
*count = 0;
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
if (count)
|
|
|
|
|
*count = tree->visible_child_count;
|
|
|
|
|
return (TSTreeChild *)(tree + 1);
|
|
|
|
|
}
|
2014-09-01 14:08:07 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07: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);
|
|
|
|
|
}
|
2014-05-26 21:50:01 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-09 13:15:40 -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-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 {
|
2014-08-27 12:56:36 -07:00
|
|
|
cursor += snprintf(*writer, limit, "(%s", symbol_names[tree->symbol]);
|
|
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-16 18:38:06 -07:00
|
|
|
|
2015-06-15 15:24:15 -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
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07: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
|
|
|
}
|