metaparse/doc/middle_of.qbk
2015-07-19 09:53:35 +02:00

80 lines
1.8 KiB
Plaintext

[#middle_of]
[section middle_of]
[h1 Synopsis]
template <class P1, class P2, class P3>
struct middle_of;
This is a [link parser_combinator parser combinator].
[table Arguments
[[Name] [Type]]
[[`P1`] [[link parser parser]]]
[[`P2`] [[link parser parser]]]
[[`P3`] [[link parser parser]]]
]
[h1 Description]
`middle_of` applies `P1`, `P2` and `P3` in sequence. It accepts an input when
all of these three parsers accept it. The result of parsing is the result of
`P2`.
[h1 Header]
#include <boost/metaparse/middle_of.hpp>
[h1 Expression semantics]
For any `p1`, `p2` and `p3` parsers
middle_of<p1, p2, p3>
is equivalent to
nth_of<1, p1, p2, p3>
[h1 Example]
#include <boost/metaparse/middle_of.hpp>
#include <boost/metaparse/int_.hpp>
#include <boost/metaparse/lit_c.hpp>
#include <boost/metaparse/token.hpp>
#include <boost/metaparse/start.hpp>
#include <boost/metaparse/string.hpp>
#include <boost/metaparse/is_error.hpp>
#include <boost/metaparse/get_result.hpp>
using namespace boost::metaparse;
using int_token = token<int_>;
using left_paren_token = token<lit_c<'('>>;
using right_paren_token = token<lit_c<')'>>;
using int_in_parens = middle_of<left_paren_token, int_token, right_paren_token>;
static_assert(
get_result<
int_in_parens::apply<BOOST_METAPARSE_STRING("(13)"), start>
>::type::value == 13,
"it should return the result of the middle parser"
);
static_assert(
is_error<
int_in_parens::apply<BOOST_METAPARSE_STRING("13"), start>
>::type::value,
"it should reject the input when there are no parens"
);
static_assert(
is_error<
int_in_parens::apply<BOOST_METAPARSE_STRING("(13"), start>
>::type::value,
"it should reject the input when there is no closing paren"
);
[endsect]