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,20 +3648,19 @@ struct EmptyType {};
typedef AstBase<EmptyType> Ast;
template <typename T = Ast>
void add_ast_action(const char *name, Definition &rule) {
if (!rule.action) {
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<T>(sv.path, line.first, line.second,
name, sv.token(),
name.c_str(), sv.token(),
std::distance(sv.ss, sv.c_str()),
sv.length(), sv.choice_count(), sv.choice());
}
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.length(), sv.choice_count(), sv.choice());
@ -3671,7 +3670,6 @@ void add_ast_action(const char *name, Definition &rule) {
return ast;
};
}
}
/*-----------------------------------------------------------------------------
* parser
@ -3802,7 +3800,11 @@ public:
template <typename T = Ast> 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;
}