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