Precompute trees' visible children

This commit is contained in:
Max Brunsfeld 2014-07-18 13:08:14 -07:00
parent b3385f20c8
commit 0e11bf7271
6 changed files with 92 additions and 71 deletions

View file

@ -23,6 +23,7 @@ struct TSTree {
struct {
size_t child_count;
struct TSTree **children;
size_t visible_child_count;
};
struct {
size_t expected_input_count;
@ -32,6 +33,11 @@ struct TSTree {
};
};
typedef struct {
TSTree *tree;
size_t position;
} TSChildWithPosition;
static inline int ts_tree_is_extra(const TSTree *tree) {
return (tree->options & TSTreeOptionsExtra);
}
@ -48,6 +54,23 @@ static inline int ts_tree_is_wrapper(const TSTree *tree) {
return (tree->options & TSTreeOptionsWrapper);
}
static inline size_t ts_tree_visible_child_count(const TSTree *tree) {
if (tree->symbol == ts_builtin_sym_error)
return 0;
else
return tree->visible_child_count;
}
static inline TSChildWithPosition * ts_tree_visible_children(const TSTree *tree, size_t *count) {
if (tree->symbol == ts_builtin_sym_error || tree->visible_child_count == 0) {
if (count) *count = 0;
return NULL;
} else {
if (count) *count = tree->visible_child_count;
return (TSChildWithPosition *)(tree + 1);
}
}
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);