From 6dece0b9d9513fdfd0e21d4b018758a01befc5e4 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sat, 11 Nov 2023 09:06:44 +0100 Subject: [PATCH] Clean debug tree --- src/parser.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index af03a9b..f1838a2 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -14,20 +14,28 @@ void _debug_print_tree(const Node& node, int depth, const string& prefix = "") { cout << prefix << _node_names[int(innerNode.type)] << "\n"; + string new_prefix = prefix; + size_t pos = new_prefix.find("└──"); + while (pos != std::string::npos) { + new_prefix.replace(pos, 9, " "); + pos = new_prefix.find("└──", pos + 4); + } + + pos = new_prefix.find("├──"); + while (pos != std::string::npos) { + new_prefix.replace(pos, 9, "│ "); + pos = new_prefix.find("├──", pos + 6); + } + for (size_t i = 0; i < innerNode.children.size(); ++i) { string childPrefix = (i == innerNode.children.size() - 1) ? "└── " : "├── "; - _debug_print_tree(innerNode.children[i], depth + 1, prefix + childPrefix); + _debug_print_tree(innerNode.children[i], depth + 1, new_prefix + childPrefix); } } else { const Token& token = get(node); cout << prefix; _debug_print_token(token); - if (holds_alternative(token.data)) { - cout << ", Value: " << get(token.data); - } else if (holds_alternative(token.data)) { - cout << ", Value: " << get(token.data); - } - cout << "\n"; + cout << endl; } }