77 lines
1.8 KiB
Plaintext
77 lines
1.8 KiB
Plaintext
[#transform]
|
|
[section transform]
|
|
|
|
[h1 Synopsis]
|
|
|
|
template <class P, class T>
|
|
struct transform;
|
|
|
|
This is a [link parser_combinator parser combinator].
|
|
|
|
[table Arguments
|
|
[[Name] [Type]]
|
|
[[`P`] [[link parser parser]]]
|
|
[[`T`] [[link metafunction_class template metafunction class] taking one argument]]
|
|
]
|
|
|
|
[h1 Description]
|
|
|
|
`transform` parses the input using `P` and transforms the result `P` returns
|
|
with `T`. The result of parsing is what `T` returns. When `P` fails, the failure
|
|
is returned unchanged.
|
|
|
|
[h1 Header]
|
|
|
|
#include <boost/metaparse/transform.hpp>
|
|
|
|
[h1 Expression semantics]
|
|
|
|
For any `p` parser, `t` metafunction class accepting one argument, `s`
|
|
compile-time string and `pos` source position
|
|
|
|
get_result<transform<p, t>::apply<s, pos>>::type
|
|
|
|
is equivalent to
|
|
|
|
t::apply<get_result<p::apply<s, pos>>::type>::type
|
|
|
|
When `p::apply<s, pos>` doesn't return an error. The combinator returns the
|
|
error otherwise.
|
|
|
|
[h1 Example]
|
|
|
|
#include <boost/metaparse/transform.hpp>
|
|
#include <boost/metaparse/digit.hpp>
|
|
#include <boost/metaparse/start.hpp>
|
|
#include <boost/metaparse/string.hpp>
|
|
#include <boost/metaparse/is_error.hpp>
|
|
#include <boost/metaparse/get_result.hpp>
|
|
|
|
#include <boost/metaparse/util/digit_to_int.hpp>
|
|
|
|
using namespace boost::metaparse;
|
|
|
|
using digit_value = transform<digit, util::digit_to_int<>>;
|
|
|
|
static_assert(
|
|
!is_error<
|
|
digit_value::apply<BOOST_METAPARSE_STRING("0"), start>
|
|
>::type::value,
|
|
"digit_val should accept a digit"
|
|
);
|
|
|
|
static_assert(
|
|
is_error<digit_value::apply<BOOST_METAPARSE_STRING("x"), start>>::type::value,
|
|
"digit_val should reject a character"
|
|
);
|
|
|
|
static_assert(
|
|
get_result<
|
|
digit_value::apply<BOOST_METAPARSE_STRING("0"), start>
|
|
>::type::value == 0,
|
|
"the result of parsing should be the int value"
|
|
);
|
|
|
|
[endsect]
|
|
|