From 95188d84b6ea2454013165cefa089c1fbe56b612 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 24 Mar 2014 00:34:13 -0700 Subject: [PATCH] Make tree struct private --- include/tree_sitter/parser.h | 12 ++++----- include/tree_sitter/runtime.h | 26 +++++-------------- spec/runtime/node_position_spec.cc | 19 +++++++------- src/runtime/document.c | 2 +- src/runtime/stack.c | 10 ++++---- src/runtime/tree.c | 41 ++++++++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 42 deletions(-) diff --git a/include/tree_sitter/parser.h b/include/tree_sitter/parser.h index 1624bdd0..d25becb4 100644 --- a/include/tree_sitter/parser.h +++ b/include/tree_sitter/parser.h @@ -287,15 +287,15 @@ static size_t ts_lr_parser_breakdown_stack(ts_lr_parser *parser, ts_input_edit * if (position <= edit->position && !children) break; stack->size--; - position -= (node->offset + node->size); + position -= ts_tree_total_size(node); for (size_t i = 0; i < child_count && position < edit->position; i++) { ts_tree *child = children[i]; state_id state = ts_stack_top_state(stack); - state_id next_state = ts_parse_actions[state][child->symbol].data.to_state; + state_id next_state = ts_parse_actions[state][ts_tree_symbol(child)].data.to_state; ts_stack_push(stack, next_state, child); ts_tree_retain(child); - position += child->offset + child->size; + position += ts_tree_total_size(child); } ts_tree_release(node); @@ -356,7 +356,7 @@ static int ts_lr_parser_handle_error(ts_lr_parser *parser) { if (ts_lexer_position(&parser->lexer) == position) ts_lexer_advance(&parser->lexer); - if (parser->lookahead->symbol == ts_builtin_sym_end) { + if (ts_tree_symbol(parser->lookahead) == ts_builtin_sym_end) { parser->stack.entries[0].node = error; return 0; } @@ -370,7 +370,7 @@ static int ts_lr_parser_handle_error(ts_lr_parser *parser) { ts_parse_action action_on_error = ts_parse_actions[stack_state][ts_builtin_sym_error]; if (action_on_error.type == ts_parse_action_type_shift) { state_id state_after_error = action_on_error.data.to_state; - if (ts_parse_actions[state_after_error][parser->lookahead->symbol].type != ts_parse_action_type_error) { + if (ts_parse_actions[state_after_error][ts_tree_symbol(parser->lookahead)].type != ts_parse_action_type_error) { ts_stack_shrink(&parser->stack, i + 1); ts_stack_push(&parser->stack, state_after_error, error); return 1; @@ -388,7 +388,7 @@ static const ts_tree * ts_parse(void *data, ts_input input, ts_input_edit *edit) state_id state = ts_stack_top_state(&parser->stack); if (!parser->lookahead) parser->lookahead = ts_lex(&parser->lexer, ts_lex_states[state]); - ts_parse_action action = ts_parse_actions[state][parser->lookahead->symbol]; + ts_parse_action action = ts_parse_actions[state][ts_tree_symbol(parser->lookahead)]; switch (action.type) { case ts_parse_action_type_shift: ts_lr_parser_shift(parser, action.data.to_state); diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 179395b4..97568ad4 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -11,25 +11,7 @@ typedef int ts_symbol; static const ts_symbol ts_builtin_sym_error = -1; static const ts_symbol ts_builtin_sym_end = -2; -typedef struct ts_tree { - ts_symbol symbol; - size_t ref_count; - size_t offset; - size_t size; - short int is_hidden; - union { - struct { - size_t count; - struct ts_tree **contents; - } children; - struct { - char lookahead_char; - size_t expected_input_count; - const ts_symbol *expected_inputs; - } error; - } data; -} ts_tree; - +typedef struct ts_tree ts_tree; ts_tree * ts_tree_make_leaf(ts_symbol symbol, size_t size, size_t offset); ts_tree * ts_tree_make_node(ts_symbol symbol, size_t child_count, ts_tree **children, size_t size, size_t offset); 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); @@ -39,6 +21,11 @@ int ts_tree_equals(const ts_tree *tree1, const ts_tree *tree2); char * ts_tree_string(const ts_tree *tree, const char **names); char * ts_tree_error_string(const ts_tree *tree, const char **names); ts_tree ** ts_tree_children(const ts_tree *tree, size_t *count); +size_t ts_tree_size(const ts_tree *tree); +size_t ts_tree_offset(const ts_tree *tree); +size_t ts_tree_total_size(const ts_tree *tree); +ts_symbol ts_tree_symbol(const ts_tree *tree); +void ts_tree_hide(ts_tree *tree, int hide); typedef struct { void *data; @@ -64,7 +51,6 @@ const ts_tree * ts_parser_parse(ts_parser *, ts_input, ts_input_edit *edit); void ts_parser_free(ts_parser *); typedef struct ts_document ts_document; - ts_document * ts_document_make(); void ts_document_free(ts_document *doc); void ts_document_set_parser(ts_document *doc, ts_parser parser); diff --git a/spec/runtime/node_position_spec.cc b/spec/runtime/node_position_spec.cc index 15d454f1..6155121b 100644 --- a/spec/runtime/node_position_spec.cc +++ b/spec/runtime/node_position_spec.cc @@ -20,7 +20,6 @@ describe("tracking the positions of AST nodes", []() { ts_document_set_input_string(doc, " [12, 5]"); const ts_tree *tree = ts_document_tree(doc); - // TODO - make this better const ts_tree *array = ts_tree_children(tree, NULL)[0]; const ts_tree *number1 = ts_tree_children(ts_tree_children(array, NULL)[1], NULL)[0]; const ts_tree *number2 = ts_tree_children(ts_tree_children(ts_tree_children(array, NULL)[2], NULL)[1], NULL)[0]; @@ -29,18 +28,18 @@ describe("tracking the positions of AST nodes", []() { AssertThat(ts_document_symbol_name(doc, number1), Equals("number")); AssertThat(ts_document_symbol_name(doc, number2), Equals("number")); - AssertThat(number1->offset, Equals(0)); - AssertThat(number1->size, Equals(2)); + AssertThat(ts_tree_offset(number1), Equals(0)); + AssertThat(ts_tree_size(number1), Equals(2)); - AssertThat(number2->offset, Equals(1)); - AssertThat(number2->size, Equals(1)); + AssertThat(ts_tree_offset(number2), Equals(1)); + AssertThat(ts_tree_size(number2), Equals(1)); - AssertThat(array->offset, Equals(2)); - AssertThat(array->size, Equals(7)); + AssertThat(ts_tree_offset(array), Equals(2)); + AssertThat(ts_tree_size(array), Equals(7)); - AssertThat(tree->offset, Equals(2)); - AssertThat(tree->size, Equals(7)); + AssertThat(ts_tree_offset(tree), Equals(2)); + AssertThat(ts_tree_size(tree), Equals(7)); }); }); -END_TEST \ No newline at end of file +END_TEST diff --git a/src/runtime/document.c b/src/runtime/document.c index 0eba4e06..fcfe0d09 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -38,7 +38,7 @@ void ts_document_edit(ts_document *document, ts_input_edit edit) { } const char * ts_document_symbol_name(const ts_document *document, const ts_tree *tree) { - return document->parser.symbol_names[tree->symbol]; + return document->parser.symbol_names[ts_tree_symbol(tree)]; } typedef struct { diff --git a/src/runtime/stack.c b/src/runtime/stack.c index 0b3eb35d..fe509cdb 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -53,7 +53,7 @@ size_t ts_stack_right_position(const ts_stack *stack) { size_t result = 0; for (size_t i = 0; i < stack->size; i++) { ts_tree *node = stack->entries[i].node; - result += node->offset + node->size; + result += ts_tree_total_size(node); } return result; } @@ -65,12 +65,12 @@ ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, int child_count, co ts_tree **children = malloc(child_count * sizeof(ts_tree *)); for (int i = 0; i < child_count; i++) { ts_tree *child = stack->entries[new_stack_size + i].node; - child->is_hidden = collapse_flags[i]; + ts_tree_hide(child, collapse_flags[i]); if (i == 0) { - offset = child->offset; - size = child->size; + offset = ts_tree_offset(child); + size = ts_tree_size(child); } else { - size += child->offset + child->size; + size += ts_tree_total_size(child); } children[i] = child; diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 87629b1c..8d28c3ad 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -2,10 +2,31 @@ #include #include +struct ts_tree { + ts_symbol symbol; + size_t ref_count; + size_t offset; + size_t size; + int is_hidden; + union { + struct { + size_t count; + size_t immediate_count; + struct ts_tree **contents; + } children; + struct { + char lookahead_char; + size_t expected_input_count; + const ts_symbol *expected_inputs; + } error; + } data; +}; + 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) { .ref_count = 1, + .is_hidden = 0, .symbol = symbol, .size = size, .offset = offset, @@ -37,10 +58,30 @@ ts_tree * ts_tree_make_error(char lookahead_char, size_t expected_input_count, c return result; } +ts_symbol ts_tree_symbol(const ts_tree *tree) { + return tree->symbol; +} + void ts_tree_retain(ts_tree *tree) { tree->ref_count++; } +size_t ts_tree_offset(const ts_tree *tree) { + return tree->offset; +} + +size_t ts_tree_size(const ts_tree *tree) { + return tree->size; +} + +size_t ts_tree_total_size(const ts_tree *tree) { + return ts_tree_offset(tree) + ts_tree_size(tree); +} + +void ts_tree_hide(ts_tree *tree, int hide) { + tree->is_hidden = hide; +} + void ts_tree_release(ts_tree *tree) { tree->ref_count--; if (tree->ref_count == 0) {