Update runtime code naming
Also, add macros so that code generator doesn't need to know about any runtime variables names or types
This commit is contained in:
parent
1b56832cb7
commit
6ed6aa05cc
11 changed files with 579 additions and 575 deletions
|
|
@ -1,37 +1,37 @@
|
|||
#include "tree_sitter/runtime.h"
|
||||
|
||||
struct TSDocument {
|
||||
TSParseFn *parse_fn;
|
||||
struct ts_document {
|
||||
ts_parse_fn *parse_fn;
|
||||
const char **symbol_names;
|
||||
const char *text;
|
||||
TSParseError error;
|
||||
TSTree *tree;
|
||||
ts_error error;
|
||||
ts_tree *tree;
|
||||
};
|
||||
|
||||
TSDocument * TSDocumentMake() {
|
||||
return new TSDocument();
|
||||
ts_document * ts_document_make() {
|
||||
return new ts_document();
|
||||
}
|
||||
|
||||
void TSDocumentSetUp(TSDocument *document, TSParseConfig config) {
|
||||
void ts_document_set_parser(ts_document *document, ts_parse_config config) {
|
||||
document->parse_fn = config.parse_fn;
|
||||
document->symbol_names = config.symbol_names;
|
||||
}
|
||||
|
||||
void TSDocumentSetText(TSDocument *document, const char *text) {
|
||||
TSParseResult result = document->parse_fn(text);
|
||||
void ts_document_set_text(ts_document *document, const char *text) {
|
||||
ts_parse_result result = document->parse_fn(text);
|
||||
document->text = text;
|
||||
document->tree = result.tree;
|
||||
document->error = result.error;
|
||||
}
|
||||
|
||||
TSTree * TSDocumentTree(const TSDocument *document) {
|
||||
ts_tree * ts_document_tree(const ts_document *document) {
|
||||
return document->tree;
|
||||
}
|
||||
|
||||
const char * TSDocumentToString(const TSDocument *document) {
|
||||
const char * ts_document_string(const ts_document *document) {
|
||||
if (document->error.expected_inputs != NULL) {
|
||||
return TSParseErrorToString(&document->error, document->text, document->symbol_names);
|
||||
return ts_error_string(&document->error, document->text, document->symbol_names);
|
||||
} else {
|
||||
return TSTreeToString(document->tree, document->symbol_names);
|
||||
return ts_tree_string(document->tree, document->symbol_names);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
using std::string;
|
||||
|
||||
const char * TSParseErrorToString(const TSParseError *error, const char *input_string, const char **symbol_names) {
|
||||
const char * ts_error_string(const ts_error *error, const char *input_string, const char **symbol_names) {
|
||||
string result = string("Unexpected character '") + input_string[error->position] + "'. Expected: ";
|
||||
for (int i = 0; i < error->expected_input_count; i++)
|
||||
result += string(error->expected_inputs[i]) + " ";
|
||||
|
|
|
|||
|
|
@ -4,42 +4,42 @@
|
|||
|
||||
using std::string;
|
||||
|
||||
TSTree * TSTreeMake(TSSymbol value, size_t child_count, TSTree **children) {
|
||||
TSTree *result = new TSTree();
|
||||
ts_tree * ts_tree_make(ts_symbol value, size_t child_count, ts_tree **children) {
|
||||
ts_tree *result = new ts_tree();
|
||||
result->value = value;
|
||||
result->child_count = child_count;
|
||||
result->children = children;
|
||||
result->ref_count = 0;
|
||||
for (int i = 0; i < child_count; i++)
|
||||
TSTreeRetain(children[i]);
|
||||
ts_tree_retain(children[i]);
|
||||
return result;
|
||||
}
|
||||
|
||||
void TSTreeRetain(TSTree *tree) {
|
||||
void ts_tree_retain(ts_tree *tree) {
|
||||
tree->ref_count++;
|
||||
}
|
||||
|
||||
void TSTreeRelease(TSTree *tree) {
|
||||
void ts_tree_release(ts_tree *tree) {
|
||||
tree->ref_count--;
|
||||
if (tree->ref_count == 0) {
|
||||
for (int i = 0; i < tree->child_count; i++)
|
||||
TSTreeRelease(tree->children[i]);
|
||||
ts_tree_release(tree->children[i]);
|
||||
free(tree);
|
||||
}
|
||||
}
|
||||
|
||||
int TSTreeEquals(const TSTree *node1, const TSTree *node2) {
|
||||
int ts_tree_equals(const ts_tree *node1, const ts_tree *node2) {
|
||||
if (node1->value != node2->value) return 0;
|
||||
if (node1->child_count != node2->child_count) return 0;
|
||||
for (int i = 0; i < node1->child_count; i++) {
|
||||
TSTree *child1 = node1->children[i];
|
||||
TSTree *child2 = node2->children[i];
|
||||
if (!TSTreeEquals(child1, child2)) return 0;
|
||||
ts_tree *child1 = node1->children[i];
|
||||
ts_tree *child2 = node2->children[i];
|
||||
if (!ts_tree_equals(child1, child2)) return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static string __tree_to_string(const TSTree *tree, const char **symbol_names) {
|
||||
static string __tree_to_string(const ts_tree *tree, const char **symbol_names) {
|
||||
if (!tree) return "#<null-tree>";
|
||||
string result = string("(") + symbol_names[tree->value];
|
||||
for (int i = 0; i < tree->child_count; i++)
|
||||
|
|
@ -47,7 +47,7 @@ static string __tree_to_string(const TSTree *tree, const char **symbol_names) {
|
|||
return result + ")";
|
||||
}
|
||||
|
||||
char * TSTreeToString(const TSTree *tree, const char **symbol_names) {
|
||||
char * ts_tree_string(const ts_tree *tree, const char **symbol_names) {
|
||||
string value(__tree_to_string(tree, symbol_names));
|
||||
char *result = (char *)malloc(value.size());
|
||||
strcpy(result, value.c_str());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue