Start work on removing parent pointers

Co-Authored-By: Rick Winfrey <rewinfrey@github.com>
This commit is contained in:
Max Brunsfeld 2018-05-09 10:16:10 -07:00
parent 8300f24fec
commit 973e4a44f0
14 changed files with 410 additions and 324 deletions

View file

@ -48,7 +48,7 @@ describe("extract_tokens", []() {
Repeat{Symbol::non_terminal(3)}
},
},
{}, {}, {}, {}
{}, {}, {}, {}, {}
});
InitialSyntaxGrammar &syntax_grammar = get<0>(result);
@ -156,7 +156,7 @@ describe("extract_tokens", []() {
})
},
},
{}, {}, {}, {}
{}, {}, {}, {}, {}
});
InitialSyntaxGrammar &syntax_grammar = get<0>(result);
@ -203,7 +203,7 @@ describe("extract_tokens", []() {
Rule::seq({ String{"ef"}, String{"cd"} })
},
},
{}, {}, {}, {}
{}, {}, {}, {}, {}
});
InitialSyntaxGrammar &syntax_grammar = get<0>(result);
@ -258,7 +258,7 @@ describe("extract_tokens", []() {
String{"a"}
},
},
{}, {}, {}, {}
{}, {}, {}, {}, {}
});
InitialSyntaxGrammar &syntax_grammar = get<0>(result);
@ -298,7 +298,7 @@ describe("extract_tokens", []() {
{
{ Symbol::non_terminal(2), Symbol::non_terminal(3) }
},
{}, {}
{}, {}, {}
});
InitialSyntaxGrammar &syntax_grammar = get<0>(result);
@ -319,7 +319,7 @@ describe("extract_tokens", []() {
String{"y"},
Pattern{" "},
},
{}, {}, {}
{}, {}, {}, {}
});
AssertThat(get<2>(result), Equals(CompileError::none()));
@ -340,7 +340,7 @@ describe("extract_tokens", []() {
{
String{"y"},
},
{}, {}, {}
{}, {}, {}, {}
});
AssertThat(get<2>(result), Equals(CompileError::none()));
@ -370,7 +370,7 @@ describe("extract_tokens", []() {
{
Symbol::non_terminal(2),
},
{}, {}, {}
{}, {}, {}, {}
});
AssertThat(get<2>(result), Equals(CompileError::none()));
@ -399,7 +399,7 @@ describe("extract_tokens", []() {
{
Symbol::non_terminal(1)
},
{}, {}, {}
{}, {}, {}, {}
});
AssertThat(get<2>(result), Equals(CompileError(
@ -417,7 +417,7 @@ describe("extract_tokens", []() {
{
Rule::choice({ Symbol::non_terminal(1), Blank{} })
},
{}, {}, {}
{}, {}, {}, {}
});
AssertThat(get<2>(result), Equals(CompileError(
@ -446,7 +446,7 @@ describe("extract_tokens", []() {
{
Variable{"rule_A", VariableTypeNamed, Symbol::non_terminal(0)}
},
{}
{}, {}
});
AssertThat(get<2>(result), Equals(CompileError(

View file

@ -1,8 +1,7 @@
#include "bandit/bandit.h"
#include "test_helper.h"
#include "helpers/tree_helpers.h"
#include "helpers/point_helpers.h"
#include "runtime/document.h"
#include "runtime/node.h"
#include <ostream>
using std::string;
@ -21,24 +20,30 @@ TreeArray *tree_array(std::vector<Tree *> trees) {
result.capacity = trees.size();
result.size = trees.size();
result.contents = (Tree **)calloc(trees.size(), sizeof(Tree *));
for (size_t i = 0; i < trees.size(); i++)
for (size_t i = 0; i < trees.size(); i++) {
result.contents[i] = trees[i];
}
return &result;
}
ostream &operator<<(std::ostream &stream, const Tree *tree) {
static TSLanguage DUMMY_LANGUAGE = {};
static TSDocument DUMMY_DOCUMENT = {};
DUMMY_DOCUMENT.parser.language = &DUMMY_LANGUAGE;
DUMMY_LANGUAGE.symbol_names = symbol_names;
TSNode node;
node.data = tree;
return stream << string(ts_node_string(node, &DUMMY_DOCUMENT));
char *string = ts_tree_string(tree, &DUMMY_LANGUAGE, false);
stream << string;
ts_free(string);
return stream;
}
ostream &operator<<(ostream &stream, const TSNode &node) {
return stream << string("{") << (const Tree *)node.data <<
string(", ") << to_string(ts_node_start_byte(node)) << string("}");
if (node.subtree) {
char *string = ts_node_string(node, node.document);
stream << "{" << string << ", " << to_string(ts_node_start_byte(node)) << "}";
ts_free(string);
return stream;
} else {
return stream << "NULL";
}
}
bool operator==(const TSNode &left, const TSNode &right) {

View file

@ -182,7 +182,7 @@ describe("Document", [&]() {
ts_document_parse(document);
ts_document_set_language(document, load_real_language("javascript"));
AssertThat(ts_document_root_node(document).data, Equals<void *>(nullptr));
AssertThat(ts_document_root_node(document).subtree, Equals<void *>(nullptr));
ts_document_parse(document);
root = ts_document_root_node(document);

View file

@ -71,6 +71,7 @@ describe("Node", [&]() {
document = ts_document_new();
ts_document_set_language(document, load_real_language("json"));
ts_document_set_input_string(document, json_string.c_str());
// ts_document_print_debugging_graphs(document, true);
ts_document_parse(document);
root_node = ts_node_child(ts_document_root_node(document), 0);
});
@ -157,7 +158,7 @@ describe("Node", [&]() {
AssertThat(ts_node_parent(number_node), Equals(root_node));
AssertThat(ts_node_parent(false_node), Equals(root_node));
AssertThat(ts_node_parent(object_node), Equals(root_node));
AssertThat(ts_node_parent(ts_document_root_node(document)).data, Equals<void *>(nullptr));
AssertThat(ts_node_parent(ts_document_root_node(document)).subtree, Equals<void *>(nullptr));
});
it("works correctly when the node contains aliased children and extras", [&]() {
@ -239,19 +240,7 @@ describe("Node", [&]() {
child = ts_node_first_named_child_for_byte(root_node, object_index + 1);
AssertThat(ts_node_type(child, document), Equals("object"));
child = ts_node_first_named_child_for_byte(root_node, object_end_index);
AssertThat(child.data, Equals<void *>(nullptr));
});
});
describe("child_index()", [&]() {
it("returns the index of the node within its parent", [&]() {
AssertThat(ts_node_child_index(ts_node_child(root_node, 0)), Equals(0u));
AssertThat(ts_node_child_index(ts_node_child(root_node, 1)), Equals(1u));
AssertThat(ts_node_child_index(ts_node_child(root_node, 2)), Equals(2u));
AssertThat(ts_node_child_index(ts_node_child(root_node, 3)), Equals(3u));
AssertThat(ts_node_child_index(ts_node_child(root_node, 4)), Equals(4u));
AssertThat(ts_node_child_index(ts_node_child(root_node, 5)), Equals(5u));
AssertThat(ts_node_child_index(ts_node_child(root_node, 6)), Equals(6u));
AssertThat(child.subtree, Equals<void *>(nullptr));
});
});
@ -335,7 +324,7 @@ describe("Node", [&]() {
AssertThat(ts_node_parent(child5), Equals(root_node));
AssertThat(ts_node_parent(child6), Equals(root_node));
AssertThat(ts_node_parent(child7), Equals(root_node));
AssertThat(ts_node_parent(ts_document_root_node(document)).data, Equals<void *>(nullptr));
AssertThat(ts_node_parent(ts_document_root_node(document)).subtree, Equals<void *>(nullptr));
});
});
@ -355,15 +344,16 @@ describe("Node", [&]() {
TSNode brace_node2 = ts_node_child(object_node, 2);
TSNode bracket_node2 = ts_node_child(root_node, 6);
AssertThat(ts_node_parent(bracket_node1), Equals(root_node));
AssertThat(ts_node_next_sibling(bracket_node1), Equals(number_node));
AssertThat(ts_node_next_sibling(number_node), Equals(array_comma_node1));
AssertThat(ts_node_next_sibling(array_comma_node1), Equals(false_node));
AssertThat(ts_node_next_sibling(false_node), Equals(array_comma_node2));
AssertThat(ts_node_next_sibling(array_comma_node2), Equals(object_node));
AssertThat(ts_node_next_sibling(object_node), Equals(bracket_node2));
AssertThat(ts_node_next_sibling(bracket_node2).data, Equals<void *>(nullptr));
AssertThat(ts_node_next_sibling(bracket_node2).subtree, Equals<void *>(nullptr));
AssertThat(ts_node_prev_sibling(bracket_node1).data, Equals<void *>(nullptr));
AssertThat(ts_node_prev_sibling(bracket_node1).subtree, Equals<void *>(nullptr));
AssertThat(ts_node_prev_sibling(number_node), Equals(bracket_node1));
AssertThat(ts_node_prev_sibling(array_comma_node1), Equals(number_node));
AssertThat(ts_node_prev_sibling(false_node), Equals(array_comma_node1));
@ -373,24 +363,24 @@ describe("Node", [&]() {
AssertThat(ts_node_next_sibling(brace_node1), Equals(pair_node));
AssertThat(ts_node_next_sibling(pair_node), Equals(brace_node2));
AssertThat(ts_node_next_sibling(brace_node2).data, Equals<void *>(nullptr));
AssertThat(ts_node_next_sibling(brace_node2).subtree, Equals<void *>(nullptr));
AssertThat(ts_node_prev_sibling(brace_node1).data, Equals<void *>(nullptr));
AssertThat(ts_node_prev_sibling(brace_node1).subtree, Equals<void *>(nullptr));
AssertThat(ts_node_prev_sibling(pair_node), Equals(brace_node1));
AssertThat(ts_node_prev_sibling(brace_node2), Equals(pair_node));
AssertThat(ts_node_next_sibling(string_node), Equals(colon_node));
AssertThat(ts_node_next_sibling(colon_node), Equals(null_node));
AssertThat(ts_node_next_sibling(null_node).data, Equals<void *>(nullptr));
AssertThat(ts_node_next_sibling(null_node).subtree, Equals<void *>(nullptr));
AssertThat(ts_node_prev_sibling(string_node).data, Equals<void *>(nullptr));
AssertThat(ts_node_prev_sibling(string_node).subtree, Equals<void *>(nullptr));
AssertThat(ts_node_prev_sibling(colon_node), Equals(string_node));
AssertThat(ts_node_prev_sibling(null_node), Equals(colon_node));
});
it("returns null when the node has no parent", [&]() {
AssertThat(ts_node_next_named_sibling(root_node).data, Equals<void *>(nullptr));
AssertThat(ts_node_prev_named_sibling(root_node).data, Equals<void *>(nullptr));
AssertThat(ts_node_next_named_sibling(root_node).subtree, Equals<void *>(nullptr));
AssertThat(ts_node_prev_named_sibling(root_node).subtree, Equals<void *>(nullptr));
});
});
@ -412,8 +402,8 @@ describe("Node", [&]() {
});
it("returns null when the node has no parent", [&]() {
AssertThat(ts_node_next_named_sibling(root_node).data, Equals<void *>(nullptr));
AssertThat(ts_node_prev_named_sibling(root_node).data, Equals<void *>(nullptr));
AssertThat(ts_node_next_named_sibling(root_node).subtree, Equals<void *>(nullptr));
AssertThat(ts_node_prev_named_sibling(root_node).subtree, Equals<void *>(nullptr));
});
});

View file

@ -5,14 +5,12 @@
#include "runtime/length.h"
void assert_consistent(const Tree *tree) {
if (tree->child_count == 0)
return;
if (tree->child_count == 0) return;
AssertThat(tree->children.contents[0]->padding, Equals<Length>(tree->padding));
Length total_children_size = length_zero();
for (size_t i = 0; i < tree->children.size; i++) {
Tree *child = tree->children.contents[i];
AssertThat(child->context.offset, Equals(total_children_size));
assert_consistent(child);
total_children_size = length_add(total_children_size, ts_tree_total_size(child));
}