diff --git a/src/parser/parser.cpp b/src/parser/parser.cpp index 6e9fef3..d880fd0 100644 --- a/src/parser/parser.cpp +++ b/src/parser/parser.cpp @@ -1,4 +1,5 @@ #include "parser.hpp" +#include #include #include #include @@ -82,33 +83,7 @@ namespace Solstice { } std::ostream& operator<<(std::ostream& stream, const Node& node) { - stream << "Node( Type=" << nodeTypeToString(node.type); - if (node.type == NodeType::Literal) { - auto lit = node.getLiteral(); - if (lit.has_value()) { - stream << ", Literal: " << *lit; - } - } else if (node.type == NodeType::Identifier) { - auto id = node.getIdentifier(); - if (id.has_value()) { - stream << ", Identifier: " << *id; - } - } - - if (!node.children.empty()) { - stream << ", Children: { "; - bool first = true; - for (const auto& node : node.children) { - if (first) { - first = false; - } else { - stream << ", "; - } - stream << node; - } - stream << " }"; - } - stream << " )"; + stream << node.stringify(); return stream; } @@ -464,4 +439,41 @@ namespace Solstice { return output; } + + std::string Node::stringify(int ident) const { + std::stringstream stream; + stream << "Node( Type=" << nodeTypeToString(type); + if (type == NodeType::Literal) { + auto lit = getLiteral(); + if (lit.has_value()) { + stream << ", Literal: " << *lit; + } + } else if (type == NodeType::Identifier) { + auto id = getIdentifier(); + if (id.has_value()) { + stream << ", Identifier: " << *id; + } + } + + if (!children.empty()) { + stream << ", Children: { "; + bool first = true; + for (const auto& node : children) { + if (first) { + first = false; + } else { + stream << ","; + } + stream << "\n" << std::string(ident, ' '); + stream << node.stringify(ident + 2); + } + int lessIdent = ident - 2; + if (lessIdent < 0) { + lessIdent = 0; + } + stream << "\n" << std::string(lessIdent, ' ') << "}"; + } + stream << ")"; + return stream.str(); + } } diff --git a/src/parser/parser.hpp b/src/parser/parser.hpp index 8674684..e7d8344 100644 --- a/src/parser/parser.hpp +++ b/src/parser/parser.hpp @@ -51,6 +51,8 @@ namespace Solstice { std::optional getLiteral() const; std::optional getIdentifier() const; + + std::string stringify(int ident = 0) const; }; std::ostream& operator<<(std::ostream& stream, const Node& node);