541 typedef typename SourceEncoding::Ch
Ch;
548 stack_(stackAllocator, stackCapacity), parseResult_(), state_(IterativeParsingStartState) {}
558 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
561 return IterativeParse<parseFlags>(is, handler);
563 parseResult_.
Clear();
567 SkipWhitespaceAndComments<parseFlags>(is);
568 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
572 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
575 ParseValue<parseFlags>(is, handler);
576 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
579 SkipWhitespaceAndComments<parseFlags>(is);
580 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
584 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
599 template <
typename InputStream,
typename Handler>
601 return Parse<kParseDefaultFlags>(is, handler);
608 parseResult_.
Clear();
609 state_ = IterativeParsingStartState;
619 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
622 SkipWhitespaceAndComments<parseFlags>(is);
624 Token t = Tokenize(is.Peek());
625 IterativeParsingState n = Predict(state_, t);
626 IterativeParsingState d = Transit<parseFlags>(state_, t, n, is, handler);
631 if (d == IterativeParsingErrorState) {
632 HandleError(state_, is);
643 SkipWhitespaceAndComments<parseFlags>(is);
644 if (is.Peek() !=
'\0') {
646 HandleError(state_, is);
659 if (!IsIterativeParsingDelimiterState(n))
666 if (state_ != IterativeParsingFinishState) {
667 HandleError(state_, is);
678 return IsIterativeParsingCompleteState(state_);
691 void SetParseError(
ParseErrorCode code,
size_t offset) { parseResult_.
Set(code, offset); }
698 void ClearStack() {
stack_.Clear(); }
710 template<
unsigned parseFlags,
typename InputStream>
711 void SkipWhitespaceAndComments(InputStream& is) {
716 if (Consume(is,
'*')) {
720 else if (Consume(is,
'*')) {
721 if (Consume(is,
'/'))
729 while (is.Peek() !=
'\0' && is.Take() !=
'\n') {}
739 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
740 void ParseObject(InputStream& is,
Handler& handler) {
747 SkipWhitespaceAndComments<parseFlags>(is);
748 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
750 if (Consume(is,
'}')) {
760 ParseString<parseFlags>(is, handler,
true);
761 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
763 SkipWhitespaceAndComments<parseFlags>(is);
764 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
769 SkipWhitespaceAndComments<parseFlags>(is);
770 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
772 ParseValue<parseFlags>(is, handler);
773 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
775 SkipWhitespaceAndComments<parseFlags>(is);
776 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
783 SkipWhitespaceAndComments<parseFlags>(is);
784 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
796 if (is.Peek() ==
'}') {
807 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
808 void ParseArray(InputStream& is,
Handler& handler) {
815 SkipWhitespaceAndComments<parseFlags>(is);
816 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
818 if (Consume(is,
']')) {
825 ParseValue<parseFlags>(is, handler);
826 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
829 SkipWhitespaceAndComments<parseFlags>(is);
830 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
832 if (Consume(is,
',')) {
833 SkipWhitespaceAndComments<parseFlags>(is);
834 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
836 else if (Consume(is,
']')) {
845 if (is.Peek() ==
']') {
855 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
856 void ParseNull(InputStream& is,
Handler& handler) {
860 if (
RAPIDJSON_LIKELY(Consume(is,
'u') && Consume(is,
'l') && Consume(is,
'l'))) {
868 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
869 void ParseTrue(InputStream& is,
Handler& handler) {
873 if (
RAPIDJSON_LIKELY(Consume(is,
'r') && Consume(is,
'u') && Consume(is,
'e'))) {
881 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
882 void ParseFalse(InputStream& is,
Handler& handler) {
886 if (
RAPIDJSON_LIKELY(Consume(is,
'a') && Consume(is,
'l') && Consume(is,
's') && Consume(is,
'e'))) {
894 template<
typename InputStream>
895 RAPIDJSON_FORCEINLINE
static bool Consume(InputStream& is,
typename InputStream::Ch expect) {
905 template<
typename InputStream>
906 unsigned ParseHex4(InputStream& is,
size_t escapeOffset) {
907 unsigned codepoint = 0;
908 for (
int i = 0; i < 4; i++) {
911 codepoint +=
static_cast<unsigned>(c);
912 if (c >=
'0' && c <=
'9')
914 else if (c >=
'A' && c <=
'F')
915 codepoint -=
'A' - 10;
916 else if (c >=
'a' && c <=
'f')
917 codepoint -=
'a' - 10;
920 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(0);
927 template <
typename CharType>
933 RAPIDJSON_FORCEINLINE
void Put(Ch c) {
934 *stack_.template Push<Ch>() = c;
938 RAPIDJSON_FORCEINLINE
void* Push(
SizeType count) {
940 return stack_.template Push<Ch>(count);
943 size_t Length()
const {
return length_; }
946 return stack_.template Pop<Ch>(length_);
958 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
959 void ParseString(InputStream& is,
Handler& handler,
bool isKey =
false) {
961 InputStream& s(copy.s);
966 bool success =
false;
968 typename InputStream::Ch *head = s.PutBegin();
969 ParseStringToStream<parseFlags, SourceEncoding, SourceEncoding>(s, s);
970 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
971 size_t length = s.PutEnd(head) - 1;
973 const typename TargetEncoding::Ch*
const str =
reinterpret_cast<typename TargetEncoding::Ch*
>(head);
974 success = (isKey ? handler.Key(str,
SizeType(length),
false) : handler.String(str,
SizeType(length), false));
977 StackStream<typename TargetEncoding::Ch> stackStream(
stack_);
978 ParseStringToStream<parseFlags, SourceEncoding, TargetEncoding>(s, stackStream);
979 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
981 const typename TargetEncoding::Ch*
const str = stackStream.Pop();
982 success = (isKey ? handler.Key(str, length,
true) : handler.String(str, length,
true));
990 template<
unsigned parseFlags,
typename SEncoding,
typename TEncoding,
typename InputStream,
typename OutputStream>
991 RAPIDJSON_FORCEINLINE
void ParseStringToStream(InputStream& is, OutputStream& os) {
993#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
994 static const char escape[256] = {
995 Z16, Z16, 0, 0,
'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'/',
996 Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'\\', 0, 0, 0,
997 0, 0,
'\b', 0, 0, 0,
'\f', 0, 0, 0, 0, 0, 0, 0,
'\n', 0,
998 0, 0,
'\r', 0,
'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
999 Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16
1007 ScanCopyUnescapedString(is, os);
1011 size_t escapeOffset = is.Tell();
1014 if ((
sizeof(
Ch) == 1 ||
unsigned(e) < 256) &&
RAPIDJSON_LIKELY(escape[
static_cast<unsigned char>(e)])) {
1016 os.Put(
static_cast<typename TEncoding::Ch
>(escape[
static_cast<unsigned char>(e)]));
1020 unsigned codepoint = ParseHex4(is, escapeOffset);
1021 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
1026 unsigned codepoint2 = ParseHex4(is, escapeOffset);
1027 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
1030 codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000;
1032 TEncoding::Encode(os, codepoint);
1049 size_t offset = is.Tell();
1052 !
Transcoder<SEncoding, TEncoding>::Transcode(is, os))))
1058 template<
typename InputStream,
typename OutputStream>
1059 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(InputStream&, OutputStream&) {
1063#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42)
1065 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(
StringStream& is, StackStream<char>& os) {
1066 const char* p = is.
src_;
1069 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1070 while (p != nextAligned)
1079 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
1080 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
1081 static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
1082 const __m128i dq = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&dquote[0]));
1083 const __m128i bs = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&bslash[0]));
1084 const __m128i sp = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&space[0]));
1087 const __m128i s = _mm_load_si128(
reinterpret_cast<const __m128i *
>(p));
1088 const __m128i t1 = _mm_cmpeq_epi8(s, dq);
1089 const __m128i t2 = _mm_cmpeq_epi8(s, bs);
1090 const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp);
1091 const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
1092 unsigned short r =
static_cast<unsigned short>(_mm_movemask_epi8(x));
1096 unsigned long offset;
1097 _BitScanForward(&offset, r);
1100 length =
static_cast<SizeType>(__builtin_ffs(r) - 1);
1103 char* q =
reinterpret_cast<char*
>(os.Push(length));
1104 for (
size_t i = 0; i < length; i++)
1111 _mm_storeu_si128(
reinterpret_cast<__m128i *
>(os.Push(16)), s);
1122 if (is.src_ == is.dst_) {
1123 SkipUnescapedString(is);
1131 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1132 while (p != nextAligned)
1142 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
1143 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
1144 static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
1145 const __m128i dq = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&dquote[0]));
1146 const __m128i bs = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&bslash[0]));
1147 const __m128i sp = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&space[0]));
1149 for (;; p += 16, q += 16) {
1150 const __m128i s = _mm_load_si128(
reinterpret_cast<const __m128i *
>(p));
1151 const __m128i t1 = _mm_cmpeq_epi8(s, dq);
1152 const __m128i t2 = _mm_cmpeq_epi8(s, bs);
1153 const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp);
1154 const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
1155 unsigned short r =
static_cast<unsigned short>(_mm_movemask_epi8(x));
1159 unsigned long offset;
1160 _BitScanForward(&offset, r);
1163 length =
static_cast<size_t>(__builtin_ffs(r) - 1);
1165 for (
const char* pend = p + length; p != pend; )
1169 _mm_storeu_si128(
reinterpret_cast<__m128i *
>(q), s);
1182 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1183 for (; p != nextAligned; p++)
1185 is.src_ = is.dst_ = p;
1190 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
1191 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
1192 static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
1193 const __m128i dq = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&dquote[0]));
1194 const __m128i bs = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&bslash[0]));
1195 const __m128i sp = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&space[0]));
1198 const __m128i s = _mm_load_si128(
reinterpret_cast<const __m128i *
>(p));
1199 const __m128i t1 = _mm_cmpeq_epi8(s, dq);
1200 const __m128i t2 = _mm_cmpeq_epi8(s, bs);
1201 const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp);
1202 const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
1203 unsigned short r =
static_cast<unsigned short>(_mm_movemask_epi8(x));
1207 unsigned long offset;
1208 _BitScanForward(&offset, r);
1211 length =
static_cast<size_t>(__builtin_ffs(r) - 1);
1218 is.src_ = is.dst_ = p;
1220#elif defined(RAPIDJSON_NEON)
1222 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(
StringStream& is, StackStream<char>& os) {
1223 const char* p = is.
src_;
1226 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1227 while (p != nextAligned)
1236 const uint8x16_t s0 = vmovq_n_u8(
'"');
1237 const uint8x16_t s1 = vmovq_n_u8(
'\\');
1238 const uint8x16_t s2 = vmovq_n_u8(
'\b');
1239 const uint8x16_t s3 = vmovq_n_u8(32);
1242 const uint8x16_t s = vld1q_u8(
reinterpret_cast<const uint8_t *
>(p));
1243 uint8x16_t x = vceqq_u8(s, s0);
1244 x = vorrq_u8(x, vceqq_u8(s, s1));
1245 x = vorrq_u8(x, vceqq_u8(s, s2));
1246 x = vorrq_u8(x, vcltq_u8(s, s3));
1249 uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0);
1250 uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1);
1253 bool escaped =
false;
1256 uint32_t lz = RAPIDJSON_CLZLL(high);
1257 length = 8 + (lz >> 3);
1261 uint32_t lz = RAPIDJSON_CLZLL(low);
1267 char* q =
reinterpret_cast<char*
>(os.Push(length));
1268 for (
size_t i = 0; i < length; i++)
1275 vst1q_u8(
reinterpret_cast<uint8_t *
>(os.Push(16)), s);
1286 if (is.src_ == is.dst_) {
1287 SkipUnescapedString(is);
1295 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1296 while (p != nextAligned)
1306 const uint8x16_t s0 = vmovq_n_u8(
'"');
1307 const uint8x16_t s1 = vmovq_n_u8(
'\\');
1308 const uint8x16_t s2 = vmovq_n_u8(
'\b');
1309 const uint8x16_t s3 = vmovq_n_u8(32);
1311 for (;; p += 16, q += 16) {
1312 const uint8x16_t s = vld1q_u8(
reinterpret_cast<uint8_t *
>(p));
1313 uint8x16_t x = vceqq_u8(s, s0);
1314 x = vorrq_u8(x, vceqq_u8(s, s1));
1315 x = vorrq_u8(x, vceqq_u8(s, s2));
1316 x = vorrq_u8(x, vcltq_u8(s, s3));
1319 uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0);
1320 uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1);
1323 bool escaped =
false;
1326 uint32_t lz = RAPIDJSON_CLZLL(high);
1327 length = 8 + (lz >> 3);
1331 uint32_t lz = RAPIDJSON_CLZLL(low);
1336 for (
const char* pend = p + length; p != pend; ) {
1341 vst1q_u8(
reinterpret_cast<uint8_t *
>(q), s);
1354 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1355 for (; p != nextAligned; p++)
1357 is.src_ = is.dst_ = p;
1362 const uint8x16_t s0 = vmovq_n_u8(
'"');
1363 const uint8x16_t s1 = vmovq_n_u8(
'\\');
1364 const uint8x16_t s2 = vmovq_n_u8(
'\b');
1365 const uint8x16_t s3 = vmovq_n_u8(32);
1368 const uint8x16_t s = vld1q_u8(
reinterpret_cast<uint8_t *
>(p));
1369 uint8x16_t x = vceqq_u8(s, s0);
1370 x = vorrq_u8(x, vceqq_u8(s, s1));
1371 x = vorrq_u8(x, vceqq_u8(s, s2));
1372 x = vorrq_u8(x, vcltq_u8(s, s3));
1375 uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0);
1376 uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1);
1380 uint32_t lz = RAPIDJSON_CLZLL(high);
1385 uint32_t lz = RAPIDJSON_CLZLL(low);
1391 is.src_ = is.dst_ = p;
1395 template<
typename InputStream,
bool backup,
bool pushOnTake>
1398 template<
typename InputStream>
1401 typedef typename InputStream::Ch
Ch;
1405 RAPIDJSON_FORCEINLINE Ch Peek()
const {
return is.Peek(); }
1406 RAPIDJSON_FORCEINLINE Ch TakePush() {
return is.Take(); }
1407 RAPIDJSON_FORCEINLINE Ch Take() {
return is.Take(); }
1408 RAPIDJSON_FORCEINLINE
void Push(
char) {}
1410 size_t Tell() {
return is.Tell(); }
1411 size_t Length() {
return 0; }
1412 const char* Pop() {
return 0; }
1420 template<
typename InputStream>
1426 RAPIDJSON_FORCEINLINE Ch TakePush() {
1427 stackStream.Put(
static_cast<char>(Base::is.Peek()));
1428 return Base::is.Take();
1431 RAPIDJSON_FORCEINLINE
void Push(
char c) {
1435 size_t Length() {
return stackStream.Length(); }
1438 stackStream.Put(
'\0');
1439 return stackStream.Pop();
1446 template<
typename InputStream>
1452 RAPIDJSON_FORCEINLINE
Ch Take() {
return Base::TakePush(); }
1455 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
1456 void ParseNumber(InputStream& is,
Handler& handler) {
1465 size_t startOffset = s.Tell();
1467 bool useNanOrInf =
false;
1470 bool minus = Consume(s,
'-');
1475 bool use64bit =
false;
1476 int significandDigit = 0;
1482 i =
static_cast<unsigned>(s.TakePush() -
'0');
1493 i = i * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1505 i = i * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1511 if (Consume(s,
'N')) {
1512 if (Consume(s,
'a') && Consume(s,
'N')) {
1513 d = std::numeric_limits<double>::quiet_NaN();
1518 if (Consume(s,
'n') && Consume(s,
'f')) {
1519 d = (minus ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity());
1523 && Consume(s,
'i') && Consume(s,
't') && Consume(s,
'y')))) {
1537 bool useDouble =
false;
1543 d =
static_cast<double>(i64);
1547 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1554 d =
static_cast<double>(i64);
1558 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1566 d = d * 10 + (s.TakePush() -
'0');
1572 size_t decimalPosition;
1573 if (Consume(s,
'.')) {
1574 decimalPosition = s.Length();
1589 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1596 d =
static_cast<double>(i64);
1599 d =
static_cast<double>(use64bit ? i64 : i);
1605 if (significandDigit < 17) {
1606 d = d * 10.0 + (s.TakePush() -
'0');
1616 decimalPosition = s.Length();
1620 if (Consume(s,
'e') || Consume(s,
'E')) {
1622 d =
static_cast<double>(use64bit ? i64 : i);
1626 bool expMinus =
false;
1627 if (Consume(s,
'+'))
1629 else if (Consume(s,
'-'))
1633 exp =
static_cast<int>(s.Take() -
'0');
1642 int maxExp = (expFrac + 2147483639) / 10;
1645 exp =
exp * 10 +
static_cast<int>(s.Take() -
'0');
1653 int maxExp = 308 - expFrac;
1655 exp =
exp * 10 +
static_cast<int>(s.Take() -
'0');
1674 typename InputStream::Ch* head = is.PutBegin();
1675 const size_t length = s.Tell() - startOffset;
1678 const typename TargetEncoding::Ch*
const str =
reinterpret_cast<typename TargetEncoding::Ch*
>(head);
1679 cont = handler.RawNumber(str,
SizeType(length),
false);
1684 StackStream<typename TargetEncoding::Ch> dstStream(
stack_);
1685 while (numCharsToCopy--) {
1688 dstStream.Put(
'\0');
1689 const typename TargetEncoding::Ch* str = dstStream.Pop();
1691 cont = handler.RawNumber(str,
SizeType(length),
true);
1695 size_t length = s.Length();
1696 const char* decimal = s.Pop();
1699 int p =
exp + expFrac;
1701 d = internal::StrtodFullPrecision(d, p, decimal, length, decimalPosition, exp);
1703 d = internal::StrtodNormalPrecision(d, p);
1706 if (d > (std::numeric_limits<double>::max)()) {
1712 cont = handler.Double(minus ? -d : d);
1714 else if (useNanOrInf) {
1715 cont = handler.Double(d);
1720 cont = handler.Int64(
static_cast<int64_t
>(~i64 + 1));
1722 cont = handler.Uint64(i64);
1726 cont = handler.Int(
static_cast<int32_t
>(~i + 1));
1728 cont = handler.Uint(i);
1737 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
1738 void ParseValue(InputStream& is,
Handler& handler) {
1739 switch (is.Peek()) {
1740 case 'n': ParseNull <parseFlags>(is, handler);
break;
1741 case 't': ParseTrue <parseFlags>(is, handler);
break;
1742 case 'f': ParseFalse <parseFlags>(is, handler);
break;
1743 case '"': ParseString<parseFlags>(is, handler);
break;
1744 case '{': ParseObject<parseFlags>(is, handler);
break;
1745 case '[': ParseArray <parseFlags>(is, handler);
break;
1747 ParseNumber<parseFlags>(is, handler);
1756 enum IterativeParsingState {
1757 IterativeParsingFinishState = 0,
1758 IterativeParsingErrorState,
1759 IterativeParsingStartState,
1762 IterativeParsingObjectInitialState,
1763 IterativeParsingMemberKeyState,
1764 IterativeParsingMemberValueState,
1765 IterativeParsingObjectFinishState,
1768 IterativeParsingArrayInitialState,
1769 IterativeParsingElementState,
1770 IterativeParsingArrayFinishState,
1773 IterativeParsingValueState,
1776 IterativeParsingElementDelimiterState,
1777 IterativeParsingMemberDelimiterState,
1778 IterativeParsingKeyValueDelimiterState,
1780 cIterativeParsingStateCount
1785 LeftBracketToken = 0,
1788 LeftCurlyBracketToken,
1789 RightCurlyBracketToken,
1803 RAPIDJSON_FORCEINLINE Token Tokenize(
Ch c)
const {
1806#define N NumberToken
1807#define N16 N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N
1809 static const unsigned char tokenMap[256] = {
1812 N, N, StringToken, N, N, N, N, N, N, N, N, N, CommaToken, N, N, N,
1813 N, N, N, N, N, N, N, N, N, N, ColonToken, N, N, N, N, N,
1815 N, N, N, N, N, N, N, N, N, N, N, LeftBracketToken, N, RightBracketToken, N, N,
1816 N, N, N, N, N, N, FalseToken, N, N, N, N, N, N, N, NullToken, N,
1817 N, N, N, N, TrueToken, N, N, N, N, N, N, LeftCurlyBracketToken, N, RightCurlyBracketToken, N, N,
1818 N16, N16, N16, N16, N16, N16, N16, N16
1824 if (
sizeof(
Ch) == 1 ||
static_cast<unsigned>(c) < 256)
1825 return static_cast<Token
>(tokenMap[
static_cast<unsigned char>(c)]);
1830 RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token)
const {
1832 static const char G[cIterativeParsingStateCount][kTokenCount] = {
1835 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1836 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1837 IterativeParsingErrorState
1841 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1842 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1843 IterativeParsingErrorState
1847 IterativeParsingArrayInitialState,
1848 IterativeParsingErrorState,
1849 IterativeParsingObjectInitialState,
1850 IterativeParsingErrorState,
1851 IterativeParsingErrorState,
1852 IterativeParsingErrorState,
1853 IterativeParsingValueState,
1854 IterativeParsingValueState,
1855 IterativeParsingValueState,
1856 IterativeParsingValueState,
1857 IterativeParsingValueState
1861 IterativeParsingErrorState,
1862 IterativeParsingErrorState,
1863 IterativeParsingErrorState,
1864 IterativeParsingObjectFinishState,
1865 IterativeParsingErrorState,
1866 IterativeParsingErrorState,
1867 IterativeParsingMemberKeyState,
1868 IterativeParsingErrorState,
1869 IterativeParsingErrorState,
1870 IterativeParsingErrorState,
1871 IterativeParsingErrorState
1875 IterativeParsingErrorState,
1876 IterativeParsingErrorState,
1877 IterativeParsingErrorState,
1878 IterativeParsingErrorState,
1879 IterativeParsingErrorState,
1880 IterativeParsingKeyValueDelimiterState,
1881 IterativeParsingErrorState,
1882 IterativeParsingErrorState,
1883 IterativeParsingErrorState,
1884 IterativeParsingErrorState,
1885 IterativeParsingErrorState
1889 IterativeParsingErrorState,
1890 IterativeParsingErrorState,
1891 IterativeParsingErrorState,
1892 IterativeParsingObjectFinishState,
1893 IterativeParsingMemberDelimiterState,
1894 IterativeParsingErrorState,
1895 IterativeParsingErrorState,
1896 IterativeParsingErrorState,
1897 IterativeParsingErrorState,
1898 IterativeParsingErrorState,
1899 IterativeParsingErrorState
1903 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1904 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1905 IterativeParsingErrorState
1909 IterativeParsingArrayInitialState,
1910 IterativeParsingArrayFinishState,
1911 IterativeParsingObjectInitialState,
1912 IterativeParsingErrorState,
1913 IterativeParsingErrorState,
1914 IterativeParsingErrorState,
1915 IterativeParsingElementState,
1916 IterativeParsingElementState,
1917 IterativeParsingElementState,
1918 IterativeParsingElementState,
1919 IterativeParsingElementState
1923 IterativeParsingErrorState,
1924 IterativeParsingArrayFinishState,
1925 IterativeParsingErrorState,
1926 IterativeParsingErrorState,
1927 IterativeParsingElementDelimiterState,
1928 IterativeParsingErrorState,
1929 IterativeParsingErrorState,
1930 IterativeParsingErrorState,
1931 IterativeParsingErrorState,
1932 IterativeParsingErrorState,
1933 IterativeParsingErrorState
1937 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1938 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1939 IterativeParsingErrorState
1943 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1944 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1945 IterativeParsingErrorState
1949 IterativeParsingArrayInitialState,
1950 IterativeParsingArrayFinishState,
1951 IterativeParsingObjectInitialState,
1952 IterativeParsingErrorState,
1953 IterativeParsingErrorState,
1954 IterativeParsingErrorState,
1955 IterativeParsingElementState,
1956 IterativeParsingElementState,
1957 IterativeParsingElementState,
1958 IterativeParsingElementState,
1959 IterativeParsingElementState
1963 IterativeParsingErrorState,
1964 IterativeParsingErrorState,
1965 IterativeParsingErrorState,
1966 IterativeParsingObjectFinishState,
1967 IterativeParsingErrorState,
1968 IterativeParsingErrorState,
1969 IterativeParsingMemberKeyState,
1970 IterativeParsingErrorState,
1971 IterativeParsingErrorState,
1972 IterativeParsingErrorState,
1973 IterativeParsingErrorState
1977 IterativeParsingArrayInitialState,
1978 IterativeParsingErrorState,
1979 IterativeParsingObjectInitialState,
1980 IterativeParsingErrorState,
1981 IterativeParsingErrorState,
1982 IterativeParsingErrorState,
1983 IterativeParsingMemberValueState,
1984 IterativeParsingMemberValueState,
1985 IterativeParsingMemberValueState,
1986 IterativeParsingMemberValueState,
1987 IterativeParsingMemberValueState
1991 return static_cast<IterativeParsingState
>(G[state][token]);
1996 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
1997 RAPIDJSON_FORCEINLINE IterativeParsingState Transit(IterativeParsingState src, Token token, IterativeParsingState dst, InputStream& is,
Handler& handler) {
2001 case IterativeParsingErrorState:
2004 case IterativeParsingObjectInitialState:
2005 case IterativeParsingArrayInitialState:
2009 IterativeParsingState n = src;
2010 if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState)
2011 n = IterativeParsingElementState;
2012 else if (src == IterativeParsingKeyValueDelimiterState)
2013 n = IterativeParsingMemberValueState;
2015 *
stack_.template Push<SizeType>(1) = n;
2017 *
stack_.template Push<SizeType>(1) = 0;
2019 bool hr = (dst == IterativeParsingObjectInitialState) ? handler.StartObject() : handler.StartArray();
2023 return IterativeParsingErrorState;
2031 case IterativeParsingMemberKeyState:
2032 ParseString<parseFlags>(is, handler,
true);
2034 return IterativeParsingErrorState;
2038 case IterativeParsingKeyValueDelimiterState:
2043 case IterativeParsingMemberValueState:
2045 ParseValue<parseFlags>(is, handler);
2047 return IterativeParsingErrorState;
2051 case IterativeParsingElementState:
2053 ParseValue<parseFlags>(is, handler);
2055 return IterativeParsingErrorState;
2059 case IterativeParsingMemberDelimiterState:
2060 case IterativeParsingElementDelimiterState:
2063 *
stack_.template Top<SizeType>() = *
stack_.template Top<SizeType>() + 1;
2066 case IterativeParsingObjectFinishState:
2071 return IterativeParsingErrorState;
2076 if (src == IterativeParsingMemberValueState)
2079 IterativeParsingState n =
static_cast<IterativeParsingState
>(*
stack_.template Pop<SizeType>(1));
2081 if (n == IterativeParsingStartState)
2082 n = IterativeParsingFinishState;
2084 bool hr = handler.EndObject(c);
2088 return IterativeParsingErrorState;
2096 case IterativeParsingArrayFinishState:
2101 return IterativeParsingErrorState;
2106 if (src == IterativeParsingElementState)
2109 IterativeParsingState n =
static_cast<IterativeParsingState
>(*
stack_.template Pop<SizeType>(1));
2111 if (n == IterativeParsingStartState)
2112 n = IterativeParsingFinishState;
2114 bool hr = handler.EndArray(c);
2118 return IterativeParsingErrorState;
2140 ParseValue<parseFlags>(is, handler);
2142 return IterativeParsingErrorState;
2144 return IterativeParsingFinishState;
2148 template <
typename InputStream>
2149 void HandleError(IterativeParsingState src, InputStream& is) {
2158 case IterativeParsingObjectInitialState:
2162 case IterativeParsingKeyValueDelimiterState:
2163 case IterativeParsingArrayInitialState:
2169 RAPIDJSON_FORCEINLINE
bool IsIterativeParsingDelimiterState(IterativeParsingState s)
const {
2170 return s >= IterativeParsingElementDelimiterState;
2173 RAPIDJSON_FORCEINLINE
bool IsIterativeParsingCompleteState(IterativeParsingState s)
const {
2174 return s <= IterativeParsingErrorState;
2177 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
2179 parseResult_.
Clear();
2180 ClearStackOnExit scope(*
this);
2181 IterativeParsingState state = IterativeParsingStartState;
2183 SkipWhitespaceAndComments<parseFlags>(is);
2184 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
2185 while (is.Peek() !=
'\0') {
2186 Token t = Tokenize(is.Peek());
2187 IterativeParsingState n = Predict(state, t);
2188 IterativeParsingState d = Transit<parseFlags>(state, t, n, is, handler);
2190 if (d == IterativeParsingErrorState) {
2191 HandleError(state, is);
2201 SkipWhitespaceAndComments<parseFlags>(is);
2202 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
2206 if (state != IterativeParsingFinishState)
2207 HandleError(state, is);
2209 return parseResult_;
2215 IterativeParsingState state_;