diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 6201ace3..c064403b 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -462,32 +462,8 @@ static ConsumeResult ts_parser__consume_lookahead(TSParser *self, int head) { } } -static int ts_tree__compare(TSTree *left, TSTree *right) { - if (left->symbol < right->symbol) - return -1; - if (right->symbol < left->symbol) - return 1; - if (left->child_count < right->child_count) - return -1; - if (right->child_count < left->child_count) - return 1; - for (size_t i = 0; i < left->child_count; i++) { - TSTree *left_child = left->children[i]; - TSTree *right_child = right->children[i]; - switch (ts_tree__compare(left_child, right_child)) { - case -1: - return -1; - case 1: - return 1; - default: - break; - } - } - return 0; -} - static TSTree *ts_parser__select_tree(void *data, TSTree *left, TSTree *right) { - if (ts_tree__compare(left, right) <= 0) + if (ts_tree_compare(left, right) <= 0) return left; else return right; diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 4c04e9b8..0591ba03 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -143,6 +143,30 @@ bool ts_tree_eq(const TSTree *self, const TSTree *other) { return true; } +int ts_tree_compare(const TSTree *left, const TSTree *right) { + if (left->symbol < right->symbol) + return -1; + if (right->symbol < left->symbol) + return 1; + if (left->child_count < right->child_count) + return -1; + if (right->child_count < left->child_count) + return 1; + for (size_t i = 0; i < left->child_count; i++) { + TSTree *left_child = left->children[i]; + TSTree *right_child = right->children[i]; + switch (ts_tree_compare(left_child, right_child)) { + case -1: + return -1; + case 1: + return 1; + default: + break; + } + } + return 0; +} + static size_t write_lookahead_to_string(char *string, size_t limit, char lookahead) { switch (lookahead) { diff --git a/src/runtime/tree.h b/src/runtime/tree.h index b443868e..93bfdecf 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -40,6 +40,7 @@ TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) void ts_tree_retain(TSTree *tree); void ts_tree_release(TSTree *tree); bool ts_tree_eq(const TSTree *tree1, const TSTree *tree2); +int ts_tree_compare(const TSTree *tree1, const TSTree *tree2); char *ts_tree_string(const TSTree *tree, const char **names, bool include_anonymous); TSLength ts_tree_total_size(const TSTree *tree);