-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
286 lines (255 loc) · 6.12 KB
/
Copy pathParser.cpp
File metadata and controls
286 lines (255 loc) · 6.12 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "Parser.h"
#include <cctype>
#include <algorithm>
using namespace json;
JObject Parser::FromString(string_view content)
{
// 单例模式
static Parser instance;
instance.init(content);
return instance.parse();
}
void Parser::init(std::string_view src)
{
m_str = src;
m_idx = 0;
trim_right(); // 去除多余的空格
}
void Parser::trim_right()
{
// 去除尾部空字符,方便最后的结束判断
m_str.erase(std::find_if(m_str.rbegin(), m_str.rend(), [](char ch)
{
return !std::isspace(ch);
}).base(), m_str.end());
}
void Parser::skip_comment()
{
// 跳过注释
if(m_str.compare(m_idx, 2, R"(//)") == 0)
{
while(true)
{
auto next_pos = m_str.find('\n', m_idx);
if(next_pos == string::npos)
{
throw std::logic_error("invalid comment area!");
}
// 查看下一行是否有注释
m_idx = next_pos + 1;
while(isspace(m_str[m_idx]))
{
m_idx++;
}
if(m_str.compare(m_idx, 2, R"(//)") != 0)
{
// 注释结束
return;
}
}
}
}
char Parser::get_next_token()
{
// 跳过空格和跳过注释
while (std::isspace(m_str[m_idx])) m_idx++;
if(m_idx >= m_str.size())
throw std::logic_error("unexpected character in parse json");
// 如果是注释,记得跳过
skip_comment();
return m_str[m_idx];
}
bool Parser::is_esc_consume(size_t pos)
{
// 检查在pos处的反斜杠是否为转义字符。
if(pos == 0 || m_str[pos] != '\\') return false; // 如果是在字符串开头或者该位置不是反斜杠,不需要转义
size_t end_pos = pos;
while(m_str[pos] == '\\') pos--;
auto cnt = end_pos - pos;
// 如果 \ 的个数为偶数,则成功抵消,如果为奇数,则未抵消
return cnt % 2 == 0;
}
JObject Parser::parse()
{
char token = get_next_token();
if(token == 'n')
{
return parse_null();
}
if(token == 't' || token == 'f')
{
return parse_bool();
}
if(token == '-' || std::isdigit(token))
{
return parse_number();
}
if(token == '\"')
{
return parse_string();
}
if(token == '[')
{
return parse_list();
}
if(token == '{')
{
return parse_dict();
}
throw std::logic_error("unexptected character in parse json");
}
JObject Parser::parse_null()
{
if(m_str.compare(m_idx, 4, "null") == 0)
{
m_idx += 4;
return {};
}
throw std::logic_error("parse null error");
}
JObject Parser::parse_number()
{
auto pos = m_idx;
// 负数符号
if(m_str[m_idx] == '-')
{
m_idx++;
}
// 整数部分
if(isdigit(m_str[m_idx]))
{
while(isdigit(m_str[m_idx]))
{
m_idx++;
}
}else{
throw std::logic_error("invalid character in number");
}
// 小数部分
if(m_str[m_idx] == '.')
{
m_idx++;
if(!std::isdigit(m_str[m_idx]))
{
throw std::logic_error("at least one digit required in parse float part!");
}
while(std::isdigit(m_str[m_idx]))
{
m_idx++;
}
}
// 提示:
// strtod 将字符串m_str从pos位置开始的部分转换为双精度的浮点数。
// 转换成功则返回双精度浮点数。
return strtod(m_str.c_str() + pos, nullptr);
}
bool Parser::parse_bool()
{
if(m_str.compare(m_idx, 4, "true") == 0)
{
m_idx += 4;
return true;
}
if(m_str.compare(m_idx, 5, "false") == 0)
{
m_idx += 5;
return false;
}
throw std::logic_error("parse bool error");
}
string Parser::parse_string()
{
auto pre_pos = ++m_idx;
auto pos = m_str.find('"', m_idx);
// 提示:string::npos 是 std::string类中的一个静态常量,
// string::npos 表示无效或不存在的位置
if(pos != string::npos)
{
// 解析还没有结束,需要判断是否是转义的结束符号,如果是转义,则需要继续探查
while(true)
{
if(m_str[pos - 1] != '\\')
{
break;
}
// 如果是转义字符,则判断是否已经被抵消,已经被抵消完则跳出,否则继续寻找下一个字符串的结束符号
if(is_esc_consume(pos - 1))
{
break;
}
pos = m_str.find('"', pos + 1);
if(pos == string::npos)
{
throw std::logic_error(R"(expected left '"' in parse string)");
}
}
m_idx = pos + 1;
return m_str.substr(pre_pos, pos - pre_pos);
}
throw std::logic_error("parse strng error");
}
JObject Parser::parse_list()
{
JObject arr((list_t()));
m_idx++;
char ch = get_next_token();
if(ch == ']')
{
m_idx++;
return arr;
}
while(true)
{
arr.push_back(parse());
ch = get_next_token();
if(ch == ']')
{
m_idx++;
break;
}
if(ch != ',')
{
throw std::logic_error("expected ',' in parse list");
}
// 跳过逗号
m_idx++;
}
return arr;
}
JObject Parser::parse_dict()
{
JObject dict((dict_t()));
m_idx++;
char ch = get_next_token();
if(ch == '}')
{
m_idx++;
return dict;
}
while(true)
{
// 解析key
string key = std::move(parse().Value<string>());
ch = get_next_token();
if(ch != ':')
{
throw std::logic_error("expected ':' in parse dict");
}
m_idx++;
// 解析value
dict[key] = parse();
ch = get_next_token();
if(ch == '}')
{
m_idx++;
break; // 解析完毕
}
if(ch != ',')
{
throw std::logic_error("expected ',' in parse dict");
}
// 跳过逗号
m_idx++;
}
return dict;
}