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:
Max Brunsfeld 2014-02-20 13:30:43 -08:00
parent 1b56832cb7
commit 6ed6aa05cc
11 changed files with 579 additions and 575 deletions

View file

@ -86,9 +86,9 @@ namespace tree_sitter {
string symbol_id(rules::Symbol symbol) {
if (symbol.is_auxiliary())
return "ts_aux_" + symbol.name;
return "ts_aux_sym_" + symbol.name;
else
return "ts_symbol_" + symbol.name;
return "ts_sym_" + symbol.name;
}
string character_code(char character) {
@ -232,14 +232,14 @@ namespace tree_sitter {
}
string symbol_enum() {
string result = "enum ts_symbol {\n";
string result = "enum {\n";
for (auto symbol : parse_table.symbols)
result += indent(symbol_id(symbol)) + ",\n";
return result + "};";
}
string rule_names_list() {
string result = "static const char *ts_symbol_names[] = {\n";
string result = "SYMBOL_NAMES {\n";
for (auto symbol : parse_table.symbols)
result += indent(string("\"") + symbol.name) + "\",\n";
return result + "};";
@ -251,7 +251,7 @@ namespace tree_sitter {
string lex_function() {
return join({
"static void ts_lex(TSParser *parser) {",
"LEX_FN() {",
indent("START_LEXER();"),
indent(switch_on_lex_state()),
indent("FINISH_LEXER();"),
@ -261,7 +261,7 @@ namespace tree_sitter {
string parse_function() {
return join({
"static TSParseResult ts_parse(const char *input) {",
"PARSE_FN() {",
indent("START_PARSER();"),
indent(switch_on_parse_state()),
indent("FINISH_PARSER();"),
@ -270,12 +270,7 @@ namespace tree_sitter {
}
string parse_config_struct() {
return join({
"TSParseConfig ts_parse_config_" + name + " = {",
indent(".parse_fn = ts_parse,"),
indent(".symbol_names = ts_symbol_names"),
"};"
});
return "EXPORT_PARSER(ts_parse_config_" + name + ");";
}
string code() {

View file

@ -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);
}
}

View file

@ -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]) + " ";

View file

@ -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());