Remove nested options structs in Tree

This commit is contained in:
Max Brunsfeld 2015-12-22 14:20:58 -08:00
parent 30961143fe
commit da1bc038e5
9 changed files with 93 additions and 149 deletions

View file

@ -128,7 +128,7 @@ describe("Stack", [&]() {
});
it("does not count 'extra' trees toward the count", [&]() {
ts_tree_set_extra(trees[1]);
trees[1]->extra = true;
Vector pop = ts_stack_pop(stack, 0, 2, false);
StackPopResult pop1 = *(StackPopResult *)vector_get(&pop, 0);

View file

@ -47,8 +47,8 @@ describe("Tree", []() {
describe("make_leaf(sym, size, padding, is_hidden)", [&]() {
it("does not record that it is fragile", [&]() {
AssertThat(ts_tree_is_fragile_left(tree1), IsFalse());
AssertThat(ts_tree_is_fragile_right(tree1), IsFalse());
AssertThat(tree1->fragile_left, IsFalse());
AssertThat(tree1->fragile_right, IsFalse());
});
});
@ -59,8 +59,8 @@ describe("Tree", []() {
ts_length_zero(),
'z');
AssertThat(ts_tree_is_fragile_left(error_tree), IsTrue());
AssertThat(ts_tree_is_fragile_right(error_tree), IsTrue());
AssertThat(error_tree->fragile_left, IsTrue());
AssertThat(error_tree->fragile_right, IsTrue());
});
});
@ -81,8 +81,8 @@ describe("Tree", []() {
TSTree *parent;
before_each([&]() {
ts_tree_set_fragile_left(tree1);
ts_tree_set_extra(tree1);
tree1->fragile_left = true;
tree1->extra = true;
parent = ts_tree_make_node(eel, 2, tree_array({
tree1,
tree2,
@ -94,7 +94,7 @@ describe("Tree", []() {
});
it("records that it is fragile on the left side", [&]() {
AssertThat(ts_tree_is_fragile_left(parent), IsTrue());
AssertThat(parent->fragile_left, IsTrue());
});
});
@ -102,8 +102,8 @@ describe("Tree", []() {
TSTree *parent;
before_each([&]() {
ts_tree_set_fragile_right(tree2);
ts_tree_set_extra(tree2);
tree2->fragile_right = true;
tree2->extra = true;
parent = ts_tree_make_node(eel, 2, tree_array({
tree1,
tree2,
@ -115,7 +115,7 @@ describe("Tree", []() {
});
it("records that it is fragile on the right side", [&]() {
AssertThat(ts_tree_is_fragile_right(parent), IsTrue());
AssertThat(parent->fragile_right, IsTrue());
});
});
@ -123,8 +123,8 @@ describe("Tree", []() {
TSTree *parent;
before_each([&]() {
ts_tree_set_fragile_right(tree1);
ts_tree_set_fragile_left(tree2);
tree1->fragile_right = true;
tree2->fragile_left = true;
parent = ts_tree_make_node(eel, 2, tree_array({
tree1,
tree2,
@ -136,8 +136,8 @@ describe("Tree", []() {
});
it("records that it is not fragile", [&]() {
AssertThat(ts_tree_is_fragile_left(parent), IsFalse());
AssertThat(ts_tree_is_fragile_right(parent), IsFalse());
AssertThat(parent->fragile_left, IsFalse());
AssertThat(parent->fragile_right, IsFalse());
});
});
});
@ -175,15 +175,15 @@ describe("Tree", []() {
assert_consistent(tree);
AssertThat(tree->options.has_changes, IsTrue());
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<TSLength>({0, 3, 0, 0}));
AssertThat(tree->size, Equals<TSLength>({13, 13, 0, 13}));
AssertThat(tree->children[0]->options.has_changes, IsTrue());
AssertThat(tree->children[0]->has_changes, IsTrue());
AssertThat(tree->children[0]->padding, Equals<TSLength>({0, 3, 0, 0}));
AssertThat(tree->children[0]->size, Equals<TSLength>({3, 3, 0, 3}));
AssertThat(tree->children[1]->options.has_changes, IsFalse());
AssertThat(tree->children[1]->has_changes, IsFalse());
AssertThat(tree->children[1]->padding, Equals<TSLength>({2, 2, 0, 2}));
AssertThat(tree->children[1]->size, Equals<TSLength>({3, 3, 0, 3}));
});
@ -195,11 +195,11 @@ describe("Tree", []() {
assert_consistent(tree);
AssertThat(tree->options.has_changes, IsTrue());
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<TSLength>({0, 5, 0, 0}));
AssertThat(tree->size, Equals<TSLength>({0, 11, 0, 0}));
AssertThat(tree->children[0]->options.has_changes, IsTrue());
AssertThat(tree->children[0]->has_changes, IsTrue());
AssertThat(tree->children[0]->padding, Equals<TSLength>({0, 5, 0, 0}));
AssertThat(tree->children[0]->size, Equals<TSLength>({0, 1, 0, 0}));
});
@ -211,15 +211,15 @@ describe("Tree", []() {
assert_consistent(tree);
AssertThat(tree->options.has_changes, IsTrue());
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<TSLength>({0, 4, 0, 0}));
AssertThat(tree->size, Equals<TSLength>({13, 13, 0, 13}));
AssertThat(tree->children[0]->options.has_changes, IsTrue());
AssertThat(tree->children[0]->has_changes, IsTrue());
AssertThat(tree->children[0]->padding, Equals<TSLength>({0, 4, 0, 0}));
AssertThat(tree->children[0]->size, Equals<TSLength>({3, 3, 0, 3}));
AssertThat(tree->children[1]->options.has_changes, IsFalse());
AssertThat(tree->children[1]->has_changes, IsFalse());
});
});
@ -229,15 +229,15 @@ describe("Tree", []() {
assert_consistent(tree);
AssertThat(tree->options.has_changes, IsTrue());
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<TSLength>({2, 2, 0, 2}));
AssertThat(tree->size, Equals<TSLength>({0, 16, 0, 0}));
AssertThat(tree->children[0]->options.has_changes, IsTrue());
AssertThat(tree->children[0]->has_changes, IsTrue());
AssertThat(tree->children[0]->padding, Equals<TSLength>({2, 2, 0, 2}));
AssertThat(tree->children[0]->size, Equals<TSLength>({0, 6, 0, 0}));
AssertThat(tree->children[1]->options.has_changes, IsFalse());
AssertThat(tree->children[1]->has_changes, IsFalse());
});
});
@ -247,19 +247,19 @@ describe("Tree", []() {
assert_consistent(tree);
AssertThat(tree->options.has_changes, IsTrue());
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<TSLength>({0, 4, 0, 0}));
AssertThat(tree->size, Equals<TSLength>({0, 4, 0, 0}));
AssertThat(tree->children[0]->options.has_changes, IsTrue());
AssertThat(tree->children[0]->has_changes, IsTrue());
AssertThat(tree->children[0]->padding, Equals<TSLength>({0, 4, 0, 0}));
AssertThat(tree->children[0]->size, Equals<TSLength>({0, 0, 0, 0}));
AssertThat(tree->children[1]->options.has_changes, IsTrue());
AssertThat(tree->children[1]->has_changes, IsTrue());
AssertThat(tree->children[1]->padding, Equals<TSLength>({0, 0, 0, 0}));
AssertThat(tree->children[1]->size, Equals<TSLength>({0, 0, 0, 0}));
AssertThat(tree->children[2]->options.has_changes, IsTrue());
AssertThat(tree->children[2]->has_changes, IsTrue());
AssertThat(tree->children[2]->padding, Equals<TSLength>({0, 1, 0, 0}));
AssertThat(tree->children[2]->size, Equals<TSLength>({3, 3, 0, 3}));
});
@ -344,7 +344,7 @@ describe("Tree", []() {
});
it("hides invisible nodes", [&]() {
tree2->options.visible = false;
tree2->visible = false;
char *string1 = ts_tree_string(parent1, names, true);
AssertThat(string(string1), Equals("(dog (cat))"));
@ -353,13 +353,13 @@ describe("Tree", []() {
describe("when the root node is not visible", [&]() {
it("still serializes it", [&]() {
parent1->options.visible = false;
parent1->visible = false;
char *string1 = ts_tree_string(parent1, names, true);
AssertThat(string(string1), Equals("(dog (cat) (cat))"));
free(string1);
tree1->options.visible = false;
tree1->visible = false;
char *string2 = ts_tree_string(tree1, names, true);
AssertThat(string(string2), Equals("(cat)"));

View file

@ -67,7 +67,7 @@ void ts_document_parse(TSDocument *self) {
return;
TSTree *reusable_tree = self->valid ? self->tree : NULL;
if (reusable_tree && !reusable_tree->options.has_changes)
if (reusable_tree && !reusable_tree->has_changes)
return;
TSTree *tree = ts_parser_parse(&self->parser, self->input, reusable_tree);
@ -85,7 +85,7 @@ void ts_document_invalidate(TSDocument *self) {
TSNode ts_document_root_node(const TSDocument *self) {
TSNode result = ts_node_make(self->tree, 0, 0, 0);
while (result.data && !ts_tree_is_visible(result.data))
while (result.data && !((TSTree *)result.data)->visible)
result = ts_node_named_child(result, 0);
return result;
}

View file

@ -103,7 +103,7 @@ static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol,
}
if (fragile)
result->context.lex_state = self->starting_state;
result->lex_state = self->starting_state;
return result;
}

View file

@ -33,7 +33,7 @@ static inline size_t ts_node__offset_row(TSNode self) {
static inline bool ts_node__is_relevant(TSNode self, bool include_anonymous) {
const TSTree *tree = ts_node__tree(self);
return include_anonymous ? tree->options.visible : tree->options.named;
return include_anonymous ? tree->visible : tree->named;
}
static inline size_t ts_node__relevant_child_count(TSNode self,
@ -110,7 +110,7 @@ static inline TSNode ts_node__prev_sibling(TSNode self, bool include_anonymous)
if (grandchild_count > 0)
return ts_node__child(child, grandchild_count - 1, include_anonymous);
}
} while (!ts_tree_is_visible(ts_node__tree(result)));
} while (!ts_node__tree(result)->visible);
return ts_node__null();
}
@ -133,7 +133,7 @@ static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous)
if (grandchild_count > 0)
return ts_node__child(child, 0, include_anonymous);
}
} while (!ts_tree_is_visible(ts_node__tree(result)));
} while (!ts_node__tree(result)->visible);
return ts_node__null();
}
@ -218,11 +218,11 @@ bool ts_node_eq(TSNode self, TSNode other) {
}
bool ts_node_is_named(TSNode self) {
return ts_node__tree(self)->options.named;
return ts_node__tree(self)->named;
}
bool ts_node_has_changes(TSNode self) {
return ts_node__tree(self)->options.has_changes;
return ts_node__tree(self)->has_changes;
}
TSNode ts_node_parent(TSNode self) {
@ -233,7 +233,7 @@ TSNode ts_node_parent(TSNode self) {
result = ts_node__direct_parent(result, &index);
if (!result.data)
return ts_node__null();
} while (!ts_tree_is_visible(result.data));
} while (!ts_node__tree(result)->visible);
return result;
}

View file

@ -60,7 +60,7 @@ static void ts_parser__breakdown_top_of_stack(TSParser *self, int head) {
TSStateId state = ts_stack_top_state(self->stack, head_index);
for (size_t j = 0; j < parent->child_count; j++) {
last_child = parent->children[j];
if (!last_child->options.extra) {
if (!last_child->extra) {
TSParseAction action =
ts_language_last_action(self->language, state, last_child->symbol);
assert(action.type == TSParseActionTypeShift);
@ -130,15 +130,15 @@ static bool ts_parser__can_reuse(TSParser *self, int head, TSTree *subtree) {
if (subtree->symbol == ts_builtin_sym_error)
return false;
if (ts_tree_is_fragile(subtree)) {
if (subtree->context.parse_state != ts_stack_top_state(self->stack, head))
if (subtree->parse_state != ts_stack_top_state(self->stack, head))
return false;
}
TSStateId state = ts_stack_top_state(self->stack, head);
if (subtree->context.lex_state != TSTREE_STATE_INDEPENDENT) {
if (subtree->lex_state != TSTREE_STATE_INDEPENDENT) {
TSStateId lex_state = self->language->lex_states[state];
if (subtree->context.lex_state != lex_state)
if (subtree->lex_state != lex_state)
return false;
}
@ -147,7 +147,7 @@ static bool ts_parser__can_reuse(TSParser *self, int head, TSTree *subtree) {
if (action->type == TSParseActionTypeError || action->can_hide_split)
return false;
if (ts_tree_is_extra(subtree) && !action->extra)
if (subtree->extra && !action->extra)
return false;
return true;
@ -173,7 +173,7 @@ static TSTree *ts_parser__get_next_lookahead(TSParser *self, int head) {
continue;
}
if (ts_tree_has_changes(state->reusable_subtree)) {
if (state->reusable_subtree->has_changes) {
if (state->is_verifying && state->reusable_subtree->child_count == 0) {
ts_parser__breakdown_top_of_stack(self, head);
state->is_verifying = false;
@ -193,7 +193,7 @@ static TSTree *ts_parser__get_next_lookahead(TSParser *self, int head) {
TSTree *result = state->reusable_subtree;
TSLength size = ts_tree_total_size(result);
LOG("reuse sym:%s size:%lu extra:%d", SYM_NAME(result->symbol), size.chars,
result->options.extra);
result->extra);
ts_parser__pop_reusable_subtree(state);
return result;
}
@ -249,7 +249,7 @@ static bool ts_parser__shift_extra(TSParser *self, int head, TSStateId state,
TSSymbolMetadata metadata = self->language->symbol_metadata[lookahead->symbol];
if (metadata.structural && ts_stack_head_count(self->stack) > 1)
lookahead = ts_tree_make_copy(lookahead);
ts_tree_set_extra(lookahead);
lookahead->extra = true;
return ts_parser__shift(self, head, state, lookahead);
}
@ -289,7 +289,7 @@ static bool ts_parser__reduce(TSParser *self, int head, TSSymbol symbol,
*/
if (!parent) {
for (size_t j = pop_result->tree_count - 1; j + 1 > 0; j--) {
if (pop_result->trees[j]->options.extra) {
if (pop_result->trees[j]->extra) {
trailing_extra_count++;
} else
break;
@ -338,11 +338,11 @@ static bool ts_parser__reduce(TSParser *self, int head, TSSymbol symbol,
TSStateId state;
TSStateId top_state = ts_stack_top_state(self->stack, new_head);
if (parent->context.parse_state != TSTREE_STATE_ERROR)
parent->context.parse_state = top_state;
if (parent->parse_state != TSTREE_STATE_ERROR)
parent->parse_state = top_state;
if (extra) {
ts_tree_set_extra(parent);
parent->extra = true;
state = top_state;
} else {
TSParseAction action =
@ -382,17 +382,16 @@ static bool ts_parser__reduce(TSParser *self, int head, TSSymbol symbol,
if (self->is_split || ts_stack_head_count(self->stack) > 1) {
for (size_t i = 0, size = self->reduce_parents.size; i < size; i++) {
TSTree **parent = vector_get(&self->reduce_parents, i);
(*parent)->options.fragile_left = true;
(*parent)->options.fragile_right = true;
(*parent)->context.parse_state = TSTREE_STATE_ERROR;
(*parent)->fragile_left = true;
(*parent)->fragile_right = true;
(*parent)->parse_state = TSTREE_STATE_ERROR;
}
}
if (fragile) {
for (size_t i = 0; i < self->reduce_parents.size; i++) {
TSTree **parent = vector_get(&self->reduce_parents, i);
ts_tree_set_fragile_left(*parent);
ts_tree_set_fragile_right(*parent);
(*parent)->fragile_left = (*parent)->fragile_right = true;
}
}
@ -409,9 +408,8 @@ static void ts_parser__reduce_error(TSParser *self, int head,
stack_entry->position =
ts_length_add(stack_entry->position, lookahead->padding);
(*parent)->size = ts_length_add((*parent)->size, lookahead->padding);
(*parent)->fragile_left = (*parent)->fragile_right = true;
lookahead->padding = ts_length_zero();
ts_tree_set_fragile_left(*parent);
ts_tree_set_fragile_right(*parent);
}
}
@ -498,7 +496,7 @@ static TSTree *ts_parser__finish(TSParser *self, int finished_stack_head) {
StackPopResult *pop_result = vector_get(&pop_results, 0);
for (size_t i = 0; i < pop_result->tree_count; i++) {
if (!pop_result->trees[i]->options.extra) {
if (!pop_result->trees[i]->extra) {
TSTree *root = pop_result->trees[i];
size_t leading_extra_count = i;
size_t trailing_extra_count = pop_result->tree_count - 1 - i;

View file

@ -283,7 +283,7 @@ Vector ts_stack_pop(Stack *self, int head_index, int child_count,
/*
* Children that are 'extra' do not count towards the total child count.
*/
if (ts_tree_is_extra(node->entry.tree) && !count_extra)
if (node->entry.tree->extra && !count_extra)
path->goal_tree_count++;
/*

View file

@ -18,15 +18,15 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size,
.named_child_count = 0,
.children = NULL,
.padding = padding,
.options.visible = metadata.visible,
.options.named = metadata.named,
.context.lex_state = TSTREE_STATE_INDEPENDENT,
.context.parse_state = TSTREE_STATE_INDEPENDENT,
.visible = metadata.visible,
.named = metadata.named,
.lex_state = TSTREE_STATE_INDEPENDENT,
.parse_state = TSTREE_STATE_INDEPENDENT,
};
if (sym == ts_builtin_sym_error) {
result->options.fragile_left = true;
result->options.fragile_right = true;
result->fragile_left = true;
result->fragile_right = true;
}
return result;
@ -77,9 +77,9 @@ void ts_tree_set_children(TSTree *self, size_t child_count, TSTree **children) {
self->size = ts_length_add(self->size, ts_tree_total_size(child));
}
if (child->options.visible) {
if (child->visible) {
self->visible_child_count++;
if (child->options.named)
if (child->named)
self->named_child_count++;
} else {
self->visible_child_count += child->visible_child_count;
@ -87,17 +87,17 @@ void ts_tree_set_children(TSTree *self, size_t child_count, TSTree **children) {
}
if (child->symbol == ts_builtin_sym_error) {
self->options.fragile_left = self->options.fragile_right = true;
self->context.parse_state = TSTREE_STATE_ERROR;
self->fragile_left = self->fragile_right = true;
self->parse_state = TSTREE_STATE_ERROR;
}
}
if (child_count > 0) {
self->context.lex_state = children[0]->context.lex_state;
if (children[0]->options.fragile_left)
self->options.fragile_left = true;
if (children[child_count - 1]->options.fragile_right)
self->options.fragile_right = true;
self->lex_state = children[0]->lex_state;
if (children[0]->fragile_left)
self->fragile_left = true;
if (children[child_count - 1]->fragile_right)
self->fragile_right = true;
}
}
@ -155,9 +155,9 @@ bool ts_tree_eq(const TSTree *self, const TSTree *other) {
if (self->symbol != other->symbol)
return false;
if (self->options.visible != other->options.visible)
if (self->visible != other->visible)
return false;
if (self->options.named != other->options.named)
if (self->named != other->named)
return false;
if (self->symbol == ts_builtin_sym_error)
return self->lookahead_char == other->lookahead_char;
@ -216,8 +216,8 @@ static size_t ts_tree__write_to_string(const TSTree *self,
char *cursor = string;
char **writer = (limit > 0) ? &cursor : &string;
bool visible = is_root || (self->options.visible &&
(include_anonymous || self->options.named));
bool visible = is_root || (self->visible &&
(include_anonymous || self->named));
if (visible && !is_root)
cursor += snprintf(*writer, limit, " ");
@ -264,7 +264,7 @@ void ts_tree_edit(TSTree *self, TSInputEdit edit) {
size_t old_end = edit.position + edit.chars_removed;
assert(old_end <= ts_tree_total_chars(self));
self->options.has_changes = true;
self->has_changes = true;
if (start < self->padding.chars) {
ts_length_set_unknown(&self->padding);

View file

@ -17,9 +17,8 @@ struct TSTree {
struct TSTree *parent;
size_t index;
TSLength offset;
TSStateId lex_state;
TSStateId parse_state;
} context;
size_t child_count;
size_t visible_child_count;
size_t named_child_count;
@ -27,20 +26,20 @@ struct TSTree {
struct TSTree **children;
char lookahead_char;
};
TSLength padding;
TSLength size;
TSSymbol symbol;
struct {
bool visible : 1;
bool named : 1;
bool extra : 1;
bool fragile_left : 1;
bool fragile_right : 1;
bool has_changes : 1;
} options;
unsigned short int ref_count;
TSStateId lex_state;
TSStateId parse_state;
unsigned short ref_count;
bool visible : 1;
bool named : 1;
bool extra : 1;
bool fragile_left : 1;
bool fragile_right : 1;
bool has_changes : 1;
};
TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, TSSymbolMetadata);
@ -68,61 +67,8 @@ static inline TSLength ts_tree_total_size(const TSTree *self) {
return ts_length_add(self->padding, self->size);
}
static inline TSPoint ts_tree_extent(const TSTree *tree) {
TSPoint result;
result.row = tree->size.rows;
result.column = tree->size.columns;
return result;
}
static inline bool ts_tree_is_extra(const TSTree *tree) {
return tree->options.extra;
}
static inline bool ts_tree_is_visible(const TSTree *tree) {
return tree->options.visible;
}
static inline void ts_tree_set_extra(TSTree *tree) {
tree->options.extra = true;
}
static inline void ts_tree_set_fragile_left(TSTree *tree) {
tree->options.fragile_left = true;
}
static inline void ts_tree_set_fragile_right(TSTree *tree) {
tree->options.fragile_right = true;
}
static inline void ts_tree_set_fragile(TSTree *tree) {
ts_tree_set_fragile_left(tree);
ts_tree_set_fragile_right(tree);
}
static inline bool ts_tree_is_fragile_left(const TSTree *tree) {
return tree->options.fragile_left;
}
static inline bool ts_tree_is_fragile_right(const TSTree *tree) {
return tree->options.fragile_right;
}
static inline bool ts_tree_is_terminal(const TSTree *tree) {
return tree->child_count == 0;
}
static inline bool ts_tree_has_changes(const TSTree *tree) {
return tree->options.has_changes;
}
static inline bool ts_tree_is_empty(const TSTree *tree) {
return ts_tree_total_size(tree).chars == 0;
}
static inline bool ts_tree_is_fragile(const TSTree *tree) {
return ts_tree_is_empty(tree) || tree->options.fragile_left ||
tree->options.fragile_right;
return tree->fragile_left || tree->fragile_right || ts_tree_total_chars(tree) == 0;
}
#ifdef __cplusplus