diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index a52637f4..ece23a10 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -46,7 +46,6 @@ void ts_document_set_language(TSDocument *, const TSLanguage *); void ts_document_set_input(TSDocument *, TSInput); void ts_document_set_input_string(TSDocument *, const char *); void ts_document_edit(TSDocument *, TSInputEdit); -const char *ts_document_string(const TSDocument *); TSNode *ts_document_root_node(const TSDocument *); #define ts_builtin_sym_error 0 diff --git a/spec/runtime/document_spec.cc b/spec/runtime/document_spec.cc index d9337dc0..795b8edf 100644 --- a/spec/runtime/document_spec.cc +++ b/spec/runtime/document_spec.cc @@ -40,10 +40,8 @@ describe("Document", [&]() { }); it("parses the document", [&]() { - TSNode *node = ts_document_root_node(doc); - AssertThat(string(ts_node_string(node)), Equals( + AssertThat(string(ts_node_string(ts_document_root_node(doc))), Equals( "(object (string) (array (number) (number)))")); - ts_node_release(node); }); }); }); @@ -63,7 +61,7 @@ describe("Document", [&]() { }); it("parses the input", [&]() { - AssertThat(string(ts_document_string(doc)), Equals( + AssertThat(string(ts_node_string(ts_document_root_node(doc))), Equals( "(object (string) (array (number) (number)))")); }); @@ -83,7 +81,7 @@ describe("Document", [&]() { }); it("updates the parse tree", [&]() { - AssertThat(string(ts_document_string(doc)), Equals( + AssertThat(string(ts_node_string(ts_document_root_node(doc))), Equals( "(object (string) (array (number) (number)) (string) (number))")); }); @@ -103,7 +101,7 @@ describe("Document", [&]() { }); it("updates the parse tree", [&]() { - AssertThat(string(ts_document_string(doc)), Equals( + AssertThat(string(ts_node_string(ts_document_root_node(doc))), Equals( "(object (string) (number) (string) (array (number) (number)))")); }); diff --git a/spec/runtime/languages/language_specs.cc b/spec/runtime/languages/language_specs.cc index f0956469..1c147693 100644 --- a/spec/runtime/languages/language_specs.cc +++ b/spec/runtime/languages/language_specs.cc @@ -28,7 +28,7 @@ describe("Languages", [&]() { for (auto &entry : test_entries_for_language(language_name)) { it(entry.description.c_str(), [&]() { ts_document_set_input_string(doc, entry.input.c_str()); - auto doc_string = ts_document_string(doc); + auto doc_string = ts_node_string(ts_document_root_node(doc)); AssertThat(doc_string, Equals(entry.tree_string.c_str())); free((void *)doc_string); }); diff --git a/spec/runtime/node_spec.cc b/spec/runtime/node_spec.cc index fc7bb391..6cc2ce8e 100644 --- a/spec/runtime/node_spec.cc +++ b/spec/runtime/node_spec.cc @@ -19,7 +19,6 @@ describe("Node", []() { after_each([&]() { ts_document_free(document); - ts_node_release(root); }); describe("child_count", [&]() { diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index edfabba3..d3538991 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -235,7 +235,7 @@ class CCodeGenerator { return lookahead + " == " + escape_char(range.min); } else { return escape_char(range.min) + string(" <= ") + lookahead + " && " + - lookahead + " <= " + escape_char(range.max); + lookahead + " <= " + escape_char(range.max); } } diff --git a/src/runtime/document.c b/src/runtime/document.c index 0f8a95af..9d4d9019 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -1,22 +1,17 @@ -#include "tree_sitter/runtime.h" #include "tree_sitter/parser.h" -#include "runtime/tree.h" #include "runtime/node.h" #include "runtime/parser.h" #include "runtime/string_input.h" struct TSDocument { TSParser parser; - const TSTree *tree; TSInput input; + TSNode *node; }; TSDocument *ts_document_make() { TSDocument *document = malloc(sizeof(TSDocument)); - *document = (TSDocument) { - .input = (TSInput) { .data = NULL, .read_fn = NULL, .seek_fn = NULL }, - .tree = NULL - }; + *document = (TSDocument) {}; return document; } @@ -24,34 +19,34 @@ void ts_document_free(TSDocument *document) { ts_parser_destroy(&document->parser); if (document->input.release_fn) document->input.release_fn(document->input.data); + if (document->node) + ts_node_release(document->node); free(document); } +static void reparse(TSDocument *document, TSInputEdit *edit) { + if (document->input.read_fn && document->parser.language) { + const TSTree *tree = ts_parser_parse(&document->parser, document->input, edit); + if (document->node) + ts_node_release(document->node); + document->node = ts_node_make_root(tree, document->parser.language->symbol_names); + } +} + void ts_document_set_language(TSDocument *document, const TSLanguage *language) { ts_parser_destroy(&document->parser); document->parser = ts_parser_make(language); - if (document->input.read_fn) - document->tree = ts_parser_parse(&document->parser, document->input, NULL); -} - -const TSTree *ts_document_tree(const TSDocument *document) { - return document->tree; -} - -const char *ts_document_string(const TSDocument *document) { - return ts_tree_string(document->tree, - document->parser.language->symbol_names); + reparse(document, NULL); } void ts_document_set_input(TSDocument *document, TSInput input) { document->input = input; - if (document->parser.language) - document->tree = ts_parser_parse(&document->parser, document->input, NULL); + reparse(document, NULL); } void ts_document_edit(TSDocument *document, TSInputEdit edit) { - document->tree = ts_parser_parse(&document->parser, document->input, &edit); + reparse(document, &edit); } const char *ts_document_symbol_name(const TSDocument *document, @@ -64,16 +59,5 @@ void ts_document_set_input_string(TSDocument *document, const char *text) { } TSNode *ts_document_root_node(const TSDocument *document) { - if (document->tree) - return ts_node_make_root(document->tree, - document->parser.language->symbol_names); - else - return NULL; -} - -TSNode *ts_document_get_node(const TSDocument *document, size_t pos) { - TSNode *root = ts_document_root_node(document); - TSNode *result = ts_node_leaf_at_pos(root, pos); - ts_node_release(root); - return result; + return document->node; }