]>
Commit | Line | Data |
---|---|---|
1 | [#sequence_apply] | |
2 | [section sequence_apply] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <template <class, ..., class> class T, class P1, ..., class Pn> | |
7 | struct sequence_applyn; | |
8 | ||
9 | This is a [link parser_combinator parser combinator]. | |
10 | ||
11 | [table Arguments | |
12 | [[T] [Template class taking `n` arguments]] | |
13 | [[`P1`..`Pn`] [[link parser parser]s]] | |
14 | ] | |
15 | ||
16 | [h1 Description] | |
17 | ||
18 | It applies the `P1` ... `Pn` [link parser parser]s on the input in order. When | |
19 | all of them succeed, the result of parsing with `sequence_applyn` is the `T` | |
20 | template class instantiated with the results of the `P1` ... `Pn` | |
21 | [link parser parser]s. When any of the `P1` ... `Pn` [link parser parser]s | |
22 | reject the input, the error is propagated. | |
23 | ||
24 | `n` has to be in the `[1..BOOST_METAPARSE_LIMIT_SEQUENCE_SIZE)` range. | |
25 | ||
26 | [h1 Header] | |
27 | ||
28 | #include <boost/metaparse/sequence_apply.hpp> | |
29 | ||
30 | [h1 Expression semantics] | |
31 | ||
32 | For any `n > 0`, `p1` ... `pn` [link parser parser]s, `t` template class with | |
33 | `n` `class` arguments, `s` compile-time string and `pos` source position the | |
34 | following are equivalent: | |
35 | ||
36 | sequence_apply<t, p1, ..., pn>::apply<s, pos>::type | |
37 | ||
38 | when [link sequence `sequence`]`<p1, ..., pn>` accepts the input: | |
39 | ||
40 | return_< | |
41 | t< | |
42 | mpl::at_c<0, get_result<sequence<p1,...,pn>::apply<s, pos>>::type>::type, | |
43 | ... | |
44 | mpl::at_c<n, get_result<sequence<p1,...,pn>::apply<s, pos>>::type>::type, | |
45 | > | |
46 | >::apply<s, pos>::type | |
47 | ||
48 | when [link sequence `sequence`]`<p1, ..., pn>` rejects the input: | |
49 | ||
50 | sequence<p1, ..., pn>::apply<s, pos>::type | |
51 | ||
52 | [h1 Example] | |
53 | ||
54 | #include <boost/metaparse/sequence_apply.hpp> | |
55 | #include <boost/metaparse/int_.hpp> | |
56 | #include <boost/metaparse/middle_of.hpp> | |
57 | #include <boost/metaparse/lit_c.hpp> | |
58 | #include <boost/metaparse/start.hpp> | |
59 | #include <boost/metaparse/string.hpp> | |
60 | #include <boost/metaparse/get_result.hpp> | |
61 | ||
62 | #include <boost/type_traits/is_same.hpp> | |
63 | ||
64 | using namespace boost::metaparse; | |
65 | ||
66 | template <int Real, int Imaginary> | |
67 | struct complex_c { typedef complex_c type; }; | |
68 | ||
69 | template <class Real, class Imaginary> | |
70 | struct complex : complex_c<Real::type::value, Imaginary::type::value> {}; | |
71 | ||
72 | typedef | |
73 | sequence_apply2<complex, int_, middle_of<lit_c<'+'>, int_, lit_c<'i'>>> | |
74 | complex_parser; | |
75 | ||
76 | static_assert( | |
77 | boost::is_same< | |
78 | complex_c<1, 2>, | |
79 | get_result< | |
80 | complex_parser::apply<BOOST_METAPARSE_STRING("1+2i"), start> | |
81 | >::type::type | |
82 | >::type::value, | |
83 | "the result of parsing should be the list of digit values" | |
84 | ); | |
85 | ||
86 | [endsect] | |
87 |