@@ -513,6 +513,83 @@ class GenericReader {
513
513
return Parse<kParseDefaultFlags >(is, handler);
514
514
}
515
515
516
+ // ! Initialize JSON text token-by-token parsing
517
+ /* !
518
+ */
519
+ void IterativeParseInit () {
520
+ parseResult_.Clear ();
521
+ state_ = IterativeParsingStartState;
522
+ }
523
+
524
+ // ! Parse one token from JSON text
525
+ /* ! \tparam InputStream Type of input stream, implementing Stream concept
526
+ \tparam Handler Type of handler, implementing Handler concept.
527
+ \param is Input stream to be parsed.
528
+ \param handler The handler to receive events.
529
+ \return Whether the parsing is successful.
530
+ */
531
+ template <unsigned parseFlags, typename InputStream, typename Handler>
532
+ bool IterativeParseNext (InputStream& is, Handler& handler) {
533
+ while (RAPIDJSON_LIKELY (is.Peek () != ' \0 ' )) {
534
+ SkipWhitespaceAndComments<parseFlags>(is);
535
+
536
+ Token t = Tokenize (is.Peek ());
537
+ IterativeParsingState n = Predict (state_, t);
538
+ IterativeParsingState d = Transit<parseFlags>(state_, t, n, is, handler);
539
+
540
+ // If we've finished or hit an error...
541
+ if (RAPIDJSON_UNLIKELY (IsIterativeParsingCompleteState (d))) {
542
+ // Report errors.
543
+ if (d == IterativeParsingErrorState) {
544
+ HandleError (state_, is);
545
+ return false ;
546
+ }
547
+
548
+ // Transition to the finish state.
549
+ RAPIDJSON_ASSERT (d == IterativeParsingFinishState);
550
+ state_ = d;
551
+
552
+ // If StopWhenDone is not set...
553
+ if (!(parseFlags & kParseStopWhenDoneFlag )) {
554
+ // ... and extra non-whitespace data is found...
555
+ SkipWhitespaceAndComments<parseFlags>(is);
556
+ if (is.Peek () != ' \0 ' ) {
557
+ // ... this is considered an error.
558
+ HandleError (state_, is);
559
+ return false ;
560
+ }
561
+ }
562
+
563
+ // Success! We are done!
564
+ return true ;
565
+ }
566
+
567
+ // Transition to the new state.
568
+ state_ = d;
569
+
570
+ // If we parsed anything other than a delimiter, we invoked the handler, so we can return true now.
571
+ if (!IsIterativeParsingDelimiterState (n))
572
+ return true ;
573
+ }
574
+
575
+ // We reached the end of file.
576
+ stack_.Clear ();
577
+
578
+ if (state_ != IterativeParsingFinishState) {
579
+ HandleError (state_, is);
580
+ return false ;
581
+ }
582
+
583
+ return true ;
584
+ }
585
+
586
+ // ! Check if token-by-token parsing JSON text is complete
587
+ /* ! \return Whether the JSON has been fully decoded.
588
+ */
589
+ RAPIDJSON_FORCEINLINE bool IterativeParseComplete () {
590
+ return IsIterativeParsingCompleteState (state_);
591
+ }
592
+
516
593
// ! Whether a parse error has occured in the last parsing.
517
594
bool HasParseError () const { return parseResult_.IsError (); }
518
595
@@ -1402,30 +1479,32 @@ class GenericReader {
1402
1479
1403
1480
// States
1404
1481
enum IterativeParsingState {
1405
- IterativeParsingStartState = 0 ,
1406
- IterativeParsingFinishState,
1407
- IterativeParsingErrorState ,
1482
+ IterativeParsingFinishState = 0 , // sink states at top
1483
+ IterativeParsingErrorState, // sink states at top
1484
+ IterativeParsingStartState ,
1408
1485
1409
1486
// Object states
1410
1487
IterativeParsingObjectInitialState,
1411
1488
IterativeParsingMemberKeyState,
1412
- IterativeParsingKeyValueDelimiterState,
1413
1489
IterativeParsingMemberValueState,
1414
- IterativeParsingMemberDelimiterState,
1415
1490
IterativeParsingObjectFinishState,
1416
1491
1417
1492
// Array states
1418
1493
IterativeParsingArrayInitialState,
1419
1494
IterativeParsingElementState,
1420
- IterativeParsingElementDelimiterState,
1421
1495
IterativeParsingArrayFinishState,
1422
1496
1423
1497
// Single value state
1424
- IterativeParsingValueState
1498
+ IterativeParsingValueState,
1499
+
1500
+ // Delimiter states (at bottom)
1501
+ IterativeParsingElementDelimiterState,
1502
+ IterativeParsingMemberDelimiterState,
1503
+ IterativeParsingKeyValueDelimiterState,
1504
+
1505
+ cIterativeParsingStateCount
1425
1506
};
1426
1507
1427
- enum { cIterativeParsingStateCount = IterativeParsingValueState + 1 };
1428
-
1429
1508
// Tokens
1430
1509
enum Token {
1431
1510
LeftBracketToken = 0 ,
@@ -1476,6 +1555,18 @@ class GenericReader {
1476
1555
RAPIDJSON_FORCEINLINE IterativeParsingState Predict (IterativeParsingState state, Token token) {
1477
1556
// current state x one lookahead token -> new state
1478
1557
static const char G[cIterativeParsingStateCount][kTokenCount ] = {
1558
+ // Finish(sink state)
1559
+ {
1560
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1561
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1562
+ IterativeParsingErrorState
1563
+ },
1564
+ // Error(sink state)
1565
+ {
1566
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1567
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1568
+ IterativeParsingErrorState
1569
+ },
1479
1570
// Start
1480
1571
{
1481
1572
IterativeParsingArrayInitialState, // Left bracket
@@ -1490,18 +1581,6 @@ class GenericReader {
1490
1581
IterativeParsingValueState, // Null
1491
1582
IterativeParsingValueState // Number
1492
1583
},
1493
- // Finish(sink state)
1494
- {
1495
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1496
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1497
- IterativeParsingErrorState
1498
- },
1499
- // Error(sink state)
1500
- {
1501
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1502
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1503
- IterativeParsingErrorState
1504
- },
1505
1584
// ObjectInitial
1506
1585
{
1507
1586
IterativeParsingErrorState, // Left bracket
@@ -1530,20 +1609,6 @@ class GenericReader {
1530
1609
IterativeParsingErrorState, // Null
1531
1610
IterativeParsingErrorState // Number
1532
1611
},
1533
- // KeyValueDelimiter
1534
- {
1535
- IterativeParsingArrayInitialState, // Left bracket(push MemberValue state)
1536
- IterativeParsingErrorState, // Right bracket
1537
- IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state)
1538
- IterativeParsingErrorState, // Right curly bracket
1539
- IterativeParsingErrorState, // Comma
1540
- IterativeParsingErrorState, // Colon
1541
- IterativeParsingMemberValueState, // String
1542
- IterativeParsingMemberValueState, // False
1543
- IterativeParsingMemberValueState, // True
1544
- IterativeParsingMemberValueState, // Null
1545
- IterativeParsingMemberValueState // Number
1546
- },
1547
1612
// MemberValue
1548
1613
{
1549
1614
IterativeParsingErrorState, // Left bracket
@@ -1558,20 +1623,6 @@ class GenericReader {
1558
1623
IterativeParsingErrorState, // Null
1559
1624
IterativeParsingErrorState // Number
1560
1625
},
1561
- // MemberDelimiter
1562
- {
1563
- IterativeParsingErrorState, // Left bracket
1564
- IterativeParsingErrorState, // Right bracket
1565
- IterativeParsingErrorState, // Left curly bracket
1566
- IterativeParsingObjectFinishState, // Right curly bracket
1567
- IterativeParsingErrorState, // Comma
1568
- IterativeParsingErrorState, // Colon
1569
- IterativeParsingMemberKeyState, // String
1570
- IterativeParsingErrorState, // False
1571
- IterativeParsingErrorState, // True
1572
- IterativeParsingErrorState, // Null
1573
- IterativeParsingErrorState // Number
1574
- },
1575
1626
// ObjectFinish(sink state)
1576
1627
{
1577
1628
IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
@@ -1606,6 +1657,18 @@ class GenericReader {
1606
1657
IterativeParsingErrorState, // Null
1607
1658
IterativeParsingErrorState // Number
1608
1659
},
1660
+ // ArrayFinish(sink state)
1661
+ {
1662
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1663
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1664
+ IterativeParsingErrorState
1665
+ },
1666
+ // Single Value (sink state)
1667
+ {
1668
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1669
+ IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1670
+ IterativeParsingErrorState
1671
+ },
1609
1672
// ElementDelimiter
1610
1673
{
1611
1674
IterativeParsingArrayInitialState, // Left bracket(push Element state)
@@ -1620,18 +1683,34 @@ class GenericReader {
1620
1683
IterativeParsingElementState, // Null
1621
1684
IterativeParsingElementState // Number
1622
1685
},
1623
- // ArrayFinish(sink state)
1686
+ // MemberDelimiter
1624
1687
{
1625
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1626
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1627
- IterativeParsingErrorState
1688
+ IterativeParsingErrorState, // Left bracket
1689
+ IterativeParsingErrorState, // Right bracket
1690
+ IterativeParsingErrorState, // Left curly bracket
1691
+ IterativeParsingObjectFinishState, // Right curly bracket
1692
+ IterativeParsingErrorState, // Comma
1693
+ IterativeParsingErrorState, // Colon
1694
+ IterativeParsingMemberKeyState, // String
1695
+ IterativeParsingErrorState, // False
1696
+ IterativeParsingErrorState, // True
1697
+ IterativeParsingErrorState, // Null
1698
+ IterativeParsingErrorState // Number
1628
1699
},
1629
- // Single Value (sink state)
1700
+ // KeyValueDelimiter
1630
1701
{
1631
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1632
- IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1633
- IterativeParsingErrorState
1634
- }
1702
+ IterativeParsingArrayInitialState, // Left bracket(push MemberValue state)
1703
+ IterativeParsingErrorState, // Right bracket
1704
+ IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state)
1705
+ IterativeParsingErrorState, // Right curly bracket
1706
+ IterativeParsingErrorState, // Comma
1707
+ IterativeParsingErrorState, // Colon
1708
+ IterativeParsingMemberValueState, // String
1709
+ IterativeParsingMemberValueState, // False
1710
+ IterativeParsingMemberValueState, // True
1711
+ IterativeParsingMemberValueState, // Null
1712
+ IterativeParsingMemberValueState // Number
1713
+ },
1635
1714
}; // End of G
1636
1715
1637
1716
return static_cast <IterativeParsingState>(G[state][token]);
@@ -1812,44 +1891,53 @@ class GenericReader {
1812
1891
}
1813
1892
}
1814
1893
1894
+ RAPIDJSON_FORCEINLINE bool IsIterativeParsingDelimiterState (IterativeParsingState s) {
1895
+ return s >= IterativeParsingElementDelimiterState;
1896
+ }
1897
+
1898
+ RAPIDJSON_FORCEINLINE bool IsIterativeParsingCompleteState (IterativeParsingState s) {
1899
+ return s <= IterativeParsingErrorState;
1900
+ }
1901
+
1815
1902
template <unsigned parseFlags, typename InputStream, typename Handler>
1816
1903
ParseResult IterativeParse (InputStream& is, Handler& handler) {
1817
1904
parseResult_.Clear ();
1818
1905
ClearStackOnExit scope (*this );
1819
1906
IterativeParsingState state = IterativeParsingStartState;
1820
-
1907
+
1821
1908
SkipWhitespaceAndComments<parseFlags>(is);
1822
1909
RAPIDJSON_PARSE_ERROR_EARLY_RETURN (parseResult_);
1823
1910
while (is.Peek () != ' \0 ' ) {
1824
1911
Token t = Tokenize (is.Peek ());
1825
1912
IterativeParsingState n = Predict (state, t);
1826
1913
IterativeParsingState d = Transit<parseFlags>(state, t, n, is, handler);
1827
-
1914
+
1828
1915
if (d == IterativeParsingErrorState) {
1829
1916
HandleError (state, is);
1830
1917
break ;
1831
1918
}
1832
-
1919
+
1833
1920
state = d;
1834
-
1921
+
1835
1922
// Do not further consume streams if a root JSON has been parsed.
1836
1923
if ((parseFlags & kParseStopWhenDoneFlag ) && state == IterativeParsingFinishState)
1837
1924
break ;
1838
-
1925
+
1839
1926
SkipWhitespaceAndComments<parseFlags>(is);
1840
1927
RAPIDJSON_PARSE_ERROR_EARLY_RETURN (parseResult_);
1841
1928
}
1842
-
1929
+
1843
1930
// Handle the end of file.
1844
1931
if (state != IterativeParsingFinishState)
1845
1932
HandleError (state, is);
1846
-
1933
+
1847
1934
return parseResult_;
1848
1935
}
1849
1936
1850
1937
static const size_t kDefaultStackCapacity = 256 ; // !< Default stack capacity in bytes for storing a single decoded string.
1851
1938
internal::Stack<StackAllocator> stack_; // !< A stack for storing decoded string temporarily during non-destructive parsing.
1852
1939
ParseResult parseResult_;
1940
+ IterativeParsingState state_;
1853
1941
}; // class GenericReader
1854
1942
1855
1943
// ! Reader with UTF8 encoding and default allocator.
0 commit comments