-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.tpp
230 lines (204 loc) · 6.52 KB
/
parser.tpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
Object Parser::expect_object() {
if (tokenizer.tokens().size() == 0) {
throw std::runtime_error("Error: empty input");
}
auto &token = tokenizer[idx];
if (token.type != TokenType::OPEN_BRACE) {
throw std::runtime_error("Error: expected '{' at line " +
std::to_string(token.line) + ", col " +
std::to_string(token.col));
}
idx++;
Object obj(depth);
while (idx < tokenizer.tokens().size()) {
auto &token = tokenizer[idx];
if (token.type == TokenType::CLOSE_BRACE) {
idx++;
return obj;
} else if (token.type == TokenType::STRING) {
std::string key = token.s_val;
idx++;
if (tokenizer[idx].type != TokenType::COLON) {
throw std::runtime_error("Error: expected ':' at line " +
std::to_string(token.line) + ", col " +
std::to_string(token.col));
}
idx++;
auto val = expect_value();
obj.elements.insert({key, val});
if (tokenizer[idx].type == TokenType::COMMA) {
idx++;
}
} else {
throw std::runtime_error("Error: expected STRING at line " +
std::to_string(token.line) + ", col " +
std::to_string(token.col));
}
}
throw std::runtime_error("Error: expected '}' at line " +
std::to_string(token.line) + ", col " +
std::to_string(token.col));
};
Value Parser::expect_value() {
if (tokenizer.tokens().size() == 0) {
throw std::runtime_error("Error: empty input");
}
auto &token = tokenizer[idx];
Value val;
switch (token.type) {
case TokenType::STRING:
idx++;
return Value(ValueType::STRING, std::make_shared<std::string>(token.s_val));
case TokenType::DOUBLE:
idx++;
return Value(ValueType::DOUBLE, std::make_shared<double>(token.double_val));
case TokenType::INT:
idx++;
return Value(ValueType::INT, std::make_shared<int>(token.int_val));
case TokenType::OPEN_BRACE:
depth += 4;
val = Value(ValueType::OBJECT, std::make_shared<Object>(expect_object()));
depth -= 4;
return val;
case TokenType::TRUE:
idx++;
return Value(ValueType::BOOL, std::make_shared<bool>(true));
case TokenType::FALSE:
idx++;
return Value(ValueType::BOOL, std::make_shared<bool>(false));
case TokenType::LEFT_BRACKET:
depth += 4;
val = Value(ValueType::ARRAY, std::make_shared<Array>(expect_array()));
depth -= 4;
return val;
default:
throw std::runtime_error("Error: expected value at line " +
std::to_string(token.line) + ", col " +
std::to_string(token.col));
}
}
Array Parser::expect_array() {
if (tokenizer.tokens().size() == 0) {
throw std::runtime_error("Error: empty input");
}
auto &token = tokenizer[idx];
if (token.type != TokenType::LEFT_BRACKET) {
throw std::runtime_error("Error: expected '[' at line " +
std::to_string(token.line) + ", col " +
std::to_string(token.col));
}
idx++;
Array arr(depth);
while (idx < tokenizer.tokens().size()) {
auto &token = tokenizer[idx];
if (token.type == TokenType::RIGHT_BRACKET) {
idx++;
return arr;
} else {
arr.elements.push_back(expect_value());
if (tokenizer[idx].type == TokenType::COMMA) {
idx++;
}
}
}
throw std::runtime_error("Error: expected ']' at line " +
std::to_string(token.line) + ", col " +
std::to_string(token.col));
}
JSON Parser::parse() {
object.root = expect_object();
return object;
}
std::string Object::to_str() const {
std::string s = "{\n";
for (auto &[key, val] : elements) {
s += std::string(depth, ' ') + "\"" + key + "\": " + val.to_str() + ", \n";
}
s.pop_back();
s.pop_back();
s.pop_back();
s += "\n" + std::string(std::max(depth - 4, 0), ' ') + "}";
return s;
}
std::string Array::to_str() const {
std::string s = "[\n";
for (auto &val : elements) {
s += std::string(depth, ' ') + val.to_str() + ", \n";
}
s.pop_back();
s.pop_back();
s.pop_back();
s += "\n" + std::string(std::max(depth - 4, 0), ' ') + "]";
return s;
}
std::ostream &operator<<(std::ostream &os, const Object &jo) {
os << jo.to_str();
return os;
}
std::ostream &operator<<(std::ostream &os, const Value &jv) {
os << jv.to_str();
return os;
}
std::ostream &operator<<(std::ostream &os, const JSON &json) {
os << json.to_str();
return os;
}
std::ostream &operator<<(std::ostream &os, const Array &ja) {
os << ja.to_str();
return os;
}
Value &Object::operator[](const std::string key) { return elements[key]; }
template <typename T> sPtr<T> Object::get(const std::string &key) const {
if (std::get_if<sPtr<T>>(&elements.at(key).value)) {
return std::get<sPtr<T>>(elements.at(key).value);
}
throw std::runtime_error("Error: expected type " +
std::string(typeid(T).name()) + " at key " + key);
}
template <typename T>
sPtr<T> Object::get(const std::string &key, const T default_val) const {
if (elements.find(key) == elements.end()) {
return default_val;
}
if (std::get_if<sPtr<T>>(&elements.at(key).value)) {
return std::get<sPtr<T>>(elements.at(key).value);
} else {
return default_val;
}
}
template <typename T> sPtr<T> Object::try_get(const std::string &key) const {
if (elements.find(key) == elements.end()) {
return nullptr;
}
if (std::get_if<sPtr<T>>(&elements.at(key).value)) {
return std::get<sPtr<T>>(elements.at(key).value);
} else {
return nullptr;
}
}
template <typename T> void Object::insert(const std::string key, T val) {
elements.insert({key, Value(val)});
}
template <> void Object::insert(const std::string key, Value val) {
elements.insert({key, val});
}
template <> void Object::insert(const std::string key, Object val) {
val.depth = depth + 4;
elements.insert({key, Value(val)});
}
std::string Value::to_str() const {
switch (type) {
case ValueType::STRING:
return "\"" + *std::get<sPtr<std::string>>(value) + "\"";
case ValueType::DOUBLE:
return std::to_string(*std::get<sPtr<double>>(value));
case ValueType::OBJECT:
return std::get<sPtr<Object>>(value)->to_str();
case ValueType::BOOL:
return std::get<sPtr<bool>>(value) ? "true" : "false";
case ValueType::ARRAY:
return std::get<sPtr<Array>>(value)->to_str();
case ValueType::INT:
return std::to_string(*std::get<sPtr<int>>(value));
}
}