transform_error_message.qbk 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. [#transform_error_message]
  2. [section transform_error_message]
  3. [h1 Synopsis]
  4. template <class P, class F>
  5. struct transform_error_message;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P`] [[link parser parser]]]
  10. [[`F`] [[link metafunction_class template metafunction class] taking one argument]]
  11. ]
  12. [h1 Description]
  13. It parses the input with `P`. When this succeeds, the result of parsing with
  14. `transform_error_message` will be the result of parsing with `P`. When it fails,
  15. the error `P` returns is forwarded to the caller of `transform_error_message`,
  16. but the message of it is transformed with `F`.
  17. [h1 Header]
  18. #include <boost/metaparse/transform_error_message.hpp>
  19. [h1 Expression semantics]
  20. For any `p` parser and `f` metafunction class accepting one argument
  21. transform_error_message<p, f>::apply<s, pos>
  22. is equivalent to `p::apply<s, pos>` when `p` accepts the input.
  23. It is equivalent to
  24. `reject<f::apply<get_message<p::apply<s, pos>>::type>, get_position<p::apply<s, pos>>>`
  25. otherwise.
  26. [h1 Example]
  27. #include <boost/metaparse/transform_error_message.hpp>
  28. #include <boost/metaparse/repeated1.hpp>
  29. #include <boost/metaparse/letter.hpp>
  30. #include <boost/metaparse/keyword.hpp>
  31. #include <boost/metaparse/last_of.hpp>
  32. #include <boost/metaparse/token.hpp>
  33. #include <boost/metaparse/string.hpp>
  34. #include <boost/metaparse/is_error.hpp>
  35. #include <boost/metaparse/start.hpp>
  36. #include <boost/metaparse/get_message.hpp>
  37. #include <boost/metaparse/define_error.hpp>
  38. #include <boost/metaparse/reject.hpp>
  39. #include <type_traits>
  40. using namespace boost::metaparse;
  41. BOOST_METAPARSE_DEFINE_ERROR(name_expected, "Name expected");
  42. struct return_name_expected
  43. {
  44. typedef return_name_expected type;
  45. template <class>
  46. struct apply : name_expected {};
  47. };
  48. using keyword_name = token<keyword<BOOST_METAPARSE_STRING("name")>>;
  49. using name_token = token<repeated1<letter>>;
  50. using name_parser =
  51. last_of<
  52. keyword_name,
  53. transform_error_message<name_token, return_name_expected>
  54. >;
  55. static_assert(
  56. !is_error<
  57. name_parser::apply<BOOST_METAPARSE_STRING("name Bela"), start>
  58. >::type::value,
  59. "name_parser should accept \"name <a name>\""
  60. );
  61. static_assert(
  62. is_error<
  63. name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start>
  64. >::type::value,
  65. "name_parser should reject input when name is a question mark"
  66. );
  67. static_assert(
  68. std::is_same<
  69. get_message<
  70. name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start>
  71. >::type,
  72. name_expected
  73. >::type::value,
  74. "the error message should be the one specified by change_error_message"
  75. );
  76. [endsect]