diff --git a/example/calc3.cc b/example/calc3.cc index 2761924..a8b0758 100644 --- a/example/calc3.cc +++ b/example/calc3.cc @@ -55,7 +55,7 @@ int main(int argc, const char** argv) shared_ptr ast; if (parser.parse(expr, ast)) { ast = AstOptimizer(true).optimize(ast); - AstPrint::print(ast); + print_ast(ast); cout << expr << " = " << eval(*ast) << endl; return 0; } diff --git a/language/culebra/main.cc b/language/culebra/main.cc index 1cfa465..afb99d3 100644 --- a/language/culebra/main.cc +++ b/language/culebra/main.cc @@ -228,7 +228,7 @@ int repl(shared_ptr 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); } } diff --git a/language/pl0/pl0.cc b/language/pl0/pl0.cc index c5a6447..63874c0 100644 --- a/language/pl0/pl0.cc +++ b/language/pl0/pl0.cc @@ -437,7 +437,7 @@ int main(int argc, const char** argv) shared_ptr 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 + diff --git a/lint/cmdline/peglint.cc b/lint/cmdline/peglint.cc index ebfb0af..36265dc 100644 --- a/lint/cmdline/peglint.cc +++ b/lint/cmdline/peglint.cc @@ -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; diff --git a/lint/online/main.cc b/lint/online/main.cc index b808b56..96365ce 100644 --- a/lint/online/main.cc +++ b/lint/online/main.cc @@ -24,7 +24,7 @@ function 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) { diff --git a/peglib.h b/peglib.h index cb7558b..a255780 100644 --- a/peglib.h +++ b/peglib.h @@ -2039,30 +2039,23 @@ struct AstBase : public Annotation std::shared_ptr> parent; }; -struct AstPrint -{ - template - static void print(const std::shared_ptr& 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); } +template +void print_ast(const std::shared_ptr& ptr, int level = 0) { + const auto& ast = *ptr; + for (auto i = 0; i < level; i++) { std::cout << " "; } + std::string name; + if (ast.name == ast.original_name) { + name = ast.name; + } else { + name = ast.original_name + " (" + ast.name + ")"; } - -private: - template - static std::string name(const T& ast) { - if (ast.name == ast.original_name) { - return ast.name; - } else { - return 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 {