Hide TSTree, expose TSNode

This commit is contained in:
Max Brunsfeld 2014-07-17 23:29:11 -07:00
parent 02904085c2
commit b3385f20c8
13 changed files with 333 additions and 122 deletions

View file

@ -1,5 +1,6 @@
#include "runtime/runtime_spec_helper.h"
#include "runtime/helpers/spy_reader.h"
#include "runtime/tree.h"
extern "C" TSParser * ts_parser_json();
@ -7,72 +8,109 @@ START_TEST
describe("incremental parsing", [&]() {
TSDocument *doc;
SpyReader *reader;
before_each([&]() {
doc = ts_document_make();
ts_document_set_parser(doc, ts_parser_json());
reader = new SpyReader("{ \"key\": [1, 2] }", 5);
ts_document_set_input(doc, reader->input);
});
after_each([&]() {
ts_document_free(doc);
delete reader;
});
it("parses the input", [&]() {
AssertThat(string(ts_document_string(doc)), Equals(
"(object (string) (array (number) (number)))"));
});
describe("incremental parsing", [&]() {
SpyReader *reader;
it("reads the entire input", [&]() {
AssertThat(reader->strings_read, Equals(vector<string>({
"{ \"key\": [1, 2] }"
})));
});
describe("modifying the end of the input", [&]() {
before_each([&]() {
size_t position(string("{ \"key\": [1, 2]").length());
string inserted_text(", \"key2\": 4");
reader->content.insert(position, inserted_text);
ts_document_edit(doc, { position, 0, inserted_text.length() });
reader = new SpyReader("{ \"key\": [1, 2] }", 5);
ts_document_set_input(doc, reader->input);
});
it("updates the parse tree", [&]() {
after_each([&]() {
delete reader;
});
it("parses the input", [&]() {
AssertThat(string(ts_document_string(doc)), Equals(
"(object (string) (array (number) (number)) (string) (number))"));
"(object (string) (array (number) (number)))"));
});
it("re-reads only the changed portion of the input", [&]() {
AssertThat(reader->strings_read.size(), Equals<size_t>(2));
AssertThat(reader->strings_read[1], Equals(", \"key2\": 4 }"));
it("reads the entire input", [&]() {
AssertThat(reader->strings_read, Equals(vector<string>({
"{ \"key\": [1, 2] }"
})));
});
describe("modifying the end of the input", [&]() {
before_each([&]() {
size_t position(string("{ \"key\": [1, 2]").length());
string inserted_text(", \"key2\": 4");
reader->content.insert(position, inserted_text);
ts_document_edit(doc, { position, 0, inserted_text.length() });
});
it("updates the parse tree", [&]() {
AssertThat(string(ts_document_string(doc)), Equals(
"(object (string) (array (number) (number)) (string) (number))"));
});
it("re-reads only the changed portion of the input", [&]() {
AssertThat(reader->strings_read.size(), Equals<size_t>(2));
AssertThat(reader->strings_read[1], Equals(", \"key2\": 4 }"));
});
});
describe("modifying the beginning of the input", [&]() {
before_each([&]() {
size_t position(string("{ ").length());
string inserted_text("\"key2\": 4, ");
reader->content.insert(position, inserted_text);
ts_document_edit(doc, { position, 0, inserted_text.length() });
});
it("updates the parse tree", [&]() {
AssertThat(string(ts_document_string(doc)), Equals(
"(object (string) (number) (string) (array (number) (number)))"));
});
it_skip("re-reads only the changed portion of the input", [&]() {
AssertThat(reader->strings_read.size(), Equals<size_t>(2));
AssertThat(reader->strings_read[1], Equals("\"key2\": 4, "));
});
});
});
describe("modifying the beginning of the input", [&]() {
before_each([&]() {
size_t position(string("{ ").length());
string inserted_text("\"key2\": 4, ");
it("records the widths and offsets of nodes", [&]() {
ts_document_set_input_string(doc, " [12, 5, 345]");
reader->content.insert(position, inserted_text);
ts_document_edit(doc, { position, 0, inserted_text.length() });
});
TSNode *array = ts_document_root_node(doc);
TSNode *number1 = ts_node_child(array, 0);
TSNode *number2 = ts_node_child(array, 1);
TSNode *number3 = ts_node_child(array, 2);
it("2 updates the parse tree", [&]() {
AssertThat(string(ts_document_string(doc)), Equals(
"(object (string) (number) (string) (array (number) (number)))"));
});
AssertThat(ts_node_name(array), Equals("array"));
AssertThat(ts_node_name(number1), Equals("number"));
AssertThat(ts_node_name(number2), Equals("number"));
it_skip("re-reads only the changed portion of the input", [&]() {
AssertThat(reader->strings_read.size(), Equals<size_t>(2));
AssertThat(reader->strings_read[1], Equals("\"key2\": 4, "));
});
AssertThat(ts_node_pos(array), Equals<size_t>(2));
AssertThat(ts_node_size(array), Equals<size_t>(12));
AssertThat(ts_node_pos(number1), Equals<size_t>(3));
AssertThat(ts_node_size(number1), Equals<size_t>(2));
AssertThat(ts_node_pos(number2), Equals<size_t>(7));
AssertThat(ts_node_size(number2), Equals<size_t>(1));
AssertThat(ts_node_pos(number3), Equals<size_t>(10));
AssertThat(ts_node_size(number3), Equals<size_t>(3));
ts_node_release(array);
ts_node_release(number1);
ts_node_release(number2);
ts_node_release(number3);
});
});
END_TEST

View file

@ -1,10 +1,9 @@
#ifndef HELPERS_TREE_HELPERS_H_
#define HELPERS_TREE_HELPERS_H_
#include "tree_sitter/runtime.h"
#include "tree_sitter/parser.h"
#include <vector>
TSTree ** tree_array(std::vector<TSTree *> trees);
#endif // HELPERS_TREE_HELPERS_H_
#endif // HELPERS_TREE_HELPERS_H_

View file

@ -1,46 +0,0 @@
#include "runtime/runtime_spec_helper.h"
#include "runtime/tree.h"
extern "C" TSParser * ts_parser_json();
START_TEST
describe("tracking the positions of AST nodes", []() {
TSDocument *doc;
before_each([&]() {
doc = ts_document_make();
ts_document_set_parser(doc, ts_parser_json());
});
after_each([&]() {
ts_document_free(doc);
});
it("records the widths and offsets of nodes", [&]() {
ts_document_set_input_string(doc, " [12, 5]");
const TSTree *tree = ts_document_tree(doc);
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"));
AssertThat(ts_document_symbol_name(doc, number2), Equals("number"));
AssertThat(number1->offset, Equals<size_t>(0));
AssertThat(number1->size, Equals<size_t>(2));
AssertThat(number2->offset, Equals<size_t>(1));
AssertThat(number2->size, Equals<size_t>(1));
AssertThat(array->offset, Equals<size_t>(2));
AssertThat(array->size, Equals<size_t>(7));
AssertThat(tree->offset, Equals<size_t>(2));
AssertThat(tree->size, Equals<size_t>(7));
});
});
END_TEST

53
spec/runtime/node_spec.cc Normal file
View file

@ -0,0 +1,53 @@
#include "runtime/runtime_spec_helper.h"
extern "C" TSParser * ts_parser_arithmetic();
START_TEST
describe("Node", []() {
TSDocument *document;
before_each([&]() {
document = ts_document_make();
ts_document_set_parser(document, ts_parser_arithmetic());
});
after_each([&]() {
ts_document_free(document);
});
describe("getting the nth child node", [&]() {
TSNode *root;
describe("when the child has more than n visible children", [&]() {
before_each([&]() {
ts_document_set_input_string(document, "x + 1");
root = ts_document_root_node(document);
AssertThat(ts_node_name(root), Equals("sum"));
AssertThat(ts_node_string(root), Equals("(sum (variable) (number))"));
});
after_each([&]() {
ts_node_release(root);
});
it("returns the nth child", [&]() {
TSNode *child1 = ts_node_child(root, 0);
AssertThat(ts_node_name(child1), Equals("variable"));
TSNode *child2 = ts_node_child(root, 1);
AssertThat(ts_node_name(child2), Equals("number"));
ts_node_release(child1);
ts_node_release(child2);
});
});
});
it("gets the first token", [&]() {
// ts_document_get_node(document, 0);
});
});
END_TEST