forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
506 lines (436 loc) · 13.3 KB
/
Parser.cpp
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//
// Parser.cpp
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: Parser
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/Ascii.h"
#include "Poco/Token.h"
#include "Poco/UTF8Encoding.h"
#undef min
#undef max
#include <limits>
#include <clocale>
#include <istream>
namespace Poco {
namespace JSON {
static const unsigned char UTF8_LEAD_BITS[4] = { 0x00, 0xC0, 0xE0, 0xF0 };
const int Parser::_asciiClass[] = {
xx, xx, xx, xx, xx, xx, xx, xx,
xx, C_WHITE, C_WHITE, xx, xx, C_WHITE, xx, xx,
xx, xx, xx, xx, xx, xx, xx, xx,
xx, xx, xx, xx, xx, xx, xx, xx,
C_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
C_ETC, C_ETC, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH,
C_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,
C_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
C_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC,
C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
C_ETC, C_ETC, C_ETC, C_LSQRB, C_BACKS, C_RSQRB, C_ETC, C_ETC,
C_ETC, C_LOW_A, C_LOW_B, C_LOW_C, C_LOW_D, C_LOW_E, C_LOW_F, C_ETC,
C_ETC, C_ETC, C_ETC, C_ETC, C_LOW_L, C_ETC, C_LOW_N, C_ETC,
C_ETC, C_ETC, C_LOW_R, C_LOW_S, C_LOW_T, C_LOW_U, C_ETC, C_ETC,
C_ETC, C_ETC, C_ETC, C_LCURB, C_ETC, C_RCURB, C_ETC, C_ETC
};
const int Parser::_stateTransitionTable[NR_STATES][NR_CLASSES] = {
/*
white 1-9 ABCDF etc
space | { } [ ] : , " \ / + - . 0 | a b c d e f l n r s t u | E | * */
/*start GO*/ {GO,GO,-6,xx,-5,xx,xx,xx,xx,xx,CB,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx},
/*ok OK*/ {OK,OK,xx,-8,xx,-7,xx,-3,xx,xx,CB,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx},
/*object OB*/ {OB,OB,xx,-9,xx,xx,xx,xx,SB,xx,CB,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx},
/*key KE*/ {KE,KE,xx,xx,xx,xx,xx,xx,SB,xx,CB,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx},
/*colon CO*/ {CO,CO,xx,xx,xx,xx,-2,xx,xx,xx,CB,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx},
/*value VA*/ {VA,VA,-6,xx,-5,xx,xx,xx,SB,xx,CB,xx,MX,xx,ZX,IX,xx,xx,xx,xx,xx,FA,xx,NU,xx,xx,TR,xx,xx,xx,xx,xx},
/*array AR*/ {AR,AR,-6,xx,-5,-7,xx,xx,SB,xx,CB,xx,MX,xx,ZX,IX,xx,xx,xx,xx,xx,FA,xx,NU,xx,xx,TR,xx,xx,xx,xx,xx},
/*string ST*/ {ST,xx,ST,ST,ST,ST,ST,ST,-4,EX,ST,ST,ST,ST,ST,ST,ST,ST,ST,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:
_pHandler(pHandler),
_state(GO),
_beforeCommentState(0),
_type(JSON_T_NONE),
_escaped(0),
_comment(0),
_utf16HighSurrogate(0),
_depth(JSON_UNLIMITED_DEPTH),
_top(-1),
_stack(JSON_PARSER_STACK_SIZE),
_parseBuffer(bufSize),
_decimalPoint('.'),
_allowNullByte(true),
_allowComments(false)
{
_parseBuffer.resize(0);
push(MODE_DONE);
}
Parser::~Parser()
{
}
void Parser::reset()
{
_state = GO;
_beforeCommentState = 0;
_type = JSON_T_NONE;
_escaped = 0;
_utf16HighSurrogate = 0;
_top = -1;
_stack.clear();
_parseBuffer.resize(0);
push(MODE_DONE);
if (_pHandler) _pHandler->reset();
}
Dynamic::Var Parser::parse(const std::string& json)
{
std::string::const_iterator it = json.begin();
std::string::const_iterator end = json.end();
Source<std::string::const_iterator> source(it, end);
int c = 0;
while(source.nextChar(c))
{
if (0 == parseChar(c, source))
throw SyntaxException("JSON syntax error");
}
if (!done())
throw JSONException("JSON syntax error");
return asVar();
}
Dynamic::Var Parser::parse(std::istream& in)
{
std::istreambuf_iterator<char> it(in.rdbuf());
std::istreambuf_iterator<char> end;
Source<std::istreambuf_iterator<char> > source(it, end);
int c = 0;
while(source.nextChar(c))
{
if (0 == parseChar(c, source)) throw JSONException("JSON syntax error");
}
if (!done())
throw JSONException("JSON syntax error");
return asVar();
}
bool Parser::push(int mode)
{
_top += 1;
if (_depth < 0)
{
if (_top >= _stack.size())
_stack.resize(_stack.size() * 2, true);
}
else
{
if (_top >= _depth) return false;
}
_stack[_top] = mode;
return true;
}
bool Parser::pop(int mode)
{
if (_top < 0 || _stack[_top] != mode)
return false;
_top -= 1;
return true;
}
void Parser::clearBuffer()
{
_parseBuffer.resize(0);
}
void Parser::parseBufferPopBackChar()
{
poco_assert(_parseBuffer.size() >= 1);
_parseBuffer.resize(_parseBuffer.size() - 1);
}
void Parser::parseBufferPushBackChar(char c)
{
if (_parseBuffer.size() + 1 >= _parseBuffer.capacity())
_parseBuffer.setCapacity(_parseBuffer.capacity() * 2);
_parseBuffer.append(c);
}
void Parser::addEscapedCharToParseBuffer(CharIntType nextChar)
{
_escaped = 0;
// remove the backslash
parseBufferPopBackChar();
switch(nextChar)
{
case 'b':
parseBufferPushBackChar('\b');
break;
case 'f':
parseBufferPushBackChar('\f');
break;
case 'n':
parseBufferPushBackChar('\n');
break;
case 'r':
parseBufferPushBackChar('\r');
break;
case 't':
parseBufferPushBackChar('\t');
break;
case '"':
parseBufferPushBackChar('"');
break;
case '\\':
parseBufferPushBackChar('\\');
break;
case '/':
parseBufferPushBackChar('/');
break;
case 'u':
parseBufferPushBackChar('\\');
parseBufferPushBackChar('u');
break;
default:
break;
}
}
void Parser::addCharToParseBuffer(CharIntType nextChar, int nextClass)
{
if (_escaped)
{
addEscapedCharToParseBuffer(nextChar);
return;
}
else if (!_comment)
{
if ((_type != JSON_T_NONE) ||
!((nextClass == C_SPACE) || (nextClass == C_WHITE)))
{
parseBufferPushBackChar((char) nextChar);
}
}
}
Parser::CharIntType Parser::decodeUnicodeChar()
{
int i;
unsigned uc = 0;
char* p;
int trailBytes;
poco_assert(_parseBuffer.size() >= 6);
p = &_parseBuffer[_parseBuffer.size() - 4];
for (i = 12; i >= 0; i -= 4, ++p)
{
unsigned x = *p;
if (x >= 'a') x -= ('a' - 10);
else if (x >= 'A') x -= ('A' - 10);
else x &= ~0x30u;
poco_assert(x < 16);
uc |= x << i;
}
if ( !_allowNullByte && uc == 0 ) return 0;
// clear UTF-16 char from buffer
_parseBuffer.resize(_parseBuffer.size() - 6);
if (_utf16HighSurrogate)
{
if (isLowSurrogate(uc))
{
uc = decodeSurrogatePair(_utf16HighSurrogate, uc);
trailBytes = 3;
_utf16HighSurrogate = 0;
}
else // high surrogate without a following low surrogate
{
return 0;
}
}
else
{
if (uc < 0x80)
{
trailBytes = 0;
}
else if (uc < 0x800)
{
trailBytes = 1;
}
else if (isHighSurrogate(uc))
{
// save the high surrogate and wait for the low surrogate
_utf16HighSurrogate = uc;
return 1;
}
else if (isLowSurrogate(uc))
{
// low surrogate without a preceding high surrogate
return 0;
}
else
{
trailBytes = 2;
}
}
_parseBuffer.append((char) ((uc >> (trailBytes * 6)) | UTF8_LEAD_BITS[trailBytes]));
for (i = trailBytes * 6 - 6; i >= 0; i -= 6)
{
_parseBuffer.append((char) (((uc >> i) & 0x3F) | 0x80));
}
return 1;
}
void Parser::parseBuffer()
{
if (_pHandler)
{
int type = _type; // just to silence g++
if (type != JSON_T_NONE)
{
assertNonContainer();
switch(type)
{
case JSON_T_TRUE:
{
_pHandler->value(true);
break;
}
case JSON_T_FALSE:
{
_pHandler->value(false);
break;
}
case JSON_T_NULL:
{
_pHandler->null();
break;
}
case JSON_T_FLOAT:
{
// Float can't end with a dot
if (_parseBuffer[_parseBuffer.size() - 1] == '.' ) throw SyntaxException("JSON syntax error");
double float_value = NumberParser::parseFloat(std::string(_parseBuffer.begin(), _parseBuffer.size()));
_pHandler->value(float_value);
break;
}
case JSON_T_INTEGER:
{
#if defined(POCO_HAVE_INT64)
try
{
Int64 value = NumberParser::parse64(std::string(_parseBuffer.begin(), _parseBuffer.size()));
// if number is 32-bit, then handle as such
if (value > std::numeric_limits<int>::max()
|| value < std::numeric_limits<int>::min() )
{
_pHandler->value(value);
}
else
{
_pHandler->value(static_cast<int>(value));
}
}
// try to handle error as unsigned in case of overflow
catch ( const SyntaxException& )
{
UInt64 value = NumberParser::parseUnsigned64(std::string(_parseBuffer.begin(), _parseBuffer.size()));
// if number is 32-bit, then handle as such
if ( value > std::numeric_limits<unsigned>::max() )
{
_pHandler->value(value);
}
else
{
_pHandler->value(static_cast<unsigned>(value));
}
}
#else
try
{
int value = NumberParser::parse(std::string(_parseBuffer.begin(), _parseBuffer.size()));
_pHandler->value(value);
}
// try to handle error as unsigned in case of overflow
catch ( const SyntaxException& )
{
unsigned value = NumberParser::parseUnsigned(std::string(_parseBuffer.begin(), _parseBuffer.size()));
_pHandler->value(value);
}
#endif
}
break;
case JSON_T_STRING:
{
_pHandler->value(std::string(_parseBuffer.begin(), _parseBuffer.size()));
break;
}
}
}
}
clearBuffer();
}
int Parser::utf8CheckFirst(char byte)
{
unsigned char u = (unsigned char) byte;
if(u < 0x80)
return 1;
if (0x80 <= u && u <= 0xBF)
{
// second, third or fourth byte of a multi-byte
// sequence, i.e. a "continuation byte"
return 0;
}
else if(u == 0xC0 || u == 0xC1)
{
// overlong encoding of an ASCII byte
return 0;
}
else if(0xC2 <= u && u <= 0xDF)
{
// 2-byte sequence
return 2;
}
else if(0xE0 <= u && u <= 0xEF)
{
// 3-byte sequence
return 3;
}
else if(0xF0 <= u && u <= 0xF4)
{
// 4-byte sequence
return 4;
}
else
{
// u >= 0xF5
// Restricted (start of 4-, 5- or 6-byte sequence) or invalid UTF-8
return 0;
}
}
} } // namespace Poco::JSON