Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

nth_of_c

Synopsis

template <int N, class P0, /* ... */, class Pk>
struct nth_of_c;

This is a parser combinator.

Table 62. Arguments

Name

Type

N

int value in the range [0..k]

P0..Pk

parsers


Description

nth_of_c applies the P0 .. Pk 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.

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.

Header

#include <boost/metaparse/nth_of.hpp>

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>>

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"
);

PrevUpHomeNext