]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#nth_of] |
2 | [section nth_of] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class N, class P0, /* ... */, class Pk> | |
7 | struct nth_of; | |
8 | ||
9 | This 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..k]`]] | |
14 | [[`P0`..`Pk`] [[link parser parser]s]] | |
15 | ] | |
16 | ||
17 | [h1 Description] | |
18 | ||
19 | `nth_of` applies the `P0` .. `Pk` parsers in sequence. It accepts an input when | |
20 | all of these parsers accept it. The result of parsing is the result of the `N`. | |
21 | parser. | |
22 | ||
23 | The maximum number of parsers `nth_of` 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 | ||
32 | For any `p0`, ..., `pn` parsers and `k` boxed integer value the following are | |
33 | equivalent | |
34 | ||
35 | nth_of<k, p0, ..., pn> | |
36 | ||
37 | nth_of_c<k::type::value, p0, ..., pn> | |
38 | ||
39 | [h1 Example] | |
40 | ||
41 | #include <boost/metaparse/nth_of.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 | #include <type_traits> | |
51 | ||
52 | using namespace boost::metaparse; | |
53 | ||
54 | using int_token = token<int_>; | |
55 | using left_paren_token = token<lit_c<'('>>; | |
56 | using right_paren_token = token<lit_c<')'>>; | |
57 | ||
58 | using int_in_parens = | |
59 | nth_of< | |
60 | std::integral_constant<int, 1>, | |
61 | left_paren_token, int_token, right_paren_token | |
62 | >; | |
63 | ||
64 | static_assert( | |
65 | get_result< | |
66 | int_in_parens::apply<BOOST_METAPARSE_STRING("(13)"), start> | |
67 | >::type::value == 13, | |
68 | "it should return the result of the second parser" | |
69 | ); | |
70 | ||
71 | static_assert( | |
72 | is_error< | |
73 | int_in_parens::apply<BOOST_METAPARSE_STRING("13"), start> | |
74 | >::type::value, | |
75 | "it should reject the input when there are no parens" | |
76 | ); | |
77 | ||
78 | static_assert( | |
79 | is_error< | |
80 | int_in_parens::apply<BOOST_METAPARSE_STRING("(13"), start> | |
81 | >::type::value, | |
82 | "it should reject the input when there is no closing paren" | |
83 | ); | |
84 | ||
85 | [endsect] | |
86 |