From 779bf0d7459d04ef90475ed9333329da0763c6a4 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 16 Jul 2014 18:38:06 -0700 Subject: [PATCH] Don't store tree's hidden children in a separate array Just mark hidden trees as such, and skip them when pretty-printing a tree --- include/tree_sitter/parser.h | 7 +- include/tree_sitter/runtime.h | 12 ++- spec/runtime/document_spec.cc | 6 +- spec/runtime/languages/arithmetic/errors.txt | 4 +- spec/runtime/languages/arithmetic/main.txt | 20 ++-- .../runtime/languages/javascript/literals.txt | 6 +- spec/runtime/languages/json/errors.txt | 10 +- spec/runtime/languages/json/main.txt | 15 ++- spec/runtime/node_position_spec.cc | 6 +- spec/runtime/parser_spec.cc | 6 +- spec/runtime/stack_spec.cc | 69 ++---------- spec/runtime/tree_spec.cc | 29 +++-- src/runtime/lexer.c | 4 +- src/runtime/parser.c | 10 +- src/runtime/stack.c | 74 ++++--------- src/runtime/tree.c | 101 +++++++++--------- src/runtime/tree.h | 31 ++++-- 17 files changed, 167 insertions(+), 243 deletions(-) diff --git a/include/tree_sitter/parser.h b/include/tree_sitter/parser.h index a636e5af..553e6e40 100644 --- a/include/tree_sitter/parser.h +++ b/include/tree_sitter/parser.h @@ -24,7 +24,7 @@ typedef struct { TSLexer ts_lexer_make(); int ts_lexer_advance(TSLexer *lexer); -TSTree * ts_lexer_build_node(TSLexer *lexer, TSSymbol symbol); +TSTree * ts_lexer_build_node(TSLexer *lexer, TSSymbol symbol, int is_hidden); static inline size_t ts_lexer_position(const TSLexer *lexer) { return lexer->chunk_start + lexer->position_in_chunk; @@ -141,13 +141,13 @@ ts_lexer_start_token(&parser->lexer); #define ACCEPT_TOKEN(symbol) \ { \ DEBUG_LEX("TOKEN %s", ts_symbol_names[symbol]); \ - return ts_lexer_build_node(&parser->lexer, symbol); \ + return ts_lexer_build_node(&parser->lexer, symbol, ts_hidden_symbol_flags[symbol]); \ } #define LEX_ERROR() \ { \ DEBUG_LEX("ERROR"); \ - return ts_lexer_build_node(&parser->lexer, ts_builtin_sym_error); \ + return ts_lexer_build_node(&parser->lexer, ts_builtin_sym_error, 0); \ } #define LEX_PANIC() \ @@ -171,7 +171,6 @@ ts_lexer_start_token(&parser->lexer); #define ACCEPT_INPUT() \ { .type = TSParseActionTypeAccept } - #define EXPORT_PARSER(constructor_name) \ TSParser * constructor_name() { \ return ts_parser_make((TSParserConfig) { \ diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 6d1f8403..203998e0 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -13,8 +13,15 @@ typedef unsigned short TSSymbol; #define ts_start_sym 2 typedef struct TSTree TSTree; -TSTree * ts_tree_make_leaf(TSSymbol symbol, size_t size, size_t offset); -TSTree * ts_tree_make_node(TSSymbol symbol, size_t child_count, size_t immediate_child_count, TSTree **children); + +typedef enum { + TSTreeOptionsHidden = 1, + TSTreeOptionsExtra = 2, + TSTreeOptionsWrapper = 4, +} TSTreeOptions; + +TSTree * ts_tree_make_leaf(TSSymbol symbol, size_t size, size_t offset, int is_hidden); +TSTree * ts_tree_make_node(TSSymbol symbol, size_t child_count, TSTree **children, int is_hidden); TSTree * ts_tree_make_error(char lookahead_char, size_t expected_input_count, const TSSymbol *expected_inputs, size_t size, size_t offset); void ts_tree_retain(TSTree *tree); void ts_tree_release(TSTree *tree); @@ -22,7 +29,6 @@ int ts_tree_equals(const TSTree *tree1, const TSTree *tree2); char * ts_tree_string(const TSTree *tree, const char **names); char * ts_tree_error_string(const TSTree *tree, const char **names); TSTree ** ts_tree_children(const TSTree *tree, size_t *count); -TSTree ** ts_tree_immediate_children(const TSTree *tree, size_t *count); size_t ts_tree_total_size(const TSTree *tree); typedef struct { diff --git a/spec/runtime/document_spec.cc b/spec/runtime/document_spec.cc index 2f8307a5..88650c1e 100644 --- a/spec/runtime/document_spec.cc +++ b/spec/runtime/document_spec.cc @@ -24,7 +24,7 @@ describe("incremental parsing", [&]() { it("parses the input", [&]() { AssertThat(string(ts_document_string(doc)), Equals( - "(value (object (string) (array (number) (number))))")); + "(object (string) (array (number) (number)))")); }); it("reads the entire input", [&]() { @@ -44,7 +44,7 @@ describe("incremental parsing", [&]() { it("updates the parse tree", [&]() { AssertThat(string(ts_document_string(doc)), Equals( - "(value (object (string) (array (number) (number)) (string) (number)))")); + "(object (string) (array (number) (number)) (string) (number))")); }); it("re-reads only the changed portion of the input", [&]() { @@ -64,7 +64,7 @@ describe("incremental parsing", [&]() { it("2 updates the parse tree", [&]() { AssertThat(string(ts_document_string(doc)), Equals( - "(value (object (string) (number) (string) (array (number) (number))))")); + "(object (string) (number) (string) (array (number) (number)))")); }); it_skip("re-reads only the changed portion of the input", [&]() { diff --git a/spec/runtime/languages/arithmetic/errors.txt b/spec/runtime/languages/arithmetic/errors.txt index ab181bb9..a459390a 100644 --- a/spec/runtime/languages/arithmetic/errors.txt +++ b/spec/runtime/languages/arithmetic/errors.txt @@ -10,6 +10,6 @@ recovers from errors inside parenthesized expressions ===================================================== x + (y * + z) * 5 --- -(expression (sum +(sum (variable) - (product (group (ERROR '+')) (number)))) + (product (group (ERROR '+')) (number))) diff --git a/spec/runtime/languages/arithmetic/main.txt b/spec/runtime/languages/arithmetic/main.txt index 86b67210..4a5a9726 100644 --- a/spec/runtime/languages/arithmetic/main.txt +++ b/spec/runtime/languages/arithmetic/main.txt @@ -3,51 +3,47 @@ parses numbers =================== 5 --- -(expression (number)) +(number) =================== parses variables =================== x --- -(expression (variable)) +(variable) =================== parses products =================== x * x --- -(expression (product - (variable) - (variable))) +(product (variable) (variable)) =================== parses sums =================== x + x --- -(expression (sum - (variable) - (variable))) +(sum (variable) (variable)) =============================================== binds multiplication more tightly than addition =============================================== a * b + c * d --- -(expression (sum +(sum (product (variable) (variable)) - (product (variable) (variable)))) + (product (variable) (variable))) ============================ parses exponents ============================ x + y * z^(a + b) --- -(expression (sum +(sum (variable) (product (variable) (exponent (variable) - (group (sum (variable) (variable))))))) + (group (sum (variable) (variable)))))) diff --git a/spec/runtime/languages/javascript/literals.txt b/spec/runtime/languages/javascript/literals.txt index 812a0bf9..76dd9115 100644 --- a/spec/runtime/languages/javascript/literals.txt +++ b/spec/runtime/languages/javascript/literals.txt @@ -39,7 +39,7 @@ var x = { (statement_block (var_declaration (identifier) (identifier))))))) ========================================== -parses comments z +parses comments ========================================== // this is the beginning of the script. // here we go. @@ -57,14 +57,14 @@ var thing = { (program (comment) (comment) - (var_declaration (identifier) (object + (program (var_declaration (identifier) (object (comment) (comment) (identifier) (function_expression (formal_parameters (identifier) (comment)) (statement_block (comment) - (expression_statement (function_call (identifier)))))))) + (expression_statement (function_call (identifier))))))))) ========================================== parses comments within expressions diff --git a/spec/runtime/languages/json/errors.txt b/spec/runtime/languages/json/errors.txt index 3bde5aa2..a2ae3413 100644 --- a/spec/runtime/languages/json/errors.txt +++ b/spec/runtime/languages/json/errors.txt @@ -17,24 +17,24 @@ recovers from errors inside arrays ========================================== [1, , 2] --- -(value (array +(array (number) (ERROR ) - (number))) + (number)) ========================================== recovers from errors inside objects ========================================== { "key1": 1, oops } --- -(value (object (string) (number) (ERROR 'o'))) +(object (string) (number) (ERROR 'o')) ========================================== recovers from errors inside nested objects ========================================== { "key1": { "key2": 1, 2 }, [, "key3": 3 } --- -(value (object +(object (string) (object (string) (number) (ERROR '2')) (ERROR '[') - (string) (number))) + (string) (number)) diff --git a/spec/runtime/languages/json/main.txt b/spec/runtime/languages/json/main.txt index e80a353b..cc9f4444 100644 --- a/spec/runtime/languages/json/main.txt +++ b/spec/runtime/languages/json/main.txt @@ -3,14 +3,14 @@ parses floating point numbers ============================= 3.14 --- -(value (number)) +(number) =================== parses empty arrays =================== [] --- -(value (array)) +(array) =================== parses arrays @@ -23,19 +23,19 @@ parses arrays { "stuff": "good" } ] --- -(value (array +(array (number) (null) (true) (false) - (object (string) (string)))) + (object (string) (string))) ==================== parses empty objects ==================== {} --- -(value (object)) +(object) =================== parses long objects @@ -45,8 +45,7 @@ parses long objects "key2": 1 } --- -(value (object +(object (string) (string) - (string) (number) -)) + (string) (number)) diff --git a/spec/runtime/node_position_spec.cc b/spec/runtime/node_position_spec.cc index 30c0826c..e60586cc 100644 --- a/spec/runtime/node_position_spec.cc +++ b/spec/runtime/node_position_spec.cc @@ -21,9 +21,9 @@ describe("tracking the positions of AST nodes", []() { ts_document_set_input_string(doc, " [12, 5]"); const TSTree *tree = ts_document_tree(doc); - const TSTree *array = ts_tree_children(tree, NULL)[0]; - const TSTree *number1 = ts_tree_children(array, NULL)[0]; - const TSTree *number2 = ts_tree_children(array, NULL)[1]; + const TSTree *array = tree->children[0]; + const TSTree *number1 = array->children[1]->children[0]; + const TSTree *number2 = array->children[2]->children[1]->children[0]; AssertThat(ts_document_symbol_name(doc, array), Equals("array")); AssertThat(ts_document_symbol_name(doc, number1), Equals("number")); diff --git a/spec/runtime/parser_spec.cc b/spec/runtime/parser_spec.cc index 981b3299..7dae60f2 100644 --- a/spec/runtime/parser_spec.cc +++ b/spec/runtime/parser_spec.cc @@ -40,14 +40,14 @@ describe("LR Parsers", [&]() { }); it("runs the lexer with the lex state corresponding to the initial state", [&]() { - lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym2, 5, 1); + lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym2, 5, 1, 0); ts_parser_step(parser); AssertThat(lex_fn_state_received, Equals(100)); }); describe("when the returned symbol indicates a shift action", [&]() { before_each([&]() { - lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym2, 5, 1); + lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym2, 5, 1, 0); }); it("advances to the state specified in the action", [&]() { @@ -63,7 +63,7 @@ describe("LR Parsers", [&]() { describe("when the returned symbol indicates an error", [&]() { before_each([&]() { - lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym1, 5, 1); + lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym1, 5, 1, 0); }); it("ends the parse, returning an error tree", [&]() { diff --git a/spec/runtime/stack_spec.cc b/spec/runtime/stack_spec.cc index e3efeb2b..6d2e3ee3 100644 --- a/spec/runtime/stack_spec.cc +++ b/spec/runtime/stack_spec.cc @@ -28,7 +28,7 @@ describe("stacks", [&]() { TSTree *node1; before_each([&]() { - node1 = ts_tree_make_leaf(sym1, 5, 1); + node1 = ts_tree_make_leaf(sym1, 5, 1, 0); ts_stack_push(&stack, 5, node1); }); @@ -48,10 +48,10 @@ describe("stacks", [&]() { before_each([&]() { nodes = tree_array({ - ts_tree_make_leaf(sym1, 5, 1), - ts_tree_make_leaf(sym1, 5, 1), - ts_tree_make_leaf(hidden_sym, 5, 1), - ts_tree_make_leaf(sym1, 5, 1), + ts_tree_make_leaf(sym1, 5, 1, 0), + ts_tree_make_leaf(sym1, 5, 1, 0), + ts_tree_make_leaf(hidden_sym, 5, 1, 0), + ts_tree_make_leaf(sym1, 5, 1, 0), }); for (TSStateId i = 0; i < 4; i++) @@ -75,76 +75,21 @@ describe("stacks", [&]() { AssertThat(node->symbol, Equals(sym2)); }); - it("makes all of the removed nodes immediate children of the new node", [&]() { + it("removes any hidden nodes from its regular list of children", [&]() { TSTree *expected_children[3] = { stack.entries[1].node, stack.entries[2].node, stack.entries[3].node, }; - TSTree *node = ts_stack_reduce(&stack, sym2, 3, hidden_symbols, 0); - size_t immediate_child_count; - TSTree **immediate_children = ts_tree_immediate_children(node, &immediate_child_count); - - AssertThat(immediate_child_count, Equals(3)); - for (size_t i = 0; i < 3; i++) - AssertThat(immediate_children[i], Equals(expected_children[i])); - }); - - it("removes any hidden nodes from its regular list of children", [&]() { - TSTree *expected_children[2] = { - stack.entries[1].node, - stack.entries[3].node, - }; - TSTree *node = ts_stack_reduce(&stack, sym2, 3, hidden_symbols, 0); size_t child_count; TSTree **children = ts_tree_children(node, &child_count); - AssertThat(child_count, Equals(2)); + AssertThat(child_count, Equals(3)); for (size_t i = 0; i < 2; i++) AssertThat(children[i], Equals(expected_children[i])); }); - - describe("when there are hidden nodes with children of their own", [&]() { - TSTree **grandchildren; - TSTree *hidden_node; - - before_each([&]() { - grandchildren = tree_array({ - ts_tree_make_leaf(sym1, 10, 2), - ts_tree_make_leaf(sym2, 10, 2), - }); - - hidden_node = ts_tree_make_node(hidden_sym, 2, 0, grandchildren); - ts_stack_push(&stack, 21, hidden_node); - }); - - after_each([&]() { - for (TSStateId i = 0; i < 2; i++) - ts_tree_release(grandchildren[i]); - free(grandchildren); - ts_tree_release(hidden_node); - }); - - it("makes those child nodes children of the new node", [&]() { - TSTree *node = ts_stack_reduce(&stack, sym2, 4, hidden_symbols, 0); - - TSTree *expected_children[4] = { - stack.entries[1].node, - stack.entries[3].node, - grandchildren[0], - grandchildren[1], - }; - - size_t child_count; - TSTree **children = ts_tree_children(node, &child_count); - - AssertThat(child_count, Equals(4)); - for (size_t i = 0; i < 4; i++) - AssertThat(children[i], Equals(expected_children[i])); - }); - }); }); }); diff --git a/spec/runtime/tree_spec.cc b/spec/runtime/tree_spec.cc index fa395e44..92bc1113 100644 --- a/spec/runtime/tree_spec.cc +++ b/spec/runtime/tree_spec.cc @@ -15,12 +15,11 @@ describe("trees", []() { TSTree *tree1, *tree2, *parent1; before_each([&]() { - tree1 = ts_tree_make_leaf(cat, 5, 2); - tree2 = ts_tree_make_leaf(cat, 3, 1); - parent1 = ts_tree_make_node(dog, 2, 2, tree_array({ + tree1 = ts_tree_make_leaf(cat, 5, 2, 0); + tree2 = ts_tree_make_leaf(cat, 3, 1, 0); + parent1 = ts_tree_make_node(dog, 2, tree_array({ tree1, tree2, // children - tree1, tree2, // immediate_children - })); + }), 0); }); after_each([&]() { @@ -30,7 +29,7 @@ describe("trees", []() { }); describe("making a parent node", [&]() { - it("computes its offset and size based on its child nodes", [&]() { + it("computes its size based on its child nodes", [&]() { AssertThat(parent1->size, Equals(9)); }); @@ -41,15 +40,14 @@ describe("trees", []() { describe("equality", [&]() { it("returns true for identical trees", [&]() { - TSTree *tree1_copy = ts_tree_make_leaf(cat, 5, 2); + TSTree *tree1_copy = ts_tree_make_leaf(cat, 5, 2, 0); AssertThat(ts_tree_equals(tree1, tree1_copy), Equals(1)); - TSTree *tree2_copy = ts_tree_make_leaf(cat, 3, 1); + TSTree *tree2_copy = ts_tree_make_leaf(cat, 3, 1, 0); AssertThat(ts_tree_equals(tree2, tree2_copy), Equals(1)); - TSTree *parent2 = ts_tree_make_node(dog, 2, 2, tree_array({ + TSTree *parent2 = ts_tree_make_node(dog, 2, tree_array({ tree1_copy, tree2_copy, - tree1_copy, tree2_copy, - })); + }), 0); AssertThat(ts_tree_equals(parent1, parent2), Equals(1)); ts_tree_release(tree1_copy); @@ -58,17 +56,16 @@ describe("trees", []() { }); it("returns false for trees with different symbols", [&]() { - TSTree *different_tree = ts_tree_make_leaf(pig, 0, 0); + TSTree *different_tree = ts_tree_make_leaf(pig, 0, 0, 0); AssertThat(ts_tree_equals(tree1, different_tree), Equals(0)); ts_tree_release(different_tree); }); it("returns false for trees with different children", [&]() { - TSTree *different_tree = ts_tree_make_leaf(pig, 0, 0); - TSTree *different_parent = ts_tree_make_node(dog, 2, 2, tree_array({ + TSTree *different_tree = ts_tree_make_leaf(pig, 0, 0, 0); + TSTree *different_parent = ts_tree_make_node(dog, 2, tree_array({ different_tree, different_tree, - tree2, tree2, - })); + }), 0); AssertThat(ts_tree_equals(different_parent, parent1), Equals(0)); AssertThat(ts_tree_equals(parent1, different_parent), Equals(0)); diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 0cf3e331..95291616 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -33,11 +33,11 @@ int ts_lexer_advance(TSLexer *lexer) { return 1; } -TSTree * ts_lexer_build_node(TSLexer *lexer, TSSymbol symbol) { +TSTree * ts_lexer_build_node(TSLexer *lexer, TSSymbol symbol, int is_hidden) { size_t current_position = ts_lexer_position(lexer); size_t size = current_position - lexer->token_start_position; size_t offset = lexer->token_start_position - lexer->token_end_position; lexer->token_end_position = current_position; - return ts_tree_make_leaf(symbol, size, offset); + return ts_tree_make_leaf(symbol, size, offset, is_hidden); } diff --git a/src/runtime/parser.c b/src/runtime/parser.c index cd04123f..ab411884 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -23,7 +23,7 @@ static size_t breakdown_stack(TSParser *parser, TSInputEdit *edit) { position = ts_stack_right_position(stack); size_t child_count; - TSTree **children = ts_tree_immediate_children(node, &child_count); + TSTree **children = ts_tree_children(node, &child_count); if (position <= edit->position && !children) break; stack->size--; @@ -96,7 +96,7 @@ void ts_parser_start(TSParser *parser, TSInput input, TSInputEdit *edit) { } void ts_parser_shift(TSParser *parser, TSStateId parse_state) { - if (parser->lookahead->is_extra) + if (ts_tree_is_extra(parser->lookahead)) parse_state = ts_stack_top_state(&parser->stack); ts_stack_push(&parser->stack, parse_state, parser->lookahead); parser->lookahead = parser->next_lookahead; @@ -104,7 +104,7 @@ void ts_parser_shift(TSParser *parser, TSStateId parse_state) { } void ts_parser_shift_extra(TSParser *parser) { - parser->lookahead->is_extra = 1; + ts_tree_set_extra(parser->lookahead); ts_parser_shift(parser, 0); } @@ -119,9 +119,9 @@ void ts_parser_reduce(TSParser *parser, TSSymbol symbol, size_t child_count) { int ts_parser_reduce_extra(TSParser *parser, TSSymbol symbol) { TSTree *top_node = ts_stack_top_node(&parser->stack); - if (top_node->symbol == symbol && !top_node->is_extra) { + if (top_node->symbol == symbol && !ts_tree_is_extra(top_node)) { ts_parser_reduce(parser, symbol, 1); - parser->lookahead->is_extra = 1; + ts_tree_set_extra(parser->lookahead); return 1; } else { return 0; diff --git a/src/runtime/stack.c b/src/runtime/stack.c index 904e7ef2..8fcad287 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -52,62 +52,32 @@ size_t ts_stack_right_position(const TSStack *stack) { } TSTree * ts_stack_reduce(TSStack *stack, - TSSymbol symbol, - size_t immediate_child_count, - const int *hidden_symbol_flags, - int gather_extra) { + TSSymbol symbol, + size_t child_count, + const int *hidden_symbol_flags, + int dont_count_extras) { // First, walk down the stack to determine which symbols will be reduced. - // The child node count is known ahead of time, but some of the - // nodes at the top of the stack might be hidden nodes, in which - // case we 'collapse' them. Some may also be extra tokens, - // which don't count towards the child node count. - static int collapse_flags[100]; - int child_count = 0; - for (size_t i = 0; i < immediate_child_count; i++) { - size_t stack_index = stack->size - 1 - i; - TSTree *child = stack->entries[stack_index].node; - size_t grandchild_count; - TSTree **grandchildren = ts_tree_children(child, &grandchild_count); - TSSymbol child_symbol = child->symbol; - - collapse_flags[i] = ( - hidden_symbol_flags[child_symbol] || - (grandchild_count == 1 && child->size == grandchildren[0]->size) - ); - - child_count += collapse_flags[i] ? grandchild_count : 1; - - if (gather_extra && child->is_extra) - immediate_child_count++; - } - - // Walk down the stack again, building up the array of children. - // Though we collapse the hidden child nodes, we also need to - // keep track of the actual immediate children so that we can - // later collapse the stack again when the document is edited. - // We store the children and immediate children in the same array, - // to reduce allocations. - size_t child_index = child_count; - TSTree **children = malloc((child_count + immediate_child_count) * sizeof(TSTree *)); - TSTree **immediate_children = children + child_count; - - for (size_t i = 0; i < immediate_child_count; i++) { + // The child node count is known ahead of time, but some children may be + // extra tokens, which don't count towards the child node count. + for (size_t i = 0; i < child_count; i++) { TSTree *child = stack->entries[stack->size - 1 - i].node; - immediate_children[immediate_child_count - 1 - i] = child; - - if (collapse_flags[i]) { - size_t grandchild_count; - TSTree **grandchildren = ts_tree_children(child, &grandchild_count); - child_index -= grandchild_count; - memcpy(children + child_index, grandchildren, (grandchild_count * sizeof(TSTree *))); - } else { - child_index--; - children[child_index] = child; - } + if (dont_count_extras && ts_tree_is_extra(child)) + child_count++; } - TSTree *lookahead = ts_tree_make_node(symbol, child_count, immediate_child_count, children); - ts_stack_shrink(stack, stack->size - immediate_child_count); + size_t start_index = stack->size - child_count; + TSTree **children = calloc(child_count, sizeof(TSTree *)); + for (size_t i = 0; i < child_count; i++) + children[i] = stack->entries[start_index + i].node; + + TSTree *lookahead = ts_tree_make_node( + symbol, + child_count, + children, + hidden_symbol_flags[symbol] + ); + + ts_stack_shrink(stack, stack->size - child_count); return lookahead; } diff --git a/src/runtime/tree.c b/src/runtime/tree.c index c55ef054..0f4d84b6 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -3,30 +3,29 @@ #include #include "runtime/tree.h" -static TSTree * ts_tree_make(TSSymbol symbol, size_t size, size_t offset) { +static TSTree * ts_tree_make(TSSymbol symbol, size_t size, size_t offset, int is_hidden) { TSTree *result = malloc(sizeof(TSTree)); *result = (TSTree) { .ref_count = 1, .symbol = symbol, .size = size, .offset = offset, + .options = is_hidden ? TSTreeOptionsHidden : 0, }; return result; } -TSTree * ts_tree_make_leaf(TSSymbol 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; +TSTree * ts_tree_make_leaf(TSSymbol symbol, size_t size, size_t offset, int is_hidden) { + TSTree *result = ts_tree_make(symbol, size, offset, is_hidden); + result->child_count = 0; + result->children = NULL; return result; } -TSTree * ts_tree_make_node(TSSymbol symbol, size_t child_count, size_t immediate_child_count, TSTree **children) { - TSTree **immediate_children = children + child_count; +TSTree * ts_tree_make_node(TSSymbol symbol, size_t child_count, TSTree **children, int is_hidden) { size_t size = 0, offset = 0; - for (size_t i = 0; i < immediate_child_count; i++) { - TSTree *child = immediate_children[i]; + for (size_t i = 0; i < child_count; i++) { + TSTree *child = children[i]; ts_tree_retain(child); if (i == 0) { offset = child->offset; @@ -35,18 +34,22 @@ TSTree * ts_tree_make_node(TSSymbol symbol, size_t child_count, size_t immediate size += child->offset + child->size; } } - 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; + + TSTree *result = ts_tree_make(symbol, size, offset, is_hidden); + result->child_count = child_count; + result->children = children; + + if (child_count == 1 && (ts_tree_is_visible(children[0]) || ts_tree_is_wrapper(children[0]))) + result->options |= (TSTreeOptionsWrapper | TSTreeOptionsHidden); + return result; } TSTree * ts_tree_make_error(char lookahead_char, size_t expected_input_count, const TSSymbol *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; + TSTree *result = ts_tree_make(ts_builtin_sym_error, size, offset, 0); + result->lookahead_char = lookahead_char; + result->expected_input_count = expected_input_count; + result->expected_inputs = expected_inputs; return result; } @@ -58,10 +61,10 @@ void ts_tree_release(TSTree *tree) { tree->ref_count--; if (tree->ref_count == 0) { size_t count; - TSTree **children = ts_tree_immediate_children(tree, &count); + TSTree **children = ts_tree_children(tree, &count); for (size_t i = 0; i < count; i++) ts_tree_release(children[i]); - free(tree->data.children.contents); + free(tree->children); free(tree); } } @@ -70,26 +73,14 @@ size_t ts_tree_total_size(const TSTree *tree) { return tree->offset + tree->size; } -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; - } - if (count) *count = tree->data.children.immediate_count; - return tree->data.children.contents + tree->data.children.count; -} - 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; - 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; + if (node1->child_count != node2->child_count) return 0; + for (size_t i = 0; i < node1->child_count; i++) + if (!ts_tree_equals(node1->children[i], node2->children[i])) return 0; } return 1; } @@ -99,8 +90,8 @@ TSTree ** ts_tree_children(const TSTree *tree, size_t *count) { if (count) *count = 0; return NULL; } - if (count) *count = tree->data.children.count; - return tree->data.children.contents; + if (count) *count = tree->child_count; + return tree->children; } static size_t write_lookahead_to_string(char *string, size_t limit, char lookahead) { @@ -112,35 +103,41 @@ static size_t write_lookahead_to_string(char *string, size_t limit, char lookahe } } -static size_t tree_write_to_string(const TSTree *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, int is_root) { char *cursor = string; - char **destination = (limit > 0) ? &cursor : &string; + char **writer = (limit > 0) ? &cursor : &string; + int visible = ts_tree_is_visible(tree); + + if (visible && !is_root) + cursor += snprintf(*writer, limit, " "); if (!tree) - return snprintf(*destination, limit, "(NULL)"); + return snprintf(*writer, limit, "(NULL)"); if (tree->symbol == ts_builtin_sym_error) { - cursor += snprintf(*destination, limit, "(ERROR "); - cursor += write_lookahead_to_string(*destination, limit, tree->data.error.lookahead_char); - cursor += snprintf(*destination, limit, ")"); + cursor += snprintf(*writer, limit, "(ERROR "); + cursor += write_lookahead_to_string(*writer, limit, tree->lookahead_char); + cursor += snprintf(*writer, limit, ")"); return cursor - string; } - cursor += snprintf(*destination, limit, "(%s", symbol_names[tree->symbol]); - size_t 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); + if (visible) { + cursor += snprintf(*writer, limit, "(%s", symbol_names[tree->symbol]); + is_root = 0; } - cursor += snprintf(*destination, limit, ")"); + + for (size_t i = 0; i < tree->child_count; i++) + cursor += tree_write_to_string(tree->children[i], symbol_names, *writer, limit, is_root); + + if (visible) + cursor += snprintf(*writer, limit, ")"); return cursor - string; } 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; + size_t size = tree_write_to_string(tree, symbol_names, SCRATCH_STRING, 0, 1) + 1; char *result = malloc(size * sizeof(char)); - tree_write_to_string(tree, symbol_names, result, size); + tree_write_to_string(tree, symbol_names, result, size, 1); return result; } diff --git a/src/runtime/tree.h b/src/runtime/tree.h index 78f8dcaf..41de2791 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -5,22 +5,37 @@ struct TSTree { TSSymbol symbol; + TSTreeOptions options; size_t ref_count; size_t offset; size_t size; - int is_extra; union { struct { - size_t count; - size_t immediate_count; - struct TSTree **contents; - } children; + size_t child_count; + struct TSTree **children; + }; struct { - char lookahead_char; size_t expected_input_count; const TSSymbol *expected_inputs; - } error; - } data; + char lookahead_char; + }; + }; }; +static inline int ts_tree_is_extra(const TSTree *tree) { + return (tree->options & TSTreeOptionsExtra); +} + +static inline int ts_tree_is_visible(const TSTree *tree) { + return !(tree->options & TSTreeOptionsHidden); +} + +static inline void ts_tree_set_extra(TSTree *tree) { + tree->options = (TSTreeOptions)(tree->options | TSTreeOptionsExtra); +} + +static inline int ts_tree_is_wrapper(const TSTree *tree) { + return (tree->options & TSTreeOptionsWrapper); +} + #endif // RUNTIME_TREE_H_