Remove redundant functions for Documents
There's no need for a `string` function since one already exists for Nodes. Now the root node is always stored on the document. This means callers of `ts_document_root_node` don't need to release its return value.
This commit is contained in:
parent
7ba3953f7e
commit
8da9219c3a
6 changed files with 23 additions and 43 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)))"));
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ describe("Node", []() {
|
|||
|
||||
after_each([&]() {
|
||||
ts_document_free(document);
|
||||
ts_node_release(root);
|
||||
});
|
||||
|
||||
describe("child_count", [&]() {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue