@@ -529,9 +529,8 @@ class GenericReader {
529
529
\return Whether the parsing is successful.
530
530
*/
531
531
template <unsigned parseFlags, typename InputStream, typename Handler>
532
- ParseResult IterativeParseNext (InputStream& is, Handler& handler) {
532
+ bool IterativeParseNext (InputStream& is, Handler& handler) {
533
533
while (is.Peek () != ' \0 ' ) {
534
- RAPIDJSON_PARSE_ERROR_EARLY_RETURN (parseResult_);
535
534
SkipWhitespaceAndComments<parseFlags>(is);
536
535
537
536
Token t = Tokenize (is.Peek ());
@@ -540,38 +539,52 @@ class GenericReader {
540
539
541
540
if (d == IterativeParsingErrorState) {
542
541
HandleError (state_, is);
543
- return parseResult_ ;
542
+ return false ;
544
543
}
545
544
546
545
state_ = d;
547
546
548
- // Do not further consume streams if a root JSON has been parsed.
549
- if (state_ == IterativeParsingFinishState) {
550
- // If StopWhenDone is not set, and stray data is found post-root, flag an error.
547
+ // Do not further consume streams if we've parsed a complete object or hit an error.
548
+ if (IsIterativeParsingCompleteState (state_)) {
549
+ // If we hit an error, we are done.
550
+ if (HasParseError ())
551
+ return false ;
552
+
553
+ // If StopWhenDone is not set...
551
554
if (!(parseFlags & kParseStopWhenDoneFlag )) {
555
+ // ... and extra non-whitespace data is found...
552
556
SkipWhitespaceAndComments<parseFlags>(is);
553
- if (is.Peek () != ' \0 ' )
557
+ if (is.Peek () != ' \0 ' ) {
558
+ // ... this is considered an error.
554
559
HandleError (state_, is);
560
+ return false ;
561
+ }
555
562
}
556
- return parseResult_;
563
+
564
+ // We are done!
565
+ return true ;
557
566
}
558
567
568
+ // If we found anything other than a delimiter, we invoked the handler, so we can return true now.
559
569
if (!IsIterativeParsingDelimiterState (n))
560
- return parseResult_ ;
570
+ return true ;
561
571
}
562
572
563
- // Handle the end of file.
564
- if (state_ != IterativeParsingFinishState)
573
+ // We reached the end of file.
574
+ stack_.Clear ();
575
+
576
+ if (state_ != IterativeParsingFinishState) {
565
577
HandleError (state_, is);
578
+ return false ;
579
+ }
566
580
567
- stack_.Clear ();
568
- return parseResult_;
581
+ return true ;
569
582
}
570
583
571
584
// ! Check if token-by-token parsing JSON text is complete
572
585
/* ! \return Whether the JSON has been fully decoded.
573
586
*/
574
- bool IterativeParseComplete () {
587
+ RAPIDJSON_FORCEINLINE bool IterativeParseComplete () {
575
588
return IsIterativeParsingCompleteState (state_);
576
589
}
577
590
@@ -1455,30 +1468,32 @@ class GenericReader {
1455
1468
1456
1469
// States
1457
1470
enum IterativeParsingState {
1458
- IterativeParsingStartState = 0 ,
1459
- IterativeParsingFinishState,
1460
- IterativeParsingErrorState ,
1471
+ IterativeParsingFinishState = 0 , // sink states at top
1472
+ IterativeParsingErrorState, // sink states at top
1473
+ IterativeParsingStartState ,
1461
1474
1462
1475
// Object states
1463
1476
IterativeParsingObjectInitialState,
1464
1477
IterativeParsingMemberKeyState,
1465
- IterativeParsingKeyValueDelimiterState,
1466
1478
IterativeParsingMemberValueState,
1467
- IterativeParsingMemberDelimiterState,
1468
1479
IterativeParsingObjectFinishState,
1469
1480
1470
1481
// Array states
1471
1482
IterativeParsingArrayInitialState,
1472
1483
IterativeParsingElementState,
1473
- IterativeParsingElementDelimiterState,
1474
1484
IterativeParsingArrayFinishState,
1475
1485
1476
1486
// Single value state
1477
- IterativeParsingValueState
1487
+ IterativeParsingValueState,
1488
+
1489
+ // Delimiter states (at bottom)
1490
+ IterativeParsingElementDelimiterState,
1491
+ IterativeParsingMemberDelimiterState,
1492
+ IterativeParsingKeyValueDelimiterState,
1493
+
1494
+ cIterativeParsingStateCount
1478
1495
};
1479
1496
1480
- enum { cIterativeParsingStateCount = IterativeParsingValueState + 1 };
1481
-
1482
1497
// Tokens
1483
1498
enum Token {
1484
1499
LeftBracketToken = 0 ,
@@ -1529,6 +1544,18 @@ class GenericReader {
1529
1544
RAPIDJSON_FORCEINLINE IterativeParsingState Predict (IterativeParsingState state, Token token) {
1530
1545
// current state x one lookahead token -> new state
1531
1546
static const char G[cIterativeParsingStateCount][kTokenCount ] = {
1547
+ // Finish(sink state)
1548
+ {
1549
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1550
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1551
+ IterativeParsingErrorState
1552
+ },
1553
+ // Error(sink state)
1554
+ {
1555
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1556
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1557
+ IterativeParsingErrorState
1558
+ },
1532
1559
// Start
1533
1560
{
1534
1561
IterativeParsingArrayInitialState, // Left bracket
@@ -1543,18 +1570,6 @@ class GenericReader {
1543
1570
IterativeParsingValueState, // Null
1544
1571
IterativeParsingValueState // Number
1545
1572
},
1546
- // Finish(sink state)
1547
- {
1548
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1549
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1550
- IterativeParsingErrorState
1551
- },
1552
- // Error(sink state)
1553
- {
1554
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1555
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1556
- IterativeParsingErrorState
1557
- },
1558
1573
// ObjectInitial
1559
1574
{
1560
1575
IterativeParsingErrorState, // Left bracket
@@ -1583,20 +1598,6 @@ class GenericReader {
1583
1598
IterativeParsingErrorState, // Null
1584
1599
IterativeParsingErrorState // Number
1585
1600
},
1586
- // KeyValueDelimiter
1587
- {
1588
- IterativeParsingArrayInitialState, // Left bracket(push MemberValue state)
1589
- IterativeParsingErrorState, // Right bracket
1590
- IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state)
1591
- IterativeParsingErrorState, // Right curly bracket
1592
- IterativeParsingErrorState, // Comma
1593
- IterativeParsingErrorState, // Colon
1594
- IterativeParsingMemberValueState, // String
1595
- IterativeParsingMemberValueState, // False
1596
- IterativeParsingMemberValueState, // True
1597
- IterativeParsingMemberValueState, // Null
1598
- IterativeParsingMemberValueState // Number
1599
- },
1600
1601
// MemberValue
1601
1602
{
1602
1603
IterativeParsingErrorState, // Left bracket
@@ -1611,20 +1612,6 @@ class GenericReader {
1611
1612
IterativeParsingErrorState, // Null
1612
1613
IterativeParsingErrorState // Number
1613
1614
},
1614
- // MemberDelimiter
1615
- {
1616
- IterativeParsingErrorState, // Left bracket
1617
- IterativeParsingErrorState, // Right bracket
1618
- IterativeParsingErrorState, // Left curly bracket
1619
- IterativeParsingObjectFinishState, // Right curly bracket
1620
- IterativeParsingErrorState, // Comma
1621
- IterativeParsingErrorState, // Colon
1622
- IterativeParsingMemberKeyState, // String
1623
- IterativeParsingErrorState, // False
1624
- IterativeParsingErrorState, // True
1625
- IterativeParsingErrorState, // Null
1626
- IterativeParsingErrorState // Number
1627
- },
1628
1615
// ObjectFinish(sink state)
1629
1616
{
1630
1617
IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
@@ -1659,6 +1646,18 @@ class GenericReader {
1659
1646
IterativeParsingErrorState, // Null
1660
1647
IterativeParsingErrorState // Number
1661
1648
},
1649
+ // ArrayFinish(sink state)
1650
+ {
1651
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1652
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1653
+ IterativeParsingErrorState
1654
+ },
1655
+ // Single Value (sink state)
1656
+ {
1657
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1658
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1659
+ IterativeParsingErrorState
1660
+ },
1662
1661
// ElementDelimiter
1663
1662
{
1664
1663
IterativeParsingArrayInitialState, // Left bracket(push Element state)
@@ -1673,18 +1672,34 @@ class GenericReader {
1673
1672
IterativeParsingElementState, // Null
1674
1673
IterativeParsingElementState // Number
1675
1674
},
1676
- // ArrayFinish(sink state)
1675
+ // MemberDelimiter
1677
1676
{
1678
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1679
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1680
- IterativeParsingErrorState
1677
+ IterativeParsingErrorState, // Left bracket
1678
+ IterativeParsingErrorState, // Right bracket
1679
+ IterativeParsingErrorState, // Left curly bracket
1680
+ IterativeParsingObjectFinishState, // Right curly bracket
1681
+ IterativeParsingErrorState, // Comma
1682
+ IterativeParsingErrorState, // Colon
1683
+ IterativeParsingMemberKeyState, // String
1684
+ IterativeParsingErrorState, // False
1685
+ IterativeParsingErrorState, // True
1686
+ IterativeParsingErrorState, // Null
1687
+ IterativeParsingErrorState // Number
1681
1688
},
1682
- // Single Value (sink state)
1689
+ // KeyValueDelimiter
1683
1690
{
1684
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1685
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1686
- IterativeParsingErrorState
1687
- }
1691
+ IterativeParsingArrayInitialState, // Left bracket(push MemberValue state)
1692
+ IterativeParsingErrorState, // Right bracket
1693
+ IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state)
1694
+ IterativeParsingErrorState, // Right curly bracket
1695
+ IterativeParsingErrorState, // Comma
1696
+ IterativeParsingErrorState, // Colon
1697
+ IterativeParsingMemberValueState, // String
1698
+ IterativeParsingMemberValueState, // False
1699
+ IterativeParsingMemberValueState, // True
1700
+ IterativeParsingMemberValueState, // Null
1701
+ IterativeParsingMemberValueState // Number
1702
+ },
1688
1703
}; // End of G
1689
1704
1690
1705
return static_cast <IterativeParsingState>(G[state][token]);
@@ -1866,20 +1881,11 @@ class GenericReader {
1866
1881
}
1867
1882
1868
1883
RAPIDJSON_FORCEINLINE bool IsIterativeParsingDelimiterState (IterativeParsingState s) {
1869
- const unsigned int delimiterStateMask =
1870
- (1 << IterativeParsingKeyValueDelimiterState) |
1871
- (1 << IterativeParsingMemberDelimiterState) |
1872
- (1 << IterativeParsingElementDelimiterState);
1873
-
1874
- return !!((1 << s) & delimiterStateMask);
1884
+ return s >= IterativeParsingElementDelimiterState;
1875
1885
}
1876
1886
1877
1887
RAPIDJSON_FORCEINLINE bool IsIterativeParsingCompleteState (IterativeParsingState s) {
1878
- const unsigned int completeStateMask =
1879
- (1 << IterativeParsingFinishState) |
1880
- (1 << IterativeParsingErrorState);
1881
-
1882
- return !!((1 << s) & completeStateMask);
1888
+ return s <= IterativeParsingErrorState;
1883
1889
}
1884
1890
1885
1891
template <unsigned parseFlags, typename InputStream, typename Handler>
0 commit comments