]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#before_9] |
2 | ['Definitions before section 9.] | |
3 | ||
4 | #include <boost/metaparse/string.hpp> | |
5 | #include <boost/metaparse/int_.hpp> | |
6 | ||
7 | #include <boost/metaparse/build_parser.hpp> | |
8 | ||
9 | using namespace boost::metaparse; | |
10 | ||
11 | using exp_parser1 = build_parser<int_>; | |
12 | #include <boost/metaparse/entire_input.hpp> | |
13 | ||
14 | using exp_parser2 = build_parser<entire_input<int_>>; | |
15 | #include <boost/metaparse/token.hpp> | |
16 | ||
17 | using exp_parser3 = build_parser<entire_input<token<int_>>>; | |
18 | #include <boost/metaparse/lit_c.hpp> | |
19 | ||
20 | #include <boost/metaparse/sequence.hpp> | |
21 | ||
22 | using exp_parser4 = build_parser<sequence<token<int_>, token<lit_c<'+'>>, token<int_>>>; | |
23 | ||
24 | #include <metashell/formatter.hpp> | |
25 | ||
26 | using int_token = token<int_>; | |
27 | ||
28 | using plus_token = token<lit_c<'+'>>; | |
29 | ||
30 | using exp_parser5 = build_parser<sequence<int_token, plus_token, int_token>>; | |
31 | #include <boost/metaparse/transform.hpp> | |
32 | ||
33 | #include <boost/mpl/plus.hpp> | |
34 | ||
35 | #include <boost/mpl/at.hpp> | |
36 | ||
37 | template <class Vector> | |
38 | struct eval_plus : | |
39 | boost::mpl::plus< | |
40 | typename boost::mpl::at_c<Vector, 0>::type, | |
41 | typename boost::mpl::at_c<Vector, 2>::type | |
42 | > {}; | |
43 | ||
44 | #include <boost/mpl/quote.hpp> | |
45 | ||
46 | using exp_parser6 = | |
47 | build_parser< | |
48 | transform< | |
49 | sequence<int_token, plus_token, int_token>, | |
50 | boost::mpl::quote1<eval_plus> | |
51 | > | |
52 | >; | |
53 | #include <boost/metaparse/any.hpp> | |
54 | ||
55 | using exp_parser7 = | |
56 | build_parser< | |
57 | sequence< | |
58 | int_token, /* The first <number> */ | |
59 | repeated<sequence<plus_token, int_token>> /* The "+ <number>" elements */ | |
60 | > | |
61 | >; | |
62 | using temp_result = exp_parser7::apply<BOOST_METAPARSE_STRING("1 + 2 + 3 + 4")>::type; | |
63 | #include <boost/mpl/fold.hpp> | |
64 | ||
65 | using vector_of_numbers = | |
66 | boost::mpl::vector< | |
67 | boost::mpl::int_<2>, | |
68 | boost::mpl::int_<5>, | |
69 | boost::mpl::int_<6> | |
70 | >; | |
71 | ||
72 | template <class Vector> | |
73 | struct sum_vector : | |
74 | boost::mpl::fold< | |
75 | Vector, | |
76 | boost::mpl::int_<0>, | |
77 | boost::mpl::lambda< | |
78 | boost::mpl::plus<boost::mpl::_1, boost::mpl::_2> | |
79 | >::type | |
80 | > | |
81 | {}; | |
82 | ||
83 | template <class Sum, class Item> | |
84 | struct sum_items : | |
85 | boost::mpl::plus< | |
86 | Sum, | |
87 | typename boost::mpl::at_c<Item, 1>::type | |
88 | > | |
89 | {}; | |
90 | using exp_parser8 = | |
91 | build_parser< | |
92 | sequence< | |
93 | int_token, /* parse the first <number> */ | |
94 | transform< | |
95 | repeated<sequence<plus_token, int_token>>, /* parse the "+ <number>" elements */ | |
96 | /* lambda expression summarising the "+ <number>" elements using fold */ | |
97 | boost::mpl::lambda< | |
98 | /* The folding expression we have just created */ | |
99 | boost::mpl::fold< | |
100 | boost::mpl::_1, /* the argument of the lambda expression, the result */ | |
101 | /* of the repeated<...> parser */ | |
102 | boost::mpl::int_<0>, | |
103 | boost::mpl::quote2<sum_items> | |
104 | > | |
105 | >::type | |
106 | > | |
107 | > | |
108 | >; | |
109 | ||
110 | using exp_parser9 = | |
111 | build_parser< | |
112 | transform< | |
113 | /* What we had so far */ | |
114 | sequence< | |
115 | int_token, | |
116 | transform< | |
117 | repeated<sequence<plus_token, int_token>>, | |
118 | boost::mpl::lambda< | |
119 | boost::mpl::fold< | |
120 | boost::mpl::_1, | |
121 | boost::mpl::int_<0>, | |
122 | boost::mpl::quote2<sum_items> | |
123 | > | |
124 | >::type | |
125 | > | |
126 | >, | |
127 | boost::mpl::quote1<sum_vector> /* summarise the vector of numbers */ | |
128 | > | |
129 | >; | |
130 | #include <boost/metaparse/foldl.hpp> | |
131 | ||
132 | using exp_parser10 = | |
133 | build_parser< | |
134 | transform< | |
135 | sequence< | |
136 | int_token, | |
137 | foldl< | |
138 | sequence<plus_token, int_token>, | |
139 | boost::mpl::int_<0>, | |
140 | boost::mpl::quote2<sum_items> | |
141 | > | |
142 | >, | |
143 | boost::mpl::quote1<sum_vector>> | |
144 | >; | |
145 | #include <boost/metaparse/foldl_start_with_parser.hpp> | |
146 | ||
147 | using exp_parser11 = | |
148 | build_parser< | |
149 | foldl_start_with_parser< | |
150 | sequence<plus_token, int_token>, /* apply this parser repeatedly */ | |
151 | int_token, /* use this parser to get the initial value */ | |
152 | boost::mpl::quote2<sum_items> /* use this function to add a new value to the summary */ | |
153 | > | |
154 | >; | |
155 | using minus_token = token<lit_c<'-'>>; | |
156 | ||
157 | #include <boost/metaparse/one_of.hpp> | |
158 | ||
159 | using exp_parser12 = | |
160 | build_parser< | |
161 | foldl_start_with_parser< | |
162 | sequence<one_of<plus_token, minus_token>, int_token>, | |
163 | int_token, | |
164 | boost::mpl::quote2<sum_items> | |
165 | > | |
166 | >; | |
167 | #include <boost/mpl/minus.hpp> | |
168 | ||
169 | template <class L, char Op, class R> struct eval_binary_op; | |
170 | ||
171 | template <class L, class R> struct eval_binary_op<L, '+', R> : boost::mpl::plus<L, R>::type {}; | |
172 | ||
173 | template <class L, class R> struct eval_binary_op<L, '-', R> : boost::mpl::minus<L, R>::type {}; | |
174 | ||
175 | template <class S, class Item> | |
176 | struct binary_op : | |
177 | eval_binary_op< | |
178 | S, | |
179 | boost::mpl::at_c<Item, 0>::type::value, | |
180 | typename boost::mpl::at_c<Item, 1>::type | |
181 | > | |
182 | {}; | |
183 | ||
184 | using exp_parser13 = | |
185 | build_parser< | |
186 | foldl_start_with_parser< | |
187 | sequence<one_of<plus_token, minus_token>, int_token>, | |
188 | int_token, | |
189 | boost::mpl::quote2<binary_op> | |
190 | > | |
191 | >; | |
192 | #include <boost/mpl/times.hpp> | |
193 | ||
194 | template <class L, class R> struct eval_binary_op<L, '*', R> : boost::mpl::times<L, R>::type {}; | |
195 | ||
196 | using times_token = token<lit_c<'*'>>; | |
197 | ||
198 | using exp_parser14 = | |
199 | build_parser< | |
200 | foldl_start_with_parser< | |
201 | sequence<one_of<plus_token, minus_token, times_token>, int_token>, | |
202 | int_token, | |
203 | boost::mpl::quote2<binary_op> | |
204 | > | |
205 | >; | |
206 | using mult_exp1 = foldl_start_with_parser<sequence<times_token, int_token>, int_token, boost::mpl::quote2<binary_op>>; | |
207 | ||
208 | using exp_parser15 = | |
209 | build_parser< | |
210 | foldl_start_with_parser< | |
211 | sequence<one_of<plus_token, minus_token>, mult_exp1>, | |
212 | mult_exp1, | |
213 | boost::mpl::quote2<binary_op> | |
214 | > | |
215 | >; | |
216 | #include <boost/mpl/divides.hpp> | |
217 | ||
218 | template <class L, class R> struct eval_binary_op<L, '/', R> : boost::mpl::divides<L, R>::type {}; | |
219 | ||
220 | using divides_token = token<lit_c<'/'>>; | |
221 | ||
222 | using mult_exp2 = | |
223 | foldl_start_with_parser< | |
224 | sequence<one_of<times_token, divides_token>, int_token>, | |
225 | int_token, | |
226 | boost::mpl::quote2<binary_op> | |
227 | >; | |
228 | ||
229 | using exp_parser16 = | |
230 | build_parser< | |
231 | foldl_start_with_parser< | |
232 | sequence<one_of<plus_token, minus_token>, mult_exp2>, | |
233 | mult_exp2, | |
234 | boost::mpl::quote2<binary_op> | |
235 | > | |
236 | >; | |
237 | template <class S, class Item> | |
238 | struct reverse_binary_op : | |
239 | eval_binary_op< | |
240 | typename boost::mpl::at_c<Item, 0>::type, | |
241 | boost::mpl::at_c<Item, 1>::type::value, | |
242 | S | |
243 | > | |
244 | {}; | |
245 | ||
246 | #include <boost/metaparse/foldr_start_with_parser.hpp> | |
247 | ||
248 | using mult_exp3 = | |
249 | foldr_start_with_parser< | |
250 | sequence<int_token, one_of<times_token, divides_token>>, /* The parser applied repeatedly */ | |
251 | int_token, /* The parser parsing the last number */ | |
252 | boost::mpl::quote2<reverse_binary_op> /* The function called for every result */ | |
253 | /* of applying the above parser */ | |
254 | >; | |
255 | ||
256 | using exp_parser17 = | |
257 | build_parser< | |
258 | foldl_start_with_parser< | |
259 | sequence<one_of<plus_token, minus_token>, mult_exp3>, | |
260 | mult_exp3, | |
261 | boost::mpl::quote2<binary_op> | |
262 | > | |
263 | >; | |
264 |