2014-02-17 12:53:57 -08:00
|
|
|
#include "tree_sitter/compiler.h"
|
2014-03-09 21:37:21 -07:00
|
|
|
#include "compiler/prepare_grammar/prepare_grammar.h"
|
|
|
|
|
#include "compiler/build_tables/build_tables.h"
|
|
|
|
|
#include "compiler/generate_code/c_code.h"
|
2015-01-12 23:01:52 -08:00
|
|
|
#include "compiler/syntax_grammar.h"
|
|
|
|
|
#include "compiler/lexical_grammar.h"
|
2016-01-10 13:44:22 -08:00
|
|
|
#include "compiler/parse_grammar.h"
|
|
|
|
|
#include "json.h"
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-07-20 21:43:27 -07:00
|
|
|
|
2015-03-15 20:31:41 -07:00
|
|
|
using std::pair;
|
2014-07-20 21:43:27 -07:00
|
|
|
using std::string;
|
|
|
|
|
using std::vector;
|
|
|
|
|
using std::get;
|
|
|
|
|
using std::make_tuple;
|
|
|
|
|
|
2016-01-10 20:04:41 -08:00
|
|
|
extern "C" TSCompileResult ts_compile_grammar(const char *input) {
|
2016-01-10 13:44:22 -08:00
|
|
|
ParseGrammarResult parse_result = parse_grammar(string(input));
|
|
|
|
|
if (!parse_result.error_message.empty()) {
|
2016-01-10 20:04:41 -08:00
|
|
|
return { "", strdup(parse_result.error_message.c_str()),
|
|
|
|
|
TSCompileErrorTypeInvalidGrammar };
|
2016-01-10 13:44:22 -08:00
|
|
|
}
|
|
|
|
|
|
2016-01-10 20:04:41 -08:00
|
|
|
auto prepare_grammar_result =
|
|
|
|
|
prepare_grammar::prepare_grammar(parse_result.grammar);
|
|
|
|
|
const SyntaxGrammar &syntax_grammar = get<0>(prepare_grammar_result);
|
|
|
|
|
const LexicalGrammar &lexical_grammar = get<1>(prepare_grammar_result);
|
|
|
|
|
CompileError error = get<2>(prepare_grammar_result);
|
|
|
|
|
if (error.type) {
|
|
|
|
|
return { "", strdup(error.message.c_str()), error.type };
|
2016-01-10 13:44:22 -08:00
|
|
|
}
|
|
|
|
|
|
2016-01-10 20:04:41 -08:00
|
|
|
auto table_build_result =
|
|
|
|
|
build_tables::build_tables(syntax_grammar, lexical_grammar);
|
|
|
|
|
const ParseTable &parse_table = get<0>(table_build_result);
|
|
|
|
|
const LexTable &lex_table = get<1>(table_build_result);
|
|
|
|
|
error = get<2>(table_build_result);
|
|
|
|
|
if (error.type) {
|
|
|
|
|
return { "", strdup(error.message.c_str()), error.type };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string code = generate_code::c_code(parse_result.name, parse_table, lex_table,
|
|
|
|
|
syntax_grammar, lexical_grammar);
|
|
|
|
|
|
|
|
|
|
return { strdup(code.c_str()), "", TSCompileErrorTypeNone };
|
2016-01-10 13:44:22 -08:00
|
|
|
}
|
|
|
|
|
|
2016-01-10 20:04:41 -08:00
|
|
|
pair<string, const CompileError> compile(const Grammar &grammar,
|
|
|
|
|
std::string name) {
|
2014-07-20 21:43:27 -07:00
|
|
|
auto prepare_grammar_result = prepare_grammar::prepare_grammar(grammar);
|
|
|
|
|
const SyntaxGrammar &syntax_grammar = get<0>(prepare_grammar_result);
|
|
|
|
|
const LexicalGrammar &lexical_grammar = get<1>(prepare_grammar_result);
|
2016-01-10 20:04:41 -08:00
|
|
|
CompileError error = get<2>(prepare_grammar_result);
|
|
|
|
|
if (error.type)
|
2015-07-27 18:29:48 -07:00
|
|
|
return { "", error };
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
auto table_build_result =
|
2015-07-27 18:29:48 -07:00
|
|
|
build_tables::build_tables(syntax_grammar, lexical_grammar);
|
2014-07-20 21:43:27 -07:00
|
|
|
const ParseTable &parse_table = get<0>(table_build_result);
|
|
|
|
|
const LexTable &lex_table = get<1>(table_build_result);
|
2015-03-15 20:31:41 -07:00
|
|
|
error = get<2>(table_build_result);
|
2016-01-10 20:04:41 -08:00
|
|
|
if (error.type)
|
2015-07-27 18:29:48 -07:00
|
|
|
return { "", error };
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
string code = generate_code::c_code(name, parse_table, lex_table,
|
|
|
|
|
syntax_grammar, lexical_grammar);
|
|
|
|
|
|
2016-01-10 20:04:41 -08:00
|
|
|
return { code, CompileError::none() };
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
} // namespace tree_sitter
|