Add plain C API for compiling a JSON grammar

This commit is contained in:
Max Brunsfeld 2016-01-10 13:44:22 -08:00
parent 36870bfced
commit b69e19c525
6 changed files with 369 additions and 0 deletions

View file

@ -4,6 +4,8 @@
#include "compiler/generate_code/c_code.h"
#include "compiler/syntax_grammar.h"
#include "compiler/lexical_grammar.h"
#include "compiler/parse_grammar.h"
#include "json.h"
namespace tree_sitter {
@ -13,6 +15,20 @@ using std::vector;
using std::get;
using std::make_tuple;
CompileResult compile(const char *input) {
ParseGrammarResult parse_result = parse_grammar(string(input));
if (!parse_result.error_message.empty()) {
return {nullptr, parse_result.error_message.c_str()};
}
auto compile_result = compile(parse_result.grammar, parse_result.name);
if (compile_result.second) {
return {nullptr, compile_result.second->message.c_str()};
}
return {compile_result.first.c_str(), nullptr};
}
pair<string, const GrammarError *> compile(const Grammar &grammar,
std::string name) {
auto prepare_grammar_result = prepare_grammar::prepare_grammar(grammar);