mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 11:55:30 +00:00
Fixed crash in Ast action handler
This commit is contained in:
parent
feebe2bb0f
commit
ef9147de08
14
peglib.h
14
peglib.h
@ -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());
|
||||
|
||||
@ -3670,7 +3669,6 @@ void add_ast_action(const char *name, Definition &rule) {
|
||||
}
|
||||
return ast;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user