-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_parse_state.cpp
More file actions
227 lines (226 loc) · 6.26 KB
/
json_parse_state.cpp
File metadata and controls
227 lines (226 loc) · 6.26 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
//=============================================================================
// ■ JSON::ParseState
//-----------------------------------------------------------------------------
// A C++ class that handles JSON parsing.
//=============================================================================
namespace JSON { class ParseState {
public:
//-------------------------------------------------------------------------
// ● Declarations
//-------------------------------------------------------------------------
lua_State* L;
SDL_RWops* rw;
// Logically, the parser looks 1 character ahead.
// Actually, we read one more character in the RWops
// than we logically read and buffer it in next_char.
char next_char;
//-------------------------------------------------------------------------
// ● (Logically) read the next character
//-------------------------------------------------------------------------
char read_char() {
char tmp = next_char;
next_char = (char) (unsigned char) SDL_ReadU8(rw);
return tmp;
}
void read_char(char expected) {
char actual = read_char();
if (expected != actual) {
luaL_error(L, "%c expected (got %c)", expected, actual);
}
}
//-------------------------------------------------------------------------
// ● Constructor
//-------------------------------------------------------------------------
ParseState(lua_State* LL, SDL_RWops* rwrw) {
L = LL;
rw = rwrw;
next_char = 0;
read_char();
}
//-------------------------------------------------------------------------
// ● Skip the whitespace in front
//-------------------------------------------------------------------------
void skip_whitespace() {
while (strchr(" \n\t\r", next_char)) read_char();
}
//-------------------------------------------------------------------------
// ● Parse a literal true, false or null
//-------------------------------------------------------------------------
void parse_literal() {
skip_whitespace();
switch(read_char()) {
case 't':
read_char('r');
read_char('u');
read_char('e');
lua_pushboolean(L, true);
break;
case 'f':
read_char('a');
read_char('l');
read_char('s');
read_char('e');
lua_pushboolean(L, false);
break;
case 'n':
read_char('u');
read_char('l');
read_char('l');
lua_pushnil(L);
break;
default:
luaL_error(L, "invalid literal");
}
}
//-------------------------------------------------------------------------
// ● Parse a number
//-------------------------------------------------------------------------
void parse_number() {
skip_whitespace();
// extract the number part first
char s[100];
char* p = s;
while (strchr("0123456789+-.eE", next_char)) {
*p = read_char();
p++;
if (p >= s + sizeof(s) - 1) {
luaL_error(L, "number too long");
}
}
*p = 0;
// Let the C library do the magic.
// Not strictly conforming, but it works.
double number = strtod(s, &p);
if (*p) {
luaL_error(L, "bad conversion to number");
}
lua_pushnumber(L, number);
}
//-------------------------------------------------------------------------
// ● Parse a string
//-------------------------------------------------------------------------
void parse_string() {
skip_whitespace();
read_char('\"');
char c;
luaL_Buffer B;
luaL_buffinit(L, &B);
while ((c = read_char()) != '\"') {
if (c == '\\') {
c = read_char();
switch (c) {
case 'b': case 't': case 'n': case 'f': case 'r':
luaL_addchar(&B, " \b \f \n \r\t"[(c >> 1) & 15]);
break;
case '\"': case '\\': case '/':
luaL_addchar(&B, c);
break;
case 'u':
{
char32_t codepoint;
char s[9];
s[0] = read_char();
s[1] = read_char();
s[2] = read_char();
s[3] = read_char();
s[4] = 0;
if ((s[0] == 'D' || s[0] == 'd') && strchr("89abAB", s[1])) {
if (read_char() != '\\' || read_char() != 'u') {
luaL_error(L, "incomplete surrogate pair");
}
s[4] = read_char();
s[5] = read_char();
s[6] = read_char();
s[7] = read_char();
s[8] = 0;
}
sscanf(s, "%" SCNxLEAST32, (uint_least32_t*) &codepoint);
if (s[4]) {
codepoint = (codepoint & 0x3ff) | (((codepoint >> 16) & 0x3ff) << 10);
codepoint += 0x10000;
}
UTF8::add_encode(&B, codepoint);
}
break;
default:
luaL_error(L, "undefined escape sequence");
}
} else {
luaL_addchar(&B, c);
}
}
luaL_pushresult(&B);
}
//-------------------------------------------------------------------------
// ● Parse an array
//-------------------------------------------------------------------------
void parse_array() {
skip_whitespace();
read_char('[');
lua_newtable(L);
for (int i = 0; skip_whitespace(), next_char != ']'; i++) {
if (i) read_char(',');
parse_value();
lua_rawseti(L, -2, i);
}
read_char(']');
}
//-------------------------------------------------------------------------
// ● Parse an object
//-------------------------------------------------------------------------
void parse_object() {
skip_whitespace();
read_char('{');
lua_newtable(L);
bool first = true;
for (;;) {
skip_whitespace();
if (next_char == '}') break;
if (first) {
first = false;
} else {
read_char(',');
}
parse_string();
skip_whitespace();
read_char(':');
parse_value();
lua_rawset(L, -3);
}
read_char('}');
}
//-------------------------------------------------------------------------
// ● Parse and push the next JSON value in the specified RWops [-0, +1, e]
//-------------------------------------------------------------------------
void parse_value() {
skip_whitespace();
switch (next_char) {
case 't': case 'f': case 'n':
parse_literal();
break;
case '{':
parse_object();
break;
case '[':
parse_array();
break;
case '\"':
parse_string();
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '-':
parse_number();
break;
// more extensive error messages
case '+':
luaL_error(L, "a number cannot start with a plus sign");
break;
case '.':
luaL_error(L, "one zero before the decimal point is mandatory");
break;
default:
luaL_error(L, "unexpected character");
}
}
}; }