123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- [#nth_of_c]
- [section nth_of_c]
- [h1 Synopsis]
- template <int N, class... Ps>
- struct nth_of_c;
- This is a [link parser_combinator parser combinator].
- [table Arguments
- [[Name] [Type]]
- [[`N`] [`int` value in the range `[0..sizeof...(Ps)]`]]
- [[`Ps`] [[link parser parser]s]]
- ]
- [h1 Description]
- `nth_of_c` 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_c` 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` integer value, where `0 <= k < n` the
- following are equivalent
- nth_of_c<k, p0, ..., pn>
-
- transform<sequence<p0, ..., pn>, boost::mpl::at_c<boost::mpl::_1, k>>
- [h1 Example]
- #include <boost/metaparse/nth_of_c.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 =
- nth_of_c<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]
|