15 #ifndef RAPIDJSON_READER_H_ 16 #define RAPIDJSON_READER_H_ 20 #include "allocators.h" 22 #include "encodedstream.h" 23 #include "internal/clzll.h" 24 #include "internal/meta.h" 25 #include "internal/stack.h" 26 #include "internal/strtod.h" 29 #if defined(RAPIDJSON_SIMD) && defined(_MSC_VER) 31 #pragma intrinsic(_BitScanForward) 33 #ifdef RAPIDJSON_SSE42 34 #include <nmmintrin.h> 35 #elif defined(RAPIDJSON_SSE2) 36 #include <emmintrin.h> 37 #elif defined(RAPIDJSON_NEON) 43 RAPIDJSON_DIAG_OFF(old-style-cast)
44 RAPIDJSON_DIAG_OFF(padded)
45 RAPIDJSON_DIAG_OFF(
switch-
enum)
46 #elif defined(_MSC_VER) 48 RAPIDJSON_DIAG_OFF(4127)
49 RAPIDJSON_DIAG_OFF(4702)
54 RAPIDJSON_DIAG_OFF(effc++)
58 #define RAPIDJSON_NOTHING 59 #ifndef RAPIDJSON_PARSE_ERROR_EARLY_RETURN 60 #define RAPIDJSON_PARSE_ERROR_EARLY_RETURN(value) \ 61 RAPIDJSON_MULTILINEMACRO_BEGIN \ 62 if (RAPIDJSON_UNLIKELY(HasParseError())) { return value; } \ 63 RAPIDJSON_MULTILINEMACRO_END 65 #define RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID \ 66 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(RAPIDJSON_NOTHING) 99 #ifndef RAPIDJSON_PARSE_ERROR_NORETURN 100 #define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset) \ 101 RAPIDJSON_MULTILINEMACRO_BEGIN \ 102 RAPIDJSON_ASSERT(!HasParseError()); \ 103 SetParseError(parseErrorCode, offset); \ 104 RAPIDJSON_MULTILINEMACRO_END 118 #ifndef RAPIDJSON_PARSE_ERROR 119 #define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset) \ 120 RAPIDJSON_MULTILINEMACRO_BEGIN \ 121 RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset); \ 122 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; \ 123 RAPIDJSON_MULTILINEMACRO_END 126 #include "error/error.h" 139 #ifndef RAPIDJSON_PARSE_DEFAULT_FLAGS 140 #define RAPIDJSON_PARSE_DEFAULT_FLAGS kParseNoFlags 148 kParseInsituFlag = 1,
149 kParseValidateEncodingFlag = 2,
150 kParseIterativeFlag = 4,
151 kParseStopWhenDoneFlag = 8,
152 kParseFullPrecisionFlag = 16,
153 kParseCommentsFlag = 32,
154 kParseNumbersAsStringsFlag = 64,
155 kParseTrailingCommasFlag = 128,
156 kParseNanAndInfFlag = 256,
157 kParseEscapedApostropheFlag = 512,
158 kParseDefaultFlags = RAPIDJSON_PARSE_DEFAULT_FLAGS
197 template<
typename Encoding = UTF8<>,
typename Derived =
void>
199 typedef typename Encoding::Ch Ch;
201 typedef typename internal::SelectIf<internal::IsSame<Derived, void>,
BaseReaderHandler, Derived>::Type Override;
203 bool Default() {
return true; }
204 bool Null() {
return static_cast<Override&
>(*this).Default(); }
205 bool Bool(
bool) {
return static_cast<Override&
>(*this).Default(); }
206 bool Int(
int) {
return static_cast<Override&
>(*this).Default(); }
207 bool Uint(
unsigned) {
return static_cast<Override&
>(*this).Default(); }
208 bool Int64(int64_t) {
return static_cast<Override&
>(*this).Default(); }
209 bool Uint64(uint64_t) {
return static_cast<Override&
>(*this).Default(); }
210 bool Double(
double) {
return static_cast<Override&
>(*this).Default(); }
212 bool RawNumber(
const Ch* str, SizeType len,
bool copy) {
return static_cast<Override&
>(*this).String(str, len, copy); }
213 bool String(
const Ch*, SizeType,
bool) {
return static_cast<Override&
>(*this).Default(); }
214 bool StartObject() {
return static_cast<Override&
>(*this).Default(); }
215 bool Key(
const Ch* str, SizeType len,
bool copy) {
return static_cast<Override&
>(*this).String(str, len, copy); }
216 bool EndObject(SizeType) {
return static_cast<Override&
>(*this).Default(); }
217 bool StartArray() {
return static_cast<Override&
>(*this).Default(); }
218 bool EndArray(SizeType) {
return static_cast<Override&
>(*this).Default(); }
226 template<typename Stream, int = StreamTraits<Stream>::copyOptimization>
230 template<
typename Stream>
233 StreamLocalCopy(Stream& original) : s(original), original_(original) {}
245 template<
typename Stream>
265 template<
typename InputStream>
266 void SkipWhitespace(InputStream& is) {
268 InputStream& s(copy.s);
270 typename InputStream::Ch c;
271 while ((c = s.Peek()) ==
' ' || c ==
'\n' || c ==
'\r' || c ==
'\t')
275 inline const char* SkipWhitespace(
const char* p,
const char* end) {
276 while (p != end && (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t'))
281 #ifdef RAPIDJSON_SSE42 282 inline const char *SkipWhitespace_SIMD(
const char* p) {
285 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
291 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
292 while (p != nextAligned)
293 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
299 static const char whitespace[16] =
" \n\r\t";
300 const __m128i w = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&whitespace[0]));
303 const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i *>(p));
304 const int r = _mm_cmpistri(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT | _SIDD_NEGATIVE_POLARITY);
310 inline const char *SkipWhitespace_SIMD(
const char* p,
const char* end) {
312 if (p != end && (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t'))
318 static const char whitespace[16] =
" \n\r\t";
319 const __m128i w = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&whitespace[0]));
321 for (; p <= end - 16; p += 16) {
322 const __m128i s = _mm_loadu_si128(reinterpret_cast<const __m128i *>(p));
323 const int r = _mm_cmpistri(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT | _SIDD_NEGATIVE_POLARITY);
328 return SkipWhitespace(p, end);
331 #elif defined(RAPIDJSON_SSE2) 334 inline const char *SkipWhitespace_SIMD(
const char* p) {
336 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
342 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
343 while (p != nextAligned)
344 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
350 #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c } 351 static const char whitespaces[4][16] = { C16(
' '), C16(
'\n'), C16(
'\r'), C16(
'\t') };
354 const __m128i w0 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&whitespaces[0][0]));
355 const __m128i w1 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&whitespaces[1][0]));
356 const __m128i w2 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&whitespaces[2][0]));
357 const __m128i w3 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&whitespaces[3][0]));
360 const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i *>(p));
361 __m128i x = _mm_cmpeq_epi8(s, w0);
362 x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1));
363 x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2));
364 x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3));
365 unsigned short r =
static_cast<unsigned short>(~_mm_movemask_epi8(x));
367 #ifdef _MSC_VER // Find the index of first non-whitespace 368 unsigned long offset;
369 _BitScanForward(&offset, r);
372 return p + __builtin_ffs(r) - 1;
378 inline const char *SkipWhitespace_SIMD(
const char* p,
const char* end) {
380 if (p != end && (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t'))
386 #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c } 387 static const char whitespaces[4][16] = { C16(
' '), C16(
'\n'), C16(
'\r'), C16(
'\t') };
390 const __m128i w0 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&whitespaces[0][0]));
391 const __m128i w1 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&whitespaces[1][0]));
392 const __m128i w2 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&whitespaces[2][0]));
393 const __m128i w3 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&whitespaces[3][0]));
395 for (; p <= end - 16; p += 16) {
396 const __m128i s = _mm_loadu_si128(reinterpret_cast<const __m128i *>(p));
397 __m128i x = _mm_cmpeq_epi8(s, w0);
398 x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1));
399 x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2));
400 x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3));
401 unsigned short r =
static_cast<unsigned short>(~_mm_movemask_epi8(x));
403 #ifdef _MSC_VER // Find the index of first non-whitespace 404 unsigned long offset;
405 _BitScanForward(&offset, r);
408 return p + __builtin_ffs(r) - 1;
413 return SkipWhitespace(p, end);
416 #elif defined(RAPIDJSON_NEON) 419 inline const char *SkipWhitespace_SIMD(
const char* p) {
421 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
427 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
428 while (p != nextAligned)
429 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
434 const uint8x16_t w0 = vmovq_n_u8(
' ');
435 const uint8x16_t w1 = vmovq_n_u8(
'\n');
436 const uint8x16_t w2 = vmovq_n_u8(
'\r');
437 const uint8x16_t w3 = vmovq_n_u8(
'\t');
440 const uint8x16_t s = vld1q_u8(reinterpret_cast<const uint8_t *>(p));
441 uint8x16_t x = vceqq_u8(s, w0);
442 x = vorrq_u8(x, vceqq_u8(s, w1));
443 x = vorrq_u8(x, vceqq_u8(s, w2));
444 x = vorrq_u8(x, vceqq_u8(s, w3));
448 uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0);
449 uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1);
453 uint32_t lz = internal::clzll(high);
454 return p + 8 + (lz >> 3);
457 uint32_t lz = internal::clzll(low);
458 return p + (lz >> 3);
463 inline const char *SkipWhitespace_SIMD(
const char* p,
const char* end) {
465 if (p != end && (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t'))
470 const uint8x16_t w0 = vmovq_n_u8(
' ');
471 const uint8x16_t w1 = vmovq_n_u8(
'\n');
472 const uint8x16_t w2 = vmovq_n_u8(
'\r');
473 const uint8x16_t w3 = vmovq_n_u8(
'\t');
475 for (; p <= end - 16; p += 16) {
476 const uint8x16_t s = vld1q_u8(reinterpret_cast<const uint8_t *>(p));
477 uint8x16_t x = vceqq_u8(s, w0);
478 x = vorrq_u8(x, vceqq_u8(s, w1));
479 x = vorrq_u8(x, vceqq_u8(s, w2));
480 x = vorrq_u8(x, vceqq_u8(s, w3));
484 uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0);
485 uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1);
489 uint32_t lz = internal::clzll(high);
490 return p + 8 + (lz >> 3);
493 uint32_t lz = internal::clzll(low);
494 return p + (lz >> 3);
498 return SkipWhitespace(p, end);
501 #endif // RAPIDJSON_NEON 503 #ifdef RAPIDJSON_SIMD 506 is.src_ =
const_cast<char*
>(SkipWhitespace_SIMD(is.src_));
510 template<>
inline void SkipWhitespace(
StringStream& is) {
511 is.
src_ = SkipWhitespace_SIMD(is.
src_);
515 is.is_.
src_ = SkipWhitespace_SIMD(is.is_.
src_, is.is_.end_);
517 #endif // RAPIDJSON_SIMD 538 template <
typename SourceEncoding,
typename TargetEncoding,
typename StackAllocator = CrtAllocator>
541 typedef typename SourceEncoding::Ch
Ch;
547 GenericReader(StackAllocator* stackAllocator = 0,
size_t stackCapacity = kDefaultStackCapacity) :
548 stack_(stackAllocator, stackCapacity), parseResult_(), state_(IterativeParsingStartState) {}
558 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
560 if (parseFlags & kParseIterativeFlag)
561 return IterativeParse<parseFlags>(is, handler);
563 parseResult_.
Clear();
565 ClearStackOnExit scope(*
this);
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_);
578 if (!(parseFlags & kParseStopWhenDoneFlag)) {
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);
641 if (!(parseFlags & kParseStopWhenDoneFlag)) {
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(); }
701 struct ClearStackOnExit {
703 ~ClearStackOnExit() { r_.ClearStack(); }
706 ClearStackOnExit(
const ClearStackOnExit&);
707 ClearStackOnExit& operator=(
const ClearStackOnExit&);
710 template<
unsigned parseFlags,
typename InputStream>
711 void SkipWhitespaceAndComments(InputStream& is) {
714 if (parseFlags & kParseCommentsFlag) {
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,
'}')) {
756 for (SizeType memberCount = 0;;) {
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;
795 if (parseFlags & kParseTrailingCommasFlag) {
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,
']')) {
824 for (SizeType elementCount = 0;;) {
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,
']')) {
844 if (parseFlags & kParseTrailingCommasFlag) {
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_);
950 StackStream(
const StackStream&);
951 StackStream& operator=(
const StackStream&);
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;
967 if (parseFlags & kParseInsituFlag) {
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;
980 SizeType length =
static_cast<SizeType
>(stackStream.Length()) - 1;
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
1006 if (!(parseFlags & kParseValidateEncodingFlag))
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)]));
1018 else if ((parseFlags & kParseEscapedApostropheFlag) &&
RAPIDJSON_LIKELY(e ==
'\'')) {
1024 unsigned codepoint = ParseHex4(is, escapeOffset);
1025 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
1032 unsigned codepoint2 = ParseHex4(is, escapeOffset);
1033 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
1036 codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000;
1044 TEncoding::Encode(os, codepoint);
1061 size_t offset = is.Tell();
1070 template<
typename InputStream,
typename OutputStream>
1071 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(InputStream&, OutputStream&) {
1075 #if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) 1077 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(
StringStream& is, StackStream<char>& os) {
1078 const char* p = is.
src_;
1081 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1082 while (p != nextAligned)
1091 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
1092 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
1093 static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
1094 const __m128i dq = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&dquote[0]));
1095 const __m128i bs = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&bslash[0]));
1096 const __m128i sp = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&space[0]));
1099 const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i *>(p));
1100 const __m128i t1 = _mm_cmpeq_epi8(s, dq);
1101 const __m128i t2 = _mm_cmpeq_epi8(s, bs);
1102 const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp);
1103 const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
1104 unsigned short r =
static_cast<unsigned short>(_mm_movemask_epi8(x));
1107 #ifdef _MSC_VER // Find the index of first escaped 1108 unsigned long offset;
1109 _BitScanForward(&offset, r);
1112 length =
static_cast<SizeType
>(__builtin_ffs(r) - 1);
1115 char* q =
reinterpret_cast<char*
>(os.Push(length));
1116 for (
size_t i = 0; i < length; i++)
1123 _mm_storeu_si128(reinterpret_cast<__m128i *>(os.Push(16)), s);
1134 if (is.src_ == is.dst_) {
1135 SkipUnescapedString(is);
1143 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1144 while (p != nextAligned)
1154 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
1155 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
1156 static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
1157 const __m128i dq = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&dquote[0]));
1158 const __m128i bs = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&bslash[0]));
1159 const __m128i sp = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&space[0]));
1161 for (;; p += 16, q += 16) {
1162 const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i *>(p));
1163 const __m128i t1 = _mm_cmpeq_epi8(s, dq);
1164 const __m128i t2 = _mm_cmpeq_epi8(s, bs);
1165 const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp);
1166 const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
1167 unsigned short r =
static_cast<unsigned short>(_mm_movemask_epi8(x));
1170 #ifdef _MSC_VER // Find the index of first escaped 1171 unsigned long offset;
1172 _BitScanForward(&offset, r);
1175 length =
static_cast<size_t>(__builtin_ffs(r) - 1);
1177 for (
const char* pend = p + length; p != pend; )
1181 _mm_storeu_si128(reinterpret_cast<__m128i *>(q), s);
1194 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1195 for (; p != nextAligned; p++)
1197 is.src_ = is.dst_ = p;
1202 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
1203 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
1204 static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F };
1205 const __m128i dq = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&dquote[0]));
1206 const __m128i bs = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&bslash[0]));
1207 const __m128i sp = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&space[0]));
1210 const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i *>(p));
1211 const __m128i t1 = _mm_cmpeq_epi8(s, dq);
1212 const __m128i t2 = _mm_cmpeq_epi8(s, bs);
1213 const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp);
1214 const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
1215 unsigned short r =
static_cast<unsigned short>(_mm_movemask_epi8(x));
1218 #ifdef _MSC_VER // Find the index of first escaped 1219 unsigned long offset;
1220 _BitScanForward(&offset, r);
1223 length =
static_cast<size_t>(__builtin_ffs(r) - 1);
1230 is.src_ = is.dst_ = p;
1232 #elif defined(RAPIDJSON_NEON) 1234 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(
StringStream& is, StackStream<char>& os) {
1235 const char* p = is.
src_;
1238 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1239 while (p != nextAligned)
1248 const uint8x16_t s0 = vmovq_n_u8(
'"');
1249 const uint8x16_t s1 = vmovq_n_u8(
'\\');
1250 const uint8x16_t s2 = vmovq_n_u8(
'\b');
1251 const uint8x16_t s3 = vmovq_n_u8(32);
1254 const uint8x16_t s = vld1q_u8(reinterpret_cast<const uint8_t *>(p));
1255 uint8x16_t x = vceqq_u8(s, s0);
1256 x = vorrq_u8(x, vceqq_u8(s, s1));
1257 x = vorrq_u8(x, vceqq_u8(s, s2));
1258 x = vorrq_u8(x, vcltq_u8(s, s3));
1261 uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0);
1262 uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1);
1264 SizeType length = 0;
1265 bool escaped =
false;
1268 uint32_t lz = internal::clzll(high);
1269 length = 8 + (lz >> 3);
1273 uint32_t lz = internal::clzll(low);
1279 char* q =
reinterpret_cast<char*
>(os.Push(length));
1280 for (
size_t i = 0; i < length; i++)
1287 vst1q_u8(reinterpret_cast<uint8_t *>(os.Push(16)), s);
1298 if (is.src_ == is.dst_) {
1299 SkipUnescapedString(is);
1307 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1308 while (p != nextAligned)
1318 const uint8x16_t s0 = vmovq_n_u8(
'"');
1319 const uint8x16_t s1 = vmovq_n_u8(
'\\');
1320 const uint8x16_t s2 = vmovq_n_u8(
'\b');
1321 const uint8x16_t s3 = vmovq_n_u8(32);
1323 for (;; p += 16, q += 16) {
1324 const uint8x16_t s = vld1q_u8(reinterpret_cast<uint8_t *>(p));
1325 uint8x16_t x = vceqq_u8(s, s0);
1326 x = vorrq_u8(x, vceqq_u8(s, s1));
1327 x = vorrq_u8(x, vceqq_u8(s, s2));
1328 x = vorrq_u8(x, vcltq_u8(s, s3));
1331 uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0);
1332 uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1);
1334 SizeType length = 0;
1335 bool escaped =
false;
1338 uint32_t lz = internal::clzll(high);
1339 length = 8 + (lz >> 3);
1343 uint32_t lz = internal::clzll(low);
1348 for (
const char* pend = p + length; p != pend; ) {
1353 vst1q_u8(reinterpret_cast<uint8_t *>(q), s);
1366 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1367 for (; p != nextAligned; p++)
1369 is.src_ = is.dst_ = p;
1374 const uint8x16_t s0 = vmovq_n_u8(
'"');
1375 const uint8x16_t s1 = vmovq_n_u8(
'\\');
1376 const uint8x16_t s2 = vmovq_n_u8(
'\b');
1377 const uint8x16_t s3 = vmovq_n_u8(32);
1380 const uint8x16_t s = vld1q_u8(reinterpret_cast<uint8_t *>(p));
1381 uint8x16_t x = vceqq_u8(s, s0);
1382 x = vorrq_u8(x, vceqq_u8(s, s1));
1383 x = vorrq_u8(x, vceqq_u8(s, s2));
1384 x = vorrq_u8(x, vcltq_u8(s, s3));
1387 uint64_t low = vgetq_lane_u64(vreinterpretq_u64_u8(x), 0);
1388 uint64_t high = vgetq_lane_u64(vreinterpretq_u64_u8(x), 1);
1392 uint32_t lz = internal::clzll(high);
1397 uint32_t lz = internal::clzll(low);
1403 is.src_ = is.dst_ = p;
1405 #endif // RAPIDJSON_NEON 1407 template<
typename InputStream,
typename StackCharacter,
bool backup,
bool pushOnTake>
1410 template<
typename InputStream,
typename StackCharacter>
1411 class NumberStream<InputStream, StackCharacter, false, false> {
1413 typedef typename InputStream::Ch Ch;
1415 NumberStream(
GenericReader& reader, InputStream& s) : is(s) { (void)reader; }
1417 RAPIDJSON_FORCEINLINE Ch Peek()
const {
return is.Peek(); }
1418 RAPIDJSON_FORCEINLINE Ch TakePush() {
return is.Take(); }
1419 RAPIDJSON_FORCEINLINE Ch Take() {
return is.Take(); }
1420 RAPIDJSON_FORCEINLINE
void Push(
char) {}
1422 size_t Tell() {
return is.Tell(); }
1423 size_t Length() {
return 0; }
1424 const StackCharacter* Pop() {
return 0; }
1427 NumberStream& operator=(
const NumberStream&);
1432 template<
typename InputStream,
typename StackCharacter>
1433 class NumberStream<InputStream, StackCharacter, true, false> :
public NumberStream<InputStream, StackCharacter, false, false> {
1434 typedef NumberStream<InputStream, StackCharacter, false, false> Base;
1436 NumberStream(
GenericReader& reader, InputStream& is) : Base(reader, is), stackStream(reader.stack_) {}
1438 RAPIDJSON_FORCEINLINE Ch TakePush() {
1439 stackStream.Put(static_cast<StackCharacter>(Base::is.Peek()));
1440 return Base::is.Take();
1443 RAPIDJSON_FORCEINLINE
void Push(StackCharacter c) {
1447 size_t Length() {
return stackStream.Length(); }
1449 const StackCharacter* Pop() {
1450 stackStream.Put(
'\0');
1451 return stackStream.Pop();
1455 StackStream<StackCharacter> stackStream;
1458 template<
typename InputStream,
typename StackCharacter>
1459 class NumberStream<InputStream, StackCharacter, true, true> :
public NumberStream<InputStream, StackCharacter, true, false> {
1460 typedef NumberStream<InputStream, StackCharacter, true, false> Base;
1462 NumberStream(
GenericReader& reader, InputStream& is) : Base(reader, is) {}
1464 RAPIDJSON_FORCEINLINE Ch Take() {
return Base::TakePush(); }
1467 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
1468 void ParseNumber(InputStream& is, Handler& handler) {
1469 typedef typename internal::SelectIf<internal::BoolType<(parseFlags & kParseNumbersAsStringsFlag) != 0>,
typename TargetEncoding::Ch,
char>::Type NumberCharacter;
1472 NumberStream<InputStream, NumberCharacter,
1473 ((parseFlags & kParseNumbersAsStringsFlag) != 0) ?
1474 ((parseFlags & kParseInsituFlag) == 0) :
1475 ((parseFlags & kParseFullPrecisionFlag) != 0),
1476 (parseFlags & kParseNumbersAsStringsFlag) != 0 &&
1477 (parseFlags & kParseInsituFlag) == 0> s(*
this, copy.s);
1479 size_t startOffset = s.Tell();
1481 bool useNanOrInf =
false;
1484 bool minus = Consume(s,
'-');
1489 bool use64bit =
false;
1490 int significandDigit = 0;
1496 i =
static_cast<unsigned>(s.TakePush() -
'0');
1507 i = i * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1519 i = i * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1524 else if ((parseFlags & kParseNanAndInfFlag) &&
RAPIDJSON_LIKELY((s.Peek() ==
'I' || s.Peek() ==
'N'))) {
1525 if (Consume(s,
'N')) {
1526 if (Consume(s,
'a') && Consume(s,
'N')) {
1527 d = std::numeric_limits<double>::quiet_NaN();
1532 if (Consume(s,
'n') && Consume(s,
'f')) {
1533 d = (minus ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity());
1537 && Consume(s,
'i') && Consume(s,
't') && Consume(s,
'y')))) {
1551 bool useDouble =
false;
1556 if (
RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC) || s.Peek() >
'8')) {
1557 d =
static_cast<double>(i64);
1561 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1567 if (
RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || s.Peek() >
'5')) {
1568 d =
static_cast<double>(i64);
1572 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1580 d = d * 10 + (s.TakePush() -
'0');
1586 size_t decimalPosition;
1587 if (Consume(s,
'.')) {
1588 decimalPosition = s.Length();
1600 if (i64 > RAPIDJSON_UINT64_C2(0x1FFFFF, 0xFFFFFFFF))
1603 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1610 d =
static_cast<double>(i64);
1613 d =
static_cast<double>(use64bit ? i64 : i);
1619 if (significandDigit < 17) {
1620 d = d * 10.0 + (s.TakePush() -
'0');
1630 decimalPosition = s.Length();
1634 if (Consume(s,
'e') || Consume(s,
'E')) {
1636 d =
static_cast<double>(use64bit ? i64 : i);
1640 bool expMinus =
false;
1641 if (Consume(s,
'+'))
1643 else if (Consume(s,
'-'))
1647 exp =
static_cast<int>(s.Take() -
'0');
1656 int maxExp = (expFrac + 2147483639) / 10;
1659 exp = exp * 10 +
static_cast<int>(s.Take() -
'0');
1667 int maxExp = 308 - expFrac;
1669 exp = exp * 10 +
static_cast<int>(s.Take() -
'0');
1685 if (parseFlags & kParseNumbersAsStringsFlag) {
1686 if (parseFlags & kParseInsituFlag) {
1688 typename InputStream::Ch* head = is.PutBegin();
1689 const size_t length = s.Tell() - startOffset;
1692 const typename TargetEncoding::Ch*
const str =
reinterpret_cast<typename TargetEncoding::Ch*
>(head);
1693 cont = handler.RawNumber(str, SizeType(length),
false);
1696 SizeType numCharsToCopy =
static_cast<SizeType
>(s.Length());
1698 StackStream<typename TargetEncoding::Ch> dstStream(stack_);
1699 while (numCharsToCopy--) {
1702 dstStream.Put(
'\0');
1703 const typename TargetEncoding::Ch* str = dstStream.Pop();
1704 const SizeType length =
static_cast<SizeType
>(dstStream.Length()) - 1;
1705 cont = handler.RawNumber(str, SizeType(length),
true);
1709 size_t length = s.Length();
1710 const NumberCharacter* decimal = s.Pop();
1713 int p = exp + expFrac;
1714 if (parseFlags & kParseFullPrecisionFlag)
1715 d = internal::StrtodFullPrecision(d, p, decimal, length, decimalPosition, exp);
1717 d = internal::StrtodNormalPrecision(d, p);
1720 if (d > (std::numeric_limits<double>::max)()) {
1726 cont = handler.Double(minus ? -d : d);
1728 else if (useNanOrInf) {
1729 cont = handler.Double(d);
1734 cont = handler.Int64(static_cast<int64_t>(~i64 + 1));
1736 cont = handler.Uint64(i64);
1740 cont = handler.Int(static_cast<int32_t>(~i + 1));
1742 cont = handler.Uint(i);
1751 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
1752 void ParseValue(InputStream& is, Handler& handler) {
1753 switch (is.Peek()) {
1754 case 'n': ParseNull <parseFlags>(is, handler);
break;
1755 case 't': ParseTrue <parseFlags>(is, handler);
break;
1756 case 'f': ParseFalse <parseFlags>(is, handler);
break;
1757 case '"': ParseString<parseFlags>(is, handler);
break;
1758 case '{': ParseObject<parseFlags>(is, handler);
break;
1759 case '[': ParseArray <parseFlags>(is, handler);
break;
1761 ParseNumber<parseFlags>(is, handler);
1770 enum IterativeParsingState {
1771 IterativeParsingFinishState = 0,
1772 IterativeParsingErrorState,
1773 IterativeParsingStartState,
1776 IterativeParsingObjectInitialState,
1777 IterativeParsingMemberKeyState,
1778 IterativeParsingMemberValueState,
1779 IterativeParsingObjectFinishState,
1782 IterativeParsingArrayInitialState,
1783 IterativeParsingElementState,
1784 IterativeParsingArrayFinishState,
1787 IterativeParsingValueState,
1790 IterativeParsingElementDelimiterState,
1791 IterativeParsingMemberDelimiterState,
1792 IterativeParsingKeyValueDelimiterState,
1794 cIterativeParsingStateCount
1799 LeftBracketToken = 0,
1802 LeftCurlyBracketToken,
1803 RightCurlyBracketToken,
1817 RAPIDJSON_FORCEINLINE Token Tokenize(Ch c)
const {
1820 #define N NumberToken 1821 #define N16 N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N 1823 static const unsigned char tokenMap[256] = {
1826 N, N, StringToken, N, N, N, N, N, N, N, N, N, CommaToken, N, N, N,
1827 N, N, N, N, N, N, N, N, N, N, ColonToken, N, N, N, N, N,
1829 N, N, N, N, N, N, N, N, N, N, N, LeftBracketToken, N, RightBracketToken, N, N,
1830 N, N, N, N, N, N, FalseToken, N, N, N, N, N, N, N, NullToken, N,
1831 N, N, N, N, TrueToken, N, N, N, N, N, N, LeftCurlyBracketToken, N, RightCurlyBracketToken, N, N,
1832 N16, N16, N16, N16, N16, N16, N16, N16
1838 if (
sizeof(Ch) == 1 || static_cast<unsigned>(c) < 256)
1839 return static_cast<Token
>(tokenMap[
static_cast<unsigned char>(c)]);
1844 RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token)
const {
1846 static const char G[cIterativeParsingStateCount][kTokenCount] = {
1849 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1850 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1851 IterativeParsingErrorState
1855 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1856 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1857 IterativeParsingErrorState
1861 IterativeParsingArrayInitialState,
1862 IterativeParsingErrorState,
1863 IterativeParsingObjectInitialState,
1864 IterativeParsingErrorState,
1865 IterativeParsingErrorState,
1866 IterativeParsingErrorState,
1867 IterativeParsingValueState,
1868 IterativeParsingValueState,
1869 IterativeParsingValueState,
1870 IterativeParsingValueState,
1871 IterativeParsingValueState
1875 IterativeParsingErrorState,
1876 IterativeParsingErrorState,
1877 IterativeParsingErrorState,
1878 IterativeParsingObjectFinishState,
1879 IterativeParsingErrorState,
1880 IterativeParsingErrorState,
1881 IterativeParsingMemberKeyState,
1882 IterativeParsingErrorState,
1883 IterativeParsingErrorState,
1884 IterativeParsingErrorState,
1885 IterativeParsingErrorState
1889 IterativeParsingErrorState,
1890 IterativeParsingErrorState,
1891 IterativeParsingErrorState,
1892 IterativeParsingErrorState,
1893 IterativeParsingErrorState,
1894 IterativeParsingKeyValueDelimiterState,
1895 IterativeParsingErrorState,
1896 IterativeParsingErrorState,
1897 IterativeParsingErrorState,
1898 IterativeParsingErrorState,
1899 IterativeParsingErrorState
1903 IterativeParsingErrorState,
1904 IterativeParsingErrorState,
1905 IterativeParsingErrorState,
1906 IterativeParsingObjectFinishState,
1907 IterativeParsingMemberDelimiterState,
1908 IterativeParsingErrorState,
1909 IterativeParsingErrorState,
1910 IterativeParsingErrorState,
1911 IterativeParsingErrorState,
1912 IterativeParsingErrorState,
1913 IterativeParsingErrorState
1917 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1918 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1919 IterativeParsingErrorState
1923 IterativeParsingArrayInitialState,
1924 IterativeParsingArrayFinishState,
1925 IterativeParsingObjectInitialState,
1926 IterativeParsingErrorState,
1927 IterativeParsingErrorState,
1928 IterativeParsingErrorState,
1929 IterativeParsingElementState,
1930 IterativeParsingElementState,
1931 IterativeParsingElementState,
1932 IterativeParsingElementState,
1933 IterativeParsingElementState
1937 IterativeParsingErrorState,
1938 IterativeParsingArrayFinishState,
1939 IterativeParsingErrorState,
1940 IterativeParsingErrorState,
1941 IterativeParsingElementDelimiterState,
1942 IterativeParsingErrorState,
1943 IterativeParsingErrorState,
1944 IterativeParsingErrorState,
1945 IterativeParsingErrorState,
1946 IterativeParsingErrorState,
1947 IterativeParsingErrorState
1951 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1952 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1953 IterativeParsingErrorState
1957 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1958 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1959 IterativeParsingErrorState
1963 IterativeParsingArrayInitialState,
1964 IterativeParsingArrayFinishState,
1965 IterativeParsingObjectInitialState,
1966 IterativeParsingErrorState,
1967 IterativeParsingErrorState,
1968 IterativeParsingErrorState,
1969 IterativeParsingElementState,
1970 IterativeParsingElementState,
1971 IterativeParsingElementState,
1972 IterativeParsingElementState,
1973 IterativeParsingElementState
1977 IterativeParsingErrorState,
1978 IterativeParsingErrorState,
1979 IterativeParsingErrorState,
1980 IterativeParsingObjectFinishState,
1981 IterativeParsingErrorState,
1982 IterativeParsingErrorState,
1983 IterativeParsingMemberKeyState,
1984 IterativeParsingErrorState,
1985 IterativeParsingErrorState,
1986 IterativeParsingErrorState,
1987 IterativeParsingErrorState
1991 IterativeParsingArrayInitialState,
1992 IterativeParsingErrorState,
1993 IterativeParsingObjectInitialState,
1994 IterativeParsingErrorState,
1995 IterativeParsingErrorState,
1996 IterativeParsingErrorState,
1997 IterativeParsingMemberValueState,
1998 IterativeParsingMemberValueState,
1999 IterativeParsingMemberValueState,
2000 IterativeParsingMemberValueState,
2001 IterativeParsingMemberValueState
2005 return static_cast<IterativeParsingState
>(G[state][token]);
2010 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
2011 RAPIDJSON_FORCEINLINE IterativeParsingState Transit(IterativeParsingState src, Token token, IterativeParsingState dst, InputStream& is, Handler& handler) {
2015 case IterativeParsingErrorState:
2018 case IterativeParsingObjectInitialState:
2019 case IterativeParsingArrayInitialState:
2023 IterativeParsingState n = src;
2024 if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState)
2025 n = IterativeParsingElementState;
2026 else if (src == IterativeParsingKeyValueDelimiterState)
2027 n = IterativeParsingMemberValueState;
2029 *stack_.template Push<SizeType>(1) = n;
2031 *stack_.template Push<SizeType>(1) = 0;
2033 bool hr = (dst == IterativeParsingObjectInitialState) ? handler.StartObject() : handler.StartArray();
2037 return IterativeParsingErrorState;
2045 case IterativeParsingMemberKeyState:
2046 ParseString<parseFlags>(is, handler,
true);
2047 if (HasParseError())
2048 return IterativeParsingErrorState;
2052 case IterativeParsingKeyValueDelimiterState:
2057 case IterativeParsingMemberValueState:
2059 ParseValue<parseFlags>(is, handler);
2060 if (HasParseError()) {
2061 return IterativeParsingErrorState;
2065 case IterativeParsingElementState:
2067 ParseValue<parseFlags>(is, handler);
2068 if (HasParseError()) {
2069 return IterativeParsingErrorState;
2073 case IterativeParsingMemberDelimiterState:
2074 case IterativeParsingElementDelimiterState:
2077 *stack_.template Top<SizeType>() = *stack_.template Top<SizeType>() + 1;
2080 case IterativeParsingObjectFinishState:
2083 if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingMemberDelimiterState) {
2085 return IterativeParsingErrorState;
2088 SizeType c = *stack_.template Pop<SizeType>(1);
2090 if (src == IterativeParsingMemberValueState)
2093 IterativeParsingState n =
static_cast<IterativeParsingState
>(*stack_.template Pop<SizeType>(1));
2095 if (n == IterativeParsingStartState)
2096 n = IterativeParsingFinishState;
2098 bool hr = handler.EndObject(c);
2102 return IterativeParsingErrorState;
2110 case IterativeParsingArrayFinishState:
2113 if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingElementDelimiterState) {
2115 return IterativeParsingErrorState;
2118 SizeType c = *stack_.template Pop<SizeType>(1);
2120 if (src == IterativeParsingElementState)
2123 IterativeParsingState n =
static_cast<IterativeParsingState
>(*stack_.template Pop<SizeType>(1));
2125 if (n == IterativeParsingStartState)
2126 n = IterativeParsingFinishState;
2128 bool hr = handler.EndArray(c);
2132 return IterativeParsingErrorState;
2154 ParseValue<parseFlags>(is, handler);
2155 if (HasParseError()) {
2156 return IterativeParsingErrorState;
2158 return IterativeParsingFinishState;
2162 template <
typename InputStream>
2163 void HandleError(IterativeParsingState src, InputStream& is) {
2164 if (HasParseError()) {
2172 case IterativeParsingObjectInitialState:
2176 case IterativeParsingKeyValueDelimiterState:
2177 case IterativeParsingArrayInitialState:
2183 RAPIDJSON_FORCEINLINE
bool IsIterativeParsingDelimiterState(IterativeParsingState s)
const {
2184 return s >= IterativeParsingElementDelimiterState;
2187 RAPIDJSON_FORCEINLINE
bool IsIterativeParsingCompleteState(IterativeParsingState s)
const {
2188 return s <= IterativeParsingErrorState;
2191 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
2192 ParseResult IterativeParse(InputStream& is, Handler& handler) {
2193 parseResult_.
Clear();
2194 ClearStackOnExit scope(*
this);
2195 IterativeParsingState state = IterativeParsingStartState;
2197 SkipWhitespaceAndComments<parseFlags>(is);
2198 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
2199 while (is.Peek() !=
'\0') {
2200 Token t = Tokenize(is.Peek());
2201 IterativeParsingState n = Predict(state, t);
2202 IterativeParsingState d = Transit<parseFlags>(state, t, n, is, handler);
2204 if (d == IterativeParsingErrorState) {
2205 HandleError(state, is);
2212 if ((parseFlags & kParseStopWhenDoneFlag) && state == IterativeParsingFinishState)
2215 SkipWhitespaceAndComments<parseFlags>(is);
2216 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
2220 if (state != IterativeParsingFinishState)
2221 HandleError(state, is);
2223 return parseResult_;
2226 static const size_t kDefaultStackCapacity = 256;
2229 IterativeParsingState state_;
2237 #if defined(__clang__) || defined(_MSC_VER) 2246 #endif // RAPIDJSON_READER_H_ Miss fraction part in number.
Definition: error.h:85
The document is empty.
Definition: error.h:67
#define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset)
(Internal) macro to indicate and handle a parse error.
Definition: reader.h:119
void Clear()
Reset error code.
Definition: error.h:134
Missing a colon after a name of object member.
Definition: error.h:73
Missing a comma or ']' after an array element.
Definition: error.h:76
Miss exponent in number.
Definition: error.h:86
Missing a closing quotation mark in string.
Definition: error.h:81
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
#define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset)
Macro to indicate a parse error.
Definition: reader.h:100
#define RAPIDJSON_LIKELY(x)
Compiler branching hint for expression with high probability to be true.
Definition: rapidjson.h:494
ParseErrorCode
Error code of parsing.
Definition: error.h:64
Incorrect hex digit after \u escape in string.
Definition: error.h:78
Encoding conversion.
Definition: encodings.h:658
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition: reader.h:559
Missing a name for object member.
Definition: error.h:72
Default implementation of Handler.
Definition: fwd.h:85
bool RawNumber(const Ch *str, SizeType len, bool copy)
enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length) ...
Definition: reader.h:212
Number too big to be stored in double.
Definition: error.h:84
Represents an in-memory input byte stream.
Definition: memorystream.h:40
const Ch * src_
Current read position.
Definition: stream.h:168
bool HasParseError() const
Whether a parse error has occurred in the last parsing.
Definition: reader.h:682
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text (with kParseDefaultFlags)
Definition: reader.h:600
Read-only string stream.
Definition: fwd.h:47
Invalid encoding in string.
Definition: error.h:82
Invalid value.
Definition: error.h:70
#define RAPIDJSON_UNLIKELY(x)
Compiler branching hint for expression with low probability to be true.
Definition: rapidjson.h:507
bool IterativeParseNext(InputStream &is, Handler &handler)
Parse one token from JSON text.
Definition: reader.h:620
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
Missing a comma or '}' after an object member.
Definition: error.h:74
Definition: allocators.h:422
SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator.
Definition: fwd.h:88
Unspecific syntax error.
Definition: error.h:89
Invalid escape character in string.
Definition: error.h:80
void IterativeParseInit()
Initialize JSON text token-by-token parsing.
Definition: reader.h:607
ParseErrorCode GetParseErrorCode() const
Get the ParseErrorCode of last parsing.
Definition: reader.h:685
GenericReader(StackAllocator *stackAllocator=0, size_t stackCapacity=kDefaultStackCapacity)
Constructor.
Definition: reader.h:547
The document root must not follow by other values.
Definition: error.h:68
Result of parsing (wraps ParseErrorCode)
Definition: error.h:106
Parsing was terminated.
Definition: error.h:88
A read-write string stream.
Definition: fwd.h:52
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition: reader.h:688
RAPIDJSON_FORCEINLINE bool IterativeParseComplete() const
Check if token-by-token parsing JSON text is complete.
Definition: reader.h:677
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:437
The surrogate pair in string is invalid.
Definition: error.h:79
UTF-8 encoding.
Definition: encodings.h:96
SourceEncoding::Ch Ch
SourceEncoding character type.
Definition: reader.h:541