Skip to content
Snippets Groups Projects
Commit da9265a5 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add ASTDotPrinter class

continue clean-up
parent ae3ed321
No related branches found
No related tags found
1 merge request!37Feature/language
#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());
}
#ifndef AST_DOT_PRINTER_HPP
#define AST_DOT_PRINTER_HPP
#include <ASTNode.hpp>
class ASTDotPrinter
{
private:
const ASTNode& m_node;
void _print(std::ostream& os, const ASTNode& node) const;
public:
friend std::ostream& operator<<(std::ostream& os, const ASTDotPrinter& ast_printer);
ASTDotPrinter(const ASTNode& node);
ASTDotPrinter(const ASTDotPrinter&) = delete;
ASTDotPrinter(ASTDotPrinter&&) = delete;
~ASTDotPrinter() = default;
};
#endif // AST_DOT_PRINTER_HPP
...@@ -6,6 +6,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) ...@@ -6,6 +6,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_library( add_library(
PugsLanguage PugsLanguage
ASTBuilder.cpp ASTBuilder.cpp
ASTDotPrinter.cpp
ASTNodeDataType.cpp ASTNodeDataType.cpp
ASTNodeAffectationExpressionBuilder.cpp ASTNodeAffectationExpressionBuilder.cpp
ASTNodeBinaryOperatorExpressionBuilder.cpp ASTNodeBinaryOperatorExpressionBuilder.cpp
......
...@@ -26,48 +26,11 @@ ...@@ -26,48 +26,11 @@
#include <ASTSymbolInitializationChecker.hpp> #include <ASTSymbolInitializationChecker.hpp>
#include <ASTSymbolTableBuilder.hpp> #include <ASTSymbolTableBuilder.hpp>
#include <ASTDotPrinter.hpp>
#include <ASTPrinter.hpp> #include <ASTPrinter.hpp>
namespace language namespace language
{ {
namespace internal
{
void
print_dot(std::ostream& os, const ASTNode& n)
{
if (n.is_root()) {
os << " x" << &n << " [ label=\"root \\n" << dataTypeName(n.m_data_type) << "\" ]\n";
} else {
if (n.has_content()) {
os << " x" << &n << " [ label=\"" << n.name() << "\\n"
<< n.string_view() << "\\n"
<< dataTypeName(n.m_data_type) << "\" ]\n";
} else {
os << " x" << &n << " [ label=\"" << n.name() << "\\n" << dataTypeName(n.m_data_type) << "\" ]\n";
}
}
if (!n.children.empty()) {
os << " x" << &n << " -> { ";
for (auto& child : n.children) {
os << "x" << child.get() << ((child == n.children.back()) ? " }\n" : ", ");
}
for (auto& child : n.children) {
print_dot(os, *child);
}
}
}
} // namespace internal
void
print_dot(std::ostream& os, const ASTNode& n)
{
Assert(n.is_root());
os << "digraph parse_tree\n{\n";
internal::print_dot(os, n);
os << "}\n";
}
namespace internal namespace internal
{ {
void void
...@@ -364,7 +327,8 @@ parser(const std::string& filename) ...@@ -364,7 +327,8 @@ parser(const std::string& filename)
{ {
std::string dot_filename{"parse_tree.dot"}; std::string dot_filename{"parse_tree.dot"};
std::ofstream fout(dot_filename); std::ofstream fout(dot_filename);
language::print_dot(fout, *root_node); ASTDotPrinter dot_printer{*root_node};
fout << dot_printer;
std::cout << " AST dot file: " << dot_filename << '\n'; std::cout << " AST dot file: " << dot_filename << '\n';
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment