transform_error.qbk 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. [#transform_error]
  2. [section transform_error]
  3. [h1 Synopsis]
  4. template <class P, class F>
  5. struct transform_error;
  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` will be the result of parsing with `P`. When it fails, `F` is
  15. evaluated with the error `P` returned as argument. Parsing with
  16. `transform_error` will fail and the error will be what `F` returns. Therefore,
  17. `F` is expected to accept and return a [link reject `reject`] value.
  18. [h1 Header]
  19. #include <boost/metaparse/transform_error.hpp>
  20. [h1 Expression semantics]
  21. For any `p` parser and `f` metafunction class accepting one argument
  22. transform_error<p, f>::apply<s, pos>
  23. is equivalent to `p::apply<s, pos>` when `p` accepts the input. It is equivalent
  24. to `f::apply<p::apply<s, pos>::type>` otherwise.
  25. [h1 Example]
  26. #include <boost/metaparse/transform_error.hpp>
  27. #include <boost/metaparse/repeated1.hpp>
  28. #include <boost/metaparse/letter.hpp>
  29. #include <boost/metaparse/keyword.hpp>
  30. #include <boost/metaparse/last_of.hpp>
  31. #include <boost/metaparse/token.hpp>
  32. #include <boost/metaparse/string.hpp>
  33. #include <boost/metaparse/is_error.hpp>
  34. #include <boost/metaparse/start.hpp>
  35. #include <boost/metaparse/get_message.hpp>
  36. #include <boost/metaparse/get_position.hpp>
  37. #include <boost/metaparse/define_error.hpp>
  38. #include <boost/metaparse/reject.hpp>
  39. #include <boost/mpl/lambda.hpp>
  40. #include <type_traits>
  41. using namespace boost::metaparse;
  42. BOOST_METAPARSE_DEFINE_ERROR(name_expected, "Name expected");
  43. using keyword_name = token<keyword<BOOST_METAPARSE_STRING("name")>>;
  44. using name_token = token<repeated1<letter>>;
  45. using name_parser =
  46. last_of<
  47. keyword_name,
  48. transform_error<
  49. name_token,
  50. boost::mpl::lambda<
  51. reject<name_expected, get_position<boost::mpl::_1> >
  52. >::type
  53. >
  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]