Rename type ts_tree -> TSTree
This commit is contained in:
parent
2795078633
commit
5f59de72a8
16 changed files with 120 additions and 120 deletions
|
|
@ -3,9 +3,9 @@
|
|||
#include <stdio.h>
|
||||
#include "runtime/tree.h"
|
||||
|
||||
static ts_tree * ts_tree_make(ts_symbol symbol, size_t size, size_t offset) {
|
||||
ts_tree *result = malloc(sizeof(ts_tree));
|
||||
*result = (ts_tree) {
|
||||
static TSTree * ts_tree_make(ts_symbol symbol, size_t size, size_t offset) {
|
||||
TSTree *result = malloc(sizeof(TSTree));
|
||||
*result = (TSTree) {
|
||||
.ref_count = 1,
|
||||
.symbol = symbol,
|
||||
.size = size,
|
||||
|
|
@ -14,19 +14,19 @@ static ts_tree * ts_tree_make(ts_symbol symbol, size_t size, size_t offset) {
|
|||
return result;
|
||||
}
|
||||
|
||||
ts_tree * ts_tree_make_leaf(ts_symbol symbol, size_t size, size_t offset) {
|
||||
ts_tree *result = ts_tree_make(symbol, size, offset);
|
||||
TSTree * ts_tree_make_leaf(ts_symbol symbol, size_t size, size_t offset) {
|
||||
TSTree *result = ts_tree_make(symbol, size, offset);
|
||||
result->data.children.count = 0;
|
||||
result->data.children.immediate_count = 0;
|
||||
result->data.children.contents = NULL;
|
||||
return result;
|
||||
}
|
||||
|
||||
ts_tree * ts_tree_make_node(ts_symbol symbol, size_t child_count, size_t immediate_child_count, ts_tree **children) {
|
||||
ts_tree **immediate_children = children + child_count;
|
||||
TSTree * ts_tree_make_node(ts_symbol symbol, size_t child_count, size_t immediate_child_count, TSTree **children) {
|
||||
TSTree **immediate_children = children + child_count;
|
||||
size_t size = 0, offset = 0;
|
||||
for (size_t i = 0; i < immediate_child_count; i++) {
|
||||
ts_tree *child = immediate_children[i];
|
||||
TSTree *child = immediate_children[i];
|
||||
ts_tree_retain(child);
|
||||
if (i == 0) {
|
||||
offset = ts_tree_offset(child);
|
||||
|
|
@ -35,42 +35,42 @@ ts_tree * ts_tree_make_node(ts_symbol symbol, size_t child_count, size_t immedia
|
|||
size += ts_tree_offset(child) + ts_tree_size(child);
|
||||
}
|
||||
}
|
||||
ts_tree *result = ts_tree_make(symbol, size, offset);
|
||||
TSTree *result = ts_tree_make(symbol, size, offset);
|
||||
result->data.children.count = child_count;
|
||||
result->data.children.immediate_count = immediate_child_count;
|
||||
result->data.children.contents = children;
|
||||
return result;
|
||||
}
|
||||
|
||||
ts_tree * ts_tree_make_error(char lookahead_char, size_t expected_input_count, const ts_symbol *expected_inputs, size_t size, size_t offset) {
|
||||
ts_tree *result = ts_tree_make(ts_builtin_sym_error, size, offset);
|
||||
TSTree * ts_tree_make_error(char lookahead_char, size_t expected_input_count, const ts_symbol *expected_inputs, size_t size, size_t offset) {
|
||||
TSTree *result = ts_tree_make(ts_builtin_sym_error, size, offset);
|
||||
result->data.error.lookahead_char = lookahead_char;
|
||||
result->data.error.expected_input_count = expected_input_count;
|
||||
result->data.error.expected_inputs = expected_inputs;
|
||||
return result;
|
||||
}
|
||||
|
||||
ts_symbol ts_tree_symbol(const ts_tree *tree) {
|
||||
ts_symbol ts_tree_symbol(const TSTree *tree) {
|
||||
return tree->symbol;
|
||||
}
|
||||
|
||||
void ts_tree_retain(ts_tree *tree) {
|
||||
void ts_tree_retain(TSTree *tree) {
|
||||
tree->ref_count++;
|
||||
}
|
||||
|
||||
size_t ts_tree_offset(const ts_tree *tree) {
|
||||
size_t ts_tree_offset(const TSTree *tree) {
|
||||
return tree->offset;
|
||||
}
|
||||
|
||||
size_t ts_tree_size(const ts_tree *tree) {
|
||||
size_t ts_tree_size(const TSTree *tree) {
|
||||
return tree->size;
|
||||
}
|
||||
|
||||
size_t ts_tree_total_size(const ts_tree *tree) {
|
||||
size_t ts_tree_total_size(const TSTree *tree) {
|
||||
return ts_tree_offset(tree) + ts_tree_size(tree);
|
||||
}
|
||||
|
||||
ts_tree ** ts_tree_immediate_children(const ts_tree *tree, size_t *count) {
|
||||
TSTree ** ts_tree_immediate_children(const TSTree *tree, size_t *count) {
|
||||
if (!tree || tree->symbol == ts_builtin_sym_error) {
|
||||
if (count) *count = 0;
|
||||
return NULL;
|
||||
|
|
@ -79,11 +79,11 @@ ts_tree ** ts_tree_immediate_children(const ts_tree *tree, size_t *count) {
|
|||
return tree->data.children.contents + tree->data.children.count;
|
||||
}
|
||||
|
||||
void ts_tree_release(ts_tree *tree) {
|
||||
void ts_tree_release(TSTree *tree) {
|
||||
tree->ref_count--;
|
||||
if (tree->ref_count == 0) {
|
||||
size_t count;
|
||||
ts_tree **children = ts_tree_immediate_children(tree, &count);
|
||||
TSTree **children = ts_tree_immediate_children(tree, &count);
|
||||
for (size_t i = 0; i < count; i++)
|
||||
ts_tree_release(children[i]);
|
||||
free(tree->data.children.contents);
|
||||
|
|
@ -91,14 +91,14 @@ void ts_tree_release(ts_tree *tree) {
|
|||
}
|
||||
}
|
||||
|
||||
int ts_tree_equals(const ts_tree *node1, const ts_tree *node2) {
|
||||
int ts_tree_equals(const TSTree *node1, const TSTree *node2) {
|
||||
if (node1->symbol != node2->symbol) return 0;
|
||||
if (node1->symbol == ts_builtin_sym_error) {
|
||||
// check error equality
|
||||
} else {
|
||||
size_t count1, count2;
|
||||
ts_tree **children1 = ts_tree_children(node1, &count1);
|
||||
ts_tree **children2 = ts_tree_children(node2, &count2);
|
||||
TSTree **children1 = ts_tree_children(node1, &count1);
|
||||
TSTree **children2 = ts_tree_children(node2, &count2);
|
||||
if (count1 != count2) return 0;
|
||||
for (size_t i = 0; i < count1; i++)
|
||||
if (!ts_tree_equals(children1[i], children2[i])) return 0;
|
||||
|
|
@ -106,7 +106,7 @@ int ts_tree_equals(const ts_tree *node1, const ts_tree *node2) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
ts_tree ** ts_tree_children(const ts_tree *tree, size_t *count) {
|
||||
TSTree ** ts_tree_children(const TSTree *tree, size_t *count) {
|
||||
if (!tree || tree->symbol == ts_builtin_sym_error) {
|
||||
if (count) *count = 0;
|
||||
return NULL;
|
||||
|
|
@ -124,7 +124,7 @@ static size_t write_lookahead_to_string(char *string, size_t limit, char lookahe
|
|||
}
|
||||
}
|
||||
|
||||
static size_t tree_write_to_string(const ts_tree *tree, const char **symbol_names, char *string, size_t limit) {
|
||||
static size_t tree_write_to_string(const TSTree *tree, const char **symbol_names, char *string, size_t limit) {
|
||||
char *cursor = string;
|
||||
char **destination = (limit > 0) ? &cursor : &string;
|
||||
|
||||
|
|
@ -139,7 +139,7 @@ static size_t tree_write_to_string(const ts_tree *tree, const char **symbol_name
|
|||
|
||||
cursor += snprintf(*destination, limit, "(%s", symbol_names[tree->symbol]);
|
||||
size_t count;
|
||||
ts_tree **children = ts_tree_children(tree, &count);
|
||||
TSTree **children = ts_tree_children(tree, &count);
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
cursor += snprintf(*destination, limit, " ");
|
||||
cursor += tree_write_to_string(children[i], symbol_names, *destination, limit);
|
||||
|
|
@ -149,7 +149,7 @@ static size_t tree_write_to_string(const ts_tree *tree, const char **symbol_name
|
|||
return cursor - string;
|
||||
}
|
||||
|
||||
char * ts_tree_string(const ts_tree *tree, const char **symbol_names) {
|
||||
char * ts_tree_string(const TSTree *tree, const char **symbol_names) {
|
||||
static char SCRATCH_STRING[1];
|
||||
size_t size = tree_write_to_string(tree, symbol_names, SCRATCH_STRING, 0) + 1;
|
||||
char *result = malloc(size * sizeof(char));
|
||||
|
|
@ -157,6 +157,6 @@ char * ts_tree_string(const ts_tree *tree, const char **symbol_names) {
|
|||
return result;
|
||||
}
|
||||
|
||||
char * ts_tree_error_string(const ts_tree *tree, const char **symbol_names) {
|
||||
char * ts_tree_error_string(const TSTree *tree, const char **symbol_names) {
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue