539 typedef typename SourceEncoding::Ch
Ch;
545 GenericReader(StackAllocator* stackAllocator = 0,
size_t stackCapacity = kDefaultStackCapacity) :
546 stack_(stackAllocator, stackCapacity), parseResult_(), state_(IterativeParsingStartState) {}
556 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
559 return IterativeParse<parseFlags>(is, handler);
561 parseResult_.
Clear();
563 ClearStackOnExit scope(*
this);
565 SkipWhitespaceAndComments<parseFlags>(is);
566 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
570 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
573 ParseValue<parseFlags>(is, handler);
574 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
577 SkipWhitespaceAndComments<parseFlags>(is);
578 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
582 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
597 template <
typename InputStream,
typename Handler>
606 parseResult_.
Clear();
607 state_ = IterativeParsingStartState;
617 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
620 SkipWhitespaceAndComments<parseFlags>(is);
622 Token t = Tokenize(is.Peek());
623 IterativeParsingState n = Predict(state_, t);
624 IterativeParsingState
d = Transit<parseFlags>(state_, t, n, is, handler);
629 if (
d == IterativeParsingErrorState) {
630 HandleError(state_, is);
641 SkipWhitespaceAndComments<parseFlags>(is);
642 if (is.Peek() !=
'\0') {
644 HandleError(state_, is);
657 if (!IsIterativeParsingDelimiterState(n))
664 if (state_ != IterativeParsingFinishState) {
665 HandleError(state_, is);
676 return IsIterativeParsingCompleteState(state_);
696 void ClearStack() { stack_.
Clear(); }
699 struct ClearStackOnExit {
701 ~ClearStackOnExit() { r_.ClearStack(); }
704 ClearStackOnExit(
const ClearStackOnExit&);
705 ClearStackOnExit& operator=(
const ClearStackOnExit&);
708 template<
unsigned parseFlags,
typename InputStream>
709 void SkipWhitespaceAndComments(InputStream& is) {
714 if (Consume(is,
'*')) {
718 else if (Consume(is,
'*')) {
719 if (Consume(is,
'/'))
727 while (is.Peek() !=
'\0' && is.Take() !=
'\n') {}
737 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
738 void ParseObject(InputStream& is,
Handler& handler) {
745 SkipWhitespaceAndComments<parseFlags>(is);
746 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
748 if (Consume(is,
'}')) {
758 ParseString<parseFlags>(is, handler,
true);
759 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
761 SkipWhitespaceAndComments<parseFlags>(is);
762 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
767 SkipWhitespaceAndComments<parseFlags>(is);
768 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
770 ParseValue<parseFlags>(is, handler);
771 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
773 SkipWhitespaceAndComments<parseFlags>(is);
774 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
781 SkipWhitespaceAndComments<parseFlags>(is);
782 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
794 if (is.Peek() ==
'}') {
805 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
806 void ParseArray(InputStream& is,
Handler& handler) {
813 SkipWhitespaceAndComments<parseFlags>(is);
814 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
816 if (Consume(is,
']')) {
823 ParseValue<parseFlags>(is, handler);
824 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
827 SkipWhitespaceAndComments<parseFlags>(is);
828 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
830 if (Consume(is,
',')) {
831 SkipWhitespaceAndComments<parseFlags>(is);
832 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
834 else if (Consume(is,
']')) {
843 if (is.Peek() ==
']') {
853 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
854 void ParseNull(InputStream& is,
Handler& handler) {
858 if (
RAPIDJSON_LIKELY(Consume(is,
'u') && Consume(is,
'l') && Consume(is,
'l'))) {
866 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
867 void ParseTrue(InputStream& is,
Handler& handler) {
871 if (
RAPIDJSON_LIKELY(Consume(is,
'r') && Consume(is,
'u') && Consume(is,
'e'))) {
879 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
880 void ParseFalse(InputStream& is,
Handler& handler) {
884 if (
RAPIDJSON_LIKELY(Consume(is,
'a') && Consume(is,
'l') && Consume(is,
's') && Consume(is,
'e'))) {
892 template<
typename InputStream>
893 RAPIDJSON_FORCEINLINE
static bool Consume(InputStream& is,
typename InputStream::Ch
expect) {
903 template<
typename InputStream>
904 unsigned ParseHex4(InputStream& is,
size_t escapeOffset) {
905 unsigned codepoint = 0;
906 for (
int i = 0; i < 4; i++) {
909 codepoint +=
static_cast<unsigned>(c);
910 if (c >=
'0' && c <=
'9')
912 else if (c >=
'A' && c <=
'F')
913 codepoint -=
'A' - 10;
914 else if (c >=
'a' && c <=
'f')
915 codepoint -=
'a' - 10;
918 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(0);
925 template <
typename CharType>
931 RAPIDJSON_FORCEINLINE
void Put(Ch c) {
932 *stack_.template Push<Ch>() = c;
938 return stack_.template Push<Ch>(
count);
941 size_t Length()
const {
return length_; }
944 return stack_.template Pop<Ch>(length_);
948 StackStream(
const StackStream&);
949 StackStream& operator=(
const StackStream&);
956 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
957 void ParseString(InputStream& is,
Handler& handler,
bool isKey =
false) {
959 InputStream&
s(
copy.s);
966 typename InputStream::Ch *head =
s.PutBegin();
967 ParseStringToStream<parseFlags, SourceEncoding, SourceEncoding>(
s,
s);
968 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
969 size_t length =
s.PutEnd(head) - 1;
971 const typename TargetEncoding::Ch*
const str =
reinterpret_cast<typename TargetEncoding::Ch*
>(head);
975 StackStream<typename TargetEncoding::Ch> stackStream(stack_);
976 ParseStringToStream<parseFlags, SourceEncoding, TargetEncoding>(
s, stackStream);
977 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
979 const typename TargetEncoding::Ch*
const str = stackStream.Pop();
980 success = (isKey ? handler.Key(str, length,
true) : handler.String(str, length,
true));
988 template<
unsigned parseFlags,
typename SEncoding,
typename TEncoding,
typename InputStream,
typename OutputStream>
989 RAPIDJSON_FORCEINLINE
void ParseStringToStream(InputStream& is, OutputStream&
os) {
991#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
992 static const char escape[256] = {
993 Z16,
Z16, 0, 0,
'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'/',
994 Z16,
Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'\\', 0, 0, 0,
995 0, 0,
'\b', 0, 0, 0,
'\f', 0, 0, 0, 0, 0, 0, 0,
'\n', 0,
996 0, 0,
'\r', 0,
'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1005 ScanCopyUnescapedString(is,
os);
1009 size_t escapeOffset = is.Tell();
1012 if ((
sizeof(
Ch) == 1 ||
unsigned(e) < 256) &&
RAPIDJSON_LIKELY(escape[
static_cast<unsigned char>(e)])) {
1014 os.Put(
static_cast<typename TEncoding::Ch
>(escape[
static_cast<unsigned char>(e)]));
1018 unsigned codepoint = ParseHex4(is, escapeOffset);
1019 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
1024 unsigned codepoint2 = ParseHex4(is, escapeOffset);
1025 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
1028 codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000;
1030 TEncoding::Encode(
os, codepoint);
1047 size_t offset = is.Tell();
1056 template<
typename InputStream,
typename OutputStream>
1057 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(InputStream&, OutputStream&) {
1061#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42)
1063 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(
StringStream& is, StackStream<char>&
os) {
1064 const char*
p = is.src_;
1067 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(
p) + 15) &
static_cast<size_t>(~15));
1068 while (
p != nextAligned)
1077 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
1078 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
1079 static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
1080 const __m128i dq = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&dquote[0]));
1081 const __m128i bs = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&bslash[0]));
1082 const __m128i
sp = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&space[0]));
1085 const __m128i
s = _mm_load_si128(
reinterpret_cast<const __m128i *
>(
p));
1086 const __m128i t1 = _mm_cmpeq_epi8(
s, dq);
1087 const __m128i t2 = _mm_cmpeq_epi8(
s, bs);
1088 const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(
s, sp), sp);
1089 const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
1090 unsigned short r =
static_cast<unsigned short>(_mm_movemask_epi8(x));
1094 unsigned long offset;
1095 _BitScanForward(&offset,
r);
1098 length =
static_cast<SizeType>(__builtin_ffs(
r) - 1);
1101 char* q =
reinterpret_cast<char*
>(
os.Push(length));
1102 for (
size_t i = 0; i < length; i++)
1109 _mm_storeu_si128(
reinterpret_cast<__m128i *
>(
os.Push(16)),
s);
1120 if (is.src_ == is.dst_) {
1121 SkipUnescapedString(is);
1129 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(
p) + 15) &
static_cast<size_t>(~15));
1130 while (
p != nextAligned)
1140 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
1141 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
1142 static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
1143 const __m128i dq = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&dquote[0]));
1144 const __m128i bs = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&bslash[0]));
1145 const __m128i
sp = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&space[0]));
1147 for (;;
p += 16, q += 16) {
1148 const __m128i
s = _mm_load_si128(
reinterpret_cast<const __m128i *
>(
p));
1149 const __m128i t1 = _mm_cmpeq_epi8(
s, dq);
1150 const __m128i t2 = _mm_cmpeq_epi8(
s, bs);
1151 const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(
s, sp), sp);
1152 const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
1153 unsigned short r =
static_cast<unsigned short>(_mm_movemask_epi8(x));
1157 unsigned long offset;
1158 _BitScanForward(&offset,
r);
1161 length =
static_cast<size_t>(__builtin_ffs(
r) - 1);
1163 for (
const char* pend =
p + length;
p != pend; )
1167 _mm_storeu_si128(
reinterpret_cast<__m128i *
>(q),
s);
1180 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(
p) + 15) &
static_cast<size_t>(~15));
1181 for (;
p != nextAligned;
p++)
1183 is.src_ = is.dst_ =
p;
1188 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
1189 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
1190 static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
1191 const __m128i dq = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&dquote[0]));
1192 const __m128i bs = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&bslash[0]));
1193 const __m128i
sp = _mm_loadu_si128(
reinterpret_cast<const __m128i *
>(&space[0]));
1196 const __m128i
s = _mm_load_si128(
reinterpret_cast<const __m128i *
>(
p));
1197 const __m128i t1 = _mm_cmpeq_epi8(
s, dq);
1198 const __m128i t2 = _mm_cmpeq_epi8(
s, bs);
1199 const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(
s, sp), sp);
1200 const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
1201 unsigned short r =
static_cast<unsigned short>(_mm_movemask_epi8(x));
1205 unsigned long offset;
1206 _BitScanForward(&offset,
r);
1209 length =
static_cast<size_t>(__builtin_ffs(
r) - 1);
1216 is.src_ = is.dst_ =
p;
1218#elif defined(RAPIDJSON_NEON)
1220 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(
StringStream& is, StackStream<char>&
os) {
1221 const char*
p = is.src_;
1224 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(
p) + 15) &
static_cast<size_t>(~15));
1225 while (
p != nextAligned)
1234 const uint8x16_t s0 = vmovq_n_u8(
'"');
1235 const uint8x16_t s1 = vmovq_n_u8(
'\\');
1236 const uint8x16_t s2 = vmovq_n_u8(
'\b');
1237 const uint8x16_t s3 = vmovq_n_u8(32);
1240 const uint8x16_t
s = vld1q_u8(
reinterpret_cast<const uint8_t *
>(
p));
1241 uint8x16_t x = vceqq_u8(
s, s0);
1242 x = vorrq_u8(x, vceqq_u8(
s, s1));
1243 x = vorrq_u8(x, vceqq_u8(
s, s2));
1244 x = vorrq_u8(x, vcltq_u8(
s, s3));
1247 uint64_t low = vgetq_lane_u64(
reinterpret_cast<uint64x2_t
>(x), 0);
1248 uint64_t high = vgetq_lane_u64(
reinterpret_cast<uint64x2_t
>(x), 1);
1251 bool escaped =
false;
1254 unsigned lz = (unsigned)__builtin_clzll(high);;
1255 length = 8 + (lz >> 3);
1259 unsigned lz = (unsigned)__builtin_clzll(low);;
1265 char* q =
reinterpret_cast<char*
>(
os.Push(length));
1266 for (
size_t i = 0; i < length; i++)
1273 vst1q_u8(
reinterpret_cast<uint8_t *
>(
os.Push(16)),
s);
1284 if (is.src_ == is.dst_) {
1285 SkipUnescapedString(is);
1293 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(
p) + 15) &
static_cast<size_t>(~15));
1294 while (
p != nextAligned)
1304 const uint8x16_t s0 = vmovq_n_u8(
'"');
1305 const uint8x16_t s1 = vmovq_n_u8(
'\\');
1306 const uint8x16_t s2 = vmovq_n_u8(
'\b');
1307 const uint8x16_t s3 = vmovq_n_u8(32);
1309 for (;;
p += 16, q += 16) {
1310 const uint8x16_t
s = vld1q_u8(
reinterpret_cast<uint8_t *
>(
p));
1311 uint8x16_t x = vceqq_u8(
s, s0);
1312 x = vorrq_u8(x, vceqq_u8(
s, s1));
1313 x = vorrq_u8(x, vceqq_u8(
s, s2));
1314 x = vorrq_u8(x, vcltq_u8(
s, s3));
1317 uint64_t low = vgetq_lane_u64(
reinterpret_cast<uint64x2_t
>(x), 0);
1318 uint64_t high = vgetq_lane_u64(
reinterpret_cast<uint64x2_t
>(x), 1);
1321 bool escaped =
false;
1324 unsigned lz = (unsigned)__builtin_clzll(high);
1325 length = 8 + (lz >> 3);
1329 unsigned lz = (unsigned)__builtin_clzll(low);
1334 for (
const char* pend =
p + length;
p != pend; ) {
1339 vst1q_u8(
reinterpret_cast<uint8_t *
>(q),
s);
1352 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(
p) + 15) &
static_cast<size_t>(~15));
1353 for (;
p != nextAligned;
p++)
1355 is.src_ = is.dst_ =
p;
1360 const uint8x16_t s0 = vmovq_n_u8(
'"');
1361 const uint8x16_t s1 = vmovq_n_u8(
'\\');
1362 const uint8x16_t s2 = vmovq_n_u8(
'\b');
1363 const uint8x16_t s3 = vmovq_n_u8(32);
1366 const uint8x16_t
s = vld1q_u8(
reinterpret_cast<uint8_t *
>(
p));
1367 uint8x16_t x = vceqq_u8(
s, s0);
1368 x = vorrq_u8(x, vceqq_u8(
s, s1));
1369 x = vorrq_u8(x, vceqq_u8(
s, s2));
1370 x = vorrq_u8(x, vcltq_u8(
s, s3));
1373 uint64_t low = vgetq_lane_u64(
reinterpret_cast<uint64x2_t
>(x), 0);
1374 uint64_t high = vgetq_lane_u64(
reinterpret_cast<uint64x2_t
>(x), 1);
1378 int lz = __builtin_clzll(high);
1383 int lz = __builtin_clzll(low);
1389 is.src_ = is.dst_ =
p;
1393 template<
typename InputStream,
bool backup,
bool pushOnTake>
1396 template<
typename InputStream>
1397 class NumberStream<InputStream, false, false> {
1399 typedef typename InputStream::Ch
Ch;
1401 NumberStream(
GenericReader& reader, InputStream&
s) : is(
s) { (void)reader; }
1403 RAPIDJSON_FORCEINLINE
Ch Peek()
const {
return is.Peek(); }
1404 RAPIDJSON_FORCEINLINE
Ch TakePush() {
return is.Take(); }
1405 RAPIDJSON_FORCEINLINE
Ch Take() {
return is.Take(); }
1406 RAPIDJSON_FORCEINLINE
void Push(
char) {}
1408 size_t Tell() {
return is.Tell(); }
1409 size_t Length() {
return 0; }
1410 const char* Pop() {
return 0; }
1413 NumberStream& operator=(
const NumberStream&);
1418 template<
typename InputStream>
1419 class NumberStream<InputStream, true, false> :
public NumberStream<InputStream, false, false> {
1420 typedef NumberStream<InputStream, false, false> Base;
1422 NumberStream(
GenericReader& reader, InputStream& is) : Base(reader, is), stackStream(reader.stack_) {}
1424 RAPIDJSON_FORCEINLINE
Ch TakePush() {
1425 stackStream.Put(
static_cast<char>(Base::is.Peek()));
1426 return Base::is.Take();
1429 RAPIDJSON_FORCEINLINE
void Push(
char c) {
1433 size_t Length() {
return stackStream.Length(); }
1436 stackStream.Put(
'\0');
1437 return stackStream.Pop();
1441 StackStream<char> stackStream;
1444 template<
typename InputStream>
1445 class NumberStream<InputStream, true, true> :
public NumberStream<InputStream, true, false> {
1446 typedef NumberStream<InputStream, true, false> Base;
1448 NumberStream(
GenericReader& reader, InputStream& is) : Base(reader, is) {}
1450 RAPIDJSON_FORCEINLINE
Ch Take() {
return Base::TakePush(); }
1453 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
1454 void ParseNumber(InputStream& is,
Handler& handler) {
1456 NumberStream<InputStream,
1463 size_t startOffset =
s.Tell();
1465 bool useNanOrInf =
false;
1468 bool minus = Consume(
s,
'-');
1473 bool use64bit =
false;
1474 int significandDigit = 0;
1480 i =
static_cast<unsigned>(
s.TakePush() -
'0');
1491 i = i * 10 +
static_cast<unsigned>(
s.TakePush() -
'0');
1503 i = i * 10 +
static_cast<unsigned>(
s.TakePush() -
'0');
1509 if (Consume(
s,
'N')) {
1510 if (Consume(
s,
'a') && Consume(
s,
'N')) {
1511 d = std::numeric_limits<double>::quiet_NaN();
1516 if (Consume(
s,
'n') && Consume(
s,
'f')) {
1517 d = (minus ? -std::numeric_limits<double>::infinity() :
std::numeric_limits<double>::infinity());
1521 && Consume(
s,
'i') && Consume(
s,
't') && Consume(
s,
'y')))) {
1535 bool useDouble =
false;
1541 d =
static_cast<double>(
i64);
1545 i64 =
i64 * 10 +
static_cast<unsigned>(
s.TakePush() -
'0');
1552 d =
static_cast<double>(
i64);
1556 i64 =
i64 * 10 +
static_cast<unsigned>(
s.TakePush() -
'0');
1564 d =
d * 10 + (
s.TakePush() -
'0');
1570 size_t decimalPosition;
1571 if (Consume(
s,
'.')) {
1572 decimalPosition =
s.Length();
1587 i64 =
i64 * 10 +
static_cast<unsigned>(
s.TakePush() -
'0');
1594 d =
static_cast<double>(
i64);
1597 d =
static_cast<double>(use64bit ?
i64 : i);
1603 if (significandDigit < 17) {
1604 d =
d * 10.0 + (
s.TakePush() -
'0');
1614 decimalPosition =
s.Length();
1618 if (Consume(
s,
'e') || Consume(
s,
'E')) {
1620 d =
static_cast<double>(use64bit ?
i64 : i);
1624 bool expMinus =
false;
1625 if (Consume(
s,
'+'))
1627 else if (Consume(
s,
'-'))
1631 exp =
static_cast<int>(
s.Take() -
'0');
1640 int maxExp = (expFrac + 2147483639) / 10;
1643 exp = exp * 10 +
static_cast<int>(
s.Take() -
'0');
1651 int maxExp = 308 - expFrac;
1653 exp = exp * 10 +
static_cast<int>(
s.Take() -
'0');
1672 typename InputStream::Ch* head = is.PutBegin();
1673 const size_t length =
s.Tell() - startOffset;
1676 const typename TargetEncoding::Ch*
const str =
reinterpret_cast<typename TargetEncoding::Ch*
>(head);
1677 cont = handler.RawNumber(str,
SizeType(length),
false);
1682 StackStream<typename TargetEncoding::Ch> dstStream(stack_);
1683 while (numCharsToCopy--) {
1686 dstStream.Put(
'\0');
1687 const typename TargetEncoding::Ch*
str = dstStream.Pop();
1689 cont = handler.RawNumber(str,
SizeType(length),
true);
1693 size_t length =
s.Length();
1694 const char* decimal =
s.Pop();
1697 int p = exp + expFrac;
1704 if (d > (std::numeric_limits<double>::max)()) {
1710 cont = handler.Double(minus ? -d : d);
1712 else if (useNanOrInf) {
1713 cont = handler.Double(d);
1718 cont = handler.Int64(
static_cast<int64_t>(~i64 + 1));
1720 cont = handler.Uint64(i64);
1724 cont = handler.Int(
static_cast<int32_t>(~i + 1));
1726 cont = handler.Uint(i);
1735 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
1736 void ParseValue(InputStream& is,
Handler& handler) {
1737 switch (is.Peek()) {
1738 case 'n': ParseNull <parseFlags>(is, handler);
break;
1739 case 't': ParseTrue <parseFlags>(is, handler);
break;
1740 case 'f': ParseFalse <parseFlags>(is, handler);
break;
1741 case '"': ParseString<parseFlags>(is, handler);
break;
1742 case '{': ParseObject<parseFlags>(is, handler);
break;
1743 case '[': ParseArray <parseFlags>(is, handler);
break;
1745 ParseNumber<parseFlags>(is, handler);
1754 enum IterativeParsingState {
1755 IterativeParsingFinishState = 0,
1756 IterativeParsingErrorState,
1757 IterativeParsingStartState,
1760 IterativeParsingObjectInitialState,
1761 IterativeParsingMemberKeyState,
1762 IterativeParsingMemberValueState,
1763 IterativeParsingObjectFinishState,
1766 IterativeParsingArrayInitialState,
1767 IterativeParsingElementState,
1768 IterativeParsingArrayFinishState,
1771 IterativeParsingValueState,
1774 IterativeParsingElementDelimiterState,
1775 IterativeParsingMemberDelimiterState,
1776 IterativeParsingKeyValueDelimiterState,
1778 cIterativeParsingStateCount
1783 LeftBracketToken = 0,
1786 LeftCurlyBracketToken,
1787 RightCurlyBracketToken,
1801 RAPIDJSON_FORCEINLINE Token Tokenize(
Ch c)
const {
1804#define N NumberToken
1805#define N16 N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N
1807 static const unsigned char tokenMap[256] = {
1810 N,
N, StringToken,
N,
N,
N,
N,
N,
N,
N,
N,
N, CommaToken,
N,
N,
N,
1811 N,
N,
N,
N,
N,
N,
N,
N,
N,
N, ColonToken,
N,
N,
N,
N,
N,
1813 N,
N,
N,
N,
N,
N,
N,
N,
N,
N,
N, LeftBracketToken,
N, RightBracketToken,
N,
N,
1814 N,
N,
N,
N,
N,
N, FalseToken,
N,
N,
N,
N,
N,
N,
N, NullToken,
N,
1815 N,
N,
N,
N, TrueToken,
N,
N,
N,
N,
N,
N, LeftCurlyBracketToken,
N, RightCurlyBracketToken,
N,
N,
1816 N16, N16, N16, N16, N16, N16, N16, N16
1822 if (
sizeof(
Ch) == 1 ||
static_cast<unsigned>(c) < 256)
1823 return static_cast<Token
>(tokenMap[
static_cast<unsigned char>(c)]);
1828 RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState
state, Token token)
const {
1830 static const char G[cIterativeParsingStateCount][kTokenCount] = {
1833 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1834 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1835 IterativeParsingErrorState
1839 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1840 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1841 IterativeParsingErrorState
1845 IterativeParsingArrayInitialState,
1846 IterativeParsingErrorState,
1847 IterativeParsingObjectInitialState,
1848 IterativeParsingErrorState,
1849 IterativeParsingErrorState,
1850 IterativeParsingErrorState,
1851 IterativeParsingValueState,
1852 IterativeParsingValueState,
1853 IterativeParsingValueState,
1854 IterativeParsingValueState,
1855 IterativeParsingValueState
1859 IterativeParsingErrorState,
1860 IterativeParsingErrorState,
1861 IterativeParsingErrorState,
1862 IterativeParsingObjectFinishState,
1863 IterativeParsingErrorState,
1864 IterativeParsingErrorState,
1865 IterativeParsingMemberKeyState,
1866 IterativeParsingErrorState,
1867 IterativeParsingErrorState,
1868 IterativeParsingErrorState,
1869 IterativeParsingErrorState
1873 IterativeParsingErrorState,
1874 IterativeParsingErrorState,
1875 IterativeParsingErrorState,
1876 IterativeParsingErrorState,
1877 IterativeParsingErrorState,
1878 IterativeParsingKeyValueDelimiterState,
1879 IterativeParsingErrorState,
1880 IterativeParsingErrorState,
1881 IterativeParsingErrorState,
1882 IterativeParsingErrorState,
1883 IterativeParsingErrorState
1887 IterativeParsingErrorState,
1888 IterativeParsingErrorState,
1889 IterativeParsingErrorState,
1890 IterativeParsingObjectFinishState,
1891 IterativeParsingMemberDelimiterState,
1892 IterativeParsingErrorState,
1893 IterativeParsingErrorState,
1894 IterativeParsingErrorState,
1895 IterativeParsingErrorState,
1896 IterativeParsingErrorState,
1897 IterativeParsingErrorState
1901 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1902 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1903 IterativeParsingErrorState
1907 IterativeParsingArrayInitialState,
1908 IterativeParsingArrayFinishState,
1909 IterativeParsingObjectInitialState,
1910 IterativeParsingErrorState,
1911 IterativeParsingErrorState,
1912 IterativeParsingErrorState,
1913 IterativeParsingElementState,
1914 IterativeParsingElementState,
1915 IterativeParsingElementState,
1916 IterativeParsingElementState,
1917 IterativeParsingElementState
1921 IterativeParsingErrorState,
1922 IterativeParsingArrayFinishState,
1923 IterativeParsingErrorState,
1924 IterativeParsingErrorState,
1925 IterativeParsingElementDelimiterState,
1926 IterativeParsingErrorState,
1927 IterativeParsingErrorState,
1928 IterativeParsingErrorState,
1929 IterativeParsingErrorState,
1930 IterativeParsingErrorState,
1931 IterativeParsingErrorState
1935 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1936 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1937 IterativeParsingErrorState
1941 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1942 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1943 IterativeParsingErrorState
1947 IterativeParsingArrayInitialState,
1948 IterativeParsingArrayFinishState,
1949 IterativeParsingObjectInitialState,
1950 IterativeParsingErrorState,
1951 IterativeParsingErrorState,
1952 IterativeParsingErrorState,
1953 IterativeParsingElementState,
1954 IterativeParsingElementState,
1955 IterativeParsingElementState,
1956 IterativeParsingElementState,
1957 IterativeParsingElementState
1961 IterativeParsingErrorState,
1962 IterativeParsingErrorState,
1963 IterativeParsingErrorState,
1964 IterativeParsingObjectFinishState,
1965 IterativeParsingErrorState,
1966 IterativeParsingErrorState,
1967 IterativeParsingMemberKeyState,
1968 IterativeParsingErrorState,
1969 IterativeParsingErrorState,
1970 IterativeParsingErrorState,
1971 IterativeParsingErrorState
1975 IterativeParsingArrayInitialState,
1976 IterativeParsingErrorState,
1977 IterativeParsingObjectInitialState,
1978 IterativeParsingErrorState,
1979 IterativeParsingErrorState,
1980 IterativeParsingErrorState,
1981 IterativeParsingMemberValueState,
1982 IterativeParsingMemberValueState,
1983 IterativeParsingMemberValueState,
1984 IterativeParsingMemberValueState,
1985 IterativeParsingMemberValueState
1989 return static_cast<IterativeParsingState
>(G[
state][token]);
1994 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
1995 RAPIDJSON_FORCEINLINE IterativeParsingState Transit(IterativeParsingState src, Token token, IterativeParsingState dst, InputStream& is,
Handler& handler) {
1999 case IterativeParsingErrorState:
2002 case IterativeParsingObjectInitialState:
2003 case IterativeParsingArrayInitialState:
2007 IterativeParsingState n = src;
2008 if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState)
2009 n = IterativeParsingElementState;
2010 else if (src == IterativeParsingKeyValueDelimiterState)
2011 n = IterativeParsingMemberValueState;
2013 *stack_.template Push<SizeType>(1) = n;
2015 *stack_.template Push<SizeType>(1) = 0;
2017 bool hr = (dst == IterativeParsingObjectInitialState) ? handler.StartObject() : handler.StartArray();
2021 return IterativeParsingErrorState;
2029 case IterativeParsingMemberKeyState:
2030 ParseString<parseFlags>(is, handler,
true);
2032 return IterativeParsingErrorState;
2036 case IterativeParsingKeyValueDelimiterState:
2041 case IterativeParsingMemberValueState:
2043 ParseValue<parseFlags>(is, handler);
2045 return IterativeParsingErrorState;
2049 case IterativeParsingElementState:
2051 ParseValue<parseFlags>(is, handler);
2053 return IterativeParsingErrorState;
2057 case IterativeParsingMemberDelimiterState:
2058 case IterativeParsingElementDelimiterState:
2061 *stack_.template Top<SizeType>() = *stack_.template Top<SizeType>() + 1;
2064 case IterativeParsingObjectFinishState:
2069 return IterativeParsingErrorState;
2072 SizeType c = *stack_.template Pop<SizeType>(1);
2074 if (src == IterativeParsingMemberValueState)
2077 IterativeParsingState n =
static_cast<IterativeParsingState
>(*stack_.template Pop<SizeType>(1));
2079 if (n == IterativeParsingStartState)
2080 n = IterativeParsingFinishState;
2082 bool hr = handler.EndObject(c);
2086 return IterativeParsingErrorState;
2094 case IterativeParsingArrayFinishState:
2099 return IterativeParsingErrorState;
2102 SizeType c = *stack_.template Pop<SizeType>(1);
2104 if (src == IterativeParsingElementState)
2107 IterativeParsingState n =
static_cast<IterativeParsingState
>(*stack_.template Pop<SizeType>(1));
2109 if (n == IterativeParsingStartState)
2110 n = IterativeParsingFinishState;
2112 bool hr = handler.EndArray(c);
2116 return IterativeParsingErrorState;
2138 ParseValue<parseFlags>(is, handler);
2140 return IterativeParsingErrorState;
2142 return IterativeParsingFinishState;
2146 template <
typename InputStream>
2147 void HandleError(IterativeParsingState src, InputStream& is) {
2156 case IterativeParsingObjectInitialState:
2160 case IterativeParsingKeyValueDelimiterState:
2161 case IterativeParsingArrayInitialState:
2167 RAPIDJSON_FORCEINLINE
bool IsIterativeParsingDelimiterState(IterativeParsingState
s)
const {
2168 return s >= IterativeParsingElementDelimiterState;
2171 RAPIDJSON_FORCEINLINE
bool IsIterativeParsingCompleteState(IterativeParsingState
s)
const {
2172 return s <= IterativeParsingErrorState;
2175 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
2177 parseResult_.
Clear();
2178 ClearStackOnExit scope(*
this);
2179 IterativeParsingState
state = IterativeParsingStartState;
2181 SkipWhitespaceAndComments<parseFlags>(is);
2182 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
2183 while (is.Peek() !=
'\0') {
2184 Token t = Tokenize(is.Peek());
2185 IterativeParsingState n = Predict(
state, t);
2186 IterativeParsingState
d = Transit<parseFlags>(
state, t, n, is, handler);
2188 if (d == IterativeParsingErrorState) {
2189 HandleError(
state, is);
2199 SkipWhitespaceAndComments<parseFlags>(is);
2200 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
2204 if (
state != IterativeParsingFinishState)
2205 HandleError(
state, is);
2207 return parseResult_;
2210 static const size_t kDefaultStackCapacity = 256;
2213 IterativeParsingState state_;