lexer.hpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.2
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #pragma once
  9. #include <array> // array
  10. #include <clocale> // localeconv
  11. #include <cstddef> // size_t
  12. #include <cstdio> // snprintf
  13. #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
  14. #include <initializer_list> // initializer_list
  15. #include <string> // char_traits, string
  16. #include <utility> // move
  17. #include <vector> // vector
  18. #include <nlohmann/detail/input/input_adapters.hpp>
  19. #include <nlohmann/detail/input/position_t.hpp>
  20. #include <nlohmann/detail/macro_scope.hpp>
  21. NLOHMANN_JSON_NAMESPACE_BEGIN
  22. namespace detail
  23. {
  24. ///////////
  25. // lexer //
  26. ///////////
  27. template<typename BasicJsonType>
  28. class lexer_base
  29. {
  30. public:
  31. /// token types for the parser
  32. enum class token_type
  33. {
  34. uninitialized, ///< indicating the scanner is uninitialized
  35. literal_true, ///< the `true` literal
  36. literal_false, ///< the `false` literal
  37. literal_null, ///< the `null` literal
  38. value_string, ///< a string -- use get_string() for actual value
  39. value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
  40. value_integer, ///< a signed integer -- use get_number_integer() for actual value
  41. value_float, ///< an floating point number -- use get_number_float() for actual value
  42. begin_array, ///< the character for array begin `[`
  43. begin_object, ///< the character for object begin `{`
  44. end_array, ///< the character for array end `]`
  45. end_object, ///< the character for object end `}`
  46. name_separator, ///< the name separator `:`
  47. value_separator, ///< the value separator `,`
  48. parse_error, ///< indicating a parse error
  49. end_of_input, ///< indicating the end of the input buffer
  50. literal_or_value ///< a literal or the begin of a value (only for diagnostics)
  51. };
  52. /// return name of values of type token_type (only used for errors)
  53. JSON_HEDLEY_RETURNS_NON_NULL
  54. JSON_HEDLEY_CONST
  55. static const char* token_type_name(const token_type t) noexcept
  56. {
  57. switch (t)
  58. {
  59. case token_type::uninitialized:
  60. return "<uninitialized>";
  61. case token_type::literal_true:
  62. return "true literal";
  63. case token_type::literal_false:
  64. return "false literal";
  65. case token_type::literal_null:
  66. return "null literal";
  67. case token_type::value_string:
  68. return "string literal";
  69. case token_type::value_unsigned:
  70. case token_type::value_integer:
  71. case token_type::value_float:
  72. return "number literal";
  73. case token_type::begin_array:
  74. return "'['";
  75. case token_type::begin_object:
  76. return "'{'";
  77. case token_type::end_array:
  78. return "']'";
  79. case token_type::end_object:
  80. return "'}'";
  81. case token_type::name_separator:
  82. return "':'";
  83. case token_type::value_separator:
  84. return "','";
  85. case token_type::parse_error:
  86. return "<parse error>";
  87. case token_type::end_of_input:
  88. return "end of input";
  89. case token_type::literal_or_value:
  90. return "'[', '{', or a literal";
  91. // LCOV_EXCL_START
  92. default: // catch non-enum values
  93. return "unknown token";
  94. // LCOV_EXCL_STOP
  95. }
  96. }
  97. };
  98. /*!
  99. @brief lexical analysis
  100. This class organizes the lexical analysis during JSON deserialization.
  101. */
  102. template<typename BasicJsonType, typename InputAdapterType>
  103. class lexer : public lexer_base<BasicJsonType>
  104. {
  105. using number_integer_t = typename BasicJsonType::number_integer_t;
  106. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  107. using number_float_t = typename BasicJsonType::number_float_t;
  108. using string_t = typename BasicJsonType::string_t;
  109. using char_type = typename InputAdapterType::char_type;
  110. using char_int_type = typename std::char_traits<char_type>::int_type;
  111. public:
  112. using token_type = typename lexer_base<BasicJsonType>::token_type;
  113. explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
  114. : ia(std::move(adapter))
  115. , ignore_comments(ignore_comments_)
  116. , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
  117. {}
  118. // delete because of pointer members
  119. lexer(const lexer&) = delete;
  120. lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  121. lexer& operator=(lexer&) = delete;
  122. lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
  123. ~lexer() = default;
  124. private:
  125. /////////////////////
  126. // locales
  127. /////////////////////
  128. /// return the locale-dependent decimal point
  129. JSON_HEDLEY_PURE
  130. static char get_decimal_point() noexcept
  131. {
  132. const auto* loc = localeconv();
  133. JSON_ASSERT(loc != nullptr);
  134. return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
  135. }
  136. /////////////////////
  137. // scan functions
  138. /////////////////////
  139. /*!
  140. @brief get codepoint from 4 hex characters following `\u`
  141. For input "\u c1 c2 c3 c4" the codepoint is:
  142. (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
  143. = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
  144. Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
  145. must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
  146. conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
  147. between the ASCII value of the character and the desired integer value.
  148. @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
  149. non-hex character)
  150. */
  151. int get_codepoint()
  152. {
  153. // this function only makes sense after reading `\u`
  154. JSON_ASSERT(current == 'u');
  155. int codepoint = 0;
  156. const auto factors = { 12u, 8u, 4u, 0u };
  157. for (const auto factor : factors)
  158. {
  159. get();
  160. if (current >= '0' && current <= '9')
  161. {
  162. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
  163. }
  164. else if (current >= 'A' && current <= 'F')
  165. {
  166. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
  167. }
  168. else if (current >= 'a' && current <= 'f')
  169. {
  170. codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
  171. }
  172. else
  173. {
  174. return -1;
  175. }
  176. }
  177. JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
  178. return codepoint;
  179. }
  180. /*!
  181. @brief check if the next byte(s) are inside a given range
  182. Adds the current byte and, for each passed range, reads a new byte and
  183. checks if it is inside the range. If a violation was detected, set up an
  184. error message and return false. Otherwise, return true.
  185. @param[in] ranges list of integers; interpreted as list of pairs of
  186. inclusive lower and upper bound, respectively
  187. @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
  188. 1, 2, or 3 pairs. This precondition is enforced by an assertion.
  189. @return true if and only if no range violation was detected
  190. */
  191. bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
  192. {
  193. JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
  194. add(current);
  195. for (auto range = ranges.begin(); range != ranges.end(); ++range)
  196. {
  197. get();
  198. if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range)))
  199. {
  200. add(current);
  201. }
  202. else
  203. {
  204. error_message = "invalid string: ill-formed UTF-8 byte";
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. /*!
  211. @brief scan a string literal
  212. This function scans a string according to Sect. 7 of RFC 8259. While
  213. scanning, bytes are escaped and copied into buffer token_buffer. Then the
  214. function returns successfully, token_buffer is *not* null-terminated (as it
  215. may contain \0 bytes), and token_buffer.size() is the number of bytes in the
  216. string.
  217. @return token_type::value_string if string could be successfully scanned,
  218. token_type::parse_error otherwise
  219. @note In case of errors, variable error_message contains a textual
  220. description.
  221. */
  222. token_type scan_string()
  223. {
  224. // reset token_buffer (ignore opening quote)
  225. reset();
  226. // we entered the function by reading an open quote
  227. JSON_ASSERT(current == '\"');
  228. while (true)
  229. {
  230. // get next character
  231. switch (get())
  232. {
  233. // end of file while parsing string
  234. case std::char_traits<char_type>::eof():
  235. {
  236. error_message = "invalid string: missing closing quote";
  237. return token_type::parse_error;
  238. }
  239. // closing quote
  240. case '\"':
  241. {
  242. return token_type::value_string;
  243. }
  244. // escapes
  245. case '\\':
  246. {
  247. switch (get())
  248. {
  249. // quotation mark
  250. case '\"':
  251. add('\"');
  252. break;
  253. // reverse solidus
  254. case '\\':
  255. add('\\');
  256. break;
  257. // solidus
  258. case '/':
  259. add('/');
  260. break;
  261. // backspace
  262. case 'b':
  263. add('\b');
  264. break;
  265. // form feed
  266. case 'f':
  267. add('\f');
  268. break;
  269. // line feed
  270. case 'n':
  271. add('\n');
  272. break;
  273. // carriage return
  274. case 'r':
  275. add('\r');
  276. break;
  277. // tab
  278. case 't':
  279. add('\t');
  280. break;
  281. // unicode escapes
  282. case 'u':
  283. {
  284. const int codepoint1 = get_codepoint();
  285. int codepoint = codepoint1; // start with codepoint1
  286. if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
  287. {
  288. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  289. return token_type::parse_error;
  290. }
  291. // check if code point is a high surrogate
  292. if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
  293. {
  294. // expect next \uxxxx entry
  295. if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
  296. {
  297. const int codepoint2 = get_codepoint();
  298. if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
  299. {
  300. error_message = "invalid string: '\\u' must be followed by 4 hex digits";
  301. return token_type::parse_error;
  302. }
  303. // check if codepoint2 is a low surrogate
  304. if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
  305. {
  306. // overwrite codepoint
  307. codepoint = static_cast<int>(
  308. // high surrogate occupies the most significant 22 bits
  309. (static_cast<unsigned int>(codepoint1) << 10u)
  310. // low surrogate occupies the least significant 15 bits
  311. + static_cast<unsigned int>(codepoint2)
  312. // there is still the 0xD800, 0xDC00 and 0x10000 noise
  313. // in the result, so we have to subtract with:
  314. // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
  315. - 0x35FDC00u);
  316. }
  317. else
  318. {
  319. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  320. return token_type::parse_error;
  321. }
  322. }
  323. else
  324. {
  325. error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
  326. return token_type::parse_error;
  327. }
  328. }
  329. else
  330. {
  331. if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
  332. {
  333. error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
  334. return token_type::parse_error;
  335. }
  336. }
  337. // result of the above calculation yields a proper codepoint
  338. JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
  339. // translate codepoint into bytes
  340. if (codepoint < 0x80)
  341. {
  342. // 1-byte characters: 0xxxxxxx (ASCII)
  343. add(static_cast<char_int_type>(codepoint));
  344. }
  345. else if (codepoint <= 0x7FF)
  346. {
  347. // 2-byte characters: 110xxxxx 10xxxxxx
  348. add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
  349. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  350. }
  351. else if (codepoint <= 0xFFFF)
  352. {
  353. // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
  354. add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
  355. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  356. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  357. }
  358. else
  359. {
  360. // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  361. add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
  362. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
  363. add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
  364. add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
  365. }
  366. break;
  367. }
  368. // other characters after escape
  369. default:
  370. error_message = "invalid string: forbidden character after backslash";
  371. return token_type::parse_error;
  372. }
  373. break;
  374. }
  375. // invalid control characters
  376. case 0x00:
  377. {
  378. error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
  379. return token_type::parse_error;
  380. }
  381. case 0x01:
  382. {
  383. error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
  384. return token_type::parse_error;
  385. }
  386. case 0x02:
  387. {
  388. error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
  389. return token_type::parse_error;
  390. }
  391. case 0x03:
  392. {
  393. error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
  394. return token_type::parse_error;
  395. }
  396. case 0x04:
  397. {
  398. error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
  399. return token_type::parse_error;
  400. }
  401. case 0x05:
  402. {
  403. error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
  404. return token_type::parse_error;
  405. }
  406. case 0x06:
  407. {
  408. error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
  409. return token_type::parse_error;
  410. }
  411. case 0x07:
  412. {
  413. error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
  414. return token_type::parse_error;
  415. }
  416. case 0x08:
  417. {
  418. error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
  419. return token_type::parse_error;
  420. }
  421. case 0x09:
  422. {
  423. error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
  424. return token_type::parse_error;
  425. }
  426. case 0x0A:
  427. {
  428. error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
  429. return token_type::parse_error;
  430. }
  431. case 0x0B:
  432. {
  433. error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
  434. return token_type::parse_error;
  435. }
  436. case 0x0C:
  437. {
  438. error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
  439. return token_type::parse_error;
  440. }
  441. case 0x0D:
  442. {
  443. error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
  444. return token_type::parse_error;
  445. }
  446. case 0x0E:
  447. {
  448. error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
  449. return token_type::parse_error;
  450. }
  451. case 0x0F:
  452. {
  453. error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
  454. return token_type::parse_error;
  455. }
  456. case 0x10:
  457. {
  458. error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
  459. return token_type::parse_error;
  460. }
  461. case 0x11:
  462. {
  463. error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
  464. return token_type::parse_error;
  465. }
  466. case 0x12:
  467. {
  468. error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
  469. return token_type::parse_error;
  470. }
  471. case 0x13:
  472. {
  473. error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
  474. return token_type::parse_error;
  475. }
  476. case 0x14:
  477. {
  478. error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
  479. return token_type::parse_error;
  480. }
  481. case 0x15:
  482. {
  483. error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
  484. return token_type::parse_error;
  485. }
  486. case 0x16:
  487. {
  488. error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
  489. return token_type::parse_error;
  490. }
  491. case 0x17:
  492. {
  493. error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
  494. return token_type::parse_error;
  495. }
  496. case 0x18:
  497. {
  498. error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
  499. return token_type::parse_error;
  500. }
  501. case 0x19:
  502. {
  503. error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
  504. return token_type::parse_error;
  505. }
  506. case 0x1A:
  507. {
  508. error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
  509. return token_type::parse_error;
  510. }
  511. case 0x1B:
  512. {
  513. error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
  514. return token_type::parse_error;
  515. }
  516. case 0x1C:
  517. {
  518. error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
  519. return token_type::parse_error;
  520. }
  521. case 0x1D:
  522. {
  523. error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
  524. return token_type::parse_error;
  525. }
  526. case 0x1E:
  527. {
  528. error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
  529. return token_type::parse_error;
  530. }
  531. case 0x1F:
  532. {
  533. error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
  534. return token_type::parse_error;
  535. }
  536. // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
  537. case 0x20:
  538. case 0x21:
  539. case 0x23:
  540. case 0x24:
  541. case 0x25:
  542. case 0x26:
  543. case 0x27:
  544. case 0x28:
  545. case 0x29:
  546. case 0x2A:
  547. case 0x2B:
  548. case 0x2C:
  549. case 0x2D:
  550. case 0x2E:
  551. case 0x2F:
  552. case 0x30:
  553. case 0x31:
  554. case 0x32:
  555. case 0x33:
  556. case 0x34:
  557. case 0x35:
  558. case 0x36:
  559. case 0x37:
  560. case 0x38:
  561. case 0x39:
  562. case 0x3A:
  563. case 0x3B:
  564. case 0x3C:
  565. case 0x3D:
  566. case 0x3E:
  567. case 0x3F:
  568. case 0x40:
  569. case 0x41:
  570. case 0x42:
  571. case 0x43:
  572. case 0x44:
  573. case 0x45:
  574. case 0x46:
  575. case 0x47:
  576. case 0x48:
  577. case 0x49:
  578. case 0x4A:
  579. case 0x4B:
  580. case 0x4C:
  581. case 0x4D:
  582. case 0x4E:
  583. case 0x4F:
  584. case 0x50:
  585. case 0x51:
  586. case 0x52:
  587. case 0x53:
  588. case 0x54:
  589. case 0x55:
  590. case 0x56:
  591. case 0x57:
  592. case 0x58:
  593. case 0x59:
  594. case 0x5A:
  595. case 0x5B:
  596. case 0x5D:
  597. case 0x5E:
  598. case 0x5F:
  599. case 0x60:
  600. case 0x61:
  601. case 0x62:
  602. case 0x63:
  603. case 0x64:
  604. case 0x65:
  605. case 0x66:
  606. case 0x67:
  607. case 0x68:
  608. case 0x69:
  609. case 0x6A:
  610. case 0x6B:
  611. case 0x6C:
  612. case 0x6D:
  613. case 0x6E:
  614. case 0x6F:
  615. case 0x70:
  616. case 0x71:
  617. case 0x72:
  618. case 0x73:
  619. case 0x74:
  620. case 0x75:
  621. case 0x76:
  622. case 0x77:
  623. case 0x78:
  624. case 0x79:
  625. case 0x7A:
  626. case 0x7B:
  627. case 0x7C:
  628. case 0x7D:
  629. case 0x7E:
  630. case 0x7F:
  631. {
  632. add(current);
  633. break;
  634. }
  635. // U+0080..U+07FF: bytes C2..DF 80..BF
  636. case 0xC2:
  637. case 0xC3:
  638. case 0xC4:
  639. case 0xC5:
  640. case 0xC6:
  641. case 0xC7:
  642. case 0xC8:
  643. case 0xC9:
  644. case 0xCA:
  645. case 0xCB:
  646. case 0xCC:
  647. case 0xCD:
  648. case 0xCE:
  649. case 0xCF:
  650. case 0xD0:
  651. case 0xD1:
  652. case 0xD2:
  653. case 0xD3:
  654. case 0xD4:
  655. case 0xD5:
  656. case 0xD6:
  657. case 0xD7:
  658. case 0xD8:
  659. case 0xD9:
  660. case 0xDA:
  661. case 0xDB:
  662. case 0xDC:
  663. case 0xDD:
  664. case 0xDE:
  665. case 0xDF:
  666. {
  667. if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
  668. {
  669. return token_type::parse_error;
  670. }
  671. break;
  672. }
  673. // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
  674. case 0xE0:
  675. {
  676. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
  677. {
  678. return token_type::parse_error;
  679. }
  680. break;
  681. }
  682. // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
  683. // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
  684. case 0xE1:
  685. case 0xE2:
  686. case 0xE3:
  687. case 0xE4:
  688. case 0xE5:
  689. case 0xE6:
  690. case 0xE7:
  691. case 0xE8:
  692. case 0xE9:
  693. case 0xEA:
  694. case 0xEB:
  695. case 0xEC:
  696. case 0xEE:
  697. case 0xEF:
  698. {
  699. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
  700. {
  701. return token_type::parse_error;
  702. }
  703. break;
  704. }
  705. // U+D000..U+D7FF: bytes ED 80..9F 80..BF
  706. case 0xED:
  707. {
  708. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
  709. {
  710. return token_type::parse_error;
  711. }
  712. break;
  713. }
  714. // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  715. case 0xF0:
  716. {
  717. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  718. {
  719. return token_type::parse_error;
  720. }
  721. break;
  722. }
  723. // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  724. case 0xF1:
  725. case 0xF2:
  726. case 0xF3:
  727. {
  728. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
  729. {
  730. return token_type::parse_error;
  731. }
  732. break;
  733. }
  734. // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  735. case 0xF4:
  736. {
  737. if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
  738. {
  739. return token_type::parse_error;
  740. }
  741. break;
  742. }
  743. // remaining bytes (80..C1 and F5..FF) are ill-formed
  744. default:
  745. {
  746. error_message = "invalid string: ill-formed UTF-8 byte";
  747. return token_type::parse_error;
  748. }
  749. }
  750. }
  751. }
  752. /*!
  753. * @brief scan a comment
  754. * @return whether comment could be scanned successfully
  755. */
  756. bool scan_comment()
  757. {
  758. switch (get())
  759. {
  760. // single-line comments skip input until a newline or EOF is read
  761. case '/':
  762. {
  763. while (true)
  764. {
  765. switch (get())
  766. {
  767. case '\n':
  768. case '\r':
  769. case std::char_traits<char_type>::eof():
  770. case '\0':
  771. return true;
  772. default:
  773. break;
  774. }
  775. }
  776. }
  777. // multi-line comments skip input until */ is read
  778. case '*':
  779. {
  780. while (true)
  781. {
  782. switch (get())
  783. {
  784. case std::char_traits<char_type>::eof():
  785. case '\0':
  786. {
  787. error_message = "invalid comment; missing closing '*/'";
  788. return false;
  789. }
  790. case '*':
  791. {
  792. switch (get())
  793. {
  794. case '/':
  795. return true;
  796. default:
  797. {
  798. unget();
  799. continue;
  800. }
  801. }
  802. }
  803. default:
  804. continue;
  805. }
  806. }
  807. }
  808. // unexpected character after reading '/'
  809. default:
  810. {
  811. error_message = "invalid comment; expecting '/' or '*' after '/'";
  812. return false;
  813. }
  814. }
  815. }
  816. JSON_HEDLEY_NON_NULL(2)
  817. static void strtof(float& f, const char* str, char** endptr) noexcept
  818. {
  819. f = std::strtof(str, endptr);
  820. }
  821. JSON_HEDLEY_NON_NULL(2)
  822. static void strtof(double& f, const char* str, char** endptr) noexcept
  823. {
  824. f = std::strtod(str, endptr);
  825. }
  826. JSON_HEDLEY_NON_NULL(2)
  827. static void strtof(long double& f, const char* str, char** endptr) noexcept
  828. {
  829. f = std::strtold(str, endptr);
  830. }
  831. /*!
  832. @brief scan a number literal
  833. This function scans a string according to Sect. 6 of RFC 8259.
  834. The function is realized with a deterministic finite state machine derived
  835. from the grammar described in RFC 8259. Starting in state "init", the
  836. input is read and used to determined the next state. Only state "done"
  837. accepts the number. State "error" is a trap state to model errors. In the
  838. table below, "anything" means any character but the ones listed before.
  839. state | 0 | 1-9 | e E | + | - | . | anything
  840. ---------|----------|----------|----------|---------|---------|----------|-----------
  841. init | zero | any1 | [error] | [error] | minus | [error] | [error]
  842. minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
  843. zero | done | done | exponent | done | done | decimal1 | done
  844. any1 | any1 | any1 | exponent | done | done | decimal1 | done
  845. decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
  846. decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
  847. exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
  848. sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
  849. any2 | any2 | any2 | done | done | done | done | done
  850. The state machine is realized with one label per state (prefixed with
  851. "scan_number_") and `goto` statements between them. The state machine
  852. contains cycles, but any cycle can be left when EOF is read. Therefore,
  853. the function is guaranteed to terminate.
  854. During scanning, the read bytes are stored in token_buffer. This string is
  855. then converted to a signed integer, an unsigned integer, or a
  856. floating-point number.
  857. @return token_type::value_unsigned, token_type::value_integer, or
  858. token_type::value_float if number could be successfully scanned,
  859. token_type::parse_error otherwise
  860. @note The scanner is independent of the current locale. Internally, the
  861. locale's decimal point is used instead of `.` to work with the
  862. locale-dependent converters.
  863. */
  864. token_type scan_number() // lgtm [cpp/use-of-goto]
  865. {
  866. // reset token_buffer to store the number's bytes
  867. reset();
  868. // the type of the parsed number; initially set to unsigned; will be
  869. // changed if minus sign, decimal point or exponent is read
  870. token_type number_type = token_type::value_unsigned;
  871. // state (init): we just found out we need to scan a number
  872. switch (current)
  873. {
  874. case '-':
  875. {
  876. add(current);
  877. goto scan_number_minus;
  878. }
  879. case '0':
  880. {
  881. add(current);
  882. goto scan_number_zero;
  883. }
  884. case '1':
  885. case '2':
  886. case '3':
  887. case '4':
  888. case '5':
  889. case '6':
  890. case '7':
  891. case '8':
  892. case '9':
  893. {
  894. add(current);
  895. goto scan_number_any1;
  896. }
  897. // all other characters are rejected outside scan_number()
  898. default: // LCOV_EXCL_LINE
  899. JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
  900. }
  901. scan_number_minus:
  902. // state: we just parsed a leading minus sign
  903. number_type = token_type::value_integer;
  904. switch (get())
  905. {
  906. case '0':
  907. {
  908. add(current);
  909. goto scan_number_zero;
  910. }
  911. case '1':
  912. case '2':
  913. case '3':
  914. case '4':
  915. case '5':
  916. case '6':
  917. case '7':
  918. case '8':
  919. case '9':
  920. {
  921. add(current);
  922. goto scan_number_any1;
  923. }
  924. default:
  925. {
  926. error_message = "invalid number; expected digit after '-'";
  927. return token_type::parse_error;
  928. }
  929. }
  930. scan_number_zero:
  931. // state: we just parse a zero (maybe with a leading minus sign)
  932. switch (get())
  933. {
  934. case '.':
  935. {
  936. add(decimal_point_char);
  937. goto scan_number_decimal1;
  938. }
  939. case 'e':
  940. case 'E':
  941. {
  942. add(current);
  943. goto scan_number_exponent;
  944. }
  945. default:
  946. goto scan_number_done;
  947. }
  948. scan_number_any1:
  949. // state: we just parsed a number 0-9 (maybe with a leading minus sign)
  950. switch (get())
  951. {
  952. case '0':
  953. case '1':
  954. case '2':
  955. case '3':
  956. case '4':
  957. case '5':
  958. case '6':
  959. case '7':
  960. case '8':
  961. case '9':
  962. {
  963. add(current);
  964. goto scan_number_any1;
  965. }
  966. case '.':
  967. {
  968. add(decimal_point_char);
  969. goto scan_number_decimal1;
  970. }
  971. case 'e':
  972. case 'E':
  973. {
  974. add(current);
  975. goto scan_number_exponent;
  976. }
  977. default:
  978. goto scan_number_done;
  979. }
  980. scan_number_decimal1:
  981. // state: we just parsed a decimal point
  982. number_type = token_type::value_float;
  983. switch (get())
  984. {
  985. case '0':
  986. case '1':
  987. case '2':
  988. case '3':
  989. case '4':
  990. case '5':
  991. case '6':
  992. case '7':
  993. case '8':
  994. case '9':
  995. {
  996. add(current);
  997. goto scan_number_decimal2;
  998. }
  999. default:
  1000. {
  1001. error_message = "invalid number; expected digit after '.'";
  1002. return token_type::parse_error;
  1003. }
  1004. }
  1005. scan_number_decimal2:
  1006. // we just parsed at least one number after a decimal point
  1007. switch (get())
  1008. {
  1009. case '0':
  1010. case '1':
  1011. case '2':
  1012. case '3':
  1013. case '4':
  1014. case '5':
  1015. case '6':
  1016. case '7':
  1017. case '8':
  1018. case '9':
  1019. {
  1020. add(current);
  1021. goto scan_number_decimal2;
  1022. }
  1023. case 'e':
  1024. case 'E':
  1025. {
  1026. add(current);
  1027. goto scan_number_exponent;
  1028. }
  1029. default:
  1030. goto scan_number_done;
  1031. }
  1032. scan_number_exponent:
  1033. // we just parsed an exponent
  1034. number_type = token_type::value_float;
  1035. switch (get())
  1036. {
  1037. case '+':
  1038. case '-':
  1039. {
  1040. add(current);
  1041. goto scan_number_sign;
  1042. }
  1043. case '0':
  1044. case '1':
  1045. case '2':
  1046. case '3':
  1047. case '4':
  1048. case '5':
  1049. case '6':
  1050. case '7':
  1051. case '8':
  1052. case '9':
  1053. {
  1054. add(current);
  1055. goto scan_number_any2;
  1056. }
  1057. default:
  1058. {
  1059. error_message =
  1060. "invalid number; expected '+', '-', or digit after exponent";
  1061. return token_type::parse_error;
  1062. }
  1063. }
  1064. scan_number_sign:
  1065. // we just parsed an exponent sign
  1066. switch (get())
  1067. {
  1068. case '0':
  1069. case '1':
  1070. case '2':
  1071. case '3':
  1072. case '4':
  1073. case '5':
  1074. case '6':
  1075. case '7':
  1076. case '8':
  1077. case '9':
  1078. {
  1079. add(current);
  1080. goto scan_number_any2;
  1081. }
  1082. default:
  1083. {
  1084. error_message = "invalid number; expected digit after exponent sign";
  1085. return token_type::parse_error;
  1086. }
  1087. }
  1088. scan_number_any2:
  1089. // we just parsed a number after the exponent or exponent sign
  1090. switch (get())
  1091. {
  1092. case '0':
  1093. case '1':
  1094. case '2':
  1095. case '3':
  1096. case '4':
  1097. case '5':
  1098. case '6':
  1099. case '7':
  1100. case '8':
  1101. case '9':
  1102. {
  1103. add(current);
  1104. goto scan_number_any2;
  1105. }
  1106. default:
  1107. goto scan_number_done;
  1108. }
  1109. scan_number_done:
  1110. // unget the character after the number (we only read it to know that
  1111. // we are done scanning a number)
  1112. unget();
  1113. char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  1114. errno = 0;
  1115. // try to parse integers first and fall back to floats
  1116. if (number_type == token_type::value_unsigned)
  1117. {
  1118. const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
  1119. // we checked the number format before
  1120. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  1121. if (errno == 0)
  1122. {
  1123. value_unsigned = static_cast<number_unsigned_t>(x);
  1124. if (value_unsigned == x)
  1125. {
  1126. return token_type::value_unsigned;
  1127. }
  1128. }
  1129. }
  1130. else if (number_type == token_type::value_integer)
  1131. {
  1132. const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
  1133. // we checked the number format before
  1134. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  1135. if (errno == 0)
  1136. {
  1137. value_integer = static_cast<number_integer_t>(x);
  1138. if (value_integer == x)
  1139. {
  1140. return token_type::value_integer;
  1141. }
  1142. }
  1143. }
  1144. // this code is reached if we parse a floating-point number or if an
  1145. // integer conversion above failed
  1146. strtof(value_float, token_buffer.data(), &endptr);
  1147. // we checked the number format before
  1148. JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
  1149. return token_type::value_float;
  1150. }
  1151. /*!
  1152. @param[in] literal_text the literal text to expect
  1153. @param[in] length the length of the passed literal text
  1154. @param[in] return_type the token type to return on success
  1155. */
  1156. JSON_HEDLEY_NON_NULL(2)
  1157. token_type scan_literal(const char_type* literal_text, const std::size_t length,
  1158. token_type return_type)
  1159. {
  1160. JSON_ASSERT(std::char_traits<char_type>::to_char_type(current) == literal_text[0]);
  1161. for (std::size_t i = 1; i < length; ++i)
  1162. {
  1163. if (JSON_HEDLEY_UNLIKELY(std::char_traits<char_type>::to_char_type(get()) != literal_text[i]))
  1164. {
  1165. error_message = "invalid literal";
  1166. return token_type::parse_error;
  1167. }
  1168. }
  1169. return return_type;
  1170. }
  1171. /////////////////////
  1172. // input management
  1173. /////////////////////
  1174. /// reset token_buffer; current character is beginning of token
  1175. void reset() noexcept
  1176. {
  1177. token_buffer.clear();
  1178. token_string.clear();
  1179. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  1180. }
  1181. /*
  1182. @brief get next character from the input
  1183. This function provides the interface to the used input adapter. It does
  1184. not throw in case the input reached EOF, but returns a
  1185. `std::char_traits<char>::eof()` in that case. Stores the scanned characters
  1186. for use in error messages.
  1187. @return character read from the input
  1188. */
  1189. char_int_type get()
  1190. {
  1191. ++position.chars_read_total;
  1192. ++position.chars_read_current_line;
  1193. if (next_unget)
  1194. {
  1195. // just reset the next_unget variable and work with current
  1196. next_unget = false;
  1197. }
  1198. else
  1199. {
  1200. current = ia.get_character();
  1201. }
  1202. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  1203. {
  1204. token_string.push_back(std::char_traits<char_type>::to_char_type(current));
  1205. }
  1206. if (current == '\n')
  1207. {
  1208. ++position.lines_read;
  1209. position.chars_read_current_line = 0;
  1210. }
  1211. return current;
  1212. }
  1213. /*!
  1214. @brief unget current character (read it again on next get)
  1215. We implement unget by setting variable next_unget to true. The input is not
  1216. changed - we just simulate ungetting by modifying chars_read_total,
  1217. chars_read_current_line, and token_string. The next call to get() will
  1218. behave as if the unget character is read again.
  1219. */
  1220. void unget()
  1221. {
  1222. next_unget = true;
  1223. --position.chars_read_total;
  1224. // in case we "unget" a newline, we have to also decrement the lines_read
  1225. if (position.chars_read_current_line == 0)
  1226. {
  1227. if (position.lines_read > 0)
  1228. {
  1229. --position.lines_read;
  1230. }
  1231. }
  1232. else
  1233. {
  1234. --position.chars_read_current_line;
  1235. }
  1236. if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
  1237. {
  1238. JSON_ASSERT(!token_string.empty());
  1239. token_string.pop_back();
  1240. }
  1241. }
  1242. /// add a character to token_buffer
  1243. void add(char_int_type c)
  1244. {
  1245. token_buffer.push_back(static_cast<typename string_t::value_type>(c));
  1246. }
  1247. public:
  1248. /////////////////////
  1249. // value getters
  1250. /////////////////////
  1251. /// return integer value
  1252. constexpr number_integer_t get_number_integer() const noexcept
  1253. {
  1254. return value_integer;
  1255. }
  1256. /// return unsigned integer value
  1257. constexpr number_unsigned_t get_number_unsigned() const noexcept
  1258. {
  1259. return value_unsigned;
  1260. }
  1261. /// return floating-point value
  1262. constexpr number_float_t get_number_float() const noexcept
  1263. {
  1264. return value_float;
  1265. }
  1266. /// return current string value (implicitly resets the token; useful only once)
  1267. string_t& get_string()
  1268. {
  1269. return token_buffer;
  1270. }
  1271. /////////////////////
  1272. // diagnostics
  1273. /////////////////////
  1274. /// return position of last read token
  1275. constexpr position_t get_position() const noexcept
  1276. {
  1277. return position;
  1278. }
  1279. /// return the last read token (for errors only). Will never contain EOF
  1280. /// (an arbitrary value that is not a valid char value, often -1), because
  1281. /// 255 may legitimately occur. May contain NUL, which should be escaped.
  1282. std::string get_token_string() const
  1283. {
  1284. // escape control characters
  1285. std::string result;
  1286. for (const auto c : token_string)
  1287. {
  1288. if (static_cast<unsigned char>(c) <= '\x1F')
  1289. {
  1290. // escape control characters
  1291. std::array<char, 9> cs{{}};
  1292. static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
  1293. result += cs.data();
  1294. }
  1295. else
  1296. {
  1297. // add character as is
  1298. result.push_back(static_cast<std::string::value_type>(c));
  1299. }
  1300. }
  1301. return result;
  1302. }
  1303. /// return syntax error message
  1304. JSON_HEDLEY_RETURNS_NON_NULL
  1305. constexpr const char* get_error_message() const noexcept
  1306. {
  1307. return error_message;
  1308. }
  1309. /////////////////////
  1310. // actual scanner
  1311. /////////////////////
  1312. /*!
  1313. @brief skip the UTF-8 byte order mark
  1314. @return true iff there is no BOM or the correct BOM has been skipped
  1315. */
  1316. bool skip_bom()
  1317. {
  1318. if (get() == 0xEF)
  1319. {
  1320. // check if we completely parse the BOM
  1321. return get() == 0xBB && get() == 0xBF;
  1322. }
  1323. // the first character is not the beginning of the BOM; unget it to
  1324. // process is later
  1325. unget();
  1326. return true;
  1327. }
  1328. void skip_whitespace()
  1329. {
  1330. do
  1331. {
  1332. get();
  1333. }
  1334. while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
  1335. }
  1336. token_type scan()
  1337. {
  1338. // initially, skip the BOM
  1339. if (position.chars_read_total == 0 && !skip_bom())
  1340. {
  1341. error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
  1342. return token_type::parse_error;
  1343. }
  1344. // read next character and ignore whitespace
  1345. skip_whitespace();
  1346. // ignore comments
  1347. while (ignore_comments && current == '/')
  1348. {
  1349. if (!scan_comment())
  1350. {
  1351. return token_type::parse_error;
  1352. }
  1353. // skip following whitespace
  1354. skip_whitespace();
  1355. }
  1356. switch (current)
  1357. {
  1358. // structural characters
  1359. case '[':
  1360. return token_type::begin_array;
  1361. case ']':
  1362. return token_type::end_array;
  1363. case '{':
  1364. return token_type::begin_object;
  1365. case '}':
  1366. return token_type::end_object;
  1367. case ':':
  1368. return token_type::name_separator;
  1369. case ',':
  1370. return token_type::value_separator;
  1371. // literals
  1372. case 't':
  1373. {
  1374. std::array<char_type, 4> true_literal = {{static_cast<char_type>('t'), static_cast<char_type>('r'), static_cast<char_type>('u'), static_cast<char_type>('e')}};
  1375. return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
  1376. }
  1377. case 'f':
  1378. {
  1379. std::array<char_type, 5> false_literal = {{static_cast<char_type>('f'), static_cast<char_type>('a'), static_cast<char_type>('l'), static_cast<char_type>('s'), static_cast<char_type>('e')}};
  1380. return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
  1381. }
  1382. case 'n':
  1383. {
  1384. std::array<char_type, 4> null_literal = {{static_cast<char_type>('n'), static_cast<char_type>('u'), static_cast<char_type>('l'), static_cast<char_type>('l')}};
  1385. return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
  1386. }
  1387. // string
  1388. case '\"':
  1389. return scan_string();
  1390. // number
  1391. case '-':
  1392. case '0':
  1393. case '1':
  1394. case '2':
  1395. case '3':
  1396. case '4':
  1397. case '5':
  1398. case '6':
  1399. case '7':
  1400. case '8':
  1401. case '9':
  1402. return scan_number();
  1403. // end of input (the null byte is needed when parsing from
  1404. // string literals)
  1405. case '\0':
  1406. case std::char_traits<char_type>::eof():
  1407. return token_type::end_of_input;
  1408. // error
  1409. default:
  1410. error_message = "invalid literal";
  1411. return token_type::parse_error;
  1412. }
  1413. }
  1414. private:
  1415. /// input adapter
  1416. InputAdapterType ia;
  1417. /// whether comments should be ignored (true) or signaled as errors (false)
  1418. const bool ignore_comments = false;
  1419. /// the current character
  1420. char_int_type current = std::char_traits<char_type>::eof();
  1421. /// whether the next get() call should just return current
  1422. bool next_unget = false;
  1423. /// the start position of the current token
  1424. position_t position {};
  1425. /// raw input token string (for error messages)
  1426. std::vector<char_type> token_string {};
  1427. /// buffer for variable-length tokens (numbers, strings)
  1428. string_t token_buffer {};
  1429. /// a description of occurred lexer errors
  1430. const char* error_message = "";
  1431. // number values
  1432. number_integer_t value_integer = 0;
  1433. number_unsigned_t value_unsigned = 0;
  1434. number_float_t value_float = 0;
  1435. /// the decimal point
  1436. const char_int_type decimal_point_char = '.';
  1437. };
  1438. } // namespace detail
  1439. NLOHMANN_JSON_NAMESPACE_END