Yume
dot_visitor.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "token.hpp"
4#include "util.hpp"
5#include "visitor.hpp"
6#include <iosfwd>
7#include <llvm/Support/raw_ostream.h>
8#include <memory>
9#include <optional>
10#include <string>
11#include <utility>
12#include <vector>
13
14namespace yume::ast {
15class AST;
16}
17
18namespace yume::diagnostic {
19class DotVisitor final : public Visitor {
20 static constexpr const char* AST_KEY = "ym_ast_";
21
22 struct DotConnection;
23
24 struct DotNode {
25 int index;
26 optional<Loc> location;
27 optional<string> type;
28 string content;
29 vector<DotConnection> children{};
30
31 DotNode(int index, optional<Loc> location, optional<string> type, string kind)
32 : index(index), location(move(location)), type(move(type)), content(move(kind)) {}
33
34 [[nodiscard]] auto simple() const -> bool { return !location.has_value(); }
35
36 void write(llvm::raw_ostream& stream) const;
37 };
38
39 struct DotConnection {
40 optional<string> line_label;
41 DotNode child;
42
43 DotConnection(optional<string> line_label, DotNode child) noexcept
44 : line_label(move(line_label)), child(move(child)) {}
45 };
46
47 llvm::raw_ostream& m_stream;
48 int m_index{};
49 DotNode* m_parent{};
50 unique_ptr<DotNode> m_root{};
51
52 auto add_node(const string& content, const char* label) -> DotNode&;
53 auto add_node(DotNode&& node, const char* label) -> DotNode&;
54
55public:
56 explicit DotVisitor(llvm::raw_ostream& stream) : m_stream{stream} {
57 m_stream << "digraph \"yume\" {\nnode [shape=box, style=rounded];\n";
58 }
59 ~DotVisitor() override {
60 m_stream << "\n}";
61 m_stream.flush();
62 }
63 DotVisitor(const DotVisitor&) = delete;
65 auto operator=(const DotVisitor&) -> DotVisitor& = delete;
66 auto operator=(DotVisitor&&) -> DotVisitor& = delete;
67
68 auto visit(const ast::AST& expr, string_view label) -> DotVisitor& override;
69 auto visit(std::nullptr_t null, string_view label) -> DotVisitor& override;
70 auto visit(const string& str, string_view label) -> DotVisitor& override;
71};
72} // namespace yume::diagnostic
All nodes in the AST tree of the program inherit from this class.
Definition: ast.hpp:224
auto visit(const ast::AST &expr, string_view label) -> DotVisitor &override
Definition: dot_visitor.cpp:84
DotVisitor(DotVisitor &&)=delete
DotVisitor(const DotVisitor &)=delete
auto operator=(DotVisitor &&) -> DotVisitor &=delete
auto operator=(const DotVisitor &) -> DotVisitor &=delete
DotVisitor(llvm::raw_ostream &stream)
Definition: dot_visitor.hpp:56
Definition: ast.cpp:8