Use a more exact test for reusability of error nodes
Based on the concept of node fragility from wagner's incremental parsing paper
This commit is contained in:
parent
75da0a5774
commit
a92067702d
4 changed files with 75 additions and 26 deletions
|
|
@ -48,19 +48,21 @@ describe("Tree", []() {
|
|||
});
|
||||
|
||||
describe("make_leaf(sym, size, padding, is_hidden)", [&]() {
|
||||
it("does not record that it contains an error", [&]() {
|
||||
AssertThat(ts_tree_has_error(tree1), IsFalse());
|
||||
it("does not record that it is fragile", [&]() {
|
||||
AssertThat(ts_tree_is_fragile_left(tree1), IsFalse());
|
||||
AssertThat(ts_tree_is_fragile_right(tree1), IsFalse());
|
||||
});
|
||||
});
|
||||
|
||||
describe("make_error(size, padding, lookahead_char)", [&]() {
|
||||
it("records that it contains an error", [&]() {
|
||||
it("records that it is fragile", [&]() {
|
||||
TSTree *error_tree = ts_tree_make_error(
|
||||
ts_length_zero(),
|
||||
ts_length_zero(),
|
||||
'z');
|
||||
|
||||
AssertThat(ts_tree_has_error(error_tree), IsTrue());
|
||||
AssertThat(ts_tree_is_fragile_left(error_tree), IsTrue());
|
||||
AssertThat(ts_tree_is_fragile_right(error_tree), IsTrue());
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -138,11 +140,12 @@ describe("Tree", []() {
|
|||
});
|
||||
});
|
||||
|
||||
describe("when one of the child nodes has an error", [&]() {
|
||||
describe("when the first node is fragile on the left side", [&]() {
|
||||
TSTree *parent;
|
||||
|
||||
before_each([&]() {
|
||||
tree2->options = (TSTreeOptions)(TSTreeOptionsHasError|TSTreeOptionsExtra);
|
||||
ts_tree_set_fragile_left(tree1);
|
||||
ts_tree_set_extra(tree1);
|
||||
parent = ts_tree_make_node(pig, 2, tree_array({
|
||||
tree1,
|
||||
tree2,
|
||||
|
|
@ -153,14 +156,51 @@ describe("Tree", []() {
|
|||
ts_tree_release(parent);
|
||||
});
|
||||
|
||||
it("records that it contains an error", [&]() {
|
||||
AssertThat(ts_tree_has_error(parent), IsTrue());
|
||||
it("records that it is fragile on the left side", [&]() {
|
||||
AssertThat(ts_tree_is_fragile_left(parent), IsTrue());
|
||||
});
|
||||
});
|
||||
|
||||
describe("when none of the child nodes have an error", [&]() {
|
||||
it("records that it does not contain an error", [&]() {
|
||||
AssertThat(ts_tree_has_error(parent1), IsFalse());
|
||||
describe("when the last node is fragile on the right side", [&]() {
|
||||
TSTree *parent;
|
||||
|
||||
before_each([&]() {
|
||||
ts_tree_set_fragile_right(tree2);
|
||||
ts_tree_set_extra(tree2);
|
||||
parent = ts_tree_make_node(pig, 2, tree_array({
|
||||
tree1,
|
||||
tree2,
|
||||
}), 0);
|
||||
});
|
||||
|
||||
after_each([&]() {
|
||||
ts_tree_release(parent);
|
||||
});
|
||||
|
||||
it("records that it is fragile on the right side", [&]() {
|
||||
AssertThat(ts_tree_is_fragile_right(parent), IsTrue());
|
||||
});
|
||||
});
|
||||
|
||||
describe("when the outer nodes aren't fragile on their outer side", [&]() {
|
||||
TSTree *parent;
|
||||
|
||||
before_each([&]() {
|
||||
ts_tree_set_fragile_right(tree1);
|
||||
ts_tree_set_fragile_left(tree2);
|
||||
parent = ts_tree_make_node(pig, 2, tree_array({
|
||||
tree1,
|
||||
tree2,
|
||||
}), 0);
|
||||
});
|
||||
|
||||
after_each([&]() {
|
||||
ts_tree_release(parent);
|
||||
});
|
||||
|
||||
it("records that it is not fragile", [&]() {
|
||||
AssertThat(ts_tree_is_fragile_left(parent), IsFalse());
|
||||
AssertThat(ts_tree_is_fragile_right(parent), IsFalse());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -106,7 +106,8 @@ static TSTree *break_down_right_stack(TSParser *parser) {
|
|||
|
||||
TSParseAction action = get_action(parser->language, state, node->symbol);
|
||||
bool is_usable = (action.type != TSParseActionTypeError) &&
|
||||
!ts_tree_has_error(node) && !ts_tree_is_extra(node);
|
||||
!ts_tree_is_extra(node) &&
|
||||
!ts_tree_is_fragile_left(node) && !ts_tree_is_fragile_right(node);
|
||||
if (is_usable && right_subtree_start == current_position.chars) {
|
||||
ts_stack_shrink(&parser->right_stack, parser->right_stack.size - 1);
|
||||
return node;
|
||||
|
|
|
|||
|
|
@ -21,7 +21,8 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength size, TSLength padding,
|
|||
|
||||
TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) {
|
||||
TSTree *result = ts_tree_make_leaf(ts_builtin_sym_error, size, padding, false);
|
||||
ts_tree_set_has_error(result);
|
||||
ts_tree_set_fragile_left(result);
|
||||
ts_tree_set_fragile_right(result);
|
||||
result->lookahead_char = lookahead_char;
|
||||
return result;
|
||||
}
|
||||
|
|
@ -35,7 +36,6 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
|
|||
*/
|
||||
TSLength size = ts_length_zero(), padding = ts_length_zero();
|
||||
size_t visible_child_count = 0;
|
||||
bool has_error = false;
|
||||
for (size_t i = 0; i < child_count; i++) {
|
||||
TSTree *child = children[i];
|
||||
ts_tree_retain(child);
|
||||
|
|
@ -51,22 +51,21 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
|
|||
visible_child_count++;
|
||||
else
|
||||
visible_child_count += child->visible_child_count;
|
||||
|
||||
if (ts_tree_has_error(child))
|
||||
has_error = true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Mark the tree as hidden if it wraps a single child node.
|
||||
*/
|
||||
TSTreeOptions options = 0;
|
||||
if (has_error)
|
||||
options |= TSTreeOptionsHasError;
|
||||
if (is_hidden)
|
||||
options |= TSTreeOptionsHidden;
|
||||
if (child_count == 1 &&
|
||||
(ts_tree_is_visible(children[0]) || ts_tree_is_wrapper(children[0])))
|
||||
options |= (TSTreeOptionsWrapper | TSTreeOptionsHidden);
|
||||
if (child_count > 0 && ts_tree_is_fragile_left(children[0]))
|
||||
options |= (TSTreeOptionsFragileLeft);
|
||||
if (child_count > 0 && ts_tree_is_fragile_right(children[child_count - 1]))
|
||||
options |= (TSTreeOptionsFragileRight);
|
||||
|
||||
/*
|
||||
* Store the visible child array adjacent to the tree itself. This avoids
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@ typedef enum {
|
|||
TSTreeOptionsHidden = 1 << 0,
|
||||
TSTreeOptionsExtra = 1 << 1,
|
||||
TSTreeOptionsWrapper = 1 << 2,
|
||||
TSTreeOptionsHasError = 1 << 3,
|
||||
TSTreeOptionsFragileLeft = 1 << 3,
|
||||
TSTreeOptionsFragileRight = 1 << 4,
|
||||
} TSTreeOptions;
|
||||
|
||||
struct TSTree {
|
||||
|
|
@ -44,10 +45,6 @@ static inline bool ts_tree_is_wrapper(const TSTree *tree) {
|
|||
return !!(tree->options & TSTreeOptionsWrapper);
|
||||
}
|
||||
|
||||
static inline bool ts_tree_has_error(const TSTree *tree) {
|
||||
return !!(tree->options & TSTreeOptionsHasError);
|
||||
}
|
||||
|
||||
static inline void ts_tree_set_options(TSTree *tree, TSTreeOptions options) {
|
||||
tree->options = (TSTreeOptions)(tree->options | options);
|
||||
}
|
||||
|
|
@ -56,8 +53,20 @@ static inline void ts_tree_set_extra(TSTree *tree) {
|
|||
ts_tree_set_options(tree, TSTreeOptionsExtra);
|
||||
}
|
||||
|
||||
static inline void ts_tree_set_has_error(TSTree *tree) {
|
||||
ts_tree_set_options(tree, TSTreeOptionsHasError);
|
||||
static inline void ts_tree_set_fragile_left(TSTree *tree) {
|
||||
ts_tree_set_options(tree, TSTreeOptionsFragileLeft);
|
||||
}
|
||||
|
||||
static inline void ts_tree_set_fragile_right(TSTree *tree) {
|
||||
ts_tree_set_options(tree, TSTreeOptionsFragileRight);
|
||||
}
|
||||
|
||||
static inline bool ts_tree_is_fragile_left(TSTree *tree) {
|
||||
return tree->options & TSTreeOptionsFragileLeft;
|
||||
}
|
||||
|
||||
static inline bool ts_tree_is_fragile_right(TSTree *tree) {
|
||||
return tree->options & TSTreeOptionsFragileRight;
|
||||
}
|
||||
|
||||
TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, bool);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue