Collapse nodes with only one child and no additional text content
This commit is contained in:
parent
316adc7788
commit
09e28e7859
8 changed files with 56 additions and 80 deletions
|
|
@ -11,6 +11,6 @@ recovers from errors inside parenthesized expressions
|
|||
x + (y * + z) * 5
|
||||
---
|
||||
(expression
|
||||
(term (factor (variable)))
|
||||
(variable)
|
||||
(plus)
|
||||
(term (factor (ERROR)) (times) (factor (number))))
|
||||
(term (factor (ERROR)) (times) (number)))
|
||||
|
|
@ -3,14 +3,14 @@ parses numbers
|
|||
===================
|
||||
5
|
||||
---
|
||||
(expression (term (factor (number))))
|
||||
(expression (number))
|
||||
|
||||
===================
|
||||
parses variables
|
||||
===================
|
||||
x
|
||||
---
|
||||
(expression (term (factor (variable))))
|
||||
(expression (variable))
|
||||
|
||||
===================
|
||||
parses products
|
||||
|
|
@ -18,9 +18,9 @@ parses products
|
|||
x * x
|
||||
---
|
||||
(expression (term
|
||||
(factor (variable))
|
||||
(variable)
|
||||
(times)
|
||||
(factor (variable))))
|
||||
(variable)))
|
||||
|
||||
===================
|
||||
parses sums
|
||||
|
|
@ -28,9 +28,9 @@ parses sums
|
|||
x + x
|
||||
---
|
||||
(expression
|
||||
(term (factor (variable)))
|
||||
(variable)
|
||||
(plus)
|
||||
(term (factor (variable))))
|
||||
(variable))
|
||||
|
||||
====================
|
||||
parses complex trees
|
||||
|
|
@ -38,17 +38,17 @@ parses complex trees
|
|||
x * y + z * a
|
||||
---
|
||||
(expression
|
||||
(term (factor (variable)) (times) (factor (variable)))
|
||||
(term (variable) (times) (variable))
|
||||
(plus)
|
||||
(term (factor (variable)) (times) (factor (variable))))
|
||||
(term (variable) (times) (variable)))
|
||||
|
||||
=================================
|
||||
handles parenthesized expressions
|
||||
=================================
|
||||
x * (y + z)
|
||||
---
|
||||
(expression
|
||||
(term (factor (variable))
|
||||
(expression (term
|
||||
(variable)
|
||||
(times)
|
||||
(factor (expression (term (factor (variable))) (plus) (term (factor (variable)))))))
|
||||
(factor (expression (variable) (plus) (variable)))))
|
||||
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ var x = {};
|
|||
{};
|
||||
---
|
||||
(program
|
||||
(statement (assignment (identifier) (expression (literal (object)))))
|
||||
(statement (expression (literal (object)))))
|
||||
(statement (assignment (identifier) (object)))
|
||||
(statement (object)))
|
||||
|
||||
==========================================
|
||||
parses if statements
|
||||
|
|
@ -16,9 +16,9 @@ if (theCondition) {
|
|||
}
|
||||
---
|
||||
(program
|
||||
(statement (if_statement
|
||||
(expression (identifier))
|
||||
(statement_block (statement (assignment (identifier) (expression (literal (number)))))))))
|
||||
(if_statement
|
||||
(identifier)
|
||||
(statement_block (statement (assignment (identifier) (number))))))
|
||||
|
||||
==========================================
|
||||
parses if-else statements
|
||||
|
|
@ -29,9 +29,8 @@ if (theCondition) {
|
|||
var x = 4;
|
||||
}
|
||||
---
|
||||
(program (statement
|
||||
(if_statement
|
||||
(expression (identifier))
|
||||
(statement_block (statement (assignment (identifier) (expression (literal (number))))))
|
||||
(statement_block (statement (assignment (identifier) (expression (literal (number)))))))))
|
||||
(program (if_statement
|
||||
(identifier)
|
||||
(statement_block (statement (assignment (identifier) (number))))
|
||||
(statement_block (statement (assignment (identifier) (number))))))
|
||||
|
||||
|
|
|
|||
|
|
@ -18,16 +18,16 @@ recovers from errors inside arrays
|
|||
[1, , 2]
|
||||
---
|
||||
(value (array
|
||||
(value (number))
|
||||
(number)
|
||||
(ERROR)
|
||||
(value (number))))
|
||||
(number)))
|
||||
|
||||
==========================================
|
||||
recovers from errors inside objects
|
||||
==========================================
|
||||
{ "key1": 1, oops }
|
||||
---
|
||||
(value (object (string) (value (number)) (ERROR)))
|
||||
(value (object (string) (number) (ERROR)))
|
||||
|
||||
==========================================
|
||||
recovers from errors inside nested objects
|
||||
|
|
@ -35,6 +35,6 @@ recovers from errors inside nested objects
|
|||
{ "key1": { "key2": 1, 2 }, [, "key3": 3 }
|
||||
---
|
||||
(value (object
|
||||
(string) (value (object (string) (value (number)) (ERROR)))
|
||||
(string) (object (string) (number) (ERROR))
|
||||
(ERROR)
|
||||
(string) (value (number))))
|
||||
(string) (number)))
|
||||
|
|
|
|||
|
|
@ -24,14 +24,11 @@ parses arrays
|
|||
]
|
||||
---
|
||||
(value (array
|
||||
(value (number))
|
||||
(value (null))
|
||||
(value (true))
|
||||
(value (false))
|
||||
(value (object
|
||||
(string) (value (string))
|
||||
))
|
||||
))
|
||||
(number)
|
||||
(null)
|
||||
(true)
|
||||
(false)
|
||||
(object (string) (string))))
|
||||
|
||||
====================
|
||||
parses empty objects
|
||||
|
|
@ -49,7 +46,7 @@ parses long objects
|
|||
}
|
||||
---
|
||||
(value (object
|
||||
(string) (value (string))
|
||||
(string) (value (number))
|
||||
(string) (string)
|
||||
(string) (number)
|
||||
))
|
||||
|
||||
|
|
|
|||
|
|
@ -25,8 +25,8 @@ describe("tracking the positions of AST nodes", []() {
|
|||
const ts_tree *number2 = ts_tree_children(array, NULL)[1];
|
||||
|
||||
AssertThat(ts_document_symbol_name(doc, array), Equals("array"));
|
||||
AssertThat(ts_document_symbol_name(doc, number1), Equals("value"));
|
||||
AssertThat(ts_document_symbol_name(doc, number2), Equals("value"));
|
||||
AssertThat(ts_document_symbol_name(doc, number1), Equals("number"));
|
||||
AssertThat(ts_document_symbol_name(doc, number2), Equals("number"));
|
||||
|
||||
AssertThat(ts_tree_offset(number1), Equals<size_t>(0));
|
||||
AssertThat(ts_tree_size(number1), Equals<size_t>(2));
|
||||
|
|
|
|||
|
|
@ -24,12 +24,7 @@ describe("incremental parsing", [&]() {
|
|||
|
||||
it("parses the input", [&]() {
|
||||
AssertThat(string(ts_document_string(doc)), Equals(
|
||||
"(value "
|
||||
"(object "
|
||||
"(string) "
|
||||
"(value (array "
|
||||
"(value (number)) "
|
||||
"(value (number))))))"));
|
||||
"(value (object (string) (array (number) (number))))"));
|
||||
});
|
||||
|
||||
it("reads the entire input", [&]() {
|
||||
|
|
@ -53,15 +48,7 @@ describe("incremental parsing", [&]() {
|
|||
|
||||
it("updates the parse tree", [&]() {
|
||||
AssertThat(string(ts_document_string(doc)), Equals(
|
||||
"(value "
|
||||
"(object "
|
||||
"(string) "
|
||||
"(value (array "
|
||||
"(value (number)) "
|
||||
"(value (number)))) "
|
||||
"(string) "
|
||||
"(value (number))))"
|
||||
));
|
||||
"(value (object (string) (array (number) (number)) (string) (number)))"));
|
||||
});
|
||||
|
||||
it("re-reads only the changed portion of the input", [&]() {
|
||||
|
|
@ -85,15 +72,7 @@ describe("incremental parsing", [&]() {
|
|||
|
||||
it("2 updates the parse tree", [&]() {
|
||||
AssertThat(string(ts_document_string(doc)), Equals(
|
||||
"(value "
|
||||
"(object "
|
||||
"(string) "
|
||||
"(value (number)) "
|
||||
"(string) "
|
||||
"(value (array "
|
||||
"(value (number)) "
|
||||
"(value (number))))))"
|
||||
));
|
||||
"(value (object (string) (number) (string) (array (number) (number))))"));
|
||||
});
|
||||
|
||||
it_skip("re-reads only the changed portion of the input", [&]() {
|
||||
|
|
|
|||
|
|
@ -60,28 +60,22 @@ size_t ts_stack_right_position(const ts_stack *stack) {
|
|||
|
||||
ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, int immediate_child_count, const int *hidden_symbol_flags) {
|
||||
size_t new_stack_size = stack->size - immediate_child_count;
|
||||
size_t size = 0, offset = 0;
|
||||
int flags[immediate_child_count];
|
||||
|
||||
int child_count = 0;
|
||||
for (int i = 0; i < immediate_child_count; i++) {
|
||||
ts_tree *child = stack->entries[new_stack_size + i].node;
|
||||
if (i == 0) {
|
||||
offset = ts_tree_offset(child);
|
||||
size = ts_tree_size(child);
|
||||
} else {
|
||||
size += ts_tree_offset(child) + ts_tree_size(child);
|
||||
}
|
||||
|
||||
if (hidden_symbol_flags[ts_tree_symbol(child)]) {
|
||||
size_t grandchild_count;
|
||||
ts_tree_children(child, &grandchild_count);
|
||||
child_count += grandchild_count;
|
||||
} else {
|
||||
child_count++;
|
||||
}
|
||||
size_t grandchild_count;
|
||||
ts_tree **grandchildren = ts_tree_children(child, &grandchild_count);
|
||||
flags[i] = (
|
||||
hidden_symbol_flags[ts_tree_symbol(child)] ||
|
||||
(grandchild_count == 1 && ts_tree_size(child) == ts_tree_size(grandchildren[0]))
|
||||
);
|
||||
child_count += (flags[i]) ? grandchild_count : 1;
|
||||
}
|
||||
|
||||
size_t child_index = 0;
|
||||
size_t size = 0, offset = 0;
|
||||
ts_tree **children = malloc((child_count + immediate_child_count) * sizeof(ts_tree *));
|
||||
ts_tree **immediate_children = children + child_count;
|
||||
|
||||
|
|
@ -89,7 +83,14 @@ ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, int immediate_child
|
|||
ts_tree *child = stack->entries[new_stack_size + i].node;
|
||||
immediate_children[i] = child;
|
||||
|
||||
if (hidden_symbol_flags[ts_tree_symbol(child)]) {
|
||||
if (i == 0) {
|
||||
offset = ts_tree_offset(child);
|
||||
size = ts_tree_size(child);
|
||||
} else {
|
||||
size += ts_tree_offset(child) + ts_tree_size(child);
|
||||
}
|
||||
|
||||
if (flags[i]) {
|
||||
size_t grandchild_count;
|
||||
ts_tree ** grandchildren = ts_tree_children(child, &grandchild_count);
|
||||
memcpy(children + child_index, grandchildren, (grandchild_count * sizeof(ts_tree *)));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue