Fixed crash in Ast action handler

This commit is contained in:
yhirose 2020-06-07 08:46:49 -04:00
parent feebe2bb0f
commit ef9147de08

View File

@ -3648,29 +3648,27 @@ struct EmptyType {};
typedef AstBase<EmptyType> Ast; typedef AstBase<EmptyType> Ast;
template <typename T = Ast> template <typename T = Ast>
void add_ast_action(const char *name, Definition &rule) { void add_ast_action(Definition &rule, const std::string &name) {
if (!rule.action) { rule.action = [&](const SemanticValues &sv) {
rule.action = [&](const SemanticValues &sv) { auto line = sv.line_info();
auto line = sv.line_info();
if (rule.is_token()) { if (rule.is_token()) {
return std::make_shared<T>(sv.path, line.first, line.second, return std::make_shared<T>(sv.path, line.first, line.second,
name, sv.token(), name.c_str(), sv.token(),
std::distance(sv.ss, sv.c_str()), std::distance(sv.ss, sv.c_str()),
sv.length(), sv.choice_count(), sv.choice()); sv.length(), sv.choice_count(), sv.choice());
} }
auto ast = std::make_shared<T>( auto ast = std::make_shared<T>(
sv.path, line.first, line.second, name, sv.path, line.first, line.second, name.c_str(),
sv.transform<std::shared_ptr<T>>(), std::distance(sv.ss, sv.c_str()), sv.transform<std::shared_ptr<T>>(), std::distance(sv.ss, sv.c_str()),
sv.length(), sv.choice_count(), sv.choice()); sv.length(), sv.choice_count(), sv.choice());
for (auto node : ast->nodes) { for (auto node : ast->nodes) {
node->parent = ast; node->parent = ast;
} }
return ast; return ast;
}; };
}
} }
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
@ -3802,7 +3800,11 @@ public:
template <typename T = Ast> parser &enable_ast() { template <typename T = Ast> parser &enable_ast() {
for (auto &x : *grammar_) { 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; return *this;
} }