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
This commit is contained in:
Max Brunsfeld 2014-07-16 18:38:06 -07:00
parent 95fbdb6fdb
commit 779bf0d745
17 changed files with 167 additions and 243 deletions

View file

@ -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);
}

View file

@ -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;

View file

@ -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;
}

View file

@ -3,30 +3,29 @@
#include <stdio.h>
#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;
}

View file

@ -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_