-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_parser.cpp
263 lines (257 loc) · 8.57 KB
/
json_parser.cpp
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include"json_parser.h"
#include"json_node.h"
/*
token_type read_next();
bool bool_reader();
int number_reader();
string string_reader();
void null_reader();
*/
json_parser::json_parser(std::string& s){
tk_reader = new token_reader(s);
}
json_parser::~json_parser(){
delete tk_reader;
}
// TODO: maybe need to distinguish the string for key and the original string
json::json_node* json_parser::parse() {
stack = new parser_stack();
status = EXPECT_SINGLE_VAL | EXPECT_BEGIN_OBJ | EXPECT_BEGIN_ARR;
while (true) {
token_type token = tk_reader->read_next();
switch (token){
case BOOL:
if (on_status(EXPECT_SINGLE_VAL)) {
stack->push(json::json_node::create_bool_node(tk_reader->bool_reader()));
status = EXPECT_END_JSON;
continue;
}
if (on_status(EXPECT_OBJ_VAL)) {
std::string key = stack->top()->get_string();
json::json_node* top;
top = stack->top();
stack->pop();
delete top;
stack->top()->add_to_map(key, json::json_node::create_bool_node(tk_reader->bool_reader()));
stack->top()->add_to_seq_vec(key);
status = EXPECT_COMMA | EXPECT_END_OBJ;
continue;
}
if (on_status(EXPECT_ARR_VAL)) {
stack->top()->add_to_vec(json::json_node::create_bool_node(tk_reader->bool_reader()));
status = EXPECT_COMMA | EXPECT_END_ARR;
continue;
}
delete stack;
throw std::runtime_error("Unexpected bool at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
case NUMBER:
if (on_status(EXPECT_SINGLE_VAL)) {
stack->push(json::json_node::create_num_node(tk_reader->number_reader()));
status = EXPECT_END_JSON;
continue;
}
if (on_status(EXPECT_OBJ_VAL)) {
std::string key = stack->top()->get_string();
json::json_node* top;
top = stack->top();
stack->pop();
delete top;
json::json_node* newnode = json::json_node::create_num_node(tk_reader->number_reader());
stack->top()->add_to_map(key, newnode);
newnode = nullptr;
stack->top()->add_to_seq_vec(key);
status = EXPECT_COMMA | EXPECT_END_OBJ;
continue;
}
if (on_status(EXPECT_ARR_VAL)) {
stack->top()->add_to_vec(json::json_node::create_num_node(tk_reader->number_reader()));
status = EXPECT_COMMA | EXPECT_END_ARR;
continue;
}
delete stack;
throw std::runtime_error("Unexpected number at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
case NUL:
if (on_status(EXPECT_SINGLE_VAL)) {
tk_reader->null_reader();
stack->push(json::json_node::create_null_node());
status = EXPECT_END_JSON;
continue;
}
if (on_status(EXPECT_OBJ_VAL)) {
tk_reader->null_reader();
std::string key = stack->top()->get_string();
json::json_node* top;
top = stack->top();
stack->pop();
delete top;
stack->top()->add_to_map(key, json::json_node::create_null_node());
stack->top()->add_to_seq_vec(key);
status = EXPECT_COMMA | EXPECT_END_OBJ;
continue;
}
if (on_status(EXPECT_ARR_VAL)) {
tk_reader->null_reader();
stack->top()->add_to_vec(json::json_node::create_null_node());
status = EXPECT_COMMA | EXPECT_END_ARR;
continue;
}
delete stack;
throw std::runtime_error("Unexpected null at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
case STRING:
if (on_status(EXPECT_SINGLE_VAL)) {
stack->push(json::json_node::create_str_node(tk_reader->string_reader()));
status = EXPECT_END_JSON;
continue;
}
if (on_status(EXPECT_OBJ_KEY)) {
stack->push(json::json_node::create_str_node(tk_reader->string_reader()));
status = EXPECT_COLON;
continue;
}
if (on_status(EXPECT_OBJ_VAL)) {
std::string key = stack->top()->get_string();
json::json_node* top;
top = stack->top();
stack->pop();
delete top;
stack->top()->add_to_map(key, json::json_node::create_str_node(tk_reader->string_reader()));
stack->top()->add_to_seq_vec(key);
status = EXPECT_COMMA | EXPECT_END_OBJ;
continue;
}
if (on_status(EXPECT_ARR_VAL)) {
stack->top()->add_to_vec(json::json_node::create_str_node(tk_reader->string_reader()));
status = EXPECT_COMMA | EXPECT_END_ARR;
continue;
}
delete stack;
throw std::runtime_error("Unexpected string at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
case COLON:
if (status == EXPECT_COLON) {
status = EXPECT_OBJ_VAL | EXPECT_BEGIN_OBJ | EXPECT_BEGIN_ARR;
continue;
}
delete stack;
throw std::runtime_error("Unexpected ':' at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
case COMMA:
if (on_status(EXPECT_COMMA)) {
if (on_status(EXPECT_END_OBJ)) {
status = EXPECT_OBJ_KEY;
continue;
}
if (on_status(EXPECT_END_ARR)) {
status = EXPECT_ARR_VAL | EXPECT_BEGIN_ARR | EXPECT_BEGIN_OBJ;
continue;
}
}
delete stack;
throw std::runtime_error("Unexpected ',' at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
case BEGIN_ARR:
if (on_status(EXPECT_BEGIN_ARR)) {
stack->push(json::json_node::create_array_node());
status = EXPECT_ARR_VAL | EXPECT_BEGIN_OBJ | EXPECT_BEGIN_ARR | EXPECT_END_ARR;
continue;
}
delete stack;
throw std::runtime_error("Unexpected '[' at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
case BEGIN_OBJ:
if (on_status(EXPECT_BEGIN_OBJ)) {
stack->push(json::json_node::create_object_node());
status = EXPECT_OBJ_KEY | EXPECT_BEGIN_OBJ | EXPECT_END_OBJ;
continue;
}
delete stack;
throw std::runtime_error("Unexpected '{' at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
case END_ARR:
if (on_status(EXPECT_END_ARR)) {
if (stack->size() == 1) {
status = EXPECT_END_JSON;
continue;
}
json::json_node* tempArray = stack->top();
stack->pop();
json::node_type type = stack->top()->get_node_type();
if (type == json::STRING) {
// key: [ CURRENT ] ,}
std::string key = stack->top()->get_string();
json::json_node* top;
top = stack->top();
stack->pop();
delete top;
stack->top()->add_to_map(key, tempArray);
stack->top()->add_to_seq_vec(key);
status = EXPECT_COMMA | EXPECT_END_OBJ;
continue;
}
if (type == json::ARRAY) {
// xx, xx, [CURRENT] ,]
stack->top()->add_to_vec(tempArray);
status = EXPECT_COMMA | EXPECT_END_ARR;
continue;
}
}
delete stack;
throw std::runtime_error("Unexpected '[' at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
case END_OBJ:
if (on_status(EXPECT_END_OBJ)) {
if (stack->size() == 1) {
status = EXPECT_END_JSON;
continue;
}
json::json_node* tempObject = stack->top();
stack->pop();
json::node_type type = stack->top()->get_node_type();
if (type == json::STRING) {
// key: { CURRENT } ,}
std::string key = stack->top()->get_string();
json::json_node* top;
top = stack->top();
stack->pop();
delete top;
stack->top()->add_to_map(key, tempObject);
stack->top()->add_to_seq_vec(key);
status = EXPECT_COMMA | EXPECT_END_OBJ;
continue;
}
if (type == json::ARRAY) {
// xx, xx, {CURRENT} ,]
stack->top()->add_to_vec(tempObject);
status = EXPECT_COMMA | EXPECT_END_ARR;
continue;
}
}
delete stack;
throw std::runtime_error("Unexpected '}' at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
case END_JSON:
if (on_status(EXPECT_END_JSON)) {
if (stack->size() == 1) {
json::json_node* ans = stack->top();
delete stack;
return ans;
}
}
delete stack;
throw std::runtime_error("Unexpected end JSON at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
default:
delete stack;
throw std::runtime_error("Unexpected character at position: "
+ std::to_string(tk_reader->reader.get_pos()) + "\nAccepted JSON string: " + tk_reader->reader.get_readed());
}
}
delete stack;
}
inline bool json_parser::on_status(int status)
{
return (this->status & status) != 0;
}