erick-alcachofa 0a3971f18e
refactor(ast): refactor AST printing to support DOT graph and improved string output
Signed-off-by: erick-alcachofa <erick@artichoke.dev>

This commit refactors the AST printing functionality by moving the
human-readable `toString` implementation into its own file
(`lib/src/Parser/AST/toString.cpp`) and introducing a new `toDot`
function in `lib/src/Parser/AST/toDot.cpp` for generating Graphviz DOT
format output.

The `AST.hpp` header is updated to declare both the new `toDot` function
and the modified `toString` function, which now uses an optional
`prefix` parameter for prettier tree output. The `Token.hpp`/`Token.cpp`
files are also adjusted to have `toString(const TokenV &)` return a
`std::string_view`, and `toString(const Token &)` provides a cleaner
string representation using only the token's value.
2025-10-19 21:54:24 -06:00

35 lines
784 B
C++

#pragma once
#include <artichoke/Parser/AST/Common.hpp>
#include <artichoke/Parser/AST/Declarations.hpp>
namespace arti::lang::ast {
namespace nodes {
/* Forward declaration of types */
/* Main declaration node types */
struct CompilationUnit;
} // namespace nodes
/* Public Aliases */
using CompilationUnitNode = Ptr<nodes::CompilationUnit>;
using AST = CompilationUnitNode;
/* Node definitions */
struct nodes::CompilationUnit {
String unitName;
Vector<TopLevelDeclNode> declarations;
};
template <ASTNodePtr Node>
auto MakeNode() {
return std::make_unique<typename Node::element_type>();
}
std::string toDot(const AST &tree);
std::string toString(const AST &tree, std::string prefix = "");
} // namespace arti::lang::ast