-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.hpp
210 lines (207 loc) · 5.1 KB
/
Parser.hpp
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
#ifndef PARSER_HPP
#define PARSER_HPP
#include "Common.hpp"
#include "Object.hpp"
#include "StringPool.hpp"
#include "Execption.hpp"
namespace mp
{
struct StreamIt
{
const char *cur, *end;
StreamIt() = default;
explicit StreamIt(const char *json) : cur(json), end(cur + strlen(json)) {}
char operator*()
{
//assert(cur < end);
return *cur;
}
explicit operator bool() { return cur < end; }
operator const char *&() { return cur; }
void skip_blanks()
{
while (cur != end && (*cur == ' ' || *cur == '\n' || *cur == '\t' || *cur == '\r'))
++cur;
}
};
struct Parser
{
StreamIt it;
Exception ex;
Object::pool_pointer pool;
Object parse(const char *json)
{
it = StreamIt(json);
ex = Exception(json);
pool.reset(new StringPool);
Object ret;
ret.pool = pool;
parse_object(ret);
if (!ex.ok())
{
std::cerr << ex.msg << std::endl;
exit(1);
}
return ret;
}
// expect and inc
void expect(char ch)
{
if (*it != ch)
ex.errorf(it, "expect %c, got %c", ch, *it);
++it;
}
void expect(const char *str)
{
const char *where = nullptr;
for (auto cur = str; *cur; ++cur)
{
if (*cur != *it)
where = cur;
++it; // do not put expression with other effect in `assert`
}
if (where)
ex.errorf(where, "expect %s, got %c", str, *where);
}
void parse_object(Object &obj)
{
it.skip_blanks();
expect('{');
String str;
Value val;
while (it)
{
it.skip_blanks();
if (*it == '}')
break;
parse_string(str);
it.skip_blanks();
expect(':');
it.skip_blanks();
parse_value(val);
obj.insert(str, std::move(val));
it.skip_blanks();
if (*it != ',') // optional last comma
break;
else
++it;
}
expect('}');
}
void parse_value(Value &val)
{
if (*it == '\"')
{
val.type = Value::str_flag;
parse_string(val.get_str());
}
else if (*it == '{')
{
auto obj = std::make_unique<Object>(); // in case an exception is thrown
obj->pool = pool;
parse_object(*obj);
val.type = Value::obj_flag;
val.get_obj() = obj.release();
}
else if (*it == '[')
{
val.type = Value::arr_flag;
parse_array(val.get_arr());
}
else if (isdigit(*it) || *it == '-')
{
parse_number(val);
}
else if (*it == 't' || *it == 'f')
{
val.type = Value::bool_flag;
parse_bool(val.get_bool());
}
else if (*it == 'n')
{
expect("null");
val.type = Value::null_flag;
}
else
ex.errorf(it, "unexpected char: %c", *it);
}
void parse_number(Value &val)
{
char *tmp;
double res = strtod(it, &tmp);
if (tmp == it)
ex.errorf(it, "wrong number format %c", *it);
static_cast<const char *&>(it) = tmp;
if (res == (int64_t)res)
{
val.type = Value::i64_flag;
val.get_i64() = (int64_t)res;
}
else
{
val.type = Value::f64_flag;
val.get_f64() = res;
}
}
void parse_string(String &str)
{
static StringBuilder builder;
const static auto escape_map = []() {
std::array<char, 256> ret;
ret.fill(-1);
ret[+'0'] = '\0', ret[+'\"'] = '\"', ret[+'\\'] = '\\', ret[+'n'] = '\n', ret[+'t'] = '\t', ret[+'r'] = '\r';
return ret;
}();
builder.clear();
expect('\"');
while (*it != '\"')
{
if (*it == '\\')
{
++it;
if (escape_map[*it] == -1)
ex.errorf(it, "unexpected excape char \\%c", *it);
builder.push_back(escape_map[*it++]);
}
else
builder.push_back(*it++);
}
expect('\"');
str = String::from_cstr(builder.to_string(*pool));
}
void parse_array(Array &arr)
{
arr = Array(16);
Value val;
expect('[');
while (true)
{
it.skip_blanks();
if (*it == ']')
break;
parse_value(val);
arr.push_back(std::move(val));
it.skip_blanks();
if (*it != ',') // optional last comma
break;
else
++it;
}
expect(']');
}
void parse_bool(bool &bo)
{
if (*it == 't')
{
expect("true");
bo = true;
}
else
{
expect("false");
bo = false;
}
}
};
} // namespace mp
#endif // PARSER_HPP