From dcdfd9fdb77aba15439eac4c3b6f4cec92701783 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 17 Aug 2017 01:39:30 -0400 Subject: [PATCH] Changed ast_to_s to be customizable. --- peglib.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/peglib.h b/peglib.h index f70b4f6..7b9f7dd 100644 --- a/peglib.h +++ b/peglib.h @@ -2224,7 +2224,12 @@ struct AstBase : public Annotation }; template -void ast_to_s(const std::shared_ptr& ptr, std::string& s, int level = 0) { +void ast_to_s_core( + const std::shared_ptr& ptr, + std::string& s, + int level, + std::function fn) { + const auto& ast = *ptr; for (auto i = 0; i < level; i++) { s += " "; @@ -2240,15 +2245,21 @@ void ast_to_s(const std::shared_ptr& ptr, std::string& s, int level = 0) { } else { s += "+ " + name + "\n"; } + if (fn) { + s += fn(ast, level + 1); + } for (auto node : ast.nodes) { - ast_to_s(node, s, level + 1); + ast_to_s_core(node, s, level + 1, fn); } } template -std::string ast_to_s(const std::shared_ptr& ptr) { +std::string ast_to_s( + const std::shared_ptr& ptr, + std::function fn = nullptr) { + std::string s; - ast_to_s(ptr, s); + ast_to_s_core(ptr, s, 0, fn); return s; }