Replace node pos/size functions with start/end char/byte functions

This commit is contained in:
Max Brunsfeld 2015-12-03 22:59:27 -08:00
parent fe5286f863
commit b3a6de6dad
7 changed files with 147 additions and 130 deletions

View file

@ -137,18 +137,18 @@ int main() {
TSNode root_node = ts_document_root_node(document);
printf(
"Root name: %s, position: %lu, size: %lu\n",
"Root name: %s, start: %lu, end: %lu\n",
ts_node_name(root_node, document),
ts_node_pos(root_node).chars,
ts_node_size(root_node).chars
ts_node_start_char(root_node),
ts_node_end_char(root_node)
);
TSNode product_node = ts_node_named_child(ts_node_child(root_node, 0), 1);
printf(
"Child name: %s, position: %lu, size: %lu\n",
"Child name: %s, start: %lu, end: %lu\n",
ts_node_name(product_node, document),
ts_node_pos(product_node).chars,
ts_node_size(product_node).chars
ts_node_start_char(product_node),
ts_node_end_char(product_node)
);
ts_document_free(document);

View file

@ -50,9 +50,10 @@ typedef unsigned short TSSymbol;
typedef struct TSLanguage TSLanguage;
typedef struct TSDocument TSDocument;
TSLength ts_node_pos(TSNode);
TSLength ts_node_size(TSNode);
TSPoint ts_node_size_point(TSNode);
size_t ts_node_start_char(TSNode);
size_t ts_node_end_char(TSNode);
size_t ts_node_start_byte(TSNode);
size_t ts_node_end_byte(TSNode);
TSPoint ts_node_start_point(TSNode);
TSPoint ts_node_end_point(TSNode);
TSSymbol ts_node_symbol(TSNode);
@ -70,8 +71,8 @@ TSNode ts_node_next_sibling(TSNode);
TSNode ts_node_next_named_sibling(TSNode);
TSNode ts_node_prev_sibling(TSNode);
TSNode ts_node_prev_named_sibling(TSNode);
TSNode ts_node_descendent_for_range(TSNode, size_t, size_t);
TSNode ts_node_named_descendent_for_range(TSNode, size_t, size_t);
TSNode ts_node_descendant_for_range(TSNode, size_t, size_t);
TSNode ts_node_named_descendant_for_range(TSNode, size_t, size_t);
TSDocument *ts_document_make();
void ts_document_free(TSDocument *);

View file

@ -24,7 +24,7 @@ ostream &operator<<(std::ostream &stream, const TSTree *tree) {
ostream &operator<<(ostream &stream, const TSNode &node) {
return stream << string("{") << (const TSTree *)node.data <<
string(", ") << to_string(ts_node_pos(node).chars) << string("}");
string(", ") << to_string(ts_node_start_char(node)) << string("}");
}
bool operator==(const TSNode &left, const TSNode &right) {

View file

@ -20,10 +20,9 @@ void expect_the_correct_tree(TSNode node, TSDocument *doc, string tree_string) {
}
void expect_a_consistent_tree(TSNode node, TSDocument *doc) {
TSLength start = ts_node_pos(node);
TSLength end = ts_length_add(start, ts_node_size(node));
size_t child_count = ts_node_child_count(node);
size_t start = ts_node_start_char(node);
size_t end = ts_node_end_char(node);
TSPoint start_point = ts_node_start_point(node);
TSPoint end_point = ts_node_end_point(node);
@ -32,15 +31,13 @@ void expect_a_consistent_tree(TSNode node, TSDocument *doc) {
for (size_t i = 0; i < child_count; i++) {
TSNode child = ts_node_child(node, i);
TSLength child_start = ts_node_pos(child);
TSLength child_end = ts_length_add(child_start, ts_node_size(child));
size_t child_start = ts_node_start_char(child);
size_t child_end = ts_node_end_char(child);
TSPoint child_start_point = ts_node_start_point(child);
TSPoint child_end_point = ts_node_end_point(child);
AssertThat(child_start.chars, IsGreaterThan(start.chars) || Equals(start.chars));
AssertThat(child_end.chars, IsLessThan(end.chars) || Equals(end.chars));
AssertThat(child_start, IsGreaterThan(start) || Equals(start));
AssertThat(child_end, IsLessThan(end) || Equals(end));
AssertThat(child_start_point, IsGreaterThan(start_point) || Equals(start_point));
AssertThat(child_end_point, IsLessThan(end_point) || Equals(end_point));

View file

@ -9,21 +9,34 @@ START_TEST
describe("Node", []() {
TSDocument *document;
TSNode array_node;
string input_string;
string input_string = "\n"
"\n"
"[\n"
" 123,\n"
" false,\n"
" {\n"
" \"x\": null\n"
" }\n"
"]";
size_t array_index = input_string.find("[\n");
size_t array_end_index = input_string.find("]") + 1;
size_t number_index = input_string.find("123");
size_t number_end_index = number_index + string("123").size();
size_t false_index = input_string.find("false");
size_t false_end_index = false_index + string("false").size();
size_t object_index = input_string.find("{");
size_t object_end_index = input_string.find("}") + 1;
size_t string_index = input_string.find("\"x\"");
size_t string_end_index = string_index + 3;
size_t colon_index = input_string.find(":");
size_t null_index = input_string.find("null");
size_t null_end_index = null_index + string("null").size();
before_each([&]() {
document = ts_document_make();
ts_document_set_language(document, ts_language_json());
input_string = "\n"
"\n"
"[\n"
" 123,\n"
" false,\n"
" {\n"
" \"x\": null\n"
" }\n"
"]";
ts_document_set_input_string(document, input_string.c_str());
ts_document_parse(document);
@ -53,27 +66,27 @@ describe("Node", []() {
AssertThat(ts_node_name(child2, document), Equals("false"));
AssertThat(ts_node_name(child3, document), Equals("object"));
AssertThat(ts_node_pos(array_node).bytes, Equals<size_t>(2));
AssertThat(ts_node_size(array_node).bytes, Equals<size_t>(41));
AssertThat(ts_node_start_byte(array_node), Equals(array_index));
AssertThat(ts_node_end_byte(array_node), Equals(array_end_index));
AssertThat(ts_node_start_char(array_node), Equals(array_index));
AssertThat(ts_node_end_char(array_node), Equals(array_end_index));
AssertThat(ts_node_start_point(array_node), Equals<TSPoint>({ 2, 0 }));
AssertThat(ts_node_end_point(array_node), Equals<TSPoint>({ 8, 1 }));
AssertThat(ts_node_pos(child1).bytes, Equals(input_string.find("123")));
AssertThat(ts_node_size(child1).bytes, Equals<size_t>(3));
AssertThat(ts_node_start_byte(child1), Equals(number_index));
AssertThat(ts_node_end_byte(child1), Equals(number_end_index));
AssertThat(ts_node_start_char(child1), Equals(number_index));
AssertThat(ts_node_end_char(child1), Equals(number_end_index));
AssertThat(ts_node_start_point(child1), Equals<TSPoint>({ 3, 2 }));
AssertThat(ts_node_end_point(child1), Equals<TSPoint>({ 3, 5 }));
AssertThat(ts_node_pos(child2).bytes, Equals(input_string.find("false")));
AssertThat(ts_node_size(child2).bytes, Equals<size_t>(5));
AssertThat(ts_node_start_byte(child2), Equals(false_index));
AssertThat(ts_node_end_byte(child2), Equals(false_end_index));
AssertThat(ts_node_start_point(child2), Equals<TSPoint>({ 4, 2 }));
AssertThat(ts_node_end_point(child2), Equals<TSPoint>({ 4, 7 }));
AssertThat(ts_node_pos(child3).bytes, Equals(input_string.find("{")));
AssertThat(ts_node_size(child3).bytes, Equals<size_t>(19));
AssertThat(ts_node_start_byte(child3), Equals(object_index));
AssertThat(ts_node_end_byte(child3), Equals(object_end_index));
AssertThat(ts_node_start_point(child3), Equals<TSPoint>({ 5, 2 }));
AssertThat(ts_node_end_point(child3), Equals<TSPoint>({ 7, 3 }));
@ -85,9 +98,18 @@ describe("Node", []() {
AssertThat(ts_node_name(grandchild1, document), Equals("string"));
AssertThat(ts_node_name(grandchild2, document), Equals("null"));
AssertThat(ts_node_start_byte(grandchild1), Equals(string_index));
AssertThat(ts_node_end_byte(grandchild1), Equals(string_end_index));
AssertThat(ts_node_start_point(grandchild1), Equals<TSPoint>({ 6, 4 }));
AssertThat(ts_node_end_point(grandchild1), Equals<TSPoint>({ 6, 7 }));
AssertThat(ts_node_start_byte(grandchild2), Equals(null_index));
AssertThat(ts_node_end_byte(grandchild2), Equals(null_end_index));
AssertThat(ts_node_start_point(grandchild2), Equals<TSPoint>({ 6, 9 }));
AssertThat(ts_node_end_point(grandchild2), Equals<TSPoint>({ 6, 13 }));
AssertThat(ts_node_parent(grandchild1), Equals(child3));
AssertThat(ts_node_parent(grandchild2), Equals(child3));
AssertThat(ts_node_parent(child1), Equals(array_node));
AssertThat(ts_node_parent(child2), Equals(array_node));
AssertThat(ts_node_parent(child3), Equals(array_node));
@ -124,27 +146,23 @@ describe("Node", []() {
AssertThat(ts_node_is_named(child6), IsTrue());
AssertThat(ts_node_is_named(child7), IsFalse());
AssertThat(ts_node_pos(child1).bytes, Equals<size_t>(2));
AssertThat(ts_node_size(child1).bytes, Equals<size_t>(1));
AssertThat(ts_node_start_byte(child1), Equals(array_index));
AssertThat(ts_node_end_byte(child1), Equals(array_index + 1));
AssertThat(ts_node_start_point(child1), Equals<TSPoint>({ 2, 0 }));
AssertThat(ts_node_end_point(child1), Equals<TSPoint>({ 2, 1 }));
AssertThat(ts_node_pos(child3).bytes, Equals<size_t>(9));
AssertThat(ts_node_size(child3).bytes, Equals<size_t>(1));
AssertThat(ts_node_start_byte(child3), Equals(number_end_index));
AssertThat(ts_node_end_byte(child3), Equals(number_end_index + 1));
AssertThat(ts_node_start_point(child3), Equals<TSPoint>({ 3, 5 }));
AssertThat(ts_node_end_point(child3), Equals<TSPoint>({ 3, 6 }));
AssertThat(ts_node_pos(child5).bytes, Equals<size_t>(18));
AssertThat(ts_node_size(child5).bytes, Equals<size_t>(1));
AssertThat(ts_node_start_byte(child5), Equals(false_end_index));
AssertThat(ts_node_end_byte(child5), Equals(false_end_index + 1));
AssertThat(ts_node_start_point(child5), Equals<TSPoint>({ 4, 7 }));
AssertThat(ts_node_end_point(child5), Equals<TSPoint>({ 4, 8 }));
AssertThat(ts_node_pos(child7).bytes, Equals<size_t>(42));
AssertThat(ts_node_size(child7).bytes, Equals<size_t>(1));
AssertThat(ts_node_start_byte(child7), Equals(array_end_index - 1));
AssertThat(ts_node_end_byte(child7), Equals(array_end_index));
AssertThat(ts_node_start_point(child7), Equals<TSPoint>({ 8, 0 }));
AssertThat(ts_node_end_point(child7), Equals<TSPoint>({ 8, 1 }));
@ -162,6 +180,11 @@ describe("Node", []() {
AssertThat(ts_node_name(grandchild4, document), Equals("null"));
AssertThat(ts_node_name(grandchild5, document), Equals("}"));
AssertThat(ts_node_parent(grandchild1), Equals(child6));
AssertThat(ts_node_parent(grandchild2), Equals(child6));
AssertThat(ts_node_parent(grandchild3), Equals(child6));
AssertThat(ts_node_parent(grandchild4), Equals(child6));
AssertThat(ts_node_parent(grandchild5), Equals(child6));
AssertThat(ts_node_parent(child1), Equals(array_node));
AssertThat(ts_node_parent(child2), Equals(array_node));
AssertThat(ts_node_parent(child3), Equals(array_node));
@ -252,16 +275,17 @@ describe("Node", []() {
describe("named_descendant_for_range(start, end)", [&]() {
describe("when there is a leaf node that spans the given range exactly", [&]() {
it("returns that leaf node", [&]() {
TSNode leaf = ts_node_named_descendent_for_range(array_node, 28, 30);
TSNode leaf = ts_node_named_descendant_for_range(array_node, string_index, string_end_index - 1);
AssertThat(ts_node_name(leaf, document), Equals("string"));
AssertThat(ts_node_size(leaf).bytes, Equals<size_t>(3));
AssertThat(ts_node_pos(leaf).bytes, Equals<size_t>(28));
AssertThat(ts_node_start_byte(leaf), Equals(string_index));
AssertThat(ts_node_end_byte(leaf), Equals(string_end_index));
AssertThat(ts_node_start_point(leaf), Equals<TSPoint>({ 6, 4 }));
AssertThat(ts_node_end_point(leaf), Equals<TSPoint>({ 6, 7 }));
size_t index = input_string.find("123");
leaf = ts_node_named_descendent_for_range(array_node, index, index + 2);
leaf = ts_node_named_descendant_for_range(array_node, number_index, number_end_index - 1);
AssertThat(ts_node_name(leaf, document), Equals("number"));
AssertThat(ts_node_pos(leaf).bytes, Equals(index));
AssertThat(ts_node_size(leaf).bytes, Equals<size_t>(3));
AssertThat(ts_node_start_byte(leaf), Equals(number_index));
AssertThat(ts_node_end_byte(leaf), Equals(number_end_index));
AssertThat(ts_node_start_point(leaf), Equals<TSPoint>({ 3, 2 }));
AssertThat(ts_node_end_point(leaf), Equals<TSPoint>({ 3, 5 }));
});
@ -269,20 +293,17 @@ describe("Node", []() {
describe("when there is a leaf node that extends beyond the given range", [&]() {
it("returns that leaf node", [&]() {
size_t index = input_string.find("\"x\"");
TSNode leaf = ts_node_named_descendent_for_range(array_node, index, index + 1);
TSNode leaf = ts_node_named_descendant_for_range(array_node, string_index, string_index + 1);
AssertThat(ts_node_name(leaf, document), Equals("string"));
AssertThat(ts_node_pos(leaf).bytes, Equals(index));
AssertThat(ts_node_size(leaf).bytes, Equals<size_t>(3));
AssertThat(ts_node_start_byte(leaf), Equals(string_index));
AssertThat(ts_node_end_byte(leaf), Equals(string_end_index));
AssertThat(ts_node_start_point(leaf), Equals<TSPoint>({ 6, 4 }));
AssertThat(ts_node_end_point(leaf), Equals<TSPoint>({ 6, 7 }));
leaf = ts_node_named_descendent_for_range(array_node, index + 1, index + 2);
leaf = ts_node_named_descendant_for_range(array_node, string_index + 1, string_index + 2);
AssertThat(ts_node_name(leaf, document), Equals("string"));
AssertThat(ts_node_pos(leaf).bytes, Equals<size_t>(index));
AssertThat(ts_node_size(leaf).bytes, Equals<size_t>(3));
AssertThat(ts_node_start_byte(leaf), Equals(string_index));
AssertThat(ts_node_end_byte(leaf), Equals(string_end_index));
AssertThat(ts_node_start_point(leaf), Equals<TSPoint>({ 6, 4 }));
AssertThat(ts_node_end_point(leaf), Equals<TSPoint>({ 6, 7 }));
});
@ -290,49 +311,38 @@ describe("Node", []() {
describe("when there is no leaf node that spans the given range", [&]() {
it("returns the smallest node that does span the range", [&]() {
size_t index = input_string.find("\"x\"");
TSNode node = ts_node_named_descendent_for_range(array_node, index, index + 3);
TSNode node = ts_node_named_descendant_for_range(array_node, string_index, string_index + 3);
AssertThat(ts_node_name(node, document), Equals("object"));
size_t object_index = input_string.find("{");
AssertThat(ts_node_pos(node).bytes, Equals<size_t>(object_index));
AssertThat(ts_node_size(node).bytes, Equals<size_t>(19));
AssertThat(ts_node_start_byte(node), Equals(object_index));
AssertThat(ts_node_end_byte(node), Equals(object_end_index));
AssertThat(ts_node_start_point(node), Equals<TSPoint>({ 5, 2 }));
AssertThat(ts_node_end_point(node), Equals<TSPoint>({ 7, 3 }));
});
it("does not return invisible nodes (repeats)", [&]() {
size_t comma_index = input_string.find(",");
TSNode node = ts_node_named_descendent_for_range(array_node, comma_index, comma_index + 1);
TSNode node = ts_node_named_descendant_for_range(array_node, number_end_index, number_end_index + 1);
AssertThat(ts_node_name(node, document), Equals("array"));
size_t array_index = input_string.find("[");
AssertThat(ts_node_pos(node).bytes, Equals<size_t>(array_index));
AssertThat(ts_node_size(node).bytes, Equals<size_t>(41));
AssertThat(ts_node_start_byte(node), Equals(array_index));
AssertThat(ts_node_end_byte(node), Equals(array_end_index));
AssertThat(ts_node_start_point(node), Equals<TSPoint>({ 2, 0 }));
AssertThat(ts_node_end_point(node), Equals<TSPoint>({ 8, 1 }));
});
});
});
describe("descendent_for_range(start, end)", [&]() {
describe("descendant_for_range(start, end)", [&]() {
it("returns the smallest concrete node that spans the given range", [&]() {
size_t colon_index = input_string.find(":");
TSNode node1 = ts_node_descendent_for_range(array_node, colon_index, colon_index);
TSNode node1 = ts_node_descendant_for_range(array_node, colon_index, colon_index);
AssertThat(ts_node_name(node1, document), Equals(":"));
AssertThat(ts_node_pos(node1).bytes, Equals<size_t>(colon_index));
AssertThat(ts_node_size(node1).bytes, Equals<size_t>(1));
AssertThat(ts_node_start_byte(node1), Equals(colon_index));
AssertThat(ts_node_end_byte(node1), Equals(colon_index + 1));
AssertThat(ts_node_start_point(node1), Equals<TSPoint>({ 6, 7 }));
AssertThat(ts_node_end_point(node1), Equals<TSPoint>({ 6, 8 }));
size_t index = input_string.find("\":");
TSNode node2 = ts_node_descendent_for_range(array_node, index, index + 2);
TSNode node2 = ts_node_descendant_for_range(array_node, string_index + 2, string_index + 4);
AssertThat(ts_node_name(node2, document), Equals("object"));
size_t object_index = input_string.find("{");
AssertThat(ts_node_pos(node2).bytes, Equals<size_t>(object_index));
AssertThat(ts_node_size(node2).bytes, Equals<size_t>(19));
AssertThat(ts_node_start_byte(node2), Equals(object_index));
AssertThat(ts_node_end_byte(node2), Equals(object_end_index));
AssertThat(ts_node_start_point(node2), Equals<TSPoint>({ 5, 2 }));
AssertThat(ts_node_end_point(node2), Equals<TSPoint>({ 7, 3 }));
});

View file

@ -32,38 +32,38 @@ describe("Parser", [&]() {
ts_document_parse(doc);
root = ts_document_root_node(doc);
AssertThat(ts_node_size(root).bytes + ts_node_pos(root).bytes, Equals(strlen(text)));
AssertThat(ts_node_end_byte(root), Equals(strlen(text)));
input->clear();
};
auto insert_text = [&](size_t position, string text) {
size_t prev_size = ts_node_size(root).bytes + ts_node_pos(root).bytes;
size_t prev_size = ts_node_end_byte(root);
ts_document_edit(doc, input->replace(position, 0, text));
ts_document_parse(doc);
root = ts_document_root_node(doc);
size_t new_size = ts_node_size(root).bytes + ts_node_pos(root).bytes;
size_t new_size = ts_node_end_byte(root);
AssertThat(new_size, Equals(prev_size + text.size()));
};
auto delete_text = [&](size_t position, size_t length) {
size_t prev_size = ts_node_size(root).bytes + ts_node_pos(root).bytes;
size_t prev_size = ts_node_end_byte(root);
ts_document_edit(doc, input->replace(position, length, ""));
ts_document_parse(doc);
root = ts_document_root_node(doc);
size_t new_size = ts_node_size(root).bytes + ts_node_pos(root).bytes;
size_t new_size = ts_node_end_byte(root);
AssertThat(new_size, Equals(prev_size - length));
};
auto replace_text = [&](size_t position, size_t length, string new_text) {
size_t prev_size = ts_node_size(root).bytes + ts_node_pos(root).bytes;
size_t prev_size = ts_node_end_byte(root);
ts_document_edit(doc, input->replace(position, length, new_text));
ts_document_parse(doc);
root = ts_document_root_node(doc);
size_t new_size = ts_node_size(root).bytes + ts_node_pos(root).bytes;
size_t new_size = ts_node_end_byte(root);
AssertThat(new_size, Equals(prev_size - length + new_text.size()));
};
@ -83,11 +83,11 @@ describe("Parser", [&]() {
TSNode last = ts_node_named_child(root, 2);
AssertThat(ts_node_name(error, doc), Equals("ERROR"));
AssertThat(ts_node_pos(error).bytes, Equals(strlen(" [123, ")))
AssertThat(ts_node_size(error).bytes, Equals(strlen("@@@@@")))
AssertThat(ts_node_start_byte(error), Equals(strlen(" [123, ")));
AssertThat(ts_node_end_byte(error), Equals(strlen(" [123, @@@@@")));
AssertThat(ts_node_name(last, doc), Equals("true"));
AssertThat(ts_node_pos(last).bytes, Equals(strlen(" [123, @@@@@, ")))
AssertThat(ts_node_start_byte(last), Equals(strlen(" [123, @@@@@, ")))
});
});
@ -104,11 +104,11 @@ describe("Parser", [&]() {
AssertThat(ts_node_symbol(error), Equals(ts_builtin_sym_error));
AssertThat(ts_node_name(error, doc), Equals("ERROR"));
AssertThat(ts_node_pos(error).bytes, Equals(strlen(" [123, ")))
AssertThat(ts_node_size(error).bytes, Equals(strlen("faaaaalse")))
AssertThat(ts_node_start_byte(error), Equals(strlen(" [123, ")))
AssertThat(ts_node_end_byte(error), Equals(strlen(" [123, faaaaalse")))
AssertThat(ts_node_name(last, doc), Equals("true"));
AssertThat(ts_node_pos(last).bytes, Equals(strlen(" [123, faaaaalse, ")));
AssertThat(ts_node_start_byte(last), Equals(strlen(" [123, faaaaalse, ")));
});
});
@ -123,11 +123,11 @@ describe("Parser", [&]() {
TSNode last = ts_node_named_child(root, 2);
AssertThat(ts_node_name(error, doc), Equals("ERROR"));
AssertThat(ts_node_pos(error).bytes, Equals(strlen(" [123, ")));
AssertThat(ts_node_size(error).bytes, Equals(strlen("true false")));
AssertThat(ts_node_start_byte(error), Equals(strlen(" [123, ")));
AssertThat(ts_node_end_byte(error), Equals(strlen(" [123, true false")));
AssertThat(ts_node_name(last, doc), Equals("true"));
AssertThat(ts_node_pos(last).bytes, Equals(strlen(" [123, true false, ")));
AssertThat(ts_node_start_byte(last), Equals(strlen(" [123, true false, ")));
});
});
@ -142,11 +142,11 @@ describe("Parser", [&]() {
TSNode last = ts_node_named_child(root, 2);
AssertThat(ts_node_name(error, doc), Equals("ERROR"));
AssertThat(ts_node_pos(error).bytes, Equals(strlen(" [123, ")));
AssertThat(ts_node_size(error).bytes, Equals<size_t>(0))
AssertThat(ts_node_start_byte(error), Equals(strlen(" [123, ")));
AssertThat(ts_node_end_byte(error), Equals(strlen(" [123, ")))
AssertThat(ts_node_name(last, doc), Equals("true"));
AssertThat(ts_node_pos(last).bytes, Equals(strlen(" [123, , ")));
AssertThat(ts_node_start_byte(last), Equals(strlen(" [123, , ")));
});
});
});
@ -293,9 +293,9 @@ describe("Parser", [&]() {
AssertThat(ts_node_string(root, doc), Equals(
"(program (product (variable) (number)))"));
TSNode node = ts_node_named_descendent_for_range(root, 1, 1);
TSNode node = ts_node_named_descendant_for_range(root, 1, 1);
AssertThat(ts_node_name(node, doc), Equals("variable"));
AssertThat(ts_node_size(node).bytes, Equals(strlen("abXYZc")));
AssertThat(ts_node_end_byte(node), Equals(strlen("abXYZc")));
});
});
@ -313,9 +313,9 @@ describe("Parser", [&]() {
AssertThat(ts_node_string(root, doc), Equals(
"(program (product (variable) (number)))"));
TSNode node = ts_node_named_descendent_for_range(root, 1, 1);
TSNode node = ts_node_named_descendant_for_range(root, 1, 1);
AssertThat(ts_node_name(node, doc), Equals("variable"));
AssertThat(ts_node_size(node).bytes, Equals(strlen("abcXYZ")));
AssertThat(ts_node_end_byte(node), Equals(strlen("abcXYZ")));
});
});
@ -422,7 +422,8 @@ describe("Parser", [&]() {
TSNode comment = ts_node_named_child(root, 1);
AssertThat(ts_node_size(comment).bytes, Equals(strlen("# this is a comment")));
AssertThat(ts_node_start_byte(comment), Equals(strlen("x ")));
AssertThat(ts_node_end_byte(comment), Equals(strlen("x # this is a comment")));
});
});
@ -433,8 +434,8 @@ describe("Parser", [&]() {
AssertThat(ts_node_string(root, doc), Equals(
"(program (variable) (comment))"));
AssertThat(ts_node_size(root).chars, Equals(strlen("x # OOO - DD")));
AssertThat(ts_node_size(root).bytes, Equals(strlen("x # \u03A9\u03A9\u03A9 \u2014 \u0394\u0394")));
AssertThat(ts_node_end_char(root), Equals(strlen("x # OOO - DD")));
AssertThat(ts_node_end_byte(root), Equals(strlen("x # \u03A9\u03A9\u03A9 \u2014 \u0394\u0394")));
});
});
});

View file

@ -128,7 +128,7 @@ static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous)
return ts_node__null();
}
static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min,
static inline TSNode ts_node__descendant_for_range(TSNode self, size_t min,
size_t max,
bool include_anonymous) {
const TSTree *tree = ts_node__tree(self), *last_visible_tree = tree;
@ -165,12 +165,20 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min,
* Public
*/
TSLength ts_node_pos(TSNode self) {
return ts_length_add(ts_node__offset(self), ts_node__tree(self)->padding);
size_t ts_node_start_char(TSNode self) {
return ts_node__offset(self).chars + ts_node__tree(self)->padding.chars;
}
TSLength ts_node_size(TSNode self) {
return ts_node__tree(self)->size;
size_t ts_node_end_char(TSNode self) {
return ts_node_start_char(self) + ts_node__tree(self)->size.chars;
}
size_t ts_node_start_byte(TSNode self) {
return ts_node__offset(self).bytes + ts_node__tree(self)->padding.bytes;
}
size_t ts_node_end_byte(TSNode self) {
return ts_node_start_byte(self) + ts_node__tree(self)->size.bytes;
}
TSPoint ts_node_size_point(TSNode self) {
@ -262,10 +270,10 @@ TSNode ts_node_prev_named_sibling(TSNode self) {
return ts_node__prev_sibling(self, false);
}
TSNode ts_node_descendent_for_range(TSNode self, size_t min, size_t max) {
return ts_node__descendent_for_range(self, min, max, true);
TSNode ts_node_descendant_for_range(TSNode self, size_t min, size_t max) {
return ts_node__descendant_for_range(self, min, max, true);
}
TSNode ts_node_named_descendent_for_range(TSNode self, size_t min, size_t max) {
return ts_node__descendent_for_range(self, min, max, false);
TSNode ts_node_named_descendant_for_range(TSNode self, size_t min, size_t max) {
return ts_node__descendant_for_range(self, min, max, false);
}