From ef9147de084ca9c94454afdb1bd42f1e0cb9b80a Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 7 Jun 2020 08:46:49 -0400 Subject: [PATCH] Fixed crash in Ast action handler --- peglib.h | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/peglib.h b/peglib.h index 73ee099..32c3011 100644 --- a/peglib.h +++ b/peglib.h @@ -3648,29 +3648,27 @@ struct EmptyType {}; typedef AstBase Ast; template -void add_ast_action(const char *name, Definition &rule) { - if (!rule.action) { - rule.action = [&](const SemanticValues &sv) { - auto line = sv.line_info(); +void add_ast_action(Definition &rule, const std::string &name) { + rule.action = [&](const SemanticValues &sv) { + auto line = sv.line_info(); - if (rule.is_token()) { - return std::make_shared(sv.path, line.first, line.second, - name, sv.token(), - std::distance(sv.ss, sv.c_str()), - sv.length(), sv.choice_count(), sv.choice()); - } + if (rule.is_token()) { + return std::make_shared(sv.path, line.first, line.second, + name.c_str(), sv.token(), + std::distance(sv.ss, sv.c_str()), + sv.length(), sv.choice_count(), sv.choice()); + } - auto ast = std::make_shared( - sv.path, line.first, line.second, name, - sv.transform>(), std::distance(sv.ss, sv.c_str()), - sv.length(), sv.choice_count(), sv.choice()); + auto ast = std::make_shared( + sv.path, line.first, line.second, name.c_str(), + sv.transform>(), std::distance(sv.ss, sv.c_str()), + sv.length(), sv.choice_count(), sv.choice()); - for (auto node : ast->nodes) { - node->parent = ast; - } - return ast; - }; - } + for (auto node : ast->nodes) { + node->parent = ast; + } + return ast; + }; } /*----------------------------------------------------------------------------- @@ -3802,7 +3800,11 @@ public: template parser &enable_ast() { for (auto &x : *grammar_) { - add_ast_action(x.first.c_str(), x.second); + const auto &name = x.first; + auto &rule = x.second; + if (!rule.action) { + add_ast_action(rule, name); + } } return *this; }