• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[#transform_error]
2[section transform_error]
3
4[h1 Synopsis]
5
6  template <class P, class F>
7  struct transform_error;
8
9This is a [link parser_combinator parser combinator].
10
11[table Arguments
12  [[Name] [Type]]
13  [[`P`]  [[link parser parser]]]
14  [[`F`]  [[link metafunction_class template metafunction class] taking one argument]]
15]
16
17[h1 Description]
18
19It parses the input with `P`. When this succeeds, the result of parsing with
20`transform_error` will be the result of parsing with `P`. When it fails, `F` is
21evaluated with the error `P` returned as argument. Parsing with
22`transform_error` will fail and the error will be what `F` returns. Therefore,
23`F` is expected to accept and return a [link reject `reject`] value.
24
25[h1 Header]
26
27  #include <boost/metaparse/transform_error.hpp>
28
29[h1 Expression semantics]
30
31For any `p` parser and `f` metafunction class accepting one argument
32
33  transform_error<p, f>::apply<s, pos>
34
35is equivalent to `p::apply<s, pos>` when `p` accepts the input. It is equivalent
36to `f::apply<p::apply<s, pos>::type>` otherwise.
37
38[h1 Example]
39
40  #include <boost/metaparse/transform_error.hpp>
41  #include <boost/metaparse/repeated1.hpp>
42  #include <boost/metaparse/letter.hpp>
43  #include <boost/metaparse/keyword.hpp>
44  #include <boost/metaparse/last_of.hpp>
45  #include <boost/metaparse/token.hpp>
46  #include <boost/metaparse/string.hpp>
47  #include <boost/metaparse/is_error.hpp>
48  #include <boost/metaparse/start.hpp>
49  #include <boost/metaparse/get_message.hpp>
50  #include <boost/metaparse/get_position.hpp>
51  #include <boost/metaparse/define_error.hpp>
52  #include <boost/metaparse/reject.hpp>
53
54  #include <boost/mpl/lambda.hpp>
55
56  #include <type_traits>
57
58  using namespace boost::metaparse;
59
60  BOOST_METAPARSE_DEFINE_ERROR(name_expected, "Name expected");
61
62  using keyword_name = token<keyword<BOOST_METAPARSE_STRING("name")>>;
63  using name_token = token<repeated1<letter>>;
64
65  using name_parser =
66    last_of<
67      keyword_name,
68      transform_error<
69        name_token,
70        boost::mpl::lambda<
71          reject<name_expected, get_position<boost::mpl::_1> >
72        >::type
73      >
74    >;
75
76  static_assert(
77    !is_error<
78      name_parser::apply<BOOST_METAPARSE_STRING("name Bela"), start>
79    >::type::value,
80    "name_parser should accept \"name <a name>\""
81  );
82
83  static_assert(
84    is_error<
85      name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start>
86    >::type::value,
87    "name_parser should reject input when name is a question mark"
88  );
89
90  static_assert(
91    std::is_same<
92      get_message<
93        name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start>
94      >::type,
95      name_expected
96    >::type::value,
97    "the error message should be the one specified by change_error_message"
98  );
99
100[endsect]
101
102