Indent parsed nodes
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
@@ -82,33 +83,7 @@ namespace Solstice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& stream, const Node& node) {
|
std::ostream& operator<<(std::ostream& stream, const Node& node) {
|
||||||
stream << "Node( Type=" << nodeTypeToString(node.type);
|
stream << node.stringify();
|
||||||
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 << " )";
|
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -464,4 +439,41 @@ namespace Solstice {
|
|||||||
|
|
||||||
return output;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,6 +51,8 @@ namespace Solstice {
|
|||||||
|
|
||||||
std::optional<Literal> getLiteral() const;
|
std::optional<Literal> getLiteral() const;
|
||||||
std::optional<std::string> getIdentifier() const;
|
std::optional<std::string> getIdentifier() const;
|
||||||
|
|
||||||
|
std::string stringify(int ident = 0) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& stream, const Node& node);
|
std::ostream& operator<<(std::ostream& stream, const Node& node);
|
||||||
|
|||||||
Reference in New Issue
Block a user