]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#iterate] |
2 | [section iterate] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class P, class N> | |
7 | struct iterate; | |
8 | ||
9 | This is a [link parser_combinator parser combinator]. | |
10 | ||
11 | [table Arguments | |
12 | [[Name] [Type]] | |
13 | [[`P`] [[link parser parser]]] | |
14 | [[`N`] [[link boxed_value boxed] non-negative integer value]] | |
15 | ] | |
16 | ||
17 | [h1 Description] | |
18 | ||
19 | It applies `P` on the input string `N` times. The result of parsing is a | |
20 | sequence of the results of the individual applications of `P`. `P` has to accept | |
21 | the input `N` times for `iterate` to accept it. | |
22 | ||
23 | [h1 Header] | |
24 | ||
25 | #include <boost/metaparse/iterate.hpp> | |
26 | ||
27 | [h1 Expression semantics] | |
28 | ||
29 | For any `p` parser, `n` wrapped integer the following are equivalent: | |
30 | ||
31 | iterate<p, n> | |
32 | ||
33 | iterate_c<p, n::type::value> | |
34 | ||
35 | [h1 Example] | |
36 | ||
37 | #include <boost/metaparse/iterate.hpp> | |
38 | #include <boost/metaparse/digit.hpp> | |
39 | #include <boost/metaparse/string.hpp> | |
40 | #include <boost/metaparse/start.hpp> | |
41 | #include <boost/metaparse/get_result.hpp> | |
42 | #include <boost/metaparse/is_error.hpp> | |
43 | ||
44 | #include <boost/mpl/vector.hpp> | |
45 | #include <boost/mpl/equal.hpp> | |
46 | #include <boost/mpl/char.hpp> | |
47 | ||
48 | #include <type_traits> | |
49 | ||
50 | using namespace boost::metaparse; | |
51 | ||
52 | static_assert( | |
53 | boost::mpl::equal< | |
54 | boost::mpl::vector< | |
55 | boost::mpl::char_<'1'>, | |
56 | boost::mpl::char_<'2'>, | |
57 | boost::mpl::char_<'3'> | |
58 | >, | |
59 | get_result< | |
60 | iterate<digit, std::integral_constant<int, 3>>::apply< | |
61 | BOOST_METAPARSE_STRING("123"), | |
62 | start | |
63 | > | |
64 | >::type | |
65 | >::type::value, | |
66 | "the result should be the sequence of the individual applications of digit" | |
67 | ); | |
68 | ||
69 | static_assert( | |
70 | boost::mpl::equal< | |
71 | boost::mpl::vector< | |
72 | boost::mpl::char_<'1'>, | |
73 | boost::mpl::char_<'2'>, | |
74 | boost::mpl::char_<'3'> | |
75 | >, | |
76 | get_result< | |
77 | iterate<digit, std::integral_constant<int, 3>>::apply< | |
78 | BOOST_METAPARSE_STRING("1234"), | |
79 | start | |
80 | > | |
81 | >::type | |
82 | >::type::value, | |
83 | "only three iterations should be made" | |
84 | ); | |
85 | ||
86 | static_assert( | |
87 | is_error< | |
88 | iterate<digit, std::integral_constant<int, 3>>::apply< | |
89 | BOOST_METAPARSE_STRING("12"), | |
90 | start | |
91 | > | |
92 | >::type::value, | |
93 | "it should fail when digit can not be applied three times" | |
94 | ); | |
95 | ||
96 | ||
97 | [endsect] | |
98 |