MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
Tree.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 Yeong-won Seo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <deque>
20
21namespace mllif::mlir {
22
26 class Node final {
27 std::string _tag;
28 std::string _name;
29 std::vector<std::pair<std::string, std::string>> _attributes;
30 std::vector<Node> _children;
31
32 static std::string Escape(std::string s) {
33 std::stringstream ss;
34 for (const auto c : s) {
35 if (!std::isprint(c)) {
36 ss << "&#" << static_cast<int>(c) << ';';
37 }
38
39 switch (c) {
40 case '"':
41 ss << "&quot;";
42 break;
43 case '\'':
44 ss << "&apos;";
45 break;
46 case '<':
47 ss << "&lt;";
48 break;
49 case '>':
50 ss << "&gt;";
51 break;
52 case '&':
53 ss << "&amp;";
54 break;
55 default:
56 ss << c;
57 break;
58 }
59 }
60 return ss.str();
61 }
62
63 public:
69 explicit Node(std::string tag, std::string name) : _tag(std::move(tag)), _name(std::move(name)) {}
70
75 std::string &tag() { return _tag; }
76
81 std::string tag() const { return _tag; }
82
87 std::string &name() { return _name; }
88
93 std::string name() const { return _name; }
94
99 std::vector<std::pair<std::string, std::string>> &attributes() { return _attributes; }
100
105 const std::vector<std::pair<std::string, std::string>> &attributes() const { return _attributes; }
106
111 std::vector<Node> &children() { return _children; }
112
117 const std::vector<Node> &children() const { return _children; }
118
123 void print(llvm::raw_ostream &os) const;
124
132 Node *insert_inplace(std::deque<std::string> &path, const std::string &tag);
133
141 Node *insert(std::deque<std::string> &path, const std::string &tag);
142 };
143
147 class Tree {
148 Node _root;
149
150 public:
154 Tree() : _root("assembly", "") {}
155
160 Node &root() { return _root; }
161 };
162
163} // namespace mllif::mlir
A node for symbol tree. It represents just simple XML serializer.
Definition Tree.h:26
std::vector< std::pair< std::string, std::string > > & attributes()
Gets attributes of node.
Definition Tree.h:99
const std::vector< std::pair< std::string, std::string > > & attributes() const
Gets attributes of node as readonly.
Definition Tree.h:105
std::string & name()
Gets a name of node.
Definition Tree.h:87
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:46
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:68
const std::vector< Node > & children() const
Gets children of node as readonly.
Definition Tree.h:117
std::string tag() const
Gets a tag of node as readonly.
Definition Tree.h:81
std::string & tag()
Gets a tag of node.
Definition Tree.h:75
void print(llvm::raw_ostream &os) const
Serialize subtree as XML to stream.
Definition Tree.cxx:20
std::vector< Node > & children()
Gets children of node.
Definition Tree.h:111
Node(std::string tag, std::string name)
Creates new node.
Definition Tree.h:69
std::string name() const
Gets a name of node as readonly.
Definition Tree.h:93
Tree()
Creates new tree.
Definition Tree.h:154
Node & root()
Gets a root node of the tree.
Definition Tree.h:160