• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[#nth_of_c]
2[section nth_of_c]
3
4[h1 Synopsis]
5
6  template <int N, class... Ps>
7  struct nth_of_c;
8
9This is a [link parser_combinator parser combinator].
10
11[table Arguments
12  [[Name]       [Type]]
13  [[`N`]        [`int` value in the range `[0..sizeof...(Ps)]`]]
14  [[`Ps`]       [[link parser parser]s]]
15]
16
17[h1 Description]
18
19`nth_of_c` applies the `Ps...` parsers in sequence. It accepts an input
20when all of these parsers accept it. The result of parsing is the result of the
21`N`. parser.
22
23On compilers, which are not C++11-compliant, the maximum number of parsers
24`nth_of_c` accepts can be specified with the
25`BOOST_METAPARSE_LIMIT_SEQUENCE_SIZE` macro. Its default value is `5`.
26
27[h1 Header]
28
29  #include <boost/metaparse/nth_of.hpp>
30
31[h1 Expression semantics]
32For any `p0`, ..., `pn` parsers and `k` integer value, where `0 <= k < n` the
33following are equivalent
34
35  nth_of_c<k, p0, ..., pn>
36
37  transform<sequence<p0, ..., pn>, boost::mpl::at_c<boost::mpl::_1, k>>
38
39[h1 Example]
40
41  #include <boost/metaparse/nth_of_c.hpp>
42  #include <boost/metaparse/int_.hpp>
43  #include <boost/metaparse/lit_c.hpp>
44  #include <boost/metaparse/token.hpp>
45  #include <boost/metaparse/start.hpp>
46  #include <boost/metaparse/string.hpp>
47  #include <boost/metaparse/is_error.hpp>
48  #include <boost/metaparse/get_result.hpp>
49
50  using namespace boost::metaparse;
51
52  using int_token = token<int_>;
53  using left_paren_token = token<lit_c<'('>>;
54  using right_paren_token = token<lit_c<')'>>;
55
56  using int_in_parens =
57    nth_of_c<1, left_paren_token, int_token, right_paren_token>;
58
59  static_assert(
60    get_result<
61      int_in_parens::apply<BOOST_METAPARSE_STRING("(13)"), start>
62    >::type::value == 13,
63    "it should return the result of the second parser"
64  );
65
66  static_assert(
67    is_error<
68      int_in_parens::apply<BOOST_METAPARSE_STRING("13"), start>
69    >::type::value,
70    "it should reject the input when there are no parens"
71  );
72
73  static_assert(
74    is_error<
75      int_in_parens::apply<BOOST_METAPARSE_STRING("(13"), start>
76    >::type::value,
77    "it should reject the input when there is no closing paren"
78  );
79
80[endsect]
81
82