metaparse/doc/transform_error_message.qbk
2015-09-08 20:27:29 +02:00

103 lines
2.7 KiB
Plaintext

[#transform_error_message]
[section transform_error_message]
[h1 Synopsis]
template <class P, class F>
struct transform_error_message;
This is a [link parser_combinator parser combinator].
[table Arguments
[[Name] [Type]]
[[`P`] [[link parser parser]]]
[[`F`] [[link metafunction_class template metafunction class] taking one argument]]
]
[h1 Description]
It parses the input with `P`. When this succeeds, the result of parsing with
`transform_error_message` will be the result of parsing with `P`. When it fails,
the error `P` returns is forwarded to the caller of `transform_error_message`,
but the message of it is transformed with `F`.
[h1 Header]
#include <boost/metaparse/transform_error_message.hpp>
[h1 Expression semantics]
For any `p` parser and `f` metafunction class accepting one argument
transform_error_message<p, f>::apply<s, pos>
is equivalent to `p::apply<s, pos>` when `p` accepts the input.
It is equivalent to
`reject<f::apply<get_message<p::apply<s, pos>>::type>, get_position<p::apply<s, pos>>>`
otherwise.
[h1 Example]
#include <boost/metaparse/transform_error_message.hpp>
#include <boost/metaparse/repeated1.hpp>
#include <boost/metaparse/letter.hpp>
#include <boost/metaparse/keyword.hpp>
#include <boost/metaparse/last_of.hpp>
#include <boost/metaparse/token.hpp>
#include <boost/metaparse/string.hpp>
#include <boost/metaparse/is_error.hpp>
#include <boost/metaparse/start.hpp>
#include <boost/metaparse/get_message.hpp>
#include <boost/metaparse/define_error.hpp>
#include <boost/metaparse/reject.hpp>
#include <type_traits>
using namespace boost::metaparse;
BOOST_METAPARSE_DEFINE_ERROR(name_expected, "Name expected");
struct return_name_expected
{
typedef return_name_expected type;
template <class>
struct apply : name_expected {};
};
using keyword_name = token<keyword<BOOST_METAPARSE_STRING("name")>>;
using name_token = token<repeated1<letter>>;
using name_parser =
last_of<
keyword_name,
transform_error_message<name_token, return_name_expected>
>;
static_assert(
!is_error<
name_parser::apply<BOOST_METAPARSE_STRING("name Bela"), start>
>::type::value,
"name_parser should accept \"name <a name>\""
);
static_assert(
is_error<
name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start>
>::type::value,
"name_parser should reject input when name is a question mark"
);
static_assert(
std::is_same<
get_message<
name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start>
>::type,
name_expected
>::type::value,
"the error message should be the one specified by change_error_message"
);
[endsect]