-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_tree_nodes.h
164 lines (124 loc) · 3.75 KB
/
parse_tree_nodes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#pragma once
#include<vector>
#include<string>
using namespace std;
// Abstract class
class Object {
protected:
void add_tabs(int tab_count, string &str) {
for (int i = 0; i < tab_count; ++i)
// str += '\t';
// 4 spaces
str += " ";
}
public:
virtual ~Object(){}
virtual void get_formatted_string(string &res, int indentation_level = 0, bool print_tab_on_first_line = false) = 0;
};
class List: public Object {
vector<Object*> children;
public:
List() {}
virtual void add_element(Object *obj) {
children.emplace_back(obj);
}
virtual void get_formatted_string(string &res, int indentation_level = 0, bool print_tab_on_first_line = false) {
if (print_tab_on_first_line) {
add_tabs(indentation_level, res);
}
res += "[\n";
for (Object *child: children) {
child->get_formatted_string(res, indentation_level + 1, true);
res += ",\n";
}
// pop the last comma and new line
res.pop_back();
res.pop_back();
res += '\n';
add_tabs(indentation_level, res);
res += ']';
}
};
class Pair: public Object {
string key;
Object* val;
public:
Pair(string key, Object *val): key(key), val(val) {}
virtual void get_formatted_string(string &res, int indentation_level = 0, bool print_tab_on_first_line = false) {
if (print_tab_on_first_line) {
add_tabs(indentation_level, res);
}
res += '"' + key + '"';
res += ": ";
val->get_formatted_string(res, indentation_level, false);
}
};
class Map: public Object {
vector<Pair> pair_list;
public:
Map() {}
virtual void add_element(string key, Object *val) {
pair_list.emplace_back(Pair(key, val));
}
virtual void get_formatted_string(string &res, int indentation_level = 0, bool print_tab_on_first_line = false) {
if (print_tab_on_first_line) {
add_tabs(indentation_level, res);
}
res += "{\n";
for (Pair pair: pair_list) {
pair.get_formatted_string(res, indentation_level + 1, true);
res += ",\n";
}
// pop the last comma and new line
res.pop_back();
res.pop_back();
res += '\n';
add_tabs(indentation_level, res);
res += '}';
}
};
class Integer: public Object {
string val;
public:
Integer(string val): val(val) {}
virtual void get_formatted_string(string &res, int indentation_level = 0, bool print_tab_on_first_line = false) {
if (print_tab_on_first_line) {
add_tabs(indentation_level, res);
}
res += val;
}
};
class String: public Object {
string val;
public:
String(const string &val): val(val) {}
virtual void get_formatted_string(string &res, int indentation_level = 0, bool print_tab_on_first_line = false) {
if (print_tab_on_first_line) {
add_tabs(indentation_level, res);
}
res += '"';
res += val;
res += '"';
}
};
class Boolean: public Object {
string val;
public:
Boolean(string val): val(val) {}
virtual void get_formatted_string(string &res, int indentation_level = 0, bool print_tab_on_first_line = false) {
if (print_tab_on_first_line) {
add_tabs(indentation_level, res);
}
res += val;
}
};
class Null: public Object {
public:
Null() {}
virtual void get_formatted_string(string &res, int indentation_level = 0, bool print_tab_on_first_line = false) {
if (print_tab_on_first_line) {
add_tabs(indentation_level, res);
}
res += "null";
}
};