-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsonparser.cpp
More file actions
executable file
·261 lines (246 loc) · 7 KB
/
Copy pathjsonparser.cpp
File metadata and controls
executable file
·261 lines (246 loc) · 7 KB
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
#include "jsonparser.hpp"
void error(const string & info)
{
std::cerr << info << std::endl;
exit(0);
}
JSonScanner::JSonScanner(Lexemes & opt, std::istream & fin)
{
#define PROC_LITERAL_INT if (in_lit_int)\
{\
opt.push_back(Token(literal_int, Closure(lit_int)));\
in_lit_int = false;\
is_minus = false;\
lit_int = 0;\
}
#define PROC_LITERAL_STR(ch) if (lit_str.length() != 0)\
{\
lit_str += ch;\
continue;\
}
char ch;
string this_line;
string lit_str;
bool in_lit_int = false;
bool is_minus = false;
long long lit_int = 0;
while (!fin.eof())
{
getline(fin, this_line);
for (int i = 0; i < this_line.length(); ++i)
{
ch = this_line[i];
switch(ch)
{
case '\"' : case '\'' :
{
if (lit_str.length() != 0)
{
opt.push_back(Token(literal_str, lit_str.substr(1)));
lit_str = "";
continue;
} else {
lit_str = ":";
continue;
}
}
break;
case ':' :
{
PROC_LITERAL_STR(':');
PROC_LITERAL_INT;
//opt.push_back(Token(projection));
continue;
}
break;
case '{':
{
PROC_LITERAL_STR('{');
PROC_LITERAL_INT;
opt.push_back(Token(left_italic_bracket));
continue;
}
break;
case ',':
{
PROC_LITERAL_STR(',');
if (in_lit_int)
{
opt.push_back(Token(literal_int, Closure(lit_int)));
in_lit_int = false;
lit_int = 0;
continue;
}
//error("Too many commas occur.");
}
break;
case '}':
{
PROC_LITERAL_STR('}');
PROC_LITERAL_INT;
opt.push_back(Token(right_italic_bracket));
continue;
}
break;
case '[':
{
PROC_LITERAL_STR('[');
PROC_LITERAL_INT;
opt.push_back(Token(left_square_bracket));
continue;
}
break;
case ']':
{
PROC_LITERAL_STR(']');
PROC_LITERAL_INT;
opt.push_back(Token(right_square_bracket));
continue;
}
break;
case ' ':
{
PROC_LITERAL_STR(' ');
PROC_LITERAL_INT;
continue;
}
break;
case '-':
{
PROC_LITERAL_STR('-');
is_minus = !is_minus;
continue;
}
default:
PROC_LITERAL_STR(ch);
if (ch <= '9' && ch >= '0')
{
if (is_minus)
{
in_lit_int = true;
lit_int *= 10;
lit_int -= (long long) (ch - '0');
continue;
} else {
in_lit_int = true;
lit_int *= 10;
lit_int += (long long) (ch - '0');
continue;
}
}
error(string("Trying to express an integer using unrecognizable character:\'") + ch + string("\'."));
}
}
}
}
std::ostream & operator<<(std::ostream & fout, const Token & _token)
{
fout << '<';
switch (_token.token_name)
{
case left_italic_bracket:
fout << "class_begin>";
break;
case left_square_bracket:
fout << "list_begin>";
break;
case right_italic_bracket:
fout << "class_end>";
break;
case right_square_bracket:
fout << "list_end>";
break;
case literal_int:
fout << "literal int, " << _token.attribute_value.get_int() << '>';
break;
case literal_str:
fout << "literal str, \"" << _token.attribute_value.get_str() << "\">";
break;
case projection:
fout << "->";
break;
}
return fout;
}
bool Closure::valid() const
{
return int_v != NULL || str_v != NULL || list_v != NULL || class_v != NULL;
}
long long &Closure::get_int() const
{
return (*int_v);
}
Closure & Closure::operator[](const std::string & key) const
{
return (*class_v)[key];
}
Closure & Closure::operator[](size_t idx) const
{
return (*list_v)[idx];
}
deque<Closure> & Closure::get_list() const
{
return (*list_v);
}
void Closure::push_back(const Closure & ins)
{
list_v -> push_back(ins);
}
std::string & Closure::get_str() const
{
return (*str_v);
}
bool Closure::operator<(const Closure & rhs)
{
return (*str_v) < (*rhs.str_v);
}
ostream & operator<<(ostream & fout, const Closure & _token)
{
if (_token.str_v != NULL)
fout << *(_token.str_v);
else if (_token.int_v != NULL)
fout << *(_token.int_v);
else if (_token.list_v != NULL)
{
deque<Closure> & tg(*_token.list_v);
for (auto i = tg.begin(); i != tg.end(); ++i)
fout << *i << "\n";
} else if (_token.class_v != NULL)
{
map<string, Closure> & tg(*_token.class_v);
for (auto i = tg.begin(); i != tg.end(); ++i)
fout << "{" << i -> first << "}->{" << i -> second << "}\n";
}
return fout;
}
Closure JSonParser::get_value(int & pos)
{
if (context[pos].token_name == left_italic_bracket)
{
Closure ret_v(Closure::use_as_a_class());
++pos;
while (context[pos].token_name != right_italic_bracket)
{
string title = get_value(pos).get_str();
Closure value = get_value(pos);
ret_v[title] = value;
}
++pos;
return ret_v;
} else {
if (context[pos].token_name == left_square_bracket)
{
Closure ret_v(Closure::use_as_a_list());
++pos;
while (context[pos].token_name != right_square_bracket)
{
Closure value = get_value(pos);
ret_v.push_back(value);
}
++pos;
return ret_v;
} else {
return context[pos++].attribute_value;
}
}
}