]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#repeated] |
2 | [section repeated] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class P> | |
7 | struct repeated; | |
8 | ||
9 | This is a [link parser_combinator parser combinator]. | |
10 | ||
11 | [table Arguments | |
12 | [[Name] [Type]] | |
13 | [[`P`] [[link parser parser]]] | |
14 | ] | |
15 | ||
16 | [h1 Description] | |
17 | ||
18 | It applies `P` on the input string repeatedly as long as `P` accepts the input. | |
19 | The result of parsing is a sequence of the results of the individual | |
20 | applications of `P`. | |
21 | ||
22 | When `P` rejects the input for the first time, `repeated` still accepts the | |
23 | input and the result of parsing is an empty sequence. | |
24 | ||
25 | Here is a diagram showing how `repeated` works by example: | |
26 | ||
27 | using int_token = token<int_>; | |
28 | ||
29 | [$images/metaparse/repeated_diag1.png [width 70%]] | |
30 | ||
31 | Further details can be found in the [link repetition Repetition] section | |
32 | of the [link manual User Manual]. | |
33 | ||
34 | [h1 Header] | |
35 | ||
36 | #include <boost/metaparse/repeated.hpp> | |
37 | ||
38 | [h1 Expression semantics] | |
39 | ||
40 | For any `p` parser the following are equivalent: | |
41 | ||
42 | repeated<p> | |
43 | ||
44 | foldl< | |
45 | p, | |
46 | /* unspecified empty sequence */, | |
47 | boost::mpl::push_back<_2, _1> | |
48 | > | |
49 | ||
50 | [h1 Example] | |
51 | ||
52 | #include <boost/metaparse/repeated.hpp> | |
53 | #include <boost/metaparse/digit_val.hpp> | |
54 | #include <boost/metaparse/start.hpp> | |
55 | #include <boost/metaparse/string.hpp> | |
56 | #include <boost/metaparse/get_result.hpp> | |
57 | ||
58 | #include <boost/mpl/equal.hpp> | |
59 | #include <boost/mpl/vector.hpp> | |
60 | #include <boost/mpl/int.hpp> | |
61 | ||
62 | using namespace boost::metaparse; | |
63 | ||
64 | using digits = repeated<digit_val>; | |
65 | ||
66 | static_assert( | |
67 | boost::mpl::equal< | |
68 | get_result<digits::apply<BOOST_METAPARSE_STRING("1234"), start>>::type, | |
69 | boost::mpl::vector< | |
70 | boost::mpl::int_<1>, | |
71 | boost::mpl::int_<2>, | |
72 | boost::mpl::int_<3>, | |
73 | boost::mpl::int_<4> | |
74 | > | |
75 | >::type::value, | |
76 | "the result of parsing should be the list of digit values" | |
77 | ); | |
78 | ||
79 | static_assert( | |
80 | boost::mpl::equal< | |
81 | get_result<digits::apply<BOOST_METAPARSE_STRING("x"), start>>::type, | |
82 | boost::mpl::vector<> | |
83 | >::type::value, | |
84 | "repeated should accept the input when it can't parse anything with digit_val" | |
85 | ); | |
86 | ||
87 | [endsect] | |
88 |