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