lexer.hpp 53 KB

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