15 #ifndef RAPIDJSON_READER_H_
16 #define RAPIDJSON_READER_H_
21 #include "encodings.h"
22 #include "internal/meta.h"
23 #include "internal/stack.h"
24 #include "internal/strtod.h"
26 #if defined(RAPIDJSON_SIMD) && defined(_MSC_VER)
28 #pragma intrinsic(_BitScanForward)
30 #ifdef RAPIDJSON_SSE42
31 #include <nmmintrin.h>
32 #elif defined(RAPIDJSON_SSE2)
33 #include <emmintrin.h>
38 RAPIDJSON_DIAG_OFF(4127)
39 RAPIDJSON_DIAG_OFF(4702)
44 RAPIDJSON_DIAG_OFF(effc++)
48 #define RAPIDJSON_NOTHING
49 #ifndef RAPIDJSON_PARSE_ERROR_EARLY_RETURN
50 #define RAPIDJSON_PARSE_ERROR_EARLY_RETURN(value) \
51 RAPIDJSON_MULTILINEMACRO_BEGIN \
52 if (HasParseError()) { return value; } \
53 RAPIDJSON_MULTILINEMACRO_END
55 #define RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID \
56 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(RAPIDJSON_NOTHING)
89 #ifndef RAPIDJSON_PARSE_ERROR_NORETURN
90 #define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset) \
91 RAPIDJSON_MULTILINEMACRO_BEGIN \
92 RAPIDJSON_ASSERT(!HasParseError()); \
93 SetParseError(parseErrorCode, offset); \
94 RAPIDJSON_MULTILINEMACRO_END
108 #ifndef RAPIDJSON_PARSE_ERROR
109 #define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset) \
110 RAPIDJSON_MULTILINEMACRO_BEGIN \
111 RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset); \
112 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; \
113 RAPIDJSON_MULTILINEMACRO_END
118 RAPIDJSON_NAMESPACE_BEGIN
129 #ifndef RAPIDJSON_PARSE_DEFAULT_FLAGS
130 #define RAPIDJSON_PARSE_DEFAULT_FLAGS kParseNoFlags
180 template<
typename Encoding = UTF8<>,
typename Derived =
void>
182 typedef typename Encoding::Ch Ch;
184 typedef typename internal::SelectIf<internal::IsSame<Derived, void>,
BaseReaderHandler, Derived>
::Type Override;
186 bool Default() {
return true; }
187 bool Null() {
return static_cast<Override&
>(*this).Default(); }
188 bool Bool(
bool) {
return static_cast<Override&
>(*this).Default(); }
189 bool Int(
int) {
return static_cast<Override&
>(*this).Default(); }
190 bool Uint(
unsigned) {
return static_cast<Override&
>(*this).Default(); }
191 bool Int64(int64_t) {
return static_cast<Override&
>(*this).Default(); }
192 bool Uint64(uint64_t) {
return static_cast<Override&
>(*this).Default(); }
193 bool Double(
double) {
return static_cast<Override&
>(*this).Default(); }
194 bool String(
const Ch*,
SizeType,
bool) {
return static_cast<Override&
>(*this).Default(); }
195 bool StartObject() {
return static_cast<Override&
>(*this).Default(); }
196 bool Key(
const Ch* str,
SizeType len,
bool copy) {
return static_cast<Override&
>(*this).String(str, len, copy); }
197 bool EndObject(
SizeType) {
return static_cast<Override&
>(*this).Default(); }
198 bool StartArray() {
return static_cast<Override&
>(*this).Default(); }
199 bool EndArray(
SizeType) {
return static_cast<Override&
>(*this).Default(); }
207 template<typename Stream, int = StreamTraits<Stream>::copyOptimization>
208 class StreamLocalCopy;
211 template<
typename Stream>
212 class StreamLocalCopy<
Stream, 1> {
214 StreamLocalCopy(
Stream& original) : s(original), original_(original) {}
215 ~StreamLocalCopy() { original_ = s; }
220 StreamLocalCopy& operator=(
const StreamLocalCopy&) ;
226 template<
typename Stream>
227 class StreamLocalCopy<Stream, 0> {
229 StreamLocalCopy(Stream& original) : s(original) {}
234 StreamLocalCopy& operator=(
const StreamLocalCopy&) ;
246 template<
typename InputStream>
248 internal::StreamLocalCopy<InputStream> copy(is);
249 InputStream& s(copy.s);
251 while (s.Peek() ==
' ' || s.Peek() ==
'\n' || s.Peek() ==
'\r' || s.Peek() ==
'\t')
255 #ifdef RAPIDJSON_SSE42
257 inline const char *SkipWhitespace_SIMD(
const char* p) {
259 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
265 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) & ~15);
266 while (p != nextAligned)
267 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
273 static const char whitespace[16] =
" \n\r\t";
274 const __m128i w = _mm_loadu_si128((
const __m128i *)&whitespace[0]);
277 const __m128i s = _mm_load_si128((
const __m128i *)p);
278 const unsigned r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY));
280 #ifdef _MSC_VER // Find the index of first non-whitespace
281 unsigned long offset;
282 _BitScanForward(&offset, r);
285 return p + __builtin_ffs(r) - 1;
291 #elif defined(RAPIDJSON_SSE2)
294 inline const char *SkipWhitespace_SIMD(
const char* p) {
296 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
302 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) & ~15);
303 while (p != nextAligned)
304 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
310 static const char whitespaces[4][17] = {
312 "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
313 "\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r",
314 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"};
316 const __m128i w0 = _mm_loadu_si128((
const __m128i *)&whitespaces[0][0]);
317 const __m128i w1 = _mm_loadu_si128((
const __m128i *)&whitespaces[1][0]);
318 const __m128i w2 = _mm_loadu_si128((
const __m128i *)&whitespaces[2][0]);
319 const __m128i w3 = _mm_loadu_si128((
const __m128i *)&whitespaces[3][0]);
322 const __m128i s = _mm_load_si128((
const __m128i *)p);
323 __m128i x = _mm_cmpeq_epi8(s, w0);
324 x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1));
325 x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2));
326 x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3));
327 unsigned short r = (
unsigned short)~_mm_movemask_epi8(x);
329 #ifdef _MSC_VER // Find the index of first non-whitespace
330 unsigned long offset;
331 _BitScanForward(&offset, r);
334 return p + __builtin_ffs(r) - 1;
340 #endif // RAPIDJSON_SSE2
342 #ifdef RAPIDJSON_SIMD
345 is.src_ =
const_cast<char*
>(SkipWhitespace_SIMD(is.src_));
350 is.
src_ = SkipWhitespace_SIMD(is.
src_);
352 #endif // RAPIDJSON_SIMD
373 template <
typename SourceEncoding,
typename TargetEncoding,
typename StackAllocator = CrtAllocator>
376 typedef typename SourceEncoding::Ch
Ch;
382 GenericReader(StackAllocator* stackAllocator = 0,
size_t stackCapacity = kDefaultStackCapacity) : stack_(stackAllocator, stackCapacity), parseResult_() {}
392 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
395 return IterativeParse<parseFlags>(is, handler);
397 parseResult_.
Clear();
399 ClearStackOnExit scope(*
this);
403 if (is.Peek() ==
'\0') {
405 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
408 ParseValue<parseFlags>(is, handler);
409 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
414 if (is.Peek() !=
'\0') {
416 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
431 template <
typename InputStream,
typename Handler>
433 return Parse<kParseDefaultFlags>(is, handler);
446 void SetParseError(
ParseErrorCode code,
size_t offset) { parseResult_.Set(code, offset); }
450 GenericReader(
const GenericReader&);
451 GenericReader& operator=(
const GenericReader&);
453 void ClearStack() { stack_.Clear(); }
456 struct ClearStackOnExit {
457 explicit ClearStackOnExit(GenericReader& r) : r_(r) {}
458 ~ClearStackOnExit() { r_.ClearStack(); }
461 ClearStackOnExit(
const ClearStackOnExit&);
462 ClearStackOnExit& operator=(
const ClearStackOnExit&);
466 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
467 void ParseObject(InputStream& is, Handler& handler) {
471 if (!handler.StartObject())
476 if (is.Peek() ==
'}') {
478 if (!handler.EndObject(0))
484 if (is.Peek() !=
'"')
487 ParseString<parseFlags>(is, handler,
true);
488 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
492 if (is.Take() !=
':')
497 ParseValue<parseFlags>(is, handler);
498 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
507 if (!handler.EndObject(memberCount))
516 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
517 void ParseArray(InputStream& is, Handler& handler) {
521 if (!handler.StartArray())
526 if (is.Peek() ==
']') {
528 if (!handler.EndArray(0))
534 ParseValue<parseFlags>(is, handler);
535 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
543 if (!handler.EndArray(elementCount))
551 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
552 void ParseNull(InputStream& is, Handler& handler) {
556 if (is.Take() ==
'u' && is.Take() ==
'l' && is.Take() ==
'l') {
564 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
565 void ParseTrue(InputStream& is, Handler& handler) {
569 if (is.Take() ==
'r' && is.Take() ==
'u' && is.Take() ==
'e') {
570 if (!handler.Bool(
true))
577 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
578 void ParseFalse(InputStream& is, Handler& handler) {
582 if (is.Take() ==
'a' && is.Take() ==
'l' && is.Take() ==
's' && is.Take() ==
'e') {
583 if (!handler.Bool(
false))
591 template<
typename InputStream>
592 unsigned ParseHex4(InputStream& is) {
593 unsigned codepoint = 0;
594 for (
int i = 0; i < 4; i++) {
597 codepoint +=
static_cast<unsigned>(c);
598 if (c >=
'0' && c <=
'9')
600 else if (c >=
'A' && c <=
'F')
601 codepoint -=
'A' - 10;
602 else if (c >=
'a' && c <=
'f')
603 codepoint -=
'a' - 10;
606 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(0);
612 template <
typename CharType>
617 StackStream(internal::Stack<StackAllocator>& stack) : stack_(stack), length_(0) {}
618 RAPIDJSON_FORCEINLINE
void Put(Ch c) {
619 *stack_.template Push<Ch>() = c;
622 size_t Length()
const {
return length_; }
624 return stack_.template Pop<Ch>(length_);
628 StackStream(
const StackStream&);
629 StackStream& operator=(
const StackStream&);
631 internal::Stack<StackAllocator>& stack_;
636 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
637 void ParseString(InputStream& is, Handler& handler,
bool isKey =
false) {
638 internal::StreamLocalCopy<InputStream> copy(is);
639 InputStream& s(copy.s);
641 bool success =
false;
643 typename InputStream::Ch *head = s.PutBegin();
644 ParseStringToStream<parseFlags, SourceEncoding, SourceEncoding>(s, s);
645 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
646 size_t length = s.PutEnd(head) - 1;
648 const typename TargetEncoding::Ch*
const str = (
typename TargetEncoding::Ch*)head;
649 success = (isKey ? handler.Key(str,
SizeType(length),
false) : handler.String(str,
SizeType(length),
false));
652 StackStream<typename TargetEncoding::Ch> stackStream(stack_);
653 ParseStringToStream<parseFlags, SourceEncoding, TargetEncoding>(s, stackStream);
654 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
656 const typename TargetEncoding::Ch*
const str = stackStream.Pop();
657 success = (isKey ? handler.Key(str, length,
true) : handler.String(str, length,
true));
665 template<
unsigned parseFlags,
typename SEncoding,
typename TEncoding,
typename InputStream,
typename OutputStream>
666 RAPIDJSON_FORCEINLINE
void ParseStringToStream(InputStream& is, OutputStream& os) {
668 #define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
669 static const char escape[256] = {
670 Z16, Z16, 0, 0,
'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'/',
671 Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'\\', 0, 0, 0,
672 0, 0,
'\b', 0, 0, 0,
'\f', 0, 0, 0, 0, 0, 0, 0,
'\n', 0,
673 0, 0,
'\r', 0,
'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
674 Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16
687 if ((
sizeof(Ch) == 1 ||
unsigned(e) < 256) && escape[(
unsigned char)e]) {
688 os.Put(escape[(
unsigned char)e]);
691 unsigned codepoint = ParseHex4(is);
692 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
693 if (codepoint >= 0xD800 && codepoint <= 0xDBFF) {
695 if (is.Take() !=
'\\' || is.Take() !=
'u')
697 unsigned codepoint2 = ParseHex4(is);
698 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
699 if (codepoint2 < 0xDC00 || codepoint2 > 0xDFFF)
701 codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000;
703 TEncoding::Encode(os, codepoint);
715 else if ((
unsigned)c < 0x20)
719 !Transcoder<SEncoding, TEncoding>::Validate(is, os) :
720 !Transcoder<SEncoding, TEncoding>::Transcode(is, os))
726 template<
typename InputStream,
bool backup>
729 template<
typename InputStream>
730 class NumberStream<InputStream, false> {
732 NumberStream(GenericReader& reader, InputStream& s) : is(s) { (void)reader; }
735 RAPIDJSON_FORCEINLINE Ch Peek()
const {
return is.Peek(); }
736 RAPIDJSON_FORCEINLINE Ch TakePush() {
return is.Take(); }
737 RAPIDJSON_FORCEINLINE Ch Take() {
return is.Take(); }
738 size_t Tell() {
return is.Tell(); }
739 size_t Length() {
return 0; }
740 const char* Pop() {
return 0; }
743 NumberStream& operator=(
const NumberStream&);
748 template<
typename InputStream>
749 class NumberStream<InputStream, true> :
public NumberStream<InputStream, false> {
750 typedef NumberStream<InputStream, false> Base;
752 NumberStream(GenericReader& reader, InputStream& is) : NumberStream<InputStream, false>(reader, is), stackStream(reader.stack_) {}
755 RAPIDJSON_FORCEINLINE Ch TakePush() {
756 stackStream.Put((
char)Base::is.Peek());
757 return Base::is.Take();
760 size_t Length() {
return stackStream.Length(); }
763 stackStream.Put(
'\0');
764 return stackStream.Pop();
768 StackStream<char> stackStream;
771 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
772 void ParseNumber(InputStream& is, Handler& handler) {
773 internal::StreamLocalCopy<InputStream> copy(is);
774 NumberStream<InputStream, (parseFlags & kParseFullPrecisionFlag) != 0> s(*
this, copy.s);
778 if (s.Peek() ==
'-') {
786 bool use64bit =
false;
787 int significandDigit = 0;
788 if (s.Peek() ==
'0') {
792 else if (s.Peek() >=
'1' && s.Peek() <=
'9') {
793 i =
static_cast<unsigned>(s.TakePush() -
'0');
796 while (s.Peek() >=
'0' && s.Peek() <=
'9') {
797 if (i >= 214748364) {
798 if (i != 214748364 || s.Peek() >
'8') {
804 i = i * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
808 while (s.Peek() >=
'0' && s.Peek() <=
'9') {
809 if (i >= 429496729) {
810 if (i != 429496729 || s.Peek() >
'5') {
816 i = i * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
824 bool useDouble =
false;
828 while (s.Peek() >=
'0' && s.Peek() <=
'9') {
835 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
839 while (s.Peek() >=
'0' && s.Peek() <=
'9') {
846 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
853 while (s.Peek() >=
'0' && s.Peek() <=
'9') {
854 if (d >= 1.7976931348623157e307)
856 d = d * 10 + (s.TakePush() -
'0');
862 size_t decimalPosition;
863 if (s.Peek() ==
'.') {
865 decimalPosition = s.Length();
867 if (!(s.Peek() >=
'0' && s.Peek() <=
'9'))
876 while (s.Peek() >=
'0' && s.Peek() <=
'9') {
880 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
890 d = use64bit ? (double)i64 : (
double)i;
895 while (s.Peek() >=
'0' && s.Peek() <=
'9') {
896 if (significandDigit < 17) {
897 d = d * 10.0 + (s.TakePush() -
'0');
907 decimalPosition = s.Length();
911 if (s.Peek() ==
'e' || s.Peek() ==
'E') {
913 d = use64bit ? i64 : i;
918 bool expMinus =
false;
921 else if (s.Peek() ==
'-') {
926 if (s.Peek() >=
'0' && s.Peek() <=
'9') {
927 exp = s.Take() -
'0';
929 while (s.Peek() >=
'0' && s.Peek() <=
'9') {
930 exp = exp * 10 + (s.Take() -
'0');
931 if (exp >= 214748364) {
932 while (s.Peek() >=
'0' && s.Peek() <=
'9')
938 int maxExp = 308 - expFrac;
939 while (s.Peek() >=
'0' && s.Peek() <=
'9') {
940 exp = exp * 10 + (s.Take() -
'0');
955 size_t length = s.Length();
956 const char* decimal = s.Pop();
959 int p = exp + expFrac;
961 d = internal::StrtodFullPrecision(d, p, decimal, length, decimalPosition, exp);
963 d = internal::StrtodNormalPrecision(d, p);
965 cont = handler.Double(minus ? -d : d);
970 cont = handler.Int64(static_cast<int64_t>(~i64 + 1));
972 cont = handler.Uint64(i64);
976 cont = handler.Int(static_cast<int32_t>(~i + 1));
978 cont = handler.Uint(i);
986 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
987 void ParseValue(InputStream& is, Handler& handler) {
989 case 'n': ParseNull <parseFlags>(is, handler);
break;
990 case 't': ParseTrue <parseFlags>(is, handler);
break;
991 case 'f': ParseFalse <parseFlags>(is, handler);
break;
992 case '"': ParseString<parseFlags>(is, handler);
break;
993 case '{': ParseObject<parseFlags>(is, handler);
break;
994 case '[': ParseArray <parseFlags>(is, handler);
break;
995 default : ParseNumber<parseFlags>(is, handler);
1002 enum IterativeParsingState {
1003 IterativeParsingStartState = 0,
1004 IterativeParsingFinishState,
1005 IterativeParsingErrorState,
1008 IterativeParsingObjectInitialState,
1009 IterativeParsingMemberKeyState,
1010 IterativeParsingKeyValueDelimiterState,
1011 IterativeParsingMemberValueState,
1012 IterativeParsingMemberDelimiterState,
1013 IterativeParsingObjectFinishState,
1016 IterativeParsingArrayInitialState,
1017 IterativeParsingElementState,
1018 IterativeParsingElementDelimiterState,
1019 IterativeParsingArrayFinishState,
1022 IterativeParsingValueState,
1024 cIterativeParsingStateCount
1029 LeftBracketToken = 0,
1032 LeftCurlyBracketToken,
1033 RightCurlyBracketToken,
1047 RAPIDJSON_FORCEINLINE Token Tokenize(Ch c) {
1050 #define N NumberToken
1051 #define N16 N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N
1053 static const unsigned char tokenMap[256] = {
1056 N, N, StringToken, N, N, N, N, N, N, N, N, N, CommaToken, N, N, N,
1057 N, N, N, N, N, N, N, N, N, N, ColonToken, N, N, N, N, N,
1059 N, N, N, N, N, N, N, N, N, N, N, LeftBracketToken, N, RightBracketToken, N, N,
1060 N, N, N, N, N, N, FalseToken, N, N, N, N, N, N, N, NullToken, N,
1061 N, N, N, N, TrueToken, N, N, N, N, N, N, LeftCurlyBracketToken, N, RightCurlyBracketToken, N, N,
1062 N16, N16, N16, N16, N16, N16, N16, N16
1068 if (
sizeof(Ch) == 1 ||
unsigned(c) < 256)
1069 return (Token)tokenMap[(
unsigned char)c];
1074 RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token) {
1076 static const char G[cIterativeParsingStateCount][kTokenCount] = {
1079 IterativeParsingArrayInitialState,
1080 IterativeParsingErrorState,
1081 IterativeParsingObjectInitialState,
1082 IterativeParsingErrorState,
1083 IterativeParsingErrorState,
1084 IterativeParsingErrorState,
1085 IterativeParsingValueState,
1086 IterativeParsingValueState,
1087 IterativeParsingValueState,
1088 IterativeParsingValueState,
1089 IterativeParsingValueState
1093 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1094 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1095 IterativeParsingErrorState
1099 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1100 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1101 IterativeParsingErrorState
1105 IterativeParsingErrorState,
1106 IterativeParsingErrorState,
1107 IterativeParsingErrorState,
1108 IterativeParsingObjectFinishState,
1109 IterativeParsingErrorState,
1110 IterativeParsingErrorState,
1111 IterativeParsingMemberKeyState,
1112 IterativeParsingErrorState,
1113 IterativeParsingErrorState,
1114 IterativeParsingErrorState,
1115 IterativeParsingErrorState
1119 IterativeParsingErrorState,
1120 IterativeParsingErrorState,
1121 IterativeParsingErrorState,
1122 IterativeParsingErrorState,
1123 IterativeParsingErrorState,
1124 IterativeParsingKeyValueDelimiterState,
1125 IterativeParsingErrorState,
1126 IterativeParsingErrorState,
1127 IterativeParsingErrorState,
1128 IterativeParsingErrorState,
1129 IterativeParsingErrorState
1133 IterativeParsingArrayInitialState,
1134 IterativeParsingErrorState,
1135 IterativeParsingObjectInitialState,
1136 IterativeParsingErrorState,
1137 IterativeParsingErrorState,
1138 IterativeParsingErrorState,
1139 IterativeParsingMemberValueState,
1140 IterativeParsingMemberValueState,
1141 IterativeParsingMemberValueState,
1142 IterativeParsingMemberValueState,
1143 IterativeParsingMemberValueState
1147 IterativeParsingErrorState,
1148 IterativeParsingErrorState,
1149 IterativeParsingErrorState,
1150 IterativeParsingObjectFinishState,
1151 IterativeParsingMemberDelimiterState,
1152 IterativeParsingErrorState,
1153 IterativeParsingErrorState,
1154 IterativeParsingErrorState,
1155 IterativeParsingErrorState,
1156 IterativeParsingErrorState,
1157 IterativeParsingErrorState
1161 IterativeParsingErrorState,
1162 IterativeParsingErrorState,
1163 IterativeParsingErrorState,
1164 IterativeParsingErrorState,
1165 IterativeParsingErrorState,
1166 IterativeParsingErrorState,
1167 IterativeParsingMemberKeyState,
1168 IterativeParsingErrorState,
1169 IterativeParsingErrorState,
1170 IterativeParsingErrorState,
1171 IterativeParsingErrorState
1175 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1176 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1177 IterativeParsingErrorState
1181 IterativeParsingArrayInitialState,
1182 IterativeParsingArrayFinishState,
1183 IterativeParsingObjectInitialState,
1184 IterativeParsingErrorState,
1185 IterativeParsingErrorState,
1186 IterativeParsingErrorState,
1187 IterativeParsingElementState,
1188 IterativeParsingElementState,
1189 IterativeParsingElementState,
1190 IterativeParsingElementState,
1191 IterativeParsingElementState
1195 IterativeParsingErrorState,
1196 IterativeParsingArrayFinishState,
1197 IterativeParsingErrorState,
1198 IterativeParsingErrorState,
1199 IterativeParsingElementDelimiterState,
1200 IterativeParsingErrorState,
1201 IterativeParsingErrorState,
1202 IterativeParsingErrorState,
1203 IterativeParsingErrorState,
1204 IterativeParsingErrorState,
1205 IterativeParsingErrorState
1209 IterativeParsingArrayInitialState,
1210 IterativeParsingErrorState,
1211 IterativeParsingObjectInitialState,
1212 IterativeParsingErrorState,
1213 IterativeParsingErrorState,
1214 IterativeParsingErrorState,
1215 IterativeParsingElementState,
1216 IterativeParsingElementState,
1217 IterativeParsingElementState,
1218 IterativeParsingElementState,
1219 IterativeParsingElementState
1223 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1224 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1225 IterativeParsingErrorState
1229 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1230 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1231 IterativeParsingErrorState
1235 return (IterativeParsingState)G[state][token];
1240 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
1241 RAPIDJSON_FORCEINLINE IterativeParsingState Transit(IterativeParsingState src, Token token, IterativeParsingState dst, InputStream& is, Handler& handler) {
1245 case IterativeParsingErrorState:
1248 case IterativeParsingObjectInitialState:
1249 case IterativeParsingArrayInitialState:
1253 IterativeParsingState n = src;
1254 if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState)
1255 n = IterativeParsingElementState;
1256 else if (src == IterativeParsingKeyValueDelimiterState)
1257 n = IterativeParsingMemberValueState;
1259 *stack_.template Push<SizeType>(1) = n;
1261 *stack_.template Push<SizeType>(1) = 0;
1263 bool hr = (dst == IterativeParsingObjectInitialState) ? handler.StartObject() : handler.StartArray();
1267 return IterativeParsingErrorState;
1275 case IterativeParsingMemberKeyState:
1276 ParseString<parseFlags>(is, handler,
true);
1277 if (HasParseError())
1278 return IterativeParsingErrorState;
1282 case IterativeParsingKeyValueDelimiterState:
1287 case IterativeParsingMemberValueState:
1289 ParseValue<parseFlags>(is, handler);
1290 if (HasParseError()) {
1291 return IterativeParsingErrorState;
1295 case IterativeParsingElementState:
1297 ParseValue<parseFlags>(is, handler);
1298 if (HasParseError()) {
1299 return IterativeParsingErrorState;
1303 case IterativeParsingMemberDelimiterState:
1304 case IterativeParsingElementDelimiterState:
1307 *stack_.template Top<SizeType>() = *stack_.template Top<SizeType>() + 1;
1310 case IterativeParsingObjectFinishState:
1313 SizeType c = *stack_.template Pop<SizeType>(1);
1315 if (src == IterativeParsingMemberValueState)
1318 IterativeParsingState n =
static_cast<IterativeParsingState
>(*stack_.template Pop<SizeType>(1));
1320 if (n == IterativeParsingStartState)
1321 n = IterativeParsingFinishState;
1323 bool hr = handler.EndObject(c);
1327 return IterativeParsingErrorState;
1335 case IterativeParsingArrayFinishState:
1338 SizeType c = *stack_.template Pop<SizeType>(1);
1340 if (src == IterativeParsingElementState)
1343 IterativeParsingState n =
static_cast<IterativeParsingState
>(*stack_.template Pop<SizeType>(1));
1345 if (n == IterativeParsingStartState)
1346 n = IterativeParsingFinishState;
1348 bool hr = handler.EndArray(c);
1352 return IterativeParsingErrorState;
1374 ParseValue<parseFlags>(is, handler);
1375 if (HasParseError()) {
1376 return IterativeParsingErrorState;
1378 return IterativeParsingFinishState;
1382 template <
typename InputStream>
1383 void HandleError(IterativeParsingState src, InputStream& is) {
1384 if (HasParseError()) {
1392 case IterativeParsingObjectInitialState:
1401 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
1402 ParseResult IterativeParse(InputStream& is, Handler& handler) {
1403 parseResult_.Clear();
1404 ClearStackOnExit scope(*
this);
1405 IterativeParsingState state = IterativeParsingStartState;
1408 while (is.Peek() !=
'\0') {
1409 Token t = Tokenize(is.Peek());
1410 IterativeParsingState n = Predict(state, t);
1411 IterativeParsingState d = Transit<parseFlags>(state, t, n, is, handler);
1413 if (d == IterativeParsingErrorState) {
1414 HandleError(state, is);
1428 if (state != IterativeParsingFinishState)
1429 HandleError(state, is);
1431 return parseResult_;
1434 static const size_t kDefaultStackCapacity = 256;
1435 internal::Stack<StackAllocator> stack_;
1436 ParseResult parseResult_;
1442 RAPIDJSON_NAMESPACE_END
1452 #endif // RAPIDJSON_READER_H_
Read-only string stream.
Definition: rapidjson.h:571
Iterative(constant complexity in terms of function call stack size) parsing.
Definition: reader.h:140
Concept for receiving events from GenericReader upon parsing. The functions return true if no error o...
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition: reader.h:393
Validate encoding of JSON strings.
Definition: reader.h:139
Invalid value.
Definition: error.h:65
The surrogate pair in string is invalid.
Definition: error.h:74
#define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset)
(Internal) macro to indicate and handle a parse error.
Definition: reader.h:109
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:261
SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator.
Definition: reader.h:374
Missing a colon after a name of object member.
Definition: error.h:68
Incorrect hex digit after \u escape in string.
Definition: error.h:73
bool HasParseError() const
Whether a parse error has occured in the last parsing.
Definition: reader.h:437
Miss fraction part in number.
Definition: error.h:80
unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:322
ParseErrorCode
Error code of parsing.
Definition: error.h:59
GenericReader(StackAllocator *stackAllocator=0, size_t stackCapacity=kDefaultStackCapacity)
Constructor.
Definition: reader.h:382
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition: reader.h:443
void Clear()
Reset error code.
Definition: error.h:123
Missing a comma or ']' after an array element.
Definition: error.h:71
SourceEncoding::Ch Ch
SourceEncoding character type.
Definition: reader.h:376
The document root must not follow by other values.
Definition: error.h:63
const Ch * src_
Current read position.
Definition: rapidjson.h:585
#define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset)
Macro to indicate a parse error.
Definition: reader.h:90
No flags are set.
Definition: reader.h:137
Unspecific syntax error.
Definition: error.h:84
Parse number in full precision (but slower).
Definition: reader.h:142
Missing a closing quotation mark in string.
Definition: error.h:76
Invalid escape character in string.
Definition: error.h:75
Result of parsing (wraps ParseErrorCode)
Definition: error.h:101
Missing a name for object member.
Definition: error.h:67
Type
Type of JSON value.
Definition: rapidjson.h:642
After parsing a complete JSON root from stream, stop further processing the rest of stream...
Definition: reader.h:141
Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS. ...
Definition: reader.h:143
ParseFlag
Combination of parseFlags.
Definition: reader.h:136
Concept for reading and writing characters.
Parsing was terminated.
Definition: error.h:83
Number too big to be stored in double.
Definition: error.h:79
Default implementation of Handler.
Definition: reader.h:181
Miss exponent in number.
Definition: error.h:81
common definitions and configuration
In-situ(destructive) parsing.
Definition: reader.h:138
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text (with kParseDefaultFlags)
Definition: reader.h:432
UTF-8 encoding.
Definition: encodings.h:96
void SkipWhitespace(StringStream &is)
Template function specialization for StringStream.
Definition: reader.h:349
ParseErrorCode GetParseErrorCode() const
Get the ParseErrorCode of last parsing.
Definition: reader.h:440
The document is empty.
Definition: error.h:62
Missing a comma or '}' after an object member.
Definition: error.h:69
Invalid encoding in string.
Definition: error.h:77
GenericReader< UTF8<>, UTF8<> > Reader
Reader with UTF8 encoding and default allocator.
Definition: reader.h:1440
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
#define RAPIDJSON_PARSE_DEFAULT_FLAGS
User-defined kParseDefaultFlags definition.
Definition: reader.h:130
A read-write string stream.
Definition: rapidjson.h:605