change_error_message.qbk 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. [#change_error_message]
  2. [section change_error_message]
  3. [h1 Synopsis]
  4. template <class P, class Msg>
  5. struct change_error_message;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P`] [[link parser parser]]]
  10. [[`Msg`] [[link parsing_error_message parsing error message]]]
  11. ]
  12. [h1 Description]
  13. It applies `P` on the input. When `P` succeeds, `change_error_message` returns
  14. the result `P` returns, otherwise `change_error_message` rejects the input and
  15. the reason will be `Msg`.
  16. [h1 Header]
  17. #include <boost/metaparse/change_error_message.hpp>
  18. [h1 Expression semantics]
  19. For any `p` parser and `m` parsing error message, `s` compile-time string and
  20. `pos` source position
  21. change_error_message<p, msg>::apply<s, pos>
  22. is equivalent to `p::apply<s, pos>` when `p` accepts the input.
  23. It is equivalent to `fail<msg>::apply<s, pos>` otherwise.
  24. [h1 Example]
  25. #include <boost/metaparse/change_error_message.hpp>
  26. #include <boost/metaparse/repeated1.hpp>
  27. #include <boost/metaparse/letter.hpp>
  28. #include <boost/metaparse/keyword.hpp>
  29. #include <boost/metaparse/last_of.hpp>
  30. #include <boost/metaparse/token.hpp>
  31. #include <boost/metaparse/string.hpp>
  32. #include <boost/metaparse/is_error.hpp>
  33. #include <boost/metaparse/start.hpp>
  34. #include <boost/metaparse/get_message.hpp>
  35. #include <boost/metaparse/define_error.hpp>
  36. #include <type_traits>
  37. using namespace boost::metaparse;
  38. BOOST_METAPARSE_DEFINE_ERROR(name_expected, "Name expected");
  39. using keyword_name = token<keyword<BOOST_METAPARSE_STRING("name")>>;
  40. using name_token = token<repeated1<letter>>;
  41. using name_parser =
  42. last_of<keyword_name, change_error_message<name_token, name_expected>>;
  43. static_assert(
  44. !is_error<
  45. name_parser::apply<BOOST_METAPARSE_STRING("name Bela"), start>
  46. >::type::value,
  47. "name_parser should accept \"name <a name>\""
  48. );
  49. static_assert(
  50. is_error<
  51. name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start>
  52. >::type::value,
  53. "name_parser should reject input when name is a question mark"
  54. );
  55. static_assert(
  56. std::is_same<
  57. get_message<
  58. name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start>
  59. >::type,
  60. name_expected
  61. >::type::value,
  62. "the error message should be the one specified by change_error_message"
  63. );
  64. [endsect]