MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
Tree.h
Go to the documentation of this file.
1#pragma once
2
3#include <deque>
4
5namespace mllif::mlir {
6
10 class Node final {
11 std::string _tag;
12 std::string _name;
13 std::vector<std::pair<std::string, std::string>> _attributes;
14 std::vector<Node> _children;
15
16 static std::string Escape(std::string s) {
17 std::stringstream ss;
18 for (const auto c : s) {
19 if (!std::isprint(c)) {
20 ss << "&#" << static_cast<int>(c) << ';';
21 }
22
23 switch (c) {
24 case '"':
25 ss << "&quot;";
26 break;
27 case '\'':
28 ss << "&apos;";
29 break;
30 case '<':
31 ss << "&lt;";
32 break;
33 case '>':
34 ss << "&gt;";
35 break;
36 case '&':
37 ss << "&amp;";
38 break;
39 default:
40 ss << c;
41 break;
42 }
43 }
44 return ss.str();
45 }
46
47 public:
53 explicit Node(std::string tag, std::string name) : _tag(std::move(tag)), _name(std::move(name)) {}
54
59 std::string &tag() { return _tag; }
60
65 std::string tag() const { return _tag; }
66
71 std::string& name() { return _name; }
72
77 std::string name() const { return _name; }
78
83 std::vector<std::pair<std::string, std::string>> &attributes() { return _attributes; }
84
89 const std::vector<std::pair<std::string, std::string>> &attributes() const { return _attributes; }
90
95 std::vector<Node> &children() { return _children; }
96
101 const std::vector<Node> &children() const { return _children; }
102
107 void print(llvm::raw_ostream &os) const;
108
116 Node *insert_inplace(std::deque<std::string> &path, const std::string &tag);
117
125 Node *insert(std::deque<std::string> &path, const std::string &tag);
126 };
127
131 class Tree {
132 Node _root;
133
134 public:
138 Tree() : _root("assembly", "") {}
139
144 Node &root() { return _root; }
145 };
146
147} // namespace mllif::mlir
A node for symbol tree. It represents just simple XML serializer.
Definition Tree.h:10
std::vector< std::pair< std::string, std::string > > & attributes()
Gets attributes of node.
Definition Tree.h:83
void print(llvm::raw_ostream &os) const
Serialize subtree as XML to stream.
Definition Tree.cxx:3
const std::vector< std::pair< std::string, std::string > > & attributes() const
Gets attributes of node as readonly.
Definition Tree.h:89
Node * insert(std::deque< std::string > &path, const std::string &tag)
Inserts new node at given path (first element of path is id of this node)
Definition Tree.cxx:48
std::string & name()
Gets a name of node.
Definition Tree.h:71
const std::vector< Node > & children() const
Gets children of node as readonly.
Definition Tree.h:101
std::string tag() const
Gets a tag of node as readonly.
Definition Tree.h:65
std::string & tag()
Gets a tag of node.
Definition Tree.h:59
std::vector< Node > & children()
Gets children of node.
Definition Tree.h:95
Node(std::string tag, std::string name)
Creates new node.
Definition Tree.h:53
std::string name() const
Gets a name of node as readonly.
Definition Tree.h:77
Node * insert_inplace(std::deque< std::string > &path, const std::string &tag)
Inserts new node in-place at given path (first element of path is not id of this node)
Definition Tree.cxx:28
Tree()
Creates new tree.
Definition Tree.h:138
Node & root()
Gets a root node of the tree.
Definition Tree.h:144