metaparse/doc/nth_of.qbk

88 lines
2.1 KiB
Plaintext

[#nth_of]
[section nth_of]
[h1 Synopsis]
template <class N, class... Ps>
struct nth_of;
This is a [link parser_combinator parser combinator].
[table Arguments
[[Name] [Type]]
[[`N`] [[link boxed_value boxed] integer value in the range `[0..sizeof...(Ps)]`]]
[[`Ps`] [[link parser parser]s]]
]
[h1 Description]
`nth_of` applies the `Ps...` parsers in sequence. It accepts an input when all
of these parsers accept it. The result of parsing is the result of the `N`.
parser.
On compilers, which are not C++11-compliant, the maximum number of parsers
`nth_of` accepts can be specified with the `BOOST_METAPARSE_LIMIT_SEQUENCE_SIZE`
macro. Its default value is `5`.
[h1 Header]
#include <boost/metaparse/nth_of.hpp>
[h1 Expression semantics]
For any `p0`, ..., `pn` parsers and `k` boxed integer value the following are
equivalent
nth_of<k, p0, ..., pn>
nth_of_c<k::type::value, p0, ..., pn>
[h1 Example]
#include <boost/metaparse/nth_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>
#include <type_traits>
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 =
nth_of<
std::integral_constant<int, 1>,
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 second 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]