]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#transform_error_message] |
2 | [section transform_error_message] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class P, class F> | |
7 | struct transform_error_message; | |
8 | ||
9 | This 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 | ||
19 | It parses the input with `P`. When this succeeds, the result of parsing with | |
20 | `transform_error_message` will be the result of parsing with `P`. When it fails, | |
21 | the error `P` returns is forwarded to the caller of `transform_error_message`, | |
22 | but the message of it is transformed with `F`. | |
23 | ||
24 | [h1 Header] | |
25 | ||
26 | #include <boost/metaparse/transform_error_message.hpp> | |
27 | ||
28 | [h1 Expression semantics] | |
29 | ||
30 | For any `p` parser and `f` metafunction class accepting one argument | |
31 | ||
32 | transform_error_message<p, f>::apply<s, pos> | |
33 | ||
34 | is equivalent to `p::apply<s, pos>` when `p` accepts the input. | |
35 | It is equivalent to | |
36 | `reject<f::apply<get_message<p::apply<s, pos>>::type>, get_position<p::apply<s, pos>>>` | |
37 | otherwise. | |
38 | ||
39 | [h1 Example] | |
40 | ||
41 | #include <boost/metaparse/transform_error_message.hpp> | |
42 | #include <boost/metaparse/repeated1.hpp> | |
43 | #include <boost/metaparse/letter.hpp> | |
44 | #include <boost/metaparse/keyword.hpp> | |
45 | #include <boost/metaparse/last_of.hpp> | |
46 | #include <boost/metaparse/token.hpp> | |
47 | #include <boost/metaparse/string.hpp> | |
48 | #include <boost/metaparse/is_error.hpp> | |
49 | #include <boost/metaparse/start.hpp> | |
50 | #include <boost/metaparse/get_message.hpp> | |
51 | #include <boost/metaparse/define_error.hpp> | |
52 | #include <boost/metaparse/reject.hpp> | |
53 | ||
54 | #include <type_traits> | |
55 | ||
56 | using namespace boost::metaparse; | |
57 | ||
58 | BOOST_METAPARSE_DEFINE_ERROR(name_expected, "Name expected"); | |
59 | ||
60 | struct return_name_expected | |
61 | { | |
62 | typedef return_name_expected type; | |
63 | ||
64 | template <class> | |
65 | struct apply : name_expected {}; | |
66 | }; | |
67 | ||
68 | using keyword_name = token<keyword<BOOST_METAPARSE_STRING("name")>>; | |
69 | using name_token = token<repeated1<letter>>; | |
70 | ||
71 | using name_parser = | |
72 | last_of< | |
73 | keyword_name, | |
74 | transform_error_message<name_token, return_name_expected> | |
75 | >; | |
76 | ||
77 | static_assert( | |
78 | !is_error< | |
79 | name_parser::apply<BOOST_METAPARSE_STRING("name Bela"), start> | |
80 | >::type::value, | |
81 | "name_parser should accept \"name <a name>\"" | |
82 | ); | |
83 | ||
84 | static_assert( | |
85 | is_error< | |
86 | name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start> | |
87 | >::type::value, | |
88 | "name_parser should reject input when name is a question mark" | |
89 | ); | |
90 | ||
91 | static_assert( | |
92 | std::is_same< | |
93 | get_message< | |
94 | name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start> | |
95 | >::type, | |
96 | name_expected | |
97 | >::type::value, | |
98 | "the error message should be the one specified by change_error_message" | |
99 | ); | |
100 | ||
101 | [endsect] | |
102 |