]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#iterate_c] |
2 | [section iterate_c] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class P, int N> | |
7 | struct iterate_c; | |
8 | ||
9 | This is a [link parser_combinator parser combinator]. | |
10 | ||
11 | [table Arguments | |
12 | [[Name] [Type]] | |
13 | [[`P`] [[link parser parser]]] | |
14 | [[`N`] [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_c` to accept it. | |
22 | ||
23 | [h1 Header] | |
24 | ||
25 | #include <boost/metaparse/iterate_c.hpp> | |
26 | ||
27 | [h1 Expression semantics] | |
28 | ||
29 | For any `p` parser, `n` integer value the following are equivalent: | |
30 | ||
31 | iterate_c<p, n> | |
32 | ||
33 | sequence< | |
34 | p, // 1. | |
35 | p, // 2. | |
36 | // ... | |
37 | p // n. | |
38 | > | |
39 | ||
40 | [h1 Example] | |
41 | ||
42 | #include <boost/metaparse/iterate_c.hpp> | |
43 | #include <boost/metaparse/digit.hpp> | |
44 | #include <boost/metaparse/string.hpp> | |
45 | #include <boost/metaparse/start.hpp> | |
46 | #include <boost/metaparse/get_result.hpp> | |
47 | #include <boost/metaparse/is_error.hpp> | |
48 | ||
49 | #include <boost/mpl/vector.hpp> | |
50 | #include <boost/mpl/equal.hpp> | |
51 | #include <boost/mpl/char.hpp> | |
52 | ||
53 | using namespace boost::metaparse; | |
54 | ||
55 | static_assert( | |
56 | boost::mpl::equal< | |
57 | boost::mpl::vector< | |
58 | boost::mpl::char_<'1'>, | |
59 | boost::mpl::char_<'2'>, | |
60 | boost::mpl::char_<'3'> | |
61 | >, | |
62 | get_result< | |
63 | iterate_c<digit, 3>::apply<BOOST_METAPARSE_STRING("123"), start> | |
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_c<digit, 3>::apply<BOOST_METAPARSE_STRING("1234"), start> | |
78 | >::type | |
79 | >::type::value, | |
80 | "only three iterations should be made" | |
81 | ); | |
82 | ||
83 | static_assert( | |
84 | is_error< | |
85 | iterate_c<digit, 3>::apply<BOOST_METAPARSE_STRING("12"), start> | |
86 | >::type::value, | |
87 | "it should fail when digit can not be applied three times" | |
88 | ); | |
89 | ||
90 | [endsect] | |
91 |