15 #ifndef RAPIDJSON_READER_H_
16 #define RAPIDJSON_READER_H_
28 #if defined(RAPIDJSON_SIMD) && defined(_MSC_VER)
30 #pragma intrinsic(_BitScanForward)
32 #ifdef RAPIDJSON_SSE42
33 #include <nmmintrin.h>
34 #elif defined(RAPIDJSON_SSE2)
35 #include <emmintrin.h>
40 RAPIDJSON_DIAG_OFF(4127)
41 RAPIDJSON_DIAG_OFF(4702)
46 RAPIDJSON_DIAG_OFF(old-style-cast)
47 RAPIDJSON_DIAG_OFF(padded)
48 RAPIDJSON_DIAG_OFF(
switch-
enum)
53 RAPIDJSON_DIAG_OFF(effc++)
57 #define RAPIDJSON_NOTHING
58 #ifndef RAPIDJSON_PARSE_ERROR_EARLY_RETURN
59 #define RAPIDJSON_PARSE_ERROR_EARLY_RETURN(value) \
60 RAPIDJSON_MULTILINEMACRO_BEGIN \
61 if (RAPIDJSON_UNLIKELY(HasParseError())) { return value; } \
62 RAPIDJSON_MULTILINEMACRO_END
64 #define RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID \
65 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(RAPIDJSON_NOTHING)
98 #ifndef RAPIDJSON_PARSE_ERROR_NORETURN
99 #define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset) \
100 RAPIDJSON_MULTILINEMACRO_BEGIN \
101 RAPIDJSON_ASSERT(!HasParseError()); \
102 SetParseError(parseErrorCode, offset); \
103 RAPIDJSON_MULTILINEMACRO_END
117 #ifndef RAPIDJSON_PARSE_ERROR
118 #define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset) \
119 RAPIDJSON_MULTILINEMACRO_BEGIN \
120 RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset); \
121 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; \
122 RAPIDJSON_MULTILINEMACRO_END
138 #ifndef RAPIDJSON_PARSE_DEFAULT_FLAGS
139 #define RAPIDJSON_PARSE_DEFAULT_FLAGS kParseNoFlags
178 bool RawNumber(const Ch* str, SizeType length, bool copy);
179 bool String(const Ch* str, SizeType length, bool copy);
181 bool Key(const Ch* str, SizeType length, bool copy);
182 bool EndObject(SizeType memberCount);
184 bool EndArray(SizeType elementCount);
195 template<
typename Encoding = UTF8<>,
typename Derived =
void>
197 typedef typename Encoding::Ch
Ch;
205 bool Uint(
unsigned) {
return static_cast<Override&
>(*this).Default(); }
224 template<typename Stream, int = StreamTraits<Stream>::copyOptimization>
228 template<
typename Stream>
243 template<
typename Stream>
263 template<
typename InputStream>
266 InputStream& s(
copy.s);
268 typename InputStream::Ch c;
269 while ((c = s.Peek()) ==
' ' || c ==
'\n' || c ==
'\r' || c ==
'\t')
274 while (p != end && (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t'))
279 #ifdef RAPIDJSON_SSE42
281 inline const char *SkipWhitespace_SIMD(
const char* p) {
283 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
289 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
290 while (p != nextAligned)
291 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
302 const int r = _mm_cmpistri(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT | _SIDD_NEGATIVE_POLARITY);
308 inline const char *SkipWhitespace_SIMD(
const char* p,
const char* end) {
310 if (p != end && (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t'))
319 for (; p <= end - 16; p += 16) {
321 const int r = _mm_cmpistri(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT | _SIDD_NEGATIVE_POLARITY);
329 #elif defined(RAPIDJSON_SSE2)
332 inline const char *SkipWhitespace_SIMD(
const char* p) {
334 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
340 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
341 while (p != nextAligned)
342 if (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t')
348 #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c }
349 static const char whitespaces[4][16] = { C16(
' '), C16(
'\n'), C16(
'\r'), C16(
'\t') };
370 return p + __builtin_ffs(
r) - 1;
376 inline const char *SkipWhitespace_SIMD(
const char* p,
const char* end) {
378 if (p != end && (*p ==
' ' || *p ==
'\n' || *p ==
'\r' || *p ==
'\t'))
384 #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c }
385 static const char whitespaces[4][16] = { C16(
' '), C16(
'\n'), C16(
'\r'), C16(
'\t') };
393 for (; p <= end - 16; p += 16) {
406 return p + __builtin_ffs(
r) - 1;
416 #ifdef RAPIDJSON_SIMD
419 is.src_ =
const_cast<char*
>(SkipWhitespace_SIMD(is.src_));
424 is.src_ = SkipWhitespace_SIMD(is.src_);
427 template<>
inline void SkipWhitespace(EncodedInputStream<UTF8<>, MemoryStream>& is) {
428 is.is_.src_ = SkipWhitespace_SIMD(is.is_.src_, is.is_.end_);
451 template <
typename SourceEncoding,
typename TargetEncoding,
typename StackAllocator = CrtAllocator>
454 typedef typename SourceEncoding::Ch
Ch;
460 GenericReader(StackAllocator* stackAllocator = 0,
size_t stackCapacity = kDefaultStackCapacity) : stack_(stackAllocator, stackCapacity), parseResult_() {}
470 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
473 return IterativeParse<parseFlags>(is,
handler);
475 parseResult_.Clear();
479 SkipWhitespaceAndComments<parseFlags>(is);
480 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
484 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
487 ParseValue<parseFlags>(is,
handler);
488 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
491 SkipWhitespaceAndComments<parseFlags>(is);
492 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
496 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
511 template <
typename InputStream,
typename Handler>
513 return Parse<kParseDefaultFlags>(is,
handler);
545 template<
unsigned parseFlags,
typename InputStream>
551 if (Consume(is,
'*')) {
555 else if (Consume(is,
'*')) {
556 if (Consume(is,
'/'))
564 while (is.Peek() !=
'\0' && is.Take() !=
'\n') {}
574 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
582 SkipWhitespaceAndComments<parseFlags>(is);
583 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
585 if (Consume(is,
'}')) {
595 ParseString<parseFlags>(is,
handler,
true);
596 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
598 SkipWhitespaceAndComments<parseFlags>(is);
599 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
604 SkipWhitespaceAndComments<parseFlags>(is);
605 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
607 ParseValue<parseFlags>(is,
handler);
608 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
610 SkipWhitespaceAndComments<parseFlags>(is);
611 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
618 SkipWhitespaceAndComments<parseFlags>(is);
619 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
631 if (is.Peek() ==
'}') {
642 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
650 SkipWhitespaceAndComments<parseFlags>(is);
651 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
653 if (Consume(is,
']')) {
660 ParseValue<parseFlags>(is,
handler);
661 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
664 SkipWhitespaceAndComments<parseFlags>(is);
665 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
667 if (Consume(is,
',')) {
668 SkipWhitespaceAndComments<parseFlags>(is);
669 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
671 else if (Consume(is,
']')) {
680 if (is.Peek() ==
']') {
690 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
695 if (
RAPIDJSON_LIKELY(Consume(is,
'u') && Consume(is,
'l') && Consume(is,
'l'))) {
703 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
708 if (
RAPIDJSON_LIKELY(Consume(is,
'r') && Consume(is,
'u') && Consume(is,
'e'))) {
716 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
721 if (
RAPIDJSON_LIKELY(Consume(is,
'a') && Consume(is,
'l') && Consume(is,
's') && Consume(is,
'e'))) {
729 template<
typename InputStream>
730 RAPIDJSON_FORCEINLINE
static bool Consume(InputStream& is,
typename InputStream::Ch
expect) {
740 template<
typename InputStream>
741 unsigned ParseHex4(InputStream& is,
size_t escapeOffset) {
742 unsigned codepoint = 0;
743 for (
int i = 0;
i < 4;
i++) {
746 codepoint +=
static_cast<unsigned>(c);
747 if (c >=
'0' && c <=
'9')
749 else if (c >=
'A' && c <=
'F')
750 codepoint -=
'A' - 10;
751 else if (c >=
'a' && c <=
'f')
752 codepoint -=
'a' - 10;
755 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(0);
762 template <
typename CharType>
768 RAPIDJSON_FORCEINLINE
void Put(
Ch c) {
769 *stack_.template Push<Ch>() = c;
775 return stack_.template Push<Ch>(
count);
778 size_t Length()
const {
return length_; }
781 return stack_.template Pop<Ch>(length_);
793 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
796 InputStream& s(
copy.s);
801 bool success =
false;
803 typename InputStream::Ch *
head = s.PutBegin();
804 ParseStringToStream<parseFlags, SourceEncoding, SourceEncoding>(s, s);
805 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
806 size_t length = s.PutEnd(
head) - 1;
808 const typename TargetEncoding::Ch*
const str =
reinterpret_cast<typename TargetEncoding::Ch*
>(
head);
813 ParseStringToStream<parseFlags, SourceEncoding, TargetEncoding>(s, stackStream);
814 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
816 const typename TargetEncoding::Ch*
const str = stackStream.
Pop();
825 template<
unsigned parseFlags,
typename SEncoding,
typename TEncoding,
typename InputStream,
typename OutputStream>
828 #define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
829 static const char escape[256] = {
830 Z16,
Z16, 0, 0,
'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'/',
831 Z16,
Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'\\', 0, 0, 0,
832 0, 0,
'\b', 0, 0, 0,
'\f', 0, 0, 0, 0, 0, 0, 0,
'\n', 0,
833 0, 0,
'\r', 0,
'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
842 ScanCopyUnescapedString(is, os);
846 size_t escapeOffset = is.Tell();
849 if ((
sizeof(
Ch) == 1 ||
unsigned(e) < 256) &&
RAPIDJSON_LIKELY(escape[
static_cast<unsigned char>(e)])) {
851 os.Put(
static_cast<typename TEncoding::Ch
>(escape[
static_cast<unsigned char>(e)]));
855 unsigned codepoint = ParseHex4(is, escapeOffset);
856 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
861 unsigned codepoint2 = ParseHex4(is, escapeOffset);
862 RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
865 codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000;
884 size_t offset = is.Tell();
893 template<
typename InputStream,
typename OutputStream>
898 #if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42)
900 static RAPIDJSON_FORCEINLINE
void ScanCopyUnescapedString(
StringStream& is, StackStream<char>& os) {
901 const char* p = is.
src_;
904 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
905 while (p != nextAligned)
914 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
915 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
916 static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 };
935 length =
static_cast<SizeType>(__builtin_ffs(
r) - 1);
938 char* q =
reinterpret_cast<char*
>(os.Push(length));
939 for (
size_t i = 0;
i < length;
i++)
957 if (is.src_ == is.dst_) {
958 SkipUnescapedString(is);
966 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
967 while (p != nextAligned)
977 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
978 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
979 static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 };
984 for (;; p += 16, q += 16) {
998 length =
static_cast<size_t>(__builtin_ffs(
r) - 1);
1000 for (
const char* pend = p + length; p != pend; )
1017 const char* nextAligned =
reinterpret_cast<const char*
>((
reinterpret_cast<size_t>(p) + 15) &
static_cast<size_t>(~15));
1018 for (; p != nextAligned; p++)
1020 is.src_ = is.dst_ = p;
1025 static const char dquote[16] = {
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"',
'\"' };
1026 static const char bslash[16] = {
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\',
'\\' };
1027 static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 };
1046 length =
static_cast<size_t>(__builtin_ffs(
r) - 1);
1053 is.src_ = is.dst_ = p;
1057 template<
typename InputStream,
bool backup,
bool pushOnTake>
1060 template<
typename InputStream>
1063 typedef typename InputStream::Ch
Ch;
1067 RAPIDJSON_FORCEINLINE
Ch Peek()
const {
return is.Peek(); }
1069 RAPIDJSON_FORCEINLINE
Ch Take() {
return is.Take(); }
1070 RAPIDJSON_FORCEINLINE
void Push(
char) {}
1072 size_t Tell() {
return is.Tell(); }
1074 const char*
Pop() {
return 0; }
1082 template<
typename InputStream>
1089 stackStream.Put(
static_cast<char>(Base::is.Peek()));
1090 return Base::is.Take();
1093 RAPIDJSON_FORCEINLINE
void Push(
char c) {
1097 size_t Length() {
return stackStream.Length(); }
1100 stackStream.Put(
'\0');
1101 return stackStream.Pop();
1108 template<
typename InputStream>
1114 RAPIDJSON_FORCEINLINE
Ch Take() {
return Base::TakePush(); }
1117 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
1127 size_t startOffset = s.Tell();
1129 bool useNanOrInf =
false;
1132 bool minus = Consume(s,
'-');
1137 bool use64bit =
false;
1138 int significandDigit = 0;
1144 i =
static_cast<unsigned>(s.TakePush() -
'0');
1155 i =
i * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1167 i =
i * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1174 if (
RAPIDJSON_LIKELY(Consume(s,
'N') && Consume(s,
'a') && Consume(s,
'N'))) {
1175 d = std::numeric_limits<double>::quiet_NaN();
1177 else if (
RAPIDJSON_LIKELY(Consume(s,
'I') && Consume(s,
'n') && Consume(s,
'f'))) {
1180 && Consume(s,
'i') && Consume(s,
't') && Consume(s,
'y'))))
1190 bool useDouble =
false;
1196 d =
static_cast<double>(i64);
1200 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1207 d =
static_cast<double>(i64);
1211 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1221 d = d * 10 + (s.TakePush() -
'0');
1227 size_t decimalPosition;
1228 if (Consume(s,
'.')) {
1229 decimalPosition = s.Length();
1244 i64 = i64 * 10 +
static_cast<unsigned>(s.TakePush() -
'0');
1251 d =
static_cast<double>(i64);
1254 d =
static_cast<double>(use64bit ? i64 :
i);
1260 if (significandDigit < 17) {
1261 d = d * 10.0 + (s.TakePush() -
'0');
1271 decimalPosition = s.Length();
1275 if (Consume(s,
'e') || Consume(s,
'E')) {
1277 d =
static_cast<double>(use64bit ? i64 :
i);
1281 bool expMinus =
false;
1282 if (Consume(s,
'+'))
1284 else if (Consume(s,
'-'))
1288 exp =
static_cast<int>(s.Take() -
'0');
1291 exp = exp * 10 +
static_cast<int>(s.Take() -
'0');
1292 if (exp >= 214748364) {
1299 int maxExp = 308 - expFrac;
1301 exp = exp * 10 +
static_cast<int>(s.Take() -
'0');
1320 typename InputStream::Ch*
head = is.PutBegin();
1321 const size_t length = s.Tell() - startOffset;
1324 const typename TargetEncoding::Ch*
const str =
reinterpret_cast<typename TargetEncoding::Ch*
>(
head);
1331 while (numCharsToCopy--) {
1334 dstStream.
Put(
'\0');
1335 const typename TargetEncoding::Ch*
str = dstStream.
Pop();
1341 size_t length = s.Length();
1342 const char* decimal = s.Pop();
1345 int p = exp + expFrac;
1353 else if (useNanOrInf) {
1376 template<
unsigned parseFlags,
typename InputStream,
typename Handler>
1378 switch (is.Peek()) {
1379 case 'n': ParseNull <parseFlags>(is,
handler);
break;
1380 case 't': ParseTrue <parseFlags>(is,
handler);
break;
1381 case 'f': ParseFalse <parseFlags>(is,
handler);
break;
1382 case '"': ParseString<parseFlags>(is,
handler);
break;
1383 case '{': ParseObject<parseFlags>(is,
handler);
break;
1384 case '[': ParseArray <parseFlags>(is,
handler);
break;
1386 ParseNumber<parseFlags>(is,
handler);
1396 IterativeParsingStartState = 0,
1415 IterativeParsingValueState
1418 enum { cIterativeParsingStateCount = IterativeParsingValueState + 1 };
1422 LeftBracketToken = 0,
1443 #define N NumberToken
1444 #define N16 N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N
1446 static const unsigned char tokenMap[256] = {
1449 N,
N, StringToken,
N,
N,
N,
N,
N,
N,
N,
N,
N, CommaToken,
N,
N,
N,
1450 N,
N,
N,
N,
N,
N,
N,
N,
N,
N, ColonToken,
N,
N,
N,
N,
N,
1452 N,
N,
N,
N,
N,
N,
N,
N,
N,
N,
N, LeftBracketToken,
N, RightBracketToken,
N,
N,
1453 N,
N,
N,
N,
N,
N, FalseToken,
N,
N,
N,
N,
N,
N,
N, NullToken,
N,
1454 N,
N,
N,
N, TrueToken,
N,
N,
N,
N,
N,
N, LeftCurlyBracketToken,
N, RightCurlyBracketToken,
N,
N,
1455 N16, N16, N16, N16, N16, N16, N16, N16
1461 if (
sizeof(
Ch) == 1 ||
static_cast<unsigned>(c) < 256)
1462 return static_cast<Token>(tokenMap[
static_cast<unsigned char>(c)]);
1469 static const char G[cIterativeParsingStateCount][kTokenCount] = {
1472 IterativeParsingArrayInitialState,
1473 IterativeParsingErrorState,
1474 IterativeParsingObjectInitialState,
1475 IterativeParsingErrorState,
1476 IterativeParsingErrorState,
1477 IterativeParsingErrorState,
1478 IterativeParsingValueState,
1479 IterativeParsingValueState,
1480 IterativeParsingValueState,
1481 IterativeParsingValueState,
1482 IterativeParsingValueState
1486 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1487 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1488 IterativeParsingErrorState
1492 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1493 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1494 IterativeParsingErrorState
1498 IterativeParsingErrorState,
1499 IterativeParsingErrorState,
1500 IterativeParsingErrorState,
1501 IterativeParsingObjectFinishState,
1502 IterativeParsingErrorState,
1503 IterativeParsingErrorState,
1504 IterativeParsingMemberKeyState,
1505 IterativeParsingErrorState,
1506 IterativeParsingErrorState,
1507 IterativeParsingErrorState,
1508 IterativeParsingErrorState
1512 IterativeParsingErrorState,
1513 IterativeParsingErrorState,
1514 IterativeParsingErrorState,
1515 IterativeParsingErrorState,
1516 IterativeParsingErrorState,
1517 IterativeParsingKeyValueDelimiterState,
1518 IterativeParsingErrorState,
1519 IterativeParsingErrorState,
1520 IterativeParsingErrorState,
1521 IterativeParsingErrorState,
1522 IterativeParsingErrorState
1526 IterativeParsingArrayInitialState,
1527 IterativeParsingErrorState,
1528 IterativeParsingObjectInitialState,
1529 IterativeParsingErrorState,
1530 IterativeParsingErrorState,
1531 IterativeParsingErrorState,
1532 IterativeParsingMemberValueState,
1533 IterativeParsingMemberValueState,
1534 IterativeParsingMemberValueState,
1535 IterativeParsingMemberValueState,
1536 IterativeParsingMemberValueState
1540 IterativeParsingErrorState,
1541 IterativeParsingErrorState,
1542 IterativeParsingErrorState,
1543 IterativeParsingObjectFinishState,
1544 IterativeParsingMemberDelimiterState,
1545 IterativeParsingErrorState,
1546 IterativeParsingErrorState,
1547 IterativeParsingErrorState,
1548 IterativeParsingErrorState,
1549 IterativeParsingErrorState,
1550 IterativeParsingErrorState
1554 IterativeParsingErrorState,
1555 IterativeParsingErrorState,
1556 IterativeParsingErrorState,
1557 IterativeParsingObjectFinishState,
1558 IterativeParsingErrorState,
1559 IterativeParsingErrorState,
1560 IterativeParsingMemberKeyState,
1561 IterativeParsingErrorState,
1562 IterativeParsingErrorState,
1563 IterativeParsingErrorState,
1564 IterativeParsingErrorState
1568 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1569 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1570 IterativeParsingErrorState
1574 IterativeParsingArrayInitialState,
1575 IterativeParsingArrayFinishState,
1576 IterativeParsingObjectInitialState,
1577 IterativeParsingErrorState,
1578 IterativeParsingErrorState,
1579 IterativeParsingErrorState,
1580 IterativeParsingElementState,
1581 IterativeParsingElementState,
1582 IterativeParsingElementState,
1583 IterativeParsingElementState,
1584 IterativeParsingElementState
1588 IterativeParsingErrorState,
1589 IterativeParsingArrayFinishState,
1590 IterativeParsingErrorState,
1591 IterativeParsingErrorState,
1592 IterativeParsingElementDelimiterState,
1593 IterativeParsingErrorState,
1594 IterativeParsingErrorState,
1595 IterativeParsingErrorState,
1596 IterativeParsingErrorState,
1597 IterativeParsingErrorState,
1598 IterativeParsingErrorState
1602 IterativeParsingArrayInitialState,
1603 IterativeParsingArrayFinishState,
1604 IterativeParsingObjectInitialState,
1605 IterativeParsingErrorState,
1606 IterativeParsingErrorState,
1607 IterativeParsingErrorState,
1608 IterativeParsingElementState,
1609 IterativeParsingElementState,
1610 IterativeParsingElementState,
1611 IterativeParsingElementState,
1612 IterativeParsingElementState
1616 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1617 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1618 IterativeParsingErrorState
1622 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1623 IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
1624 IterativeParsingErrorState
1633 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
1638 case IterativeParsingErrorState:
1641 case IterativeParsingObjectInitialState:
1642 case IterativeParsingArrayInitialState:
1647 if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState)
1648 n = IterativeParsingElementState;
1649 else if (src == IterativeParsingKeyValueDelimiterState)
1650 n = IterativeParsingMemberValueState;
1652 *stack_.template Push<SizeType>(1) =
n;
1654 *stack_.template Push<SizeType>(1) = 0;
1656 bool hr = (dst == IterativeParsingObjectInitialState) ?
handler.StartObject() :
handler.StartArray();
1660 return IterativeParsingErrorState;
1668 case IterativeParsingMemberKeyState:
1669 ParseString<parseFlags>(is,
handler,
true);
1670 if (HasParseError())
1671 return IterativeParsingErrorState;
1675 case IterativeParsingKeyValueDelimiterState:
1680 case IterativeParsingMemberValueState:
1682 ParseValue<parseFlags>(is,
handler);
1683 if (HasParseError()) {
1684 return IterativeParsingErrorState;
1688 case IterativeParsingElementState:
1690 ParseValue<parseFlags>(is,
handler);
1691 if (HasParseError()) {
1692 return IterativeParsingErrorState;
1696 case IterativeParsingMemberDelimiterState:
1697 case IterativeParsingElementDelimiterState:
1700 *stack_.template Top<SizeType>() = *stack_.template Top<SizeType>() + 1;
1703 case IterativeParsingObjectFinishState:
1708 return IterativeParsingErrorState;
1711 SizeType c = *stack_.template Pop<SizeType>(1);
1713 if (src == IterativeParsingMemberValueState)
1718 if (
n == IterativeParsingStartState)
1719 n = IterativeParsingFinishState;
1721 bool hr =
handler.EndObject(c);
1725 return IterativeParsingErrorState;
1733 case IterativeParsingArrayFinishState:
1738 return IterativeParsingErrorState;
1741 SizeType c = *stack_.template Pop<SizeType>(1);
1743 if (src == IterativeParsingElementState)
1748 if (
n == IterativeParsingStartState)
1749 n = IterativeParsingFinishState;
1751 bool hr =
handler.EndArray(c);
1755 return IterativeParsingErrorState;
1777 ParseValue<parseFlags>(is,
handler);
1778 if (HasParseError()) {
1779 return IterativeParsingErrorState;
1781 return IterativeParsingFinishState;
1785 template <
typename InputStream>
1787 if (HasParseError()) {
1795 case IterativeParsingObjectInitialState:
1799 case IterativeParsingKeyValueDelimiterState:
1800 case IterativeParsingArrayInitialState:
1806 template <
unsigned parseFlags,
typename InputStream,
typename Handler>
1808 parseResult_.
Clear();
1812 SkipWhitespaceAndComments<parseFlags>(is);
1813 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
1814 while (is.Peek() !=
'\0') {
1815 Token t = Tokenize(is.Peek());
1819 if (d == IterativeParsingErrorState) {
1820 HandleError(
state, is);
1830 SkipWhitespaceAndComments<parseFlags>(is);
1831 RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
1835 if (
state != IterativeParsingFinishState)
1836 HandleError(
state, is);
1838 return parseResult_;
1841 static const size_t kDefaultStackCapacity = 256;
StackStream & operator=(const StackStream &)
RAPIDJSON_FORCEINLINE void * Push(SizeType count)
RAPIDJSON_FORCEINLINE void Put(Ch c)
internal::Stack< StackAllocator > & stack_
StackStream(internal::Stack< StackAllocator > &stack)
StackStream(const StackStream &)
SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator.
ParseErrorCode GetParseErrorCode() const
Get the ParseErrorCode of last parsing.
SourceEncoding::Ch Ch
SourceEncoding character type.
void ParseFalse(InputStream &is, Handler &handler)
internal::Stack< StackAllocator > stack_
A stack for storing decoded string temporarily during non-destructive parsing.
GenericReader & operator=(const GenericReader &)
void ParseNumber(InputStream &is, Handler &handler)
@ IterativeParsingObjectFinishState
@ IterativeParsingErrorState
@ IterativeParsingObjectInitialState
@ IterativeParsingMemberValueState
@ IterativeParsingArrayFinishState
@ IterativeParsingArrayInitialState
@ IterativeParsingElementDelimiterState
@ IterativeParsingElementState
@ IterativeParsingFinishState
@ IterativeParsingMemberKeyState
@ IterativeParsingKeyValueDelimiterState
@ IterativeParsingMemberDelimiterState
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text (with kParseDefaultFlags)
void ParseNull(InputStream &is, Handler &handler)
static RAPIDJSON_FORCEINLINE bool Consume(InputStream &is, typename InputStream::Ch expect)
void ParseObject(InputStream &is, Handler &handler)
void SetParseError(ParseErrorCode code, size_t offset)
GenericReader(StackAllocator *stackAllocator=0, size_t stackCapacity=kDefaultStackCapacity)
Constructor.
unsigned ParseHex4(InputStream &is, size_t escapeOffset)
void HandleError(IterativeParsingState src, InputStream &is)
GenericReader(const GenericReader &)
void ParseTrue(InputStream &is, Handler &handler)
RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token)
RAPIDJSON_FORCEINLINE Token Tokenize(Ch c)
RAPIDJSON_FORCEINLINE void ParseStringToStream(InputStream &is, OutputStream &os)
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
void SkipWhitespaceAndComments(InputStream &is)
bool HasParseError() const
Whether a parse error has occured in the last parsing.
void ParseString(InputStream &is, Handler &handler, bool isKey=false)
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
ParseResult IterativeParse(InputStream &is, Handler &handler)
void ParseArray(InputStream &is, Handler &handler)
void ParseValue(InputStream &is, Handler &handler)
RAPIDJSON_FORCEINLINE IterativeParsingState Transit(IterativeParsingState src, Token token, IterativeParsingState dst, InputStream &is, Handler &handler)
static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InputStream &, OutputStream &)
Concept for receiving events from GenericReader upon parsing. The functions return true if no error o...
Concept for reading and writing characters.
StreamLocalCopy & operator=(const StreamLocalCopy &)
StreamLocalCopy(Stream &original)
StreamLocalCopy(Stream &original)
StreamLocalCopy & operator=(const StreamLocalCopy &)
void(*)(CSeq_entry_Handle seh, IWorkbench *wb, const CSerialObject &obj) handler
boolean whitespace(char c)
static const char * str(char *buf, int n)
static const char * expect
#define RAPIDJSON_LIKELY(x)
Compiler branching hint for expression with high probability to be true.
#define RAPIDJSON_UNLIKELY(x)
Compiler branching hint for expression with low probability to be true.
#define RAPIDJSON_PARSE_DEFAULT_FLAGS
User-defined kParseDefaultFlags definition.
#define RAPIDJSON_ASSERT(x)
Assertion.
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
ParseErrorCode
Error code of parsing.
#define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset)
Macro to indicate a parse error.
#define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset)
(Internal) macro to indicate and handle a parse error.
@ kParseErrorDocumentEmpty
The document is empty.
@ kParseErrorArrayMissCommaOrSquareBracket
Missing a comma or ']' after an array element.
@ kParseErrorDocumentRootNotSingular
The document root must not follow by other values.
@ kParseErrorStringUnicodeEscapeInvalidHex
Incorrect hex digit after \u escape in string.
@ kParseErrorNumberTooBig
Number too big to be stored in double.
@ kParseErrorNumberMissExponent
Miss exponent in number.
@ kParseErrorObjectMissCommaOrCurlyBracket
Missing a comma or '}' after an object member.
@ kParseErrorObjectMissColon
Missing a colon after a name of object member.
@ kParseErrorStringInvalidEncoding
Invalid encoding in string.
@ kParseErrorStringUnicodeSurrogateInvalid
The surrogate pair in string is invalid.
@ kParseErrorUnspecificSyntaxError
Unspecific syntax error.
@ kParseErrorStringEscapeInvalid
Invalid escape character in string.
@ kParseErrorTermination
Parsing was terminated.
@ kParseErrorObjectMissName
Missing a name for object member.
@ kParseErrorValueInvalid
Invalid value.
@ kParseErrorNumberMissFraction
Miss fraction part in number.
@ kParseErrorStringMissQuotationMark
Missing a closing quotation mark in string.
double StrtodNormalPrecision(double d, int p)
double StrtodFullPrecision(double d, int p, const char *decimals, size_t length, size_t decimalPosition, int exp)
GenericStringStream< UTF8< char > > StringStream
String stream with UTF8 encoding.
unsigned SizeType
Size type (for string lengths, array sizes, etc.)
GenericInsituStringStream< UTF8< char > > InsituStringStream
Insitu string stream with UTF8 encoding.
ParseFlag
Combination of parseFlags.
@ kParseInsituFlag
In-situ(destructive) parsing.
@ kParseNumbersAsStringsFlag
Parse all numbers (ints/doubles) as strings.
@ kParseValidateEncodingFlag
Validate encoding of JSON strings.
@ kParseDefaultFlags
Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS.
@ kParseIterativeFlag
Iterative(constant complexity in terms of function call stack size) parsing.
@ kParseNanAndInfFlag
Allow parsing NaN, Inf, Infinity, -Inf and -Infinity as doubles.
@ kParseCommentsFlag
Allow one-line (//) and multi-line (/**/) comments.
@ kParseFullPrecisionFlag
Parse number in full precision (but slower).
@ kParseNoFlags
No flags are set.
@ kParseStopWhenDoneFlag
After parsing a complete JSON root from stream, stop further processing the rest of stream....
@ kParseTrailingCommasFlag
Allow trailing commas at the end of objects and arrays.
const char * SkipWhitespace(const char *p, const char *end)
GenericReader< UTF8< char >, UTF8< char >, CrtAllocator > Reader
Reader with UTF8 encoding and default allocator.
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
void copy(Njn::Matrix< S > *matrix_, const Njn::Matrix< T > &matrix0_)
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
static __m128i _mm_max_epu8(__m128i a, __m128i b)
static int _mm_movemask_epi8(__m128i a)
static __m128i _mm_loadu_si128(const __m128i *p)
static __m128i _mm_cmpeq_epi8(__m128i a, __m128i b)
static __m128i _mm_load_si128(const __m128i *p)
static __m128i _mm_or_si128(__m128i, __m128i)
static void _mm_storeu_si128(__m128i *p, __m128i a)
Default implementation of Handler.
bool RawNumber(const Ch *str, SizeType len, bool copy)
enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length)
bool Key(const Ch *str, SizeType len, bool copy)
bool String(const Ch *, SizeType, bool)
internal::SelectIf< internal::IsSame< Derived, void >, BaseReaderHandler, Derived >::Type Override
ClearStackOnExit(GenericReader &r)
ClearStackOnExit(const ClearStackOnExit &)
ClearStackOnExit & operator=(const ClearStackOnExit &)
const Ch * src_
Current read position.
Result of parsing (wraps ParseErrorCode)
void Clear()
Reset error code.
void Encode(const CRawScoreVector< Key, Score > &, vector< char > &)