Skip to content

Commit dcdfd9f

Browse files
committed
Changed ast_to_s to be customizable.
1 parent 463ed17 commit dcdfd9f

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

peglib.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,7 +2224,12 @@ struct AstBase : public Annotation
22242224
};
22252225

22262226
template <typename T>
2227-
void ast_to_s(const std::shared_ptr<T>& ptr, std::string& s, int level = 0) {
2227+
void ast_to_s_core(
2228+
const std::shared_ptr<T>& ptr,
2229+
std::string& s,
2230+
int level,
2231+
std::function<std::string (const T& ast, int level)> fn) {
2232+
22282233
const auto& ast = *ptr;
22292234
for (auto i = 0; i < level; i++) {
22302235
s += " ";
@@ -2240,15 +2245,21 @@ void ast_to_s(const std::shared_ptr<T>& ptr, std::string& s, int level = 0) {
22402245
} else {
22412246
s += "+ " + name + "\n";
22422247
}
2248+
if (fn) {
2249+
s += fn(ast, level + 1);
2250+
}
22432251
for (auto node : ast.nodes) {
2244-
ast_to_s(node, s, level + 1);
2252+
ast_to_s_core(node, s, level + 1, fn);
22452253
}
22462254
}
22472255

22482256
template <typename T>
2249-
std::string ast_to_s(const std::shared_ptr<T>& ptr) {
2257+
std::string ast_to_s(
2258+
const std::shared_ptr<T>& ptr,
2259+
std::function<std::string (const T& ast, int level)> fn = nullptr) {
2260+
22502261
std::string s;
2251-
ast_to_s(ptr, s);
2262+
ast_to_s_core(ptr, s, 0, fn);
22522263
return s;
22532264
}
22542265

0 commit comments

Comments
 (0)