From 36eae5d5e990dc20ba3690f82464f5411e66c776 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 28 Oct 2015 12:09:28 -0700 Subject: [PATCH] Add option to tree_string to include anonymous tokens --- spec/runtime/helpers/tree_helpers.cc | 2 +- spec/runtime/tree_spec.cc | 10 +++++----- src/runtime/node.c | 2 +- src/runtime/tree.c | 19 +++++++++++++------ src/runtime/tree.h | 3 ++- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/spec/runtime/helpers/tree_helpers.cc b/spec/runtime/helpers/tree_helpers.cc index cb2f3131..2871b695 100644 --- a/spec/runtime/helpers/tree_helpers.cc +++ b/spec/runtime/helpers/tree_helpers.cc @@ -15,7 +15,7 @@ TSTree ** tree_array(std::vector 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) { diff --git a/spec/runtime/tree_spec.cc b/spec/runtime/tree_spec.cc index 5f536ad8..c2629fb3 100644 --- a/spec/runtime/tree_spec.cc +++ b/spec/runtime/tree_spec.cc @@ -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); }); diff --git a/src/runtime/node.c b/src/runtime/node.c index 02f437e4..f6919d2a 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -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) { diff --git a/src/runtime/tree.c b/src/runtime/tree.c index e7894303..822e51f8 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -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; } diff --git a/src/runtime/tree.h b/src/runtime/tree.h index e29d3e21..88208831 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -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);