Add option to tree_string to include anonymous tokens

This commit is contained in:
Max Brunsfeld 2015-10-28 12:09:28 -07:00
parent 9b68ee62c8
commit 36eae5d5e9
5 changed files with 22 additions and 14 deletions

View file

@ -15,7 +15,7 @@ TSTree ** tree_array(std::vector<TSTree *> trees) {
}
std::ostream &operator<<(std::ostream &stream, const TSTree *tree) {
return stream << std::string(ts_tree_string(tree, symbol_names));;
return stream << std::string(ts_tree_string(tree, symbol_names, true));;
}
std::ostream &operator<<(std::ostream &stream, const TSNode node) {

View file

@ -316,11 +316,11 @@ describe("Tree", []() {
describe("serialization", [&]() {
it("returns a readable string", [&]() {
char *string1 = ts_tree_string(tree1, names);
char *string1 = ts_tree_string(tree1, names, true);
AssertThat(string(string1), Equals("(cat)"));
free(string1);
char *string2 = ts_tree_string(parent1, names);
char *string2 = ts_tree_string(parent1, names, true);
AssertThat(string(string2), Equals("(dog (cat) (cat))"));
free(string2);
});
@ -328,7 +328,7 @@ describe("Tree", []() {
it("hides invisible nodes", [&]() {
tree2->options.type = TSNodeTypeHidden;
char *string1 = ts_tree_string(parent1, names);
char *string1 = ts_tree_string(parent1, names, true);
AssertThat(string(string1), Equals("(dog (cat))"));
free(string1);
});
@ -337,13 +337,13 @@ describe("Tree", []() {
it("still serializes it", [&]() {
parent1->options.type = TSNodeTypeHidden;
char *string1 = ts_tree_string(parent1, names);
char *string1 = ts_tree_string(parent1, names, true);
AssertThat(string(string1), Equals("(dog (cat) (cat))"));
free(string1);
tree1->options.type = TSNodeTypeHidden;
char *string2 = ts_tree_string(tree1, names);
char *string2 = ts_tree_string(tree1, names, true);
AssertThat(string(string2), Equals("(cat)"));
free(string2);
});

View file

@ -166,7 +166,7 @@ const char *ts_node_name(TSNode self, const TSDocument *document) {
const char *ts_node_string(TSNode self, const TSDocument *document) {
return ts_tree_string(ts_node__tree(self),
document->parser.language->symbol_names);
document->parser.language->symbol_names, false);
}
bool ts_node_eq(TSNode self, TSNode other) {

View file

@ -144,13 +144,16 @@ static size_t write_lookahead_to_string(char *string, size_t limit,
static size_t ts_tree__write_to_string(const TSTree *self,
const char **symbol_names, char *string,
size_t limit, int is_root) {
size_t limit, int is_root,
bool include_anonymous) {
if (!self)
return snprintf(string, limit, "(NULL)");
char *cursor = string;
char **writer = (limit > 0) ? &cursor : &string;
int visible = self->options.type == TSNodeTypeNamed || is_root;
TSNodeType min_node_type =
include_anonymous ? TSNodeTypeAnonymous : TSNodeTypeNamed;
int visible = self->options.type >= min_node_type || is_root;
if (visible && !is_root)
cursor += snprintf(*writer, limit, " ");
@ -166,7 +169,8 @@ static size_t ts_tree__write_to_string(const TSTree *self,
for (size_t i = 0; i < self->child_count; i++) {
TSTree *child = self->children[i];
cursor += ts_tree__write_to_string(child, symbol_names, *writer, limit, 0);
cursor += ts_tree__write_to_string(child, symbol_names, *writer, limit, 0,
include_anonymous);
}
if (visible)
@ -175,11 +179,14 @@ static size_t ts_tree__write_to_string(const TSTree *self,
return cursor - string;
}
char *ts_tree_string(const TSTree *self, const char **symbol_names) {
char *ts_tree_string(const TSTree *self, const char **symbol_names,
bool include_anonymous) {
static char SCRATCH[1];
size_t size = ts_tree__write_to_string(self, symbol_names, SCRATCH, 0, 1) + 1;
size_t size = 1 + ts_tree__write_to_string(self, symbol_names, SCRATCH, 0, 1,
include_anonymous);
char *result = malloc(size * sizeof(char));
ts_tree__write_to_string(self, symbol_names, result, size, 1);
ts_tree__write_to_string(self, symbol_names, result, size, 1,
include_anonymous);
return result;
}

View file

@ -40,7 +40,8 @@ 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);
char *ts_tree_string(const TSTree *tree, const char **names);
char *ts_tree_string(const TSTree *tree, const char **names,
bool include_anonymous);
TSLength ts_tree_total_size(const TSTree *tree);
void ts_tree_prepend_children(TSTree *, size_t, TSTree **);
void ts_tree_edit(TSTree *, TSInputEdit);