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:
parent
95fbdb6fdb
commit
779bf0d745
17 changed files with 167 additions and 243 deletions
|
|
@ -24,7 +24,7 @@ describe("incremental parsing", [&]() {
|
|||
|
||||
it("parses the input", [&]() {
|
||||
AssertThat(string(ts_document_string(doc)), Equals(
|
||||
"(value (object (string) (array (number) (number))))"));
|
||||
"(object (string) (array (number) (number)))"));
|
||||
});
|
||||
|
||||
it("reads the entire input", [&]() {
|
||||
|
|
@ -44,7 +44,7 @@ describe("incremental parsing", [&]() {
|
|||
|
||||
it("updates the parse tree", [&]() {
|
||||
AssertThat(string(ts_document_string(doc)), Equals(
|
||||
"(value (object (string) (array (number) (number)) (string) (number)))"));
|
||||
"(object (string) (array (number) (number)) (string) (number))"));
|
||||
});
|
||||
|
||||
it("re-reads only the changed portion of the input", [&]() {
|
||||
|
|
@ -64,7 +64,7 @@ describe("incremental parsing", [&]() {
|
|||
|
||||
it("2 updates the parse tree", [&]() {
|
||||
AssertThat(string(ts_document_string(doc)), Equals(
|
||||
"(value (object (string) (number) (string) (array (number) (number))))"));
|
||||
"(object (string) (number) (string) (array (number) (number)))"));
|
||||
});
|
||||
|
||||
it_skip("re-reads only the changed portion of the input", [&]() {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,6 @@ recovers from errors inside parenthesized expressions
|
|||
=====================================================
|
||||
x + (y * + z) * 5
|
||||
---
|
||||
(expression (sum
|
||||
(sum
|
||||
(variable)
|
||||
(product (group (ERROR '+')) (number))))
|
||||
(product (group (ERROR '+')) (number)))
|
||||
|
|
|
|||
|
|
@ -3,51 +3,47 @@ parses numbers
|
|||
===================
|
||||
5
|
||||
---
|
||||
(expression (number))
|
||||
(number)
|
||||
|
||||
===================
|
||||
parses variables
|
||||
===================
|
||||
x
|
||||
---
|
||||
(expression (variable))
|
||||
(variable)
|
||||
|
||||
===================
|
||||
parses products
|
||||
===================
|
||||
x * x
|
||||
---
|
||||
(expression (product
|
||||
(variable)
|
||||
(variable)))
|
||||
(product (variable) (variable))
|
||||
|
||||
===================
|
||||
parses sums
|
||||
===================
|
||||
x + x
|
||||
---
|
||||
(expression (sum
|
||||
(variable)
|
||||
(variable)))
|
||||
(sum (variable) (variable))
|
||||
|
||||
===============================================
|
||||
binds multiplication more tightly than addition
|
||||
===============================================
|
||||
a * b + c * d
|
||||
---
|
||||
(expression (sum
|
||||
(sum
|
||||
(product (variable) (variable))
|
||||
(product (variable) (variable))))
|
||||
(product (variable) (variable)))
|
||||
|
||||
============================
|
||||
parses exponents
|
||||
============================
|
||||
x + y * z^(a + b)
|
||||
---
|
||||
(expression (sum
|
||||
(sum
|
||||
(variable)
|
||||
(product
|
||||
(variable)
|
||||
(exponent
|
||||
(variable)
|
||||
(group (sum (variable) (variable)))))))
|
||||
(group (sum (variable) (variable))))))
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ var x = {
|
|||
(statement_block (var_declaration (identifier) (identifier)))))))
|
||||
|
||||
==========================================
|
||||
parses comments z
|
||||
parses comments
|
||||
==========================================
|
||||
// this is the beginning of the script.
|
||||
// here we go.
|
||||
|
|
@ -57,14 +57,14 @@ var thing = {
|
|||
(program
|
||||
(comment)
|
||||
(comment)
|
||||
(var_declaration (identifier) (object
|
||||
(program (var_declaration (identifier) (object
|
||||
(comment)
|
||||
(comment)
|
||||
(identifier) (function_expression
|
||||
(formal_parameters (identifier) (comment))
|
||||
(statement_block
|
||||
(comment)
|
||||
(expression_statement (function_call (identifier))))))))
|
||||
(expression_statement (function_call (identifier)))))))))
|
||||
|
||||
==========================================
|
||||
parses comments within expressions
|
||||
|
|
|
|||
|
|
@ -17,24 +17,24 @@ recovers from errors inside arrays
|
|||
==========================================
|
||||
[1, , 2]
|
||||
---
|
||||
(value (array
|
||||
(array
|
||||
(number)
|
||||
(ERROR <EOF>)
|
||||
(number)))
|
||||
(number))
|
||||
|
||||
==========================================
|
||||
recovers from errors inside objects
|
||||
==========================================
|
||||
{ "key1": 1, oops }
|
||||
---
|
||||
(value (object (string) (number) (ERROR 'o')))
|
||||
(object (string) (number) (ERROR 'o'))
|
||||
|
||||
==========================================
|
||||
recovers from errors inside nested objects
|
||||
==========================================
|
||||
{ "key1": { "key2": 1, 2 }, [, "key3": 3 }
|
||||
---
|
||||
(value (object
|
||||
(object
|
||||
(string) (object (string) (number) (ERROR '2'))
|
||||
(ERROR '[')
|
||||
(string) (number)))
|
||||
(string) (number))
|
||||
|
|
|
|||
|
|
@ -3,14 +3,14 @@ parses floating point numbers
|
|||
=============================
|
||||
3.14
|
||||
---
|
||||
(value (number))
|
||||
(number)
|
||||
|
||||
===================
|
||||
parses empty arrays
|
||||
===================
|
||||
[]
|
||||
---
|
||||
(value (array))
|
||||
(array)
|
||||
|
||||
===================
|
||||
parses arrays
|
||||
|
|
@ -23,19 +23,19 @@ parses arrays
|
|||
{ "stuff": "good" }
|
||||
]
|
||||
---
|
||||
(value (array
|
||||
(array
|
||||
(number)
|
||||
(null)
|
||||
(true)
|
||||
(false)
|
||||
(object (string) (string))))
|
||||
(object (string) (string)))
|
||||
|
||||
====================
|
||||
parses empty objects
|
||||
====================
|
||||
{}
|
||||
---
|
||||
(value (object))
|
||||
(object)
|
||||
|
||||
===================
|
||||
parses long objects
|
||||
|
|
@ -45,8 +45,7 @@ parses long objects
|
|||
"key2": 1
|
||||
}
|
||||
---
|
||||
(value (object
|
||||
(object
|
||||
(string) (string)
|
||||
(string) (number)
|
||||
))
|
||||
(string) (number))
|
||||
|
||||
|
|
|
|||
|
|
@ -21,9 +21,9 @@ describe("tracking the positions of AST nodes", []() {
|
|||
ts_document_set_input_string(doc, " [12, 5]");
|
||||
|
||||
const TSTree *tree = ts_document_tree(doc);
|
||||
const TSTree *array = ts_tree_children(tree, NULL)[0];
|
||||
const TSTree *number1 = ts_tree_children(array, NULL)[0];
|
||||
const TSTree *number2 = ts_tree_children(array, NULL)[1];
|
||||
const TSTree *array = tree->children[0];
|
||||
const TSTree *number1 = array->children[1]->children[0];
|
||||
const TSTree *number2 = array->children[2]->children[1]->children[0];
|
||||
|
||||
AssertThat(ts_document_symbol_name(doc, array), Equals("array"));
|
||||
AssertThat(ts_document_symbol_name(doc, number1), Equals("number"));
|
||||
|
|
|
|||
|
|
@ -40,14 +40,14 @@ describe("LR Parsers", [&]() {
|
|||
});
|
||||
|
||||
it("runs the lexer with the lex state corresponding to the initial state", [&]() {
|
||||
lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym2, 5, 1);
|
||||
lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym2, 5, 1, 0);
|
||||
ts_parser_step(parser);
|
||||
AssertThat(lex_fn_state_received, Equals(100));
|
||||
});
|
||||
|
||||
describe("when the returned symbol indicates a shift action", [&]() {
|
||||
before_each([&]() {
|
||||
lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym2, 5, 1);
|
||||
lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym2, 5, 1, 0);
|
||||
});
|
||||
|
||||
it("advances to the state specified in the action", [&]() {
|
||||
|
|
@ -63,7 +63,7 @@ describe("LR Parsers", [&]() {
|
|||
|
||||
describe("when the returned symbol indicates an error", [&]() {
|
||||
before_each([&]() {
|
||||
lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym1, 5, 1);
|
||||
lex_fn_node_to_return = ts_tree_make_leaf(dummy_sym1, 5, 1, 0);
|
||||
});
|
||||
|
||||
it("ends the parse, returning an error tree", [&]() {
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ describe("stacks", [&]() {
|
|||
TSTree *node1;
|
||||
|
||||
before_each([&]() {
|
||||
node1 = ts_tree_make_leaf(sym1, 5, 1);
|
||||
node1 = ts_tree_make_leaf(sym1, 5, 1, 0);
|
||||
ts_stack_push(&stack, 5, node1);
|
||||
});
|
||||
|
||||
|
|
@ -48,10 +48,10 @@ describe("stacks", [&]() {
|
|||
|
||||
before_each([&]() {
|
||||
nodes = tree_array({
|
||||
ts_tree_make_leaf(sym1, 5, 1),
|
||||
ts_tree_make_leaf(sym1, 5, 1),
|
||||
ts_tree_make_leaf(hidden_sym, 5, 1),
|
||||
ts_tree_make_leaf(sym1, 5, 1),
|
||||
ts_tree_make_leaf(sym1, 5, 1, 0),
|
||||
ts_tree_make_leaf(sym1, 5, 1, 0),
|
||||
ts_tree_make_leaf(hidden_sym, 5, 1, 0),
|
||||
ts_tree_make_leaf(sym1, 5, 1, 0),
|
||||
});
|
||||
|
||||
for (TSStateId i = 0; i < 4; i++)
|
||||
|
|
@ -75,76 +75,21 @@ describe("stacks", [&]() {
|
|||
AssertThat(node->symbol, Equals(sym2));
|
||||
});
|
||||
|
||||
it("makes all of the removed nodes immediate children of the new node", [&]() {
|
||||
it("removes any hidden nodes from its regular list of children", [&]() {
|
||||
TSTree *expected_children[3] = {
|
||||
stack.entries[1].node,
|
||||
stack.entries[2].node,
|
||||
stack.entries[3].node,
|
||||
};
|
||||
|
||||
TSTree *node = ts_stack_reduce(&stack, sym2, 3, hidden_symbols, 0);
|
||||
size_t immediate_child_count;
|
||||
TSTree **immediate_children = ts_tree_immediate_children(node, &immediate_child_count);
|
||||
|
||||
AssertThat(immediate_child_count, Equals<size_t>(3));
|
||||
for (size_t i = 0; i < 3; i++)
|
||||
AssertThat(immediate_children[i], Equals(expected_children[i]));
|
||||
});
|
||||
|
||||
it("removes any hidden nodes from its regular list of children", [&]() {
|
||||
TSTree *expected_children[2] = {
|
||||
stack.entries[1].node,
|
||||
stack.entries[3].node,
|
||||
};
|
||||
|
||||
TSTree *node = ts_stack_reduce(&stack, sym2, 3, hidden_symbols, 0);
|
||||
size_t child_count;
|
||||
TSTree **children = ts_tree_children(node, &child_count);
|
||||
|
||||
AssertThat(child_count, Equals<size_t>(2));
|
||||
AssertThat(child_count, Equals<size_t>(3));
|
||||
for (size_t i = 0; i < 2; i++)
|
||||
AssertThat(children[i], Equals(expected_children[i]));
|
||||
});
|
||||
|
||||
describe("when there are hidden nodes with children of their own", [&]() {
|
||||
TSTree **grandchildren;
|
||||
TSTree *hidden_node;
|
||||
|
||||
before_each([&]() {
|
||||
grandchildren = tree_array({
|
||||
ts_tree_make_leaf(sym1, 10, 2),
|
||||
ts_tree_make_leaf(sym2, 10, 2),
|
||||
});
|
||||
|
||||
hidden_node = ts_tree_make_node(hidden_sym, 2, 0, grandchildren);
|
||||
ts_stack_push(&stack, 21, hidden_node);
|
||||
});
|
||||
|
||||
after_each([&]() {
|
||||
for (TSStateId i = 0; i < 2; i++)
|
||||
ts_tree_release(grandchildren[i]);
|
||||
free(grandchildren);
|
||||
ts_tree_release(hidden_node);
|
||||
});
|
||||
|
||||
it("makes those child nodes children of the new node", [&]() {
|
||||
TSTree *node = ts_stack_reduce(&stack, sym2, 4, hidden_symbols, 0);
|
||||
|
||||
TSTree *expected_children[4] = {
|
||||
stack.entries[1].node,
|
||||
stack.entries[3].node,
|
||||
grandchildren[0],
|
||||
grandchildren[1],
|
||||
};
|
||||
|
||||
size_t child_count;
|
||||
TSTree **children = ts_tree_children(node, &child_count);
|
||||
|
||||
AssertThat(child_count, Equals<size_t>(4));
|
||||
for (size_t i = 0; i < 4; i++)
|
||||
AssertThat(children[i], Equals(expected_children[i]));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -15,12 +15,11 @@ describe("trees", []() {
|
|||
TSTree *tree1, *tree2, *parent1;
|
||||
|
||||
before_each([&]() {
|
||||
tree1 = ts_tree_make_leaf(cat, 5, 2);
|
||||
tree2 = ts_tree_make_leaf(cat, 3, 1);
|
||||
parent1 = ts_tree_make_node(dog, 2, 2, tree_array({
|
||||
tree1 = ts_tree_make_leaf(cat, 5, 2, 0);
|
||||
tree2 = ts_tree_make_leaf(cat, 3, 1, 0);
|
||||
parent1 = ts_tree_make_node(dog, 2, tree_array({
|
||||
tree1, tree2, // children
|
||||
tree1, tree2, // immediate_children
|
||||
}));
|
||||
}), 0);
|
||||
});
|
||||
|
||||
after_each([&]() {
|
||||
|
|
@ -30,7 +29,7 @@ describe("trees", []() {
|
|||
});
|
||||
|
||||
describe("making a parent node", [&]() {
|
||||
it("computes its offset and size based on its child nodes", [&]() {
|
||||
it("computes its size based on its child nodes", [&]() {
|
||||
AssertThat(parent1->size, Equals<size_t>(9));
|
||||
});
|
||||
|
||||
|
|
@ -41,15 +40,14 @@ describe("trees", []() {
|
|||
|
||||
describe("equality", [&]() {
|
||||
it("returns true for identical trees", [&]() {
|
||||
TSTree *tree1_copy = ts_tree_make_leaf(cat, 5, 2);
|
||||
TSTree *tree1_copy = ts_tree_make_leaf(cat, 5, 2, 0);
|
||||
AssertThat(ts_tree_equals(tree1, tree1_copy), Equals(1));
|
||||
TSTree *tree2_copy = ts_tree_make_leaf(cat, 3, 1);
|
||||
TSTree *tree2_copy = ts_tree_make_leaf(cat, 3, 1, 0);
|
||||
AssertThat(ts_tree_equals(tree2, tree2_copy), Equals(1));
|
||||
|
||||
TSTree *parent2 = ts_tree_make_node(dog, 2, 2, tree_array({
|
||||
TSTree *parent2 = ts_tree_make_node(dog, 2, tree_array({
|
||||
tree1_copy, tree2_copy,
|
||||
tree1_copy, tree2_copy,
|
||||
}));
|
||||
}), 0);
|
||||
AssertThat(ts_tree_equals(parent1, parent2), Equals(1));
|
||||
|
||||
ts_tree_release(tree1_copy);
|
||||
|
|
@ -58,17 +56,16 @@ describe("trees", []() {
|
|||
});
|
||||
|
||||
it("returns false for trees with different symbols", [&]() {
|
||||
TSTree *different_tree = ts_tree_make_leaf(pig, 0, 0);
|
||||
TSTree *different_tree = ts_tree_make_leaf(pig, 0, 0, 0);
|
||||
AssertThat(ts_tree_equals(tree1, different_tree), Equals(0));
|
||||
ts_tree_release(different_tree);
|
||||
});
|
||||
|
||||
it("returns false for trees with different children", [&]() {
|
||||
TSTree *different_tree = ts_tree_make_leaf(pig, 0, 0);
|
||||
TSTree *different_parent = ts_tree_make_node(dog, 2, 2, tree_array({
|
||||
TSTree *different_tree = ts_tree_make_leaf(pig, 0, 0, 0);
|
||||
TSTree *different_parent = ts_tree_make_node(dog, 2, tree_array({
|
||||
different_tree, different_tree,
|
||||
tree2, tree2,
|
||||
}));
|
||||
}), 0);
|
||||
|
||||
AssertThat(ts_tree_equals(different_parent, parent1), Equals(0));
|
||||
AssertThat(ts_tree_equals(parent1, different_parent), Equals(0));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue