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
128 RAPIDJSON_NAMESPACE_BEGIN
139 #ifndef RAPIDJSON_PARSE_DEFAULT_FLAGS
140 #define RAPIDJSON_PARSE_DEFAULT_FLAGS kParseNoFlags
180 bool RawNumber(const Ch* str, SizeType length, bool copy);
181 bool String(const Ch* str, SizeType length, bool copy);
183 bool Key(const Ch* str, SizeType length, bool copy);
184 bool EndObject(SizeType memberCount);
186 bool EndArray(SizeType elementCount);
197 template<
typename Encoding = UTF8<>,
typename Derived =
void>
198 struct BaseReaderHandler {
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>
227 class StreamLocalCopy;
230 template<
typename Stream>
231 class StreamLocalCopy<
Stream, 1> {
233 StreamLocalCopy(
Stream& original) : s(original), original_(original) {}
234 ~StreamLocalCopy() { original_ = s; }
239 StreamLocalCopy& operator=(
const StreamLocalCopy&) ;
245 template<
typename Stream>
246 class StreamLocalCopy<
Stream, 0> {
248 StreamLocalCopy(
Stream& original) : s(original) {}
253 StreamLocalCopy& operator=(
const StreamLocalCopy&) ;
265 template<
typename InputStream>
267 internal::StreamLocalCopy<InputStream> copy(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
283 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);
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;
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);
501 #endif // RAPIDJSON_NEON
503 #ifdef RAPIDJSON_SIMD
506 is.src_ = const_cast<char*>(SkipWhitespace_SIMD(is.src_));
511 is.
src_ = SkipWhitespace_SIMD(is.
src_);
514 template<>
inline void SkipWhitespace(EncodedInputStream<UTF8<>, MemoryStream>& is) {
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>
539 class GenericReader {
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>
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_);
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); }
695 GenericReader(
const GenericReader&);
696 GenericReader& operator=(
const GenericReader&);
698 void ClearStack() { stack_.Clear(); }
701 struct ClearStackOnExit {
702 explicit ClearStackOnExit(GenericReader& r) : r_(r) {}
703 ~ClearStackOnExit() { r_.ClearStack(); }
706 ClearStackOnExit(
const ClearStackOnExit&);
707 ClearStackOnExit& operator=(
const ClearStackOnExit&);
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>
932 StackStream(internal::Stack<StackAllocator>& stack) : stack_(stack), length_(0) {}
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&);
953 internal::Stack<StackAllocator>& stack_;
958 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
959 void ParseString(InputStream& is,
Handler& handler,
bool isKey =
false) {
960 internal::StreamLocalCopy<InputStream> copy(is);
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;
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
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)]));
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();
1063 !Transcoder<SEncoding, TEncoding>::Validate(is, os) :
1064 !Transcoder<SEncoding, TEncoding>::Transcode(is, os))))
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);
1130 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(InsituStringStream& is, InsituStringStream& os) {
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);
1189 static RAPIDJSON_FORCEINLINE
void SkipUnescapedString(InsituStringStream& is) {
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);
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);
1294 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(InsituStringStream& is, InsituStringStream& os) {
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);
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);
1361 static RAPIDJSON_FORCEINLINE
void SkipUnescapedString(InsituStringStream& is) {
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,
bool backup,
bool pushOnTake>
1410 template<
typename InputStream>
1411 class NumberStream<InputStream, 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 char* Pop() {
return 0; }
1427 NumberStream& operator=(
const NumberStream&);
1432 template<
typename InputStream>
1433 class NumberStream<InputStream, true, false> :
public NumberStream<InputStream, false, false> {
1434 typedef NumberStream<InputStream, 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<char>(Base::is.Peek()));
1440 return Base::is.Take();
1443 RAPIDJSON_FORCEINLINE
void Push(
char c) {
1447 size_t Length() {
return stackStream.Length(); }
1450 stackStream.Put(
'\0');
1451 return stackStream.Pop();
1455 StackStream<char> stackStream;
1458 template<
typename InputStream>
1459 class NumberStream<InputStream, true, true> :
public NumberStream<InputStream, true, false> {
1460 typedef NumberStream<InputStream, 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 internal::StreamLocalCopy<InputStream> copy(is);
1470 NumberStream<InputStream,
1477 size_t startOffset = s.Tell();
1479 bool useNanOrInf =
false;
1482 bool minus = Consume(s,
'-');
1487 bool use64bit =
false;
1488 int significandDigit = 0;
1494 i = static_cast<unsigned>(s.TakePush() -
'0');
1505 i = i * 10 + static_cast<unsigned>(s.TakePush() -
'0');
1517 i = i * 10 + static_cast<unsigned>(s.TakePush() -
'0');
1523 if (Consume(s,
'N')) {
1524 if (Consume(s,
'a') && Consume(s,
'N')) {
1525 d = std::numeric_limits<double>::quiet_NaN();
1530 if (Consume(s,
'n') && Consume(s,
'f')) {
1531 d = (minus ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity());
1535 && Consume(s,
'i') && Consume(s,
't') && Consume(s,
'y')))) {
1549 bool useDouble =
false;
1555 d = static_cast<double>(i64);
1559 i64 = i64 * 10 + static_cast<unsigned>(s.TakePush() -
'0');
1566 d = static_cast<double>(i64);
1570 i64 = i64 * 10 + static_cast<unsigned>(s.TakePush() -
'0');
1578 d = d * 10 + (s.TakePush() -
'0');
1584 size_t decimalPosition;
1585 if (Consume(s,
'.')) {
1586 decimalPosition = s.Length();
1601 i64 = i64 * 10 + static_cast<unsigned>(s.TakePush() -
'0');
1608 d = static_cast<double>(i64);
1611 d = static_cast<double>(use64bit ? i64 : i);
1617 if (significandDigit < 17) {
1618 d = d * 10.0 + (s.TakePush() -
'0');
1628 decimalPosition = s.Length();
1632 if (Consume(s,
'e') || Consume(s,
'E')) {
1634 d = static_cast<double>(use64bit ? i64 : i);
1638 bool expMinus =
false;
1639 if (Consume(s,
'+'))
1641 else if (Consume(s,
'-'))
1645 exp = static_cast<int>(s.Take() -
'0');
1654 int maxExp = (expFrac + 2147483639) / 10;
1657 exp = exp * 10 + static_cast<int>(s.Take() -
'0');
1665 int maxExp = 308 - expFrac;
1667 exp = exp * 10 + static_cast<int>(s.Take() -
'0');
1686 typename InputStream::Ch* head = is.PutBegin();
1687 const size_t length = s.Tell() - startOffset;
1690 const typename TargetEncoding::Ch*
const str = reinterpret_cast<typename TargetEncoding::Ch*>(head);
1691 cont = handler.RawNumber(str,
SizeType(length),
false);
1694 SizeType numCharsToCopy = static_cast<SizeType>(s.Length());
1696 StackStream<typename TargetEncoding::Ch> dstStream(stack_);
1697 while (numCharsToCopy--) {
1698 Transcoder<UTF8<>, TargetEncoding>::Transcode(srcStream, dstStream);
1700 dstStream.Put(
'\0');
1701 const typename TargetEncoding::Ch* str = dstStream.Pop();
1702 const SizeType length = static_cast<SizeType>(dstStream.Length()) - 1;
1703 cont = handler.RawNumber(str,
SizeType(length),
true);
1707 size_t length = s.Length();
1708 const char* decimal = s.Pop();
1711 int p = exp + expFrac;
1713 d = internal::StrtodFullPrecision(d, p, decimal, length, decimalPosition, exp);
1715 d = internal::StrtodNormalPrecision(d, p);
1718 if (d > (std::numeric_limits<double>::max)()) {
1724 cont = handler.Double(minus ? -d : d);
1726 else if (useNanOrInf) {
1727 cont = handler.Double(d);
1732 cont = handler.Int64(static_cast<int64_t>(~i64 + 1));
1734 cont = handler.Uint64(i64);
1738 cont = handler.Int(static_cast<int32_t>(~i + 1));
1740 cont = handler.Uint(i);
1749 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
1750 void ParseValue(InputStream& is,
Handler& handler) {
1751 switch (is.Peek()) {
1752 case 'n': ParseNull <parseFlags>(is, handler);
break;
1753 case 't': ParseTrue <parseFlags>(is, handler);
break;
1754 case 'f': ParseFalse <parseFlags>(is, handler);
break;
1755 case '"': ParseString<parseFlags>(is, handler);
break;
1756 case '{': ParseObject<parseFlags>(is, handler);
break;
1757 case '[': ParseArray <parseFlags>(is, handler);
break;
1759 ParseNumber<parseFlags>(is, handler);
1768 enum IterativeParsingState {
1769 IterativeParsingFinishState = 0,
1770 IterativeParsingErrorState,
1771 IterativeParsingStartState,
1774 IterativeParsingObjectInitialState,
1775 IterativeParsingMemberKeyState,
1776 IterativeParsingMemberValueState,
1777 IterativeParsingObjectFinishState,
1780 IterativeParsingArrayInitialState,
1781 IterativeParsingElementState,
1782 IterativeParsingArrayFinishState,
1785 IterativeParsingValueState,
1788 IterativeParsingElementDelimiterState,
1789 IterativeParsingMemberDelimiterState,
1790 IterativeParsingKeyValueDelimiterState,
1792 cIterativeParsingStateCount
1797 LeftBracketToken = 0,
1800 LeftCurlyBracketToken,
1801 RightCurlyBracketToken,
1815 RAPIDJSON_FORCEINLINE Token Tokenize(Ch c)
const {
1818 #define N NumberToken
1819 #define N16 N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N
1821 static const unsigned char tokenMap[256] = {
1824 N, N, StringToken, N, N, N, N, N, N, N, N, N, CommaToken, N, N, N,
1825 N, N, N, N, N, N, N, N, N, N, ColonToken, N, N, N, N, N,
1827 N, N, N, N, N, N, N, N, N, N, N, LeftBracketToken, N, RightBracketToken, N, N,
1828 N, N, N, N, N, N, FalseToken, N, N, N, N, N, N, N, NullToken, N,
1829 N, N, N, N, TrueToken, N, N, N, N, N, N, LeftCurlyBracketToken, N, RightCurlyBracketToken, N, N,
1830 N16, N16, N16, N16, N16, N16, N16, N16
1836 if (
sizeof(Ch) == 1 || static_cast<unsigned>(c) < 256)
1837 return static_cast<Token>(tokenMap[static_cast<unsigned char>(c)]);
1842 RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token)
const {
1844 static const char G[cIterativeParsingStateCount][kTokenCount] = {
1847 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1848 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1849 IterativeParsingErrorState
1853 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1854 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1855 IterativeParsingErrorState
1859 IterativeParsingArrayInitialState,
1860 IterativeParsingErrorState,
1861 IterativeParsingObjectInitialState,
1862 IterativeParsingErrorState,
1863 IterativeParsingErrorState,
1864 IterativeParsingErrorState,
1865 IterativeParsingValueState,
1866 IterativeParsingValueState,
1867 IterativeParsingValueState,
1868 IterativeParsingValueState,
1869 IterativeParsingValueState
1873 IterativeParsingErrorState,
1874 IterativeParsingErrorState,
1875 IterativeParsingErrorState,
1876 IterativeParsingObjectFinishState,
1877 IterativeParsingErrorState,
1878 IterativeParsingErrorState,
1879 IterativeParsingMemberKeyState,
1880 IterativeParsingErrorState,
1881 IterativeParsingErrorState,
1882 IterativeParsingErrorState,
1883 IterativeParsingErrorState
1887 IterativeParsingErrorState,
1888 IterativeParsingErrorState,
1889 IterativeParsingErrorState,
1890 IterativeParsingErrorState,
1891 IterativeParsingErrorState,
1892 IterativeParsingKeyValueDelimiterState,
1893 IterativeParsingErrorState,
1894 IterativeParsingErrorState,
1895 IterativeParsingErrorState,
1896 IterativeParsingErrorState,
1897 IterativeParsingErrorState
1901 IterativeParsingErrorState,
1902 IterativeParsingErrorState,
1903 IterativeParsingErrorState,
1904 IterativeParsingObjectFinishState,
1905 IterativeParsingMemberDelimiterState,
1906 IterativeParsingErrorState,
1907 IterativeParsingErrorState,
1908 IterativeParsingErrorState,
1909 IterativeParsingErrorState,
1910 IterativeParsingErrorState,
1911 IterativeParsingErrorState
1915 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1916 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1917 IterativeParsingErrorState
1921 IterativeParsingArrayInitialState,
1922 IterativeParsingArrayFinishState,
1923 IterativeParsingObjectInitialState,
1924 IterativeParsingErrorState,
1925 IterativeParsingErrorState,
1926 IterativeParsingErrorState,
1927 IterativeParsingElementState,
1928 IterativeParsingElementState,
1929 IterativeParsingElementState,
1930 IterativeParsingElementState,
1931 IterativeParsingElementState
1935 IterativeParsingErrorState,
1936 IterativeParsingArrayFinishState,
1937 IterativeParsingErrorState,
1938 IterativeParsingErrorState,
1939 IterativeParsingElementDelimiterState,
1940 IterativeParsingErrorState,
1941 IterativeParsingErrorState,
1942 IterativeParsingErrorState,
1943 IterativeParsingErrorState,
1944 IterativeParsingErrorState,
1945 IterativeParsingErrorState
1949 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1950 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1951 IterativeParsingErrorState
1955 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1956 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1957 IterativeParsingErrorState
1961 IterativeParsingArrayInitialState,
1962 IterativeParsingArrayFinishState,
1963 IterativeParsingObjectInitialState,
1964 IterativeParsingErrorState,
1965 IterativeParsingErrorState,
1966 IterativeParsingErrorState,
1967 IterativeParsingElementState,
1968 IterativeParsingElementState,
1969 IterativeParsingElementState,
1970 IterativeParsingElementState,
1971 IterativeParsingElementState
1975 IterativeParsingErrorState,
1976 IterativeParsingErrorState,
1977 IterativeParsingErrorState,
1978 IterativeParsingObjectFinishState,
1979 IterativeParsingErrorState,
1980 IterativeParsingErrorState,
1981 IterativeParsingMemberKeyState,
1982 IterativeParsingErrorState,
1983 IterativeParsingErrorState,
1984 IterativeParsingErrorState,
1985 IterativeParsingErrorState
1989 IterativeParsingArrayInitialState,
1990 IterativeParsingErrorState,
1991 IterativeParsingObjectInitialState,
1992 IterativeParsingErrorState,
1993 IterativeParsingErrorState,
1994 IterativeParsingErrorState,
1995 IterativeParsingMemberValueState,
1996 IterativeParsingMemberValueState,
1997 IterativeParsingMemberValueState,
1998 IterativeParsingMemberValueState,
1999 IterativeParsingMemberValueState
2003 return static_cast<IterativeParsingState>(G[state][token]);
2008 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
2009 RAPIDJSON_FORCEINLINE IterativeParsingState Transit(IterativeParsingState src, Token token, IterativeParsingState dst, InputStream& is,
Handler& handler) {
2013 case IterativeParsingErrorState:
2016 case IterativeParsingObjectInitialState:
2017 case IterativeParsingArrayInitialState:
2021 IterativeParsingState n = src;
2022 if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState)
2023 n = IterativeParsingElementState;
2024 else if (src == IterativeParsingKeyValueDelimiterState)
2025 n = IterativeParsingMemberValueState;
2027 *stack_.template Push<SizeType>(1) = n;
2029 *stack_.template Push<SizeType>(1) = 0;
2031 bool hr = (dst == IterativeParsingObjectInitialState) ? handler.StartObject() : handler.StartArray();
2035 return IterativeParsingErrorState;
2043 case IterativeParsingMemberKeyState:
2044 ParseString<parseFlags>(is, handler,
true);
2045 if (HasParseError())
2046 return IterativeParsingErrorState;
2050 case IterativeParsingKeyValueDelimiterState:
2055 case IterativeParsingMemberValueState:
2057 ParseValue<parseFlags>(is, handler);
2058 if (HasParseError()) {
2059 return IterativeParsingErrorState;
2063 case IterativeParsingElementState:
2065 ParseValue<parseFlags>(is, handler);
2066 if (HasParseError()) {
2067 return IterativeParsingErrorState;
2071 case IterativeParsingMemberDelimiterState:
2072 case IterativeParsingElementDelimiterState:
2075 *stack_.template Top<SizeType>() = *stack_.template Top<SizeType>() + 1;
2078 case IterativeParsingObjectFinishState:
2083 return IterativeParsingErrorState;
2086 SizeType c = *stack_.template Pop<SizeType>(1);
2088 if (src == IterativeParsingMemberValueState)
2091 IterativeParsingState n = static_cast<IterativeParsingState>(*stack_.template Pop<SizeType>(1));
2093 if (n == IterativeParsingStartState)
2094 n = IterativeParsingFinishState;
2096 bool hr = handler.EndObject(c);
2100 return IterativeParsingErrorState;
2108 case IterativeParsingArrayFinishState:
2113 return IterativeParsingErrorState;
2116 SizeType c = *stack_.template Pop<SizeType>(1);
2118 if (src == IterativeParsingElementState)
2121 IterativeParsingState n = static_cast<IterativeParsingState>(*stack_.template Pop<SizeType>(1));
2123 if (n == IterativeParsingStartState)
2124 n = IterativeParsingFinishState;
2126 bool hr = handler.EndArray(c);
2130 return IterativeParsingErrorState;
2152 ParseValue<parseFlags>(is, handler);
2153 if (HasParseError()) {
2154 return IterativeParsingErrorState;
2156 return IterativeParsingFinishState;
2160 template <
typename InputStream>
2161 void HandleError(IterativeParsingState src, InputStream& is) {
2162 if (HasParseError()) {
2170 case IterativeParsingObjectInitialState:
2174 case IterativeParsingKeyValueDelimiterState:
2175 case IterativeParsingArrayInitialState:
2181 RAPIDJSON_FORCEINLINE
bool IsIterativeParsingDelimiterState(IterativeParsingState s)
const {
2182 return s >= IterativeParsingElementDelimiterState;
2185 RAPIDJSON_FORCEINLINE
bool IsIterativeParsingCompleteState(IterativeParsingState s)
const {
2186 return s <= IterativeParsingErrorState;
2189 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
2190 ParseResult IterativeParse(InputStream& is,
Handler& handler) {
2191 parseResult_.Clear();
2192 ClearStackOnExit scope(*
this);
2193 IterativeParsingState state = IterativeParsingStartState;
2195 SkipWhitespaceAndComments<parseFlags>(is);
2196 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
2197 while (is.Peek() !=
'\0') {
2198 Token t = Tokenize(is.Peek());
2199 IterativeParsingState n = Predict(state, t);
2200 IterativeParsingState d = Transit<parseFlags>(state, t, n, is, handler);
2202 if (d == IterativeParsingErrorState) {
2203 HandleError(state, is);
2213 SkipWhitespaceAndComments<parseFlags>(is);
2214 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
2218 if (state != IterativeParsingFinishState)
2219 HandleError(state, is);
2221 return parseResult_;
2224 static const size_t kDefaultStackCapacity = 256;
2225 internal::Stack<StackAllocator> stack_;
2226 ParseResult parseResult_;
2227 IterativeParsingState state_;
2231 typedef GenericReader<UTF8<>, UTF8<> >
Reader;
2233 RAPIDJSON_NAMESPACE_END
2235 #if defined(__clang__) || defined(_MSC_VER)
2244 #endif // RAPIDJSON_READER_H_