mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Fixed build errors.
This commit is contained in:
parent
568da082dc
commit
337741e17e
@ -55,7 +55,7 @@ int main(int argc, const char** argv)
|
||||
shared_ptr<Ast> ast;
|
||||
if (parser.parse(expr, ast)) {
|
||||
ast = AstOptimizer(true).optimize(ast);
|
||||
AstPrint::print(ast);
|
||||
print_ast(ast);
|
||||
cout << expr << " = " << eval(*ast) << endl;
|
||||
return 0;
|
||||
}
|
||||
|
@ -228,7 +228,7 @@ int repl(shared_ptr<Environment> env, bool print_ast)
|
||||
auto ret = run("(repl)", env, line.c_str(), line.size(), val, msgs, ast);
|
||||
if (ret) {
|
||||
if (print_ast) {
|
||||
AstPrint::print(ast);
|
||||
peg::print_ast(ast);
|
||||
}
|
||||
cout << val << endl;
|
||||
linenoise::AddHistory(line.c_str());
|
||||
@ -297,7 +297,7 @@ int main(int argc, const char** argv)
|
||||
}
|
||||
return -1;
|
||||
} else if (print_ast) {
|
||||
AstPrint::print(ast);
|
||||
peg::print_ast(ast);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -437,7 +437,7 @@ int main(int argc, const char** argv)
|
||||
shared_ptr<AstPL0> ast;
|
||||
if (parser.parse_n(source.data(), source.size(), ast, path)) {
|
||||
if (argc > 2 && string("--ast") == argv[2]) {
|
||||
AstPrint::print(ast);
|
||||
print_ast(ast);
|
||||
}
|
||||
try {
|
||||
SymbolTable::build_on_ast(ast);
|
||||
@ -452,3 +452,4 @@ int main(int argc, const char** argv)
|
||||
}
|
||||
|
||||
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|
||||
|
||||
|
@ -97,7 +97,7 @@ int main(int argc, const char** argv)
|
||||
}
|
||||
|
||||
ast = peg::AstOptimizer(opt_optimize_ast_nodes).optimize(ast);
|
||||
peg::AstPrint::print(ast);
|
||||
peg::print_ast(ast);
|
||||
} else {
|
||||
if (!parser.parse_n(source.data(), source.size())) {
|
||||
return -1;
|
||||
|
@ -24,7 +24,7 @@ function<void (size_t, size_t, const string&)> makeJSONFormatter(string& json)
|
||||
};
|
||||
}
|
||||
|
||||
bool parse_grammar(const string& text, peglib::peg& peg, string& json)
|
||||
bool parse_grammar(const string& text, peg::parser& peg, string& json)
|
||||
{
|
||||
peg.log = makeJSONFormatter(json);
|
||||
json += "[";
|
||||
@ -33,7 +33,7 @@ bool parse_grammar(const string& text, peglib::peg& peg, string& json)
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool parse_code(const string& text, peglib::peg& peg, string& json)
|
||||
bool parse_code(const string& text, peg::parser& peg, string& json)
|
||||
{
|
||||
peg.log = makeJSONFormatter(json);
|
||||
json += "[";
|
||||
@ -57,7 +57,7 @@ int main(void)
|
||||
string grammarResult;
|
||||
string codeResult;
|
||||
|
||||
peglib::peg peg;
|
||||
peg::parser peg;
|
||||
auto ret = parse_grammar(grammarText, peg, grammarResult);
|
||||
|
||||
if (ret && peg) {
|
||||
|
27
peglib.h
27
peglib.h
@ -2039,30 +2039,23 @@ struct AstBase : public Annotation
|
||||
std::shared_ptr<AstBase<Annotation>> parent;
|
||||
};
|
||||
|
||||
struct AstPrint
|
||||
{
|
||||
template <typename T>
|
||||
static void print(const std::shared_ptr<T>& ptr, int level = 0) {
|
||||
void print_ast(const std::shared_ptr<T>& ptr, int level = 0) {
|
||||
const auto& ast = *ptr;
|
||||
for (auto i = 0; i < level; i++) { std::cout << " "; }
|
||||
if (ast.is_token) {
|
||||
std::cout << "- " << name(ast) << ": '" << ast.token << "'" << std::endl;
|
||||
} else {
|
||||
std::cout << "+ " << name(ast) << std::endl;
|
||||
}
|
||||
for (auto node : ast.nodes) { print(node, level + 1); }
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
static std::string name(const T& ast) {
|
||||
std::string name;
|
||||
if (ast.name == ast.original_name) {
|
||||
return ast.name;
|
||||
name = ast.name;
|
||||
} else {
|
||||
return ast.original_name + " (" + ast.name + ")";
|
||||
name = ast.original_name + " (" + ast.name + ")";
|
||||
}
|
||||
if (ast.is_token) {
|
||||
std::cout << "- " << name << ": '" << ast.token << "'" << std::endl;
|
||||
} else {
|
||||
std::cout << "+ " << name << std::endl;
|
||||
}
|
||||
for (auto node : ast.nodes) { print_ast(node, level + 1); }
|
||||
}
|
||||
};
|
||||
|
||||
struct AstOptimizer
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user