2
0

reader.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. #ifndef RAPIDJSON_READER_H_
  2. #define RAPIDJSON_READER_H_
  3. // Copyright (c) 2011 Milo Yip ([email protected])
  4. // Version 0.1
  5. #include "rapidjson.h"
  6. #include "encodings.h"
  7. #include "internal/pow10.h"
  8. #include "internal/stack.h"
  9. #include <csetjmp>
  10. #ifdef RAPIDJSON_SSE42
  11. #include <nmmintrin.h>
  12. #elif defined(RAPIDJSON_SSE2)
  13. #include <emmintrin.h>
  14. #endif
  15. #ifdef _MSC_VER
  16. #pragma warning(push)
  17. #pragma warning(disable : 4127) // conditional expression is constant
  18. #endif
  19. #ifndef RAPIDJSON_PARSE_ERROR
  20. #define RAPIDJSON_PARSE_ERROR(msg, offset) \
  21. RAPIDJSON_MULTILINEMACRO_BEGIN \
  22. parseError_ = msg; \
  23. errorOffset_ = offset; \
  24. longjmp(jmpbuf_, 1); \
  25. RAPIDJSON_MULTILINEMACRO_END
  26. #endif
  27. namespace rapidjson {
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // ParseFlag
  30. //! Combination of parseFlags
  31. enum ParseFlag {
  32. kParseDefaultFlags = 0, //!< Default parse flags. Non-destructive parsing. Text strings are decoded into allocated buffer.
  33. kParseInsituFlag = 1, //!< In-situ(destructive) parsing.
  34. kParseValidateEncodingFlag = 2, //!< Validate encoding of JSON strings.
  35. };
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // Handler
  38. /*! \class rapidjson::Handler
  39. \brief Concept for receiving events from GenericReader upon parsing.
  40. \code
  41. concept Handler {
  42. typename Ch;
  43. void Null();
  44. void Bool(bool b);
  45. void Int(int i);
  46. void Uint(unsigned i);
  47. void Int64(int64_t i);
  48. void Uint64(uint64_t i);
  49. void Double(double d);
  50. void String(const Ch* str, SizeType length, bool copy);
  51. void StartObject();
  52. void EndObject(SizeType memberCount);
  53. void StartArray();
  54. void EndArray(SizeType elementCount);
  55. };
  56. \endcode
  57. */
  58. ///////////////////////////////////////////////////////////////////////////////
  59. // BaseReaderHandler
  60. //! Default implementation of Handler.
  61. /*! This can be used as base class of any reader handler.
  62. \implements Handler
  63. */
  64. template<typename Encoding = UTF8<> >
  65. struct BaseReaderHandler {
  66. typedef typename Encoding::Ch Ch;
  67. void Default() {}
  68. void Null() { Default(); }
  69. void Bool(bool) { Default(); }
  70. void Int(int) { Default(); }
  71. void Uint(unsigned) { Default(); }
  72. void Int64(int64_t) { Default(); }
  73. void Uint64(uint64_t) { Default(); }
  74. void Double(double) { Default(); }
  75. void String(const Ch*, SizeType, bool) { Default(); }
  76. void StartObject() { Default(); }
  77. void EndObject(SizeType) { Default(); }
  78. void StartArray() { Default(); }
  79. void EndArray(SizeType) { Default(); }
  80. };
  81. ///////////////////////////////////////////////////////////////////////////////
  82. // SkipWhitespace
  83. //! Skip the JSON white spaces in a stream.
  84. /*! \param stream A input stream for skipping white spaces.
  85. \note This function has SSE2/SSE4.2 specialization.
  86. */
  87. template<typename InputStream>
  88. void SkipWhitespace(InputStream& is) {
  89. InputStream s = is; // Use a local copy for optimization
  90. while (s.Peek() == ' ' || s.Peek() == '\n' || s.Peek() == '\r' || s.Peek() == '\t')
  91. s.Take();
  92. is = s;
  93. }
  94. #ifdef RAPIDJSON_SSE42
  95. //! Skip whitespace with SSE 4.2 pcmpistrm instruction, testing 16 8-byte characters at once.
  96. inline const char *SkipWhitespace_SIMD(const char* p) {
  97. static const char whitespace[16] = " \n\r\t";
  98. __m128i w = _mm_loadu_si128((const __m128i *)&whitespace[0]);
  99. for (;;) {
  100. __m128i s = _mm_loadu_si128((const __m128i *)p);
  101. unsigned r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY));
  102. if (r == 0) // all 16 characters are whitespace
  103. p += 16;
  104. else { // some of characters may be non-whitespace
  105. #ifdef _MSC_VER // Find the index of first non-whitespace
  106. unsigned long offset;
  107. if (_BitScanForward(&offset, r))
  108. return p + offset;
  109. #else
  110. if (r != 0)
  111. return p + __builtin_ffs(r) - 1;
  112. #endif
  113. }
  114. }
  115. }
  116. #elif defined(RAPIDJSON_SSE2)
  117. //! Skip whitespace with SSE2 instructions, testing 16 8-byte characters at once.
  118. inline const char *SkipWhitespace_SIMD(const char* p) {
  119. static const char whitespaces[4][17] = {
  120. " ",
  121. "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
  122. "\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r",
  123. "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"};
  124. __m128i w0 = _mm_loadu_si128((const __m128i *)&whitespaces[0][0]);
  125. __m128i w1 = _mm_loadu_si128((const __m128i *)&whitespaces[1][0]);
  126. __m128i w2 = _mm_loadu_si128((const __m128i *)&whitespaces[2][0]);
  127. __m128i w3 = _mm_loadu_si128((const __m128i *)&whitespaces[3][0]);
  128. for (;;) {
  129. __m128i s = _mm_loadu_si128((const __m128i *)p);
  130. __m128i x = _mm_cmpeq_epi8(s, w0);
  131. x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1));
  132. x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2));
  133. x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3));
  134. unsigned short r = ~_mm_movemask_epi8(x);
  135. if (r == 0) // all 16 characters are whitespace
  136. p += 16;
  137. else { // some of characters may be non-whitespace
  138. #ifdef _MSC_VER // Find the index of first non-whitespace
  139. unsigned long offset;
  140. if (_BitScanForward(&offset, r))
  141. return p + offset;
  142. #else
  143. if (r != 0)
  144. return p + __builtin_ffs(r) - 1;
  145. #endif
  146. }
  147. }
  148. }
  149. #endif // RAPIDJSON_SSE2
  150. #ifdef RAPIDJSON_SIMD
  151. //! Template function specialization for InsituStringStream
  152. template<> inline void SkipWhitespace(InsituStringStream& is) {
  153. is.src_ = const_cast<char*>(SkipWhitespace_SIMD(is.src_));
  154. }
  155. //! Template function specialization for StringStream
  156. template<> inline void SkipWhitespace(StringStream& is) {
  157. is.src_ = SkipWhitespace_SIMD(is.src_);
  158. }
  159. #endif // RAPIDJSON_SIMD
  160. ///////////////////////////////////////////////////////////////////////////////
  161. // GenericReader
  162. //! SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator.
  163. /*! GenericReader parses JSON text from a stream, and send events synchronously to an
  164. object implementing Handler concept.
  165. It needs to allocate a stack for storing a single decoded string during
  166. non-destructive parsing.
  167. For in-situ parsing, the decoded string is directly written to the source
  168. text string, no temporary buffer is required.
  169. A GenericReader object can be reused for parsing multiple JSON text.
  170. \tparam SourceEncoding Encoding of the input stream.
  171. \tparam TargetEncoding Encoding of the parse output.
  172. \tparam Allocator Allocator type for stack.
  173. */
  174. template <typename SourceEncoding, typename TargetEncoding, typename Allocator = MemoryPoolAllocator<> >
  175. class GenericReader {
  176. public:
  177. typedef typename SourceEncoding::Ch Ch;
  178. //! Constructor.
  179. /*! \param allocator Optional allocator for allocating stack memory. (Only use for non-destructive parsing)
  180. \param stackCapacity stack capacity in bytes for storing a single decoded string. (Only use for non-destructive parsing)
  181. */
  182. GenericReader(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(allocator, stackCapacity), parseError_(0), errorOffset_(0) {}
  183. //! Parse JSON text.
  184. /*! \tparam parseFlags Combination of ParseFlag.
  185. \tparam InputStream Type of input stream.
  186. \tparam Handler Type of handler which must implement Handler concept.
  187. \param stream Input stream to be parsed.
  188. \param handler The handler to receive events.
  189. \return Whether the parsing is successful.
  190. */
  191. template <unsigned parseFlags, typename InputStream, typename Handler>
  192. bool Parse(InputStream& is, Handler& handler) {
  193. parseError_ = 0;
  194. errorOffset_ = 0;
  195. #ifdef _MSC_VER
  196. #pragma warning(push)
  197. #pragma warning(disable : 4611) // interaction between '_setjmp' and C++ object destruction is non-portable
  198. #endif
  199. if (setjmp(jmpbuf_)) {
  200. #ifdef _MSC_VER
  201. #pragma warning(pop)
  202. #endif
  203. stack_.Clear();
  204. return false;
  205. }
  206. SkipWhitespace(is);
  207. if (is.Peek() == '\0')
  208. RAPIDJSON_PARSE_ERROR("Text only contains white space(s)", is.Tell());
  209. else {
  210. switch (is.Peek()) {
  211. case '{': ParseObject<parseFlags>(is, handler); break;
  212. case '[': ParseArray<parseFlags>(is, handler); break;
  213. default: RAPIDJSON_PARSE_ERROR("Expect either an object or array at root", is.Tell());
  214. }
  215. SkipWhitespace(is);
  216. if (is.Peek() != '\0')
  217. RAPIDJSON_PARSE_ERROR("Nothing should follow the root object or array.", is.Tell());
  218. }
  219. return true;
  220. }
  221. bool HasParseError() const { return parseError_ != 0; }
  222. const char* GetParseError() const { return parseError_; }
  223. size_t GetErrorOffset() const { return errorOffset_; }
  224. private:
  225. // Parse object: { string : value, ... }
  226. template<unsigned parseFlags, typename InputStream, typename Handler>
  227. void ParseObject(InputStream& is, Handler& handler) {
  228. RAPIDJSON_ASSERT(is.Peek() == '{');
  229. is.Take(); // Skip '{'
  230. handler.StartObject();
  231. SkipWhitespace(is);
  232. if (is.Peek() == '}') {
  233. is.Take();
  234. handler.EndObject(0); // empty object
  235. return;
  236. }
  237. for (SizeType memberCount = 0;;) {
  238. if (is.Peek() != '"')
  239. RAPIDJSON_PARSE_ERROR("Name of an object member must be a string", is.Tell());
  240. ParseString<parseFlags>(is, handler);
  241. SkipWhitespace(is);
  242. if (is.Take() != ':')
  243. RAPIDJSON_PARSE_ERROR("There must be a colon after the name of object member", is.Tell());
  244. SkipWhitespace(is);
  245. ParseValue<parseFlags>(is, handler);
  246. SkipWhitespace(is);
  247. ++memberCount;
  248. switch(is.Take()) {
  249. case ',': SkipWhitespace(is); break;
  250. case '}': handler.EndObject(memberCount); return;
  251. default: RAPIDJSON_PARSE_ERROR("Must be a comma or '}' after an object member", is.Tell());
  252. }
  253. }
  254. }
  255. // Parse array: [ value, ... ]
  256. template<unsigned parseFlags, typename InputStream, typename Handler>
  257. void ParseArray(InputStream& is, Handler& handler) {
  258. RAPIDJSON_ASSERT(is.Peek() == '[');
  259. is.Take(); // Skip '['
  260. handler.StartArray();
  261. SkipWhitespace(is);
  262. if (is.Peek() == ']') {
  263. is.Take();
  264. handler.EndArray(0); // empty array
  265. return;
  266. }
  267. for (SizeType elementCount = 0;;) {
  268. ParseValue<parseFlags>(is, handler);
  269. ++elementCount;
  270. SkipWhitespace(is);
  271. switch (is.Take()) {
  272. case ',': SkipWhitespace(is); break;
  273. case ']': handler.EndArray(elementCount); return;
  274. default: RAPIDJSON_PARSE_ERROR("Must be a comma or ']' after an array element.", is.Tell());
  275. }
  276. }
  277. }
  278. template<unsigned parseFlags, typename InputStream, typename Handler>
  279. void ParseNull(InputStream& is, Handler& handler) {
  280. RAPIDJSON_ASSERT(is.Peek() == 'n');
  281. is.Take();
  282. if (is.Take() == 'u' && is.Take() == 'l' && is.Take() == 'l')
  283. handler.Null();
  284. else
  285. RAPIDJSON_PARSE_ERROR("Invalid value", is.Tell() - 1);
  286. }
  287. template<unsigned parseFlags, typename InputStream, typename Handler>
  288. void ParseTrue(InputStream& is, Handler& handler) {
  289. RAPIDJSON_ASSERT(is.Peek() == 't');
  290. is.Take();
  291. if (is.Take() == 'r' && is.Take() == 'u' && is.Take() == 'e')
  292. handler.Bool(true);
  293. else
  294. RAPIDJSON_PARSE_ERROR("Invalid value", is.Tell());
  295. }
  296. template<unsigned parseFlags, typename InputStream, typename Handler>
  297. void ParseFalse(InputStream& is, Handler& handler) {
  298. RAPIDJSON_ASSERT(is.Peek() == 'f');
  299. is.Take();
  300. if (is.Take() == 'a' && is.Take() == 'l' && is.Take() == 's' && is.Take() == 'e')
  301. handler.Bool(false);
  302. else
  303. RAPIDJSON_PARSE_ERROR("Invalid value", is.Tell() - 1);
  304. }
  305. // Helper function to parse four hexidecimal digits in \uXXXX in ParseString().
  306. template<typename InputStream>
  307. unsigned ParseHex4(InputStream& is) {
  308. InputStream s = is; // Use a local copy for optimization
  309. unsigned codepoint = 0;
  310. for (int i = 0; i < 4; i++) {
  311. Ch c = s.Take();
  312. codepoint <<= 4;
  313. codepoint += c;
  314. if (c >= '0' && c <= '9')
  315. codepoint -= '0';
  316. else if (c >= 'A' && c <= 'F')
  317. codepoint -= 'A' - 10;
  318. else if (c >= 'a' && c <= 'f')
  319. codepoint -= 'a' - 10;
  320. else
  321. RAPIDJSON_PARSE_ERROR("Incorrect hex digit after \\u escape", s.Tell() - 1);
  322. }
  323. is = s; // Restore is
  324. return codepoint;
  325. }
  326. class StackStream {
  327. public:
  328. typedef typename TargetEncoding::Ch Ch;
  329. StackStream(internal::Stack<Allocator>& stack) : stack_(stack), length_(0) {}
  330. void Put(Ch c) {
  331. *stack_.template Push<Ch>() = c;
  332. ++length_;
  333. }
  334. internal::Stack<Allocator>& stack_;
  335. SizeType length_;
  336. private:
  337. // Prohibit assignment for VC C4512 warning
  338. StackStream& operator=(const StackStream&);
  339. };
  340. // Parse string and generate String event. Different code paths for kParseInsituFlag.
  341. template<unsigned parseFlags, typename InputStream, typename Handler>
  342. void ParseString(InputStream& is, Handler& handler) {
  343. InputStream s = is; // Local copy for optimization
  344. if (parseFlags & kParseInsituFlag) {
  345. Ch *head = s.PutBegin();
  346. ParseStringToStream<parseFlags, SourceEncoding, SourceEncoding>(s, s);
  347. size_t length = s.PutEnd(head) - 1;
  348. RAPIDJSON_ASSERT(length <= 0xFFFFFFFF);
  349. handler.String((typename TargetEncoding::Ch*)head, SizeType(length), false);
  350. }
  351. else {
  352. StackStream stackStream(stack_);
  353. ParseStringToStream<parseFlags, SourceEncoding, TargetEncoding>(s, stackStream);
  354. handler.String(stack_.template Pop<typename TargetEncoding::Ch>(stackStream.length_), stackStream.length_ - 1, true);
  355. }
  356. is = s; // Restore is
  357. }
  358. // Parse string to an output is
  359. // This function handles the prefix/suffix double quotes, escaping, and optional encoding validation.
  360. template<unsigned parseFlags, typename SEncoding, typename TEncoding, typename InputStream, typename OutputStream>
  361. RAPIDJSON_FORCEINLINE void ParseStringToStream(InputStream& is, OutputStream& os) {
  362. #define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  363. static const char escape[256] = {
  364. Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/',
  365. Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0,
  366. 0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0,
  367. 0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  368. Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16
  369. };
  370. #undef Z16
  371. RAPIDJSON_ASSERT(is.Peek() == '\"');
  372. is.Take(); // Skip '\"'
  373. for (;;) {
  374. Ch c = is.Peek();
  375. if (c == '\\') { // Escape
  376. is.Take();
  377. Ch e = is.Take();
  378. if ((sizeof(Ch) == 1 || unsigned(e) < 256) && escape[(unsigned char)e])
  379. os.Put(escape[(unsigned char)e]);
  380. else if (e == 'u') { // Unicode
  381. unsigned codepoint = ParseHex4(is);
  382. if (codepoint >= 0xD800 && codepoint <= 0xDBFF) {
  383. // Handle UTF-16 surrogate pair
  384. if (is.Take() != '\\' || is.Take() != 'u')
  385. RAPIDJSON_PARSE_ERROR("Missing the second \\u in surrogate pair", is.Tell() - 2);
  386. unsigned codepoint2 = ParseHex4(is);
  387. if (codepoint2 < 0xDC00 || codepoint2 > 0xDFFF)
  388. RAPIDJSON_PARSE_ERROR("The second \\u in surrogate pair is invalid", is.Tell() - 2);
  389. codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000;
  390. }
  391. TEncoding::Encode(os, codepoint);
  392. }
  393. else
  394. RAPIDJSON_PARSE_ERROR("Unknown escape character", is.Tell() - 1);
  395. }
  396. else if (c == '"') { // Closing double quote
  397. is.Take();
  398. os.Put('\0'); // null-terminate the string
  399. return;
  400. }
  401. else if (c == '\0')
  402. RAPIDJSON_PARSE_ERROR("lacks ending quotation before the end of string", is.Tell() - 1);
  403. else if ((unsigned)c < 0x20) // RFC 4627: unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
  404. RAPIDJSON_PARSE_ERROR("Incorrect unescaped character in string", is.Tell() - 1);
  405. else {
  406. if (parseFlags & kParseValidateEncodingFlag ?
  407. !Transcoder<SEncoding, TEncoding>::Validate(is, os) :
  408. !Transcoder<SEncoding, TEncoding>::Transcode(is, os))
  409. RAPIDJSON_PARSE_ERROR("Invalid encoding", is.Tell());
  410. }
  411. }
  412. }
  413. template<unsigned parseFlags, typename InputStream, typename Handler>
  414. void ParseNumber(InputStream& is, Handler& handler) {
  415. InputStream s = is; // Local copy for optimization
  416. // Parse minus
  417. bool minus = false;
  418. if (s.Peek() == '-') {
  419. minus = true;
  420. s.Take();
  421. }
  422. // Parse int: zero / ( digit1-9 *DIGIT )
  423. unsigned i;
  424. bool try64bit = false;
  425. if (s.Peek() == '0') {
  426. i = 0;
  427. s.Take();
  428. }
  429. else if (s.Peek() >= '1' && s.Peek() <= '9') {
  430. i = s.Take() - '0';
  431. if (minus)
  432. while (s.Peek() >= '0' && s.Peek() <= '9') {
  433. if (i >= 214748364) { // 2^31 = 2147483648
  434. if (i != 214748364 || s.Peek() > '8') {
  435. try64bit = true;
  436. break;
  437. }
  438. }
  439. i = i * 10 + (s.Take() - '0');
  440. }
  441. else
  442. while (s.Peek() >= '0' && s.Peek() <= '9') {
  443. if (i >= 429496729) { // 2^32 - 1 = 4294967295
  444. if (i != 429496729 || s.Peek() > '5') {
  445. try64bit = true;
  446. break;
  447. }
  448. }
  449. i = i * 10 + (s.Take() - '0');
  450. }
  451. }
  452. else
  453. RAPIDJSON_PARSE_ERROR("Expect a value here.", is.Tell());
  454. // Parse 64bit int
  455. uint64_t i64 = 0;
  456. bool useDouble = false;
  457. if (try64bit) {
  458. i64 = i;
  459. if (minus)
  460. while (s.Peek() >= '0' && s.Peek() <= '9') {
  461. if (i64 >= 922337203685477580uLL) // 2^63 = 9223372036854775808
  462. if (i64 != 922337203685477580uLL || s.Peek() > '8') {
  463. useDouble = true;
  464. break;
  465. }
  466. i64 = i64 * 10 + (s.Take() - '0');
  467. }
  468. else
  469. while (s.Peek() >= '0' && s.Peek() <= '9') {
  470. if (i64 >= 1844674407370955161uLL) // 2^64 - 1 = 18446744073709551615
  471. if (i64 != 1844674407370955161uLL || s.Peek() > '5') {
  472. useDouble = true;
  473. break;
  474. }
  475. i64 = i64 * 10 + (s.Take() - '0');
  476. }
  477. }
  478. // Force double for big integer
  479. double d = 0.0;
  480. if (useDouble) {
  481. d = (double)i64;
  482. while (s.Peek() >= '0' && s.Peek() <= '9') {
  483. if (d >= 1E307)
  484. RAPIDJSON_PARSE_ERROR("Number too big to store in double", is.Tell());
  485. d = d * 10 + (s.Take() - '0');
  486. }
  487. }
  488. // Parse frac = decimal-point 1*DIGIT
  489. int expFrac = 0;
  490. if (s.Peek() == '.') {
  491. if (!useDouble) {
  492. d = try64bit ? (double)i64 : (double)i;
  493. useDouble = true;
  494. }
  495. s.Take();
  496. if (s.Peek() >= '0' && s.Peek() <= '9') {
  497. d = d * 10 + (s.Take() - '0');
  498. --expFrac;
  499. }
  500. else
  501. RAPIDJSON_PARSE_ERROR("At least one digit in fraction part", is.Tell());
  502. while (s.Peek() >= '0' && s.Peek() <= '9') {
  503. if (expFrac > -16) {
  504. d = d * 10 + (s.Peek() - '0');
  505. --expFrac;
  506. }
  507. s.Take();
  508. }
  509. }
  510. // Parse exp = e [ minus / plus ] 1*DIGIT
  511. int exp = 0;
  512. if (s.Peek() == 'e' || s.Peek() == 'E') {
  513. if (!useDouble) {
  514. d = try64bit ? (double)i64 : (double)i;
  515. useDouble = true;
  516. }
  517. s.Take();
  518. bool expMinus = false;
  519. if (s.Peek() == '+')
  520. s.Take();
  521. else if (s.Peek() == '-') {
  522. s.Take();
  523. expMinus = true;
  524. }
  525. if (s.Peek() >= '0' && s.Peek() <= '9') {
  526. exp = s.Take() - '0';
  527. while (s.Peek() >= '0' && s.Peek() <= '9') {
  528. exp = exp * 10 + (s.Take() - '0');
  529. if (exp > 308)
  530. RAPIDJSON_PARSE_ERROR("Number too big to store in double", is.Tell());
  531. }
  532. }
  533. else
  534. RAPIDJSON_PARSE_ERROR("At least one digit in exponent", s.Tell());
  535. if (expMinus)
  536. exp = -exp;
  537. }
  538. // Finish parsing, call event according to the type of number.
  539. if (useDouble) {
  540. d *= internal::Pow10(exp + expFrac);
  541. handler.Double(minus ? -d : d);
  542. }
  543. else {
  544. if (try64bit) {
  545. if (minus)
  546. handler.Int64(-(int64_t)i64);
  547. else
  548. handler.Uint64(i64);
  549. }
  550. else {
  551. if (minus)
  552. handler.Int(-(int)i);
  553. else
  554. handler.Uint(i);
  555. }
  556. }
  557. is = s; // restore is
  558. }
  559. // Parse any JSON value
  560. template<unsigned parseFlags, typename InputStream, typename Handler>
  561. void ParseValue(InputStream& is, Handler& handler) {
  562. switch (is.Peek()) {
  563. case 'n': ParseNull <parseFlags>(is, handler); break;
  564. case 't': ParseTrue <parseFlags>(is, handler); break;
  565. case 'f': ParseFalse <parseFlags>(is, handler); break;
  566. case '"': ParseString<parseFlags>(is, handler); break;
  567. case '{': ParseObject<parseFlags>(is, handler); break;
  568. case '[': ParseArray <parseFlags>(is, handler); break;
  569. default : ParseNumber<parseFlags>(is, handler);
  570. }
  571. }
  572. static const size_t kDefaultStackCapacity = 256; //!< Default stack capacity in bytes for storing a single decoded string.
  573. internal::Stack<Allocator> stack_; //!< A stack for storing decoded string temporarily during non-destructive parsing.
  574. jmp_buf jmpbuf_; //!< setjmp buffer for fast exit from nested parsing function calls.
  575. const char* parseError_;
  576. size_t errorOffset_;
  577. }; // class GenericReader
  578. //! Reader with UTF8 encoding and default allocator.
  579. typedef GenericReader<UTF8<>, UTF8<> > Reader;
  580. } // namespace rapidjson
  581. #ifdef _MSC_VER
  582. #pragma warning(pop)
  583. #endif
  584. #endif // RAPIDJSON_READER_H_