-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
214 lines (211 loc) · 3.66 KB
/
Copy pathParser.cpp
File metadata and controls
214 lines (211 loc) · 3.66 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
#include "Parser.h"
#include <stdexcept>
using namespace Seazzz::json;
Parser::Parser()
{
m_str = "";
m_idx = 0;
}
void Parser::load(const string & str)
{
m_str = str;
m_idx = 0;
parse();
}
void Parser::skip_white_space()
{
while (m_str[m_idx] == ' ' || m_str[m_idx] == '\n' || m_str[m_idx] == '\r' || m_str[m_idx] == '\t')
{
m_idx++;
}
}
char Parser::get_next_char()
{
skip_white_space();
char ch = m_str[m_idx];
m_idx++;
return ch;
}
Json Parser::parse()
{
char ch = get_next_char();
if (ch == 'n' && get_next_char() == 'u' && get_next_char() == 'l' && get_next_char() == 'l')
return parse_null();
else if (ch == 't' || ch == 'f')
{
ch = get_next_char();
if (ch == 'r' || ch == 'a')
{
ch = get_next_char();
if (ch == 'u' || ch == 'l')
{
ch = get_next_char();
if (ch == 'e')
{
m_idx -= 4;
return parse_bool();
}
else if(ch =='s')
{
ch = get_next_char();
m_idx -= 5;
if (ch == 'e') return parse_bool();
}
}
}
}
else if (ch >= '0' && ch <= '9')
return parse_number();
else if (ch == '"')
return Json(parse_string());
else if (ch == '[')
return parse_array();
else if (ch == '{')
return parse_object();
throw new std::logic_error("undefined");
return NULL;
}
Json Parser::parse_null()
{
if (m_str.compare(m_idx, 4, "null") == 0)
return Json();
return NULL;
}
Json Parser::parse_bool()
{
if (m_str.compare(m_idx, 4, "true") == 0)
{
m_idx += 4;
return Json(true);
}
else if (m_str.compare(m_idx, 5, "false") == 0)
{
m_idx += 5;
return Json(false);
}
return 0;
}
Json Parser::parse_number(){
int pos = m_idx--;
if (m_str[m_idx] == '-')
{
m_idx++;
}
if (m_str[m_idx] < '0' || m_str[m_idx]>'9')
{
throw new std::logic_error("parse number error");
}
while (m_str[m_idx] >= '0' && m_str[m_idx] <= '9')
{
m_idx++;
}
if (m_str[m_idx] != '.')
{
int i = std::atoi(m_str.c_str() + pos);
return Json(i);
}
m_idx++;
if (m_str[m_idx] < '0' || m_str[m_idx]>'9')
{
throw new std::logic_error("parse number error");
}
while (m_str[m_idx] >= '0' && m_str[m_idx] <= '9')
{
m_idx++;
}
double f = std::atof(m_str.c_str() + pos);
return Json(f);
}
string Parser::parse_string()
{
string out;
while (true)
{
char ch = m_str[m_idx++];
if (ch == '"')
break;
if (ch == '\\')
{
ch = m_str[m_idx++];
switch (ch)
{
case '\n':
out += '\n';
break;
case '\r':
out += '\r';
break;
case '\t':
out += '\t';
break;
case '\b':
out += '\b';
break;
case'\f':
out += '\f';
break;
case'"':
out += "\\\"";
break;
case'\\':
out += "\\\\";
break;
case'u':
out += "\\u";
break;
default:
break;
}
}
else {
out += ch;
}
}
return out;
}
Json Parser::parse_array()
{
Json arr(Json::json_array);
char ch = get_next_char();
if (ch == ']')
{
return arr;
}
m_idx--;
while (true)
{
arr.append(parse());
ch = get_next_char();
if (ch == ']')
break;
m_idx++;
}
}
Json Parser::parse_object()
{
Json obj = Json::json_object;
char ch = get_next_char();
if (ch == '}')
return obj;
m_idx--;
while (true)
{
ch = get_next_char();
if (ch != '"')
{
throw new std::logic_error("AAAAAAAAAAAAAAAAA");
}
string key = parse_string();
ch = get_next_char();
if (ch!= ':')
{
throw new std::logic_error("AAAAAAAAAAAAAAAAA");
}
obj[key] = parse();
if (ch == '}')
break;
if (ch != ',')
throw new std::logic_error("AAAAAAAAAAAAAAAAAAA");
m_idx++;
}
}