mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Changed ast_to_s to be customizable.
This commit is contained in:
parent
463ed17967
commit
dcdfd9fdb7
19
peglib.h
19
peglib.h
@ -2224,7 +2224,12 @@ struct AstBase : public Annotation
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void ast_to_s(const std::shared_ptr<T>& ptr, std::string& s, int level = 0) {
|
||||
void ast_to_s_core(
|
||||
const std::shared_ptr<T>& ptr,
|
||||
std::string& s,
|
||||
int level,
|
||||
std::function<std::string (const T& ast, int level)> 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<T>& 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 <typename T>
|
||||
std::string ast_to_s(const std::shared_ptr<T>& ptr) {
|
||||
std::string ast_to_s(
|
||||
const std::shared_ptr<T>& ptr,
|
||||
std::function<std::string (const T& ast, int level)> fn = nullptr) {
|
||||
|
||||
std::string s;
|
||||
ast_to_s(ptr, s);
|
||||
ast_to_s_core(ptr, s, 0, fn);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user