From 3f5d54f8111c95dea31be61d8ffcb9ac2932cfb3 Mon Sep 17 00:00:00 2001 From: Yuji Hirose Date: Fri, 27 Nov 2015 15:32:17 -0500 Subject: [PATCH] Removed print_ast, and added ast_to_s. --- lint/cmdline/peglint.cc | 3 ++- lint/cmdline/server.cc | 27 ++------------------------- peglib.h | 27 ++++++++++++++++++++------- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/lint/cmdline/peglint.cc b/lint/cmdline/peglint.cc index 1ff35f6..604f509 100644 --- a/lint/cmdline/peglint.cc +++ b/lint/cmdline/peglint.cc @@ -124,7 +124,8 @@ int main(int argc, const char** argv) } ast = peg::AstOptimizer(opt_optimize_ast_nodes).optimize(ast); - peg::print_ast(ast); + std::cout << peg::ast_to_s(ast); + } else { if (!parser.parse_n(source.data(), source.size())) { return -1; diff --git a/lint/cmdline/server.cc b/lint/cmdline/server.cc index 4b31bb6..71b0e21 100644 --- a/lint/cmdline/server.cc +++ b/lint/cmdline/server.cc @@ -265,29 +265,6 @@ bool parse_code(const string& text, peg::parser& peg, string& json, shared_ptr

-void dump_ast(const shared_ptr& ptr, string& json, int level = 0) -{ - const auto& ast = *ptr; - for (auto i = 0; i < level; i++) { - json += " "; - } - string name; - if (ast.name == ast.original_name) { - name = ast.name; - } else { - name = ast.original_name + " (" + ast.name + ")"; - } - if (ast.is_token) { - json += "- " + name + "(" + ast.token + ")\\n"; - } else { - json += "+ " + name +"\\n"; - } - for (auto node : ast.nodes) { - dump_ast(node, json, level + 1); - } -} - std::string replace_text(std::string &s, const std::string &toReplace, const std::string &replaceWith) { return(s.replace(s.find(toReplace), toReplace.length(), replaceWith)); @@ -319,8 +296,8 @@ int run_server(int port, const vector& syntax, const vector& source) const auto& codeText = req.params.at("code"); shared_ptr ast; if (parse_code(codeText, peg, codeResult, ast)) { - dump_ast(ast, astResult); - dump_ast(peg::AstOptimizer(true).optimize(ast), astResultOptimized); + astResult = peg::ast_to_s(ast); + astResultOptimized = peg::ast_to_s(peg::AstOptimizer(true).optimize(ast)); } } diff --git a/peglib.h b/peglib.h index b3e9932..214d5e2 100644 --- a/peglib.h +++ b/peglib.h @@ -2129,21 +2129,34 @@ struct AstBase : public Annotation }; template -void print_ast(const std::shared_ptr& ptr, int level = 0) { +void ast_to_s(const std::shared_ptr& ptr, std::string& s, int level = 0) { const auto& ast = *ptr; - for (auto i = 0; i < level; i++) { std::cout << " "; } + for (auto i = 0; i < level; i++) { + s += " "; + } std::string name; if (ast.name == ast.original_name) { name = ast.name; - } else { + } + else { name = ast.original_name + " (" + ast.name + ")"; } if (ast.is_token) { - std::cout << "- " << name << ": '" << ast.token << "'" << std::endl; - } else { - std::cout << "+ " << name << std::endl; + s += "- " + name + "(" + ast.token + ")\\n"; + } + else { + s += "+ " + name + "\\n"; } - for (auto node : ast.nodes) { print_ast(node, level + 1); } + for (auto node : ast.nodes) { + ast_to_s(node, s, level + 1); + } +} + +template +std::string ast_to_s(const std::shared_ptr& ptr) { + std::string s; + ast_to_s(ptr, s); + return s; } struct AstOptimizer