/
Parser.cpp
702 lines (610 loc) · 12.9 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
//
// Parser.cpp
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: Parser
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/Ascii.h"
#include "Poco/Token.h"
#undef min
#undef max
#include <limits>
namespace Poco {
namespace JSON {
class SeparatorToken: public Token
{
public:
SeparatorToken()
{
}
virtual ~SeparatorToken()
{
}
Class tokenClass() const
{
return Token::SEPARATOR_TOKEN;
}
bool start(char c, std::istream& istr)
{
if ( c == '{'
|| c == '}'
|| c == ']'
|| c == '['
|| c == ','
|| c == ':' )
{
_value = c;
return true;
}
if ( c == '\'' )
{
throw JSONException("Invalid quote found");
}
else return false;
}
void finish(std::istream& istr)
{
}
};
class StringToken: public Token
{
public:
StringToken()
{
}
virtual ~StringToken()
{
}
Class tokenClass() const
{
return Token::STRING_LITERAL_TOKEN;
}
bool start(char c, std::istream& istr)
{
if ( c == '"')
{
_value = ""; // We don't need the quote!
return true;
}
else return false;
}
void finish(std::istream& istr)
{
int c = istr.get();
while (c != -1)
{
if ( c == 0 )
{
throw JSONException("Null byte not allowed");
}
if ( 0 < c && c <= 0x1F )
{
throw JSONException(format("Control character 0x%x not allowed", (unsigned int) c));
}
if ( c == '"' )
break;
if ( c == '\\' ) // Escaped String
{
c = istr.get();
switch(c)
{
case '"' :
c = '"';
break;
case '\\' :
c = '\\';
break;
case '/' :
c = '/';
break;
case 'b' :
c = '\b';
break;
case 'f' :
c = '\f';
break;
case 'n' :
c = '\n';
break;
case 'r' :
c = '\r';
break;
case 't' :
c = '\t';
break;
case 'u' : // Unicode
{
Poco::Int32 unicode = decodeUnicode(istr);
if ( unicode == 0 )
{
throw JSONException("\\u0000 is not allowed");
}
if ( unicode >= 0xD800 && unicode <= 0xDBFF )
{
c = istr.get();
if ( c != '\\' )
{
throw JSONException("Invalid unicode surrogate pair");
}
c = istr.get();
if ( c != 'u' )
{
throw JSONException("Invalid unicode surrogate pair");
}
Poco::Int32 surrogatePair = decodeUnicode(istr);
if ( 0xDC00 <= surrogatePair && surrogatePair <= 0xDFFF )
{
unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF);
}
else
{
throw JSONException("Invalid unicode surrogate pair");
}
}
else if ( 0xDC00 <= unicode && unicode <= 0xDFFF )
{
throw JSONException("Invalid unicode");
}
c = unicode;
break;
}
default:
{
throw JSONException(format("Invalid escape '%c' character used", (char) c));
}
}
}
_value += c;
c = istr.get();
}
if ( c == -1 )
{
throw JSONException("Unterminated string found");
}
}
Poco::Int32 decodeUnicode(std::istream& istr)
{
Poco::Int32 value = 0;
for(int i = 0; i < 4; i++)
{
value <<= 4;
int nc = istr.peek();
if ( nc == -1 )
{
throw JSONException("Invalid unicode sequence");
}
istr.get(); // No EOF, so read the character
if (nc >= '0' && nc <= '9')
value += nc - '0';
else if (nc >= 'A' && nc <= 'F')
value += 10 + nc - 'A';
else if (nc >= 'a' && nc <= 'f')
value += 10 + nc - 'a';
else
throw JSONException("Invalid unicode sequence. Hexadecimal digit expected");
}
return value;
}
};
class KeywordToken : public Token
{
public:
KeywordToken()
{
}
virtual ~KeywordToken()
{
}
Class tokenClass() const
{
return Token::KEYWORD_TOKEN;
}
bool start(char c, std::istream& istr)
{
if ( Ascii::isAlpha(c) )
{
_value = c;
return true;
}
return false;
}
void finish(std::istream& istr)
{
int c = istr.peek();
while (c != -1 && Ascii::isAlpha(c) )
{
istr.get();
_value += c;
c = istr.peek();
}
}
};
class NumberToken: public Token
{
public:
NumberToken() : _activeClass(INTEGER_LITERAL_TOKEN)
{
}
virtual ~NumberToken()
{
}
Class tokenClass() const
{
return _activeClass;
}
bool start(char c, std::istream& istr)
{
// Reset the active class to integer
_activeClass = INTEGER_LITERAL_TOKEN;
if ( c == -1 )
return false;
if ( Ascii::isDigit(c) )
{
if ( c == '0' )
{
int nc = istr.peek();
if ( Ascii::isDigit(nc) ) // A digit after a zero is not allowed
{
throw JSONException("Number can't start with a zero");
}
}
_value = c;
return true;
}
if ( c == '-' )
{
_value = c;
int nc = istr.peek();
if ( Ascii::isDigit(nc) )
{
if ( nc == '0' )
{
_value += '0';
istr.get();
nc = istr.peek();
if ( Ascii::isDigit(nc) ) // A digit after -0 is not allowed
{
throw JSONException("Number can't start with a zero");
}
}
return true;
}
}
return false;
}
void finish(std::istream& istr)
{
int c;
while( (c = istr.peek()) != -1)
{
if ( Ascii::isDigit(c) )
{
_value += c;
istr.get();
}
else
{
switch(c)
{
case '.': // Float
{
if ( _activeClass == Token::FLOAT_LITERAL_TOKEN )
{
throw JSONException("Invalid float value");
}
_activeClass = Token::FLOAT_LITERAL_TOKEN;
_value += c;
istr.get();
// After a . we need a digit
c = istr.peek();
if ( ! Ascii::isDigit(c) )
{
throw JSONException("Invalid float value");
}
break;
}
case 'E':
case 'e':
{
if ( _activeClass == Token::DOUBLE_LITERAL_TOKEN )
{
throw JSONException("Invalid double value");
}
_activeClass = Token::DOUBLE_LITERAL_TOKEN;
// Add the e or E
_value += c;
istr.get();
// When the next char is - or + then read the next char
c = istr.peek();
if ( c == '-' || c == '+' )
{
_value += c;
istr.get();
c = istr.peek();
}
if ( ! Ascii::isDigit(c) )
{
throw JSONException("Invalid double value");
}
break;
}
default:
return; // End of number token
}
istr.get(); // If we get here we have a valid character for a number
_value += c;
}
}
}
private:
Class _activeClass;
};
Parser::Parser() : _tokenizer(), _handler(NULL)
{
_tokenizer.addToken(new WhitespaceToken());
_tokenizer.addToken(new InvalidToken());
_tokenizer.addToken(new SeparatorToken());
_tokenizer.addToken(new StringToken());
_tokenizer.addToken(new NumberToken());
_tokenizer.addToken(new KeywordToken());
}
Parser::~Parser()
{
}
const Token* Parser::nextToken()
{
const Token* token = _tokenizer.next();
if ( token->is(Token::EOF_TOKEN) )
{
throw JSONException("Unexpected EOF found");
}
return token;
}
void Parser::parse(std::istream& in)
{
_tokenizer.attachToStream(in);
const Token* token = nextToken();
if ( token->is(Token::SEPARATOR_TOKEN) )
{
// This must be a { or a [
if ( token->asChar() == '{' )
{
readObject();
}
else if ( token->asChar() == '[' )
{
readArray();
}
else
{
throw JSONException(format("Invalid separator '%c' found. Expecting { or [", token->asChar()));
}
token = _tokenizer.next();
if ( ! token->is(Token::EOF_TOKEN) )
{
throw JSONException(format("EOF expected but found '%s'", token->asString()));
}
}
else
{
throw JSONException(format("Invalid token '%s' found. Expecting { or [", token->asString()));
}
}
void Parser::readObject()
{
if ( _handler != NULL )
{
_handler->startObject();
}
if ( readRow(true) ) // First call is special: check for empty object
{
while(readRow());
}
if ( _handler != NULL )
{
_handler->endObject();
}
}
bool Parser::readRow(bool firstCall)
{
const Token* token = nextToken();
if ( firstCall && token->tokenClass() == Token::SEPARATOR_TOKEN && token->asChar() == '}' )
{
return false; // End of object is possible for an empty object
}
if ( token->tokenClass() == Token::STRING_LITERAL_TOKEN )
{
std::string propertyName = token->tokenString();
if ( _handler != NULL )
{
_handler->key(propertyName);
}
token = nextToken();
if ( token->is(Token::SEPARATOR_TOKEN)
&& token->asChar() == ':' )
{
readValue(nextToken());
token = nextToken();
if ( token->is(Token::SEPARATOR_TOKEN) )
{
if ( token->asChar() == ',' )
{
return true; // Read next row
}
else if ( token->asChar() == '}' )
{
return false; // End of object
}
else
{
throw JSONException(format("Invalid separator '%c' found. Expecting , or }", token->asChar()));
}
}
else
{
throw JSONException(format("Invalid token '%s' found. Expecting , or }", token->asString()));
}
}
else
{
throw JSONException(format("Invalid token '%s' found. Expecting :", token->asString()));
}
}
else
{
throw JSONException(format("Invalid token '%s' found. Expecting key", token->asString()));
}
}
void Parser::readValue(const Token* token)
{
switch(token->tokenClass())
{
default:
case Token::IDENTIFIER_TOKEN:
case Token::OPERATOR_TOKEN:
case Token::CHAR_LITERAL_TOKEN:
break;
case Token::INTEGER_LITERAL_TOKEN:
if ( _handler != NULL )
{
#if defined(POCO_HAVE_INT64)
Int64 value = token->asInteger64();
// if number is 32-bit, then handle as such
if ( value > std::numeric_limits<int>::max()
|| value < std::numeric_limits<int>::min() )
{
_handler->value(value);
}
else
{
_handler->value(static_cast<int>(value));
}
#else
int value = token->asInteger();
_handle->value(value);
#endif
}
break;
case Token::KEYWORD_TOKEN:
{
if ( token->tokenString().compare("null") == 0 )
{
if ( _handler != NULL )
{
_handler->null();
}
}
else if ( token->tokenString().compare("true") == 0 )
{
if ( _handler != NULL )
{
_handler->value(true);
}
}
else if ( token->tokenString().compare("false") == 0 )
{
if ( _handler != NULL )
{
_handler->value(false);
}
}
else
{
throw JSONException(format("Invalid keyword '%s' found", token->asString()));
}
break;
}
case Token::FLOAT_LITERAL_TOKEN:
// Fall through
case Token::DOUBLE_LITERAL_TOKEN:
if ( _handler != NULL )
{
_handler->value(token->asFloat());
}
break;
case Token::STRING_LITERAL_TOKEN:
if ( _handler != NULL )
{
_handler->value(token->tokenString());
}
break;
case Token::SEPARATOR_TOKEN:
{
if ( token->asChar() == '{' )
{
readObject();
}
else if ( token->asChar() == '[' )
{
readArray();
}
break;
}
}
}
void Parser::readArray()
{
if ( _handler != NULL )
{
_handler->startArray();
}
if ( readElements(true) ) // First call is special: check for empty array
{
while(readElements());
}
if ( _handler != NULL )
{
_handler->endArray();
}
}
bool Parser::readElements(bool firstCall)
{
const Token* token = nextToken();
if ( firstCall && token->is(Token::SEPARATOR_TOKEN) && token->asChar() == ']' )
{
// End of array is possible for an empty array
return false;
}
readValue(token);
token = nextToken();
if ( token->is(Token::SEPARATOR_TOKEN) )
{
if ( token->asChar() == ']' )
return false; // End of array
if ( token->asChar() == ',' )
return true;
throw JSONException(format("Invalid separator '%c' found. Expecting , or ]", token->asChar()));
}
throw JSONException(format("Invalid token '%s' found.", token->asString()));
}
} } // namespace Poco::JSON