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