Select Git revision
ASTDotPrinter.cpp
ASTDotPrinter.cpp 1.17 KiB
#include <ASTDotPrinter.hpp>
#include <EscapedString.hpp>
#include <PEGGrammar.hpp>
void
ASTDotPrinter::_print(std::ostream& os, const ASTNode& node) const
{
if (node.is_root()) {
os << " x" << &node << " [ label=\"root \\n" << dataTypeName(node.m_data_type) << "\" ]\n";
} else {
if (node.has_content()) {
os << " x" << &node << " [ label=\"" << node.name() << "\\n"
<< node.string_view() << "\\n"
<< dataTypeName(node.m_data_type) << "\" ]\n";
} else {
os << " x" << &node << " [ label=\"" << node.name() << "\\n" << dataTypeName(node.m_data_type) << "\" ]\n";
}
}
if (!node.children.empty()) {
os << " x" << &node << " -> { ";
for (auto& child : node.children) {
os << "x" << child.get() << ((child == node.children.back()) ? " }\n" : ", ");
}
for (auto& child : node.children) {
this->_print(os, *child);
}
}
}
std::ostream&
operator<<(std::ostream& os, const ASTDotPrinter& ast_printer)
{
os << "digraph parse_tree\n{\n";
ast_printer._print(os, ast_printer.m_node);
os << "}\n";
return os;
}
ASTDotPrinter::ASTDotPrinter(const ASTNode& node) : m_node{node}
{
Assert(node.is_root());
}