parser.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #pragma once
  2. #include <cmath> // isfinite
  3. #include <cstdint> // uint8_t
  4. #include <functional> // function
  5. #include <string> // string
  6. #include <utility> // move
  7. #include <vector> // vector
  8. #include <nlohmann/detail/exceptions.hpp>
  9. #include <nlohmann/detail/input/input_adapters.hpp>
  10. #include <nlohmann/detail/input/json_sax.hpp>
  11. #include <nlohmann/detail/input/lexer.hpp>
  12. #include <nlohmann/detail/macro_scope.hpp>
  13. #include <nlohmann/detail/meta/is_sax.hpp>
  14. #include <nlohmann/detail/value_t.hpp>
  15. namespace nlohmann
  16. {
  17. namespace detail
  18. {
  19. ////////////
  20. // parser //
  21. ////////////
  22. enum class parse_event_t : std::uint8_t
  23. {
  24. /// the parser read `{` and started to process a JSON object
  25. object_start,
  26. /// the parser read `}` and finished processing a JSON object
  27. object_end,
  28. /// the parser read `[` and started to process a JSON array
  29. array_start,
  30. /// the parser read `]` and finished processing a JSON array
  31. array_end,
  32. /// the parser read a key of a value in an object
  33. key,
  34. /// the parser finished reading a JSON value
  35. value
  36. };
  37. template<typename BasicJsonType>
  38. using parser_callback_t =
  39. std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
  40. /*!
  41. @brief syntax analysis
  42. This class implements a recursive descent parser.
  43. */
  44. template<typename BasicJsonType, typename InputAdapterType>
  45. class parser
  46. {
  47. using number_integer_t = typename BasicJsonType::number_integer_t;
  48. using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
  49. using number_float_t = typename BasicJsonType::number_float_t;
  50. using string_t = typename BasicJsonType::string_t;
  51. using lexer_t = lexer<BasicJsonType, InputAdapterType>;
  52. using token_type = typename lexer_t::token_type;
  53. public:
  54. /// a parser reading from an input adapter
  55. explicit parser(InputAdapterType&& adapter,
  56. const parser_callback_t<BasicJsonType> cb = nullptr,
  57. const bool allow_exceptions_ = true,
  58. const bool skip_comments = false)
  59. : callback(cb)
  60. , m_lexer(std::move(adapter), skip_comments)
  61. , allow_exceptions(allow_exceptions_)
  62. {
  63. // read first token
  64. get_token();
  65. }
  66. /*!
  67. @brief public parser interface
  68. @param[in] strict whether to expect the last token to be EOF
  69. @param[in,out] result parsed JSON value
  70. @throw parse_error.101 in case of an unexpected token
  71. @throw parse_error.102 if to_unicode fails or surrogate error
  72. @throw parse_error.103 if to_unicode fails
  73. */
  74. void parse(const bool strict, BasicJsonType& result)
  75. {
  76. if (callback)
  77. {
  78. json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
  79. sax_parse_internal(&sdp);
  80. // in strict mode, input must be completely read
  81. if (strict && (get_token() != token_type::end_of_input))
  82. {
  83. sdp.parse_error(m_lexer.get_position(),
  84. m_lexer.get_token_string(),
  85. parse_error::create(101, m_lexer.get_position(),
  86. exception_message(token_type::end_of_input, "value"), BasicJsonType()));
  87. }
  88. // in case of an error, return discarded value
  89. if (sdp.is_errored())
  90. {
  91. result = value_t::discarded;
  92. return;
  93. }
  94. // set top-level value to null if it was discarded by the callback
  95. // function
  96. if (result.is_discarded())
  97. {
  98. result = nullptr;
  99. }
  100. }
  101. else
  102. {
  103. json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
  104. sax_parse_internal(&sdp);
  105. // in strict mode, input must be completely read
  106. if (strict && (get_token() != token_type::end_of_input))
  107. {
  108. sdp.parse_error(m_lexer.get_position(),
  109. m_lexer.get_token_string(),
  110. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType()));
  111. }
  112. // in case of an error, return discarded value
  113. if (sdp.is_errored())
  114. {
  115. result = value_t::discarded;
  116. return;
  117. }
  118. }
  119. result.assert_invariant();
  120. }
  121. /*!
  122. @brief public accept interface
  123. @param[in] strict whether to expect the last token to be EOF
  124. @return whether the input is a proper JSON text
  125. */
  126. bool accept(const bool strict = true)
  127. {
  128. json_sax_acceptor<BasicJsonType> sax_acceptor;
  129. return sax_parse(&sax_acceptor, strict);
  130. }
  131. template<typename SAX>
  132. JSON_HEDLEY_NON_NULL(2)
  133. bool sax_parse(SAX* sax, const bool strict = true)
  134. {
  135. (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
  136. const bool result = sax_parse_internal(sax);
  137. // strict mode: next byte must be EOF
  138. if (result && strict && (get_token() != token_type::end_of_input))
  139. {
  140. return sax->parse_error(m_lexer.get_position(),
  141. m_lexer.get_token_string(),
  142. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType()));
  143. }
  144. return result;
  145. }
  146. private:
  147. template<typename SAX>
  148. JSON_HEDLEY_NON_NULL(2)
  149. bool sax_parse_internal(SAX* sax)
  150. {
  151. // stack to remember the hierarchy of structured values we are parsing
  152. // true = array; false = object
  153. std::vector<bool> states;
  154. // value to avoid a goto (see comment where set to true)
  155. bool skip_to_state_evaluation = false;
  156. while (true)
  157. {
  158. if (!skip_to_state_evaluation)
  159. {
  160. // invariant: get_token() was called before each iteration
  161. switch (last_token)
  162. {
  163. case token_type::begin_object:
  164. {
  165. if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
  166. {
  167. return false;
  168. }
  169. // closing } -> we are done
  170. if (get_token() == token_type::end_object)
  171. {
  172. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  173. {
  174. return false;
  175. }
  176. break;
  177. }
  178. // parse key
  179. if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
  180. {
  181. return sax->parse_error(m_lexer.get_position(),
  182. m_lexer.get_token_string(),
  183. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType()));
  184. }
  185. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  186. {
  187. return false;
  188. }
  189. // parse separator (:)
  190. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  191. {
  192. return sax->parse_error(m_lexer.get_position(),
  193. m_lexer.get_token_string(),
  194. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType()));
  195. }
  196. // remember we are now inside an object
  197. states.push_back(false);
  198. // parse values
  199. get_token();
  200. continue;
  201. }
  202. case token_type::begin_array:
  203. {
  204. if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
  205. {
  206. return false;
  207. }
  208. // closing ] -> we are done
  209. if (get_token() == token_type::end_array)
  210. {
  211. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  212. {
  213. return false;
  214. }
  215. break;
  216. }
  217. // remember we are now inside an array
  218. states.push_back(true);
  219. // parse values (no need to call get_token)
  220. continue;
  221. }
  222. case token_type::value_float:
  223. {
  224. const auto res = m_lexer.get_number_float();
  225. if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
  226. {
  227. return sax->parse_error(m_lexer.get_position(),
  228. m_lexer.get_token_string(),
  229. out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", BasicJsonType()));
  230. }
  231. if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
  232. {
  233. return false;
  234. }
  235. break;
  236. }
  237. case token_type::literal_false:
  238. {
  239. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
  240. {
  241. return false;
  242. }
  243. break;
  244. }
  245. case token_type::literal_null:
  246. {
  247. if (JSON_HEDLEY_UNLIKELY(!sax->null()))
  248. {
  249. return false;
  250. }
  251. break;
  252. }
  253. case token_type::literal_true:
  254. {
  255. if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
  256. {
  257. return false;
  258. }
  259. break;
  260. }
  261. case token_type::value_integer:
  262. {
  263. if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
  264. {
  265. return false;
  266. }
  267. break;
  268. }
  269. case token_type::value_string:
  270. {
  271. if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
  272. {
  273. return false;
  274. }
  275. break;
  276. }
  277. case token_type::value_unsigned:
  278. {
  279. if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
  280. {
  281. return false;
  282. }
  283. break;
  284. }
  285. case token_type::parse_error:
  286. {
  287. // using "uninitialized" to avoid "expected" message
  288. return sax->parse_error(m_lexer.get_position(),
  289. m_lexer.get_token_string(),
  290. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), BasicJsonType()));
  291. }
  292. case token_type::uninitialized:
  293. case token_type::end_array:
  294. case token_type::end_object:
  295. case token_type::name_separator:
  296. case token_type::value_separator:
  297. case token_type::end_of_input:
  298. case token_type::literal_or_value:
  299. default: // the last token was unexpected
  300. {
  301. return sax->parse_error(m_lexer.get_position(),
  302. m_lexer.get_token_string(),
  303. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), BasicJsonType()));
  304. }
  305. }
  306. }
  307. else
  308. {
  309. skip_to_state_evaluation = false;
  310. }
  311. // we reached this line after we successfully parsed a value
  312. if (states.empty())
  313. {
  314. // empty stack: we reached the end of the hierarchy: done
  315. return true;
  316. }
  317. if (states.back()) // array
  318. {
  319. // comma -> next value
  320. if (get_token() == token_type::value_separator)
  321. {
  322. // parse a new value
  323. get_token();
  324. continue;
  325. }
  326. // closing ]
  327. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
  328. {
  329. if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
  330. {
  331. return false;
  332. }
  333. // We are done with this array. Before we can parse a
  334. // new value, we need to evaluate the new state first.
  335. // By setting skip_to_state_evaluation to false, we
  336. // are effectively jumping to the beginning of this if.
  337. JSON_ASSERT(!states.empty());
  338. states.pop_back();
  339. skip_to_state_evaluation = true;
  340. continue;
  341. }
  342. return sax->parse_error(m_lexer.get_position(),
  343. m_lexer.get_token_string(),
  344. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), BasicJsonType()));
  345. }
  346. // states.back() is false -> object
  347. // comma -> next value
  348. if (get_token() == token_type::value_separator)
  349. {
  350. // parse key
  351. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
  352. {
  353. return sax->parse_error(m_lexer.get_position(),
  354. m_lexer.get_token_string(),
  355. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType()));
  356. }
  357. if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
  358. {
  359. return false;
  360. }
  361. // parse separator (:)
  362. if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
  363. {
  364. return sax->parse_error(m_lexer.get_position(),
  365. m_lexer.get_token_string(),
  366. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType()));
  367. }
  368. // parse values
  369. get_token();
  370. continue;
  371. }
  372. // closing }
  373. if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
  374. {
  375. if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
  376. {
  377. return false;
  378. }
  379. // We are done with this object. Before we can parse a
  380. // new value, we need to evaluate the new state first.
  381. // By setting skip_to_state_evaluation to false, we
  382. // are effectively jumping to the beginning of this if.
  383. JSON_ASSERT(!states.empty());
  384. states.pop_back();
  385. skip_to_state_evaluation = true;
  386. continue;
  387. }
  388. return sax->parse_error(m_lexer.get_position(),
  389. m_lexer.get_token_string(),
  390. parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), BasicJsonType()));
  391. }
  392. }
  393. /// get next token from lexer
  394. token_type get_token()
  395. {
  396. return last_token = m_lexer.scan();
  397. }
  398. std::string exception_message(const token_type expected, const std::string& context)
  399. {
  400. std::string error_msg = "syntax error ";
  401. if (!context.empty())
  402. {
  403. error_msg += "while parsing " + context + " ";
  404. }
  405. error_msg += "- ";
  406. if (last_token == token_type::parse_error)
  407. {
  408. error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" +
  409. m_lexer.get_token_string() + "'";
  410. }
  411. else
  412. {
  413. error_msg += "unexpected " + std::string(lexer_t::token_type_name(last_token));
  414. }
  415. if (expected != token_type::uninitialized)
  416. {
  417. error_msg += "; expected " + std::string(lexer_t::token_type_name(expected));
  418. }
  419. return error_msg;
  420. }
  421. private:
  422. /// callback function
  423. const parser_callback_t<BasicJsonType> callback = nullptr;
  424. /// the type of the last read token
  425. token_type last_token = token_type::uninitialized;
  426. /// the lexer
  427. lexer_t m_lexer;
  428. /// whether to throw exceptions in case of errors
  429. const bool allow_exceptions = true;
  430. };
  431. } // namespace detail
  432. } // namespace nlohmann