Add runtime specs

This commit is contained in:
Max Brunsfeld 2013-12-17 13:14:41 -08:00
parent 9618efd12a
commit 3417ad5adb
11 changed files with 542 additions and 294 deletions

View file

@ -30,21 +30,18 @@ namespace tree_sitter {
string _switch(string condition, string body) {
return "switch (" + condition + ") {\n" +
indent(body) + "\n" +
indent(body) +
"}";
}
string _case(string value, string body) {
return "case " + value + ": {\n" +
indent(body) + "\n" +
indent("break;") + "\n"
"}\n";
return "case " + value + ":\n" +
indent(body) + "\n";
}
string _default(string body) {
return "default: {\n" +
indent(body) + "\n"
"}";
return "default:\n" +
indent(body) + "\n";
}
class CCodeGenerator {
@ -99,7 +96,7 @@ namespace tree_sitter {
string body = "";
for (int i = 0; i < parse_table.states.size(); i++)
body += _case(std::to_string(i), switch_on_lookahead(parse_table.states[i]));
body += _default("ERROR()");
body += _default("ERROR();");
return _switch("PARSE_STATE()", body);
}
@ -114,18 +111,16 @@ namespace tree_sitter {
string parse_function() {
return
"TSTree * ts_parse_arithmetic() {\n" +
indent("SETUP_PARSER()") + "\n" +
"start:\n" +
"TSTree ts_parse_arithmetic(const char *input) {\n" +
indent("START_PARSER();") + "\n" +
indent(switch_on_current_state(parse_table)) + "\n" +
"done:\n" +
indent("return PARSE_TREE();") + "\n"
indent("FINISH_PARSER();") + "\n"
"}";
}
string code() {
return
"#include <tree_sitter/runtime.h>\n"
"#include \"runtime.h\"\n"
"#include <stdlib.h>\n"
"\n\n" +
symbol_enum() +

42
src/runtime/parser.c Normal file
View file

@ -0,0 +1,42 @@
#include "runtime.h"
static int INITIAL_STATE_STACK_SIZE = 100;
static int INITIAL_SYMBOL_STACK_SIZE = 100;
TSParser TSParserMake(const char *input) {
TSState *state_stack = calloc(INITIAL_STATE_STACK_SIZE, sizeof(*state_stack));
TSSymbol *symbol_stack = calloc(INITIAL_SYMBOL_STACK_SIZE, sizeof(*symbol_stack));
TSParser result = {
.input = input,
.tree = TSTreeMake(),
.position = 0,
.state_stack = state_stack,
.symbol_stack = symbol_stack,
.state_count = 0,
.symbol_count = 0
};
return result;
}
void TSParserShift(TSParser *parser, TSState state) {
}
void TSParserReduce(TSParser *parser, TSSymbol symbol, int child_count) {
}
void TSParserError(TSParser *parser) {
}
TSSymbol TSParserLookahead(const TSParser *parser) {
return 1;
}
TSState TSParserState(const TSParser *parser) {
return 5;
}

9
src/runtime/tree.c Normal file
View file

@ -0,0 +1,9 @@
#include "runtime.h"
TSTree TSTreeMake() {
TSTree result = {
};
return result;
}