2 ['Definitions before section 11.3.1.]
4 #include <boost/metaparse/string.hpp>
5 #include <boost/metaparse/int_.hpp>
7 #include <boost/metaparse/build_parser.hpp>
9 using namespace boost::metaparse;
11 using exp_parser1 = build_parser<int_>;
12 #include <boost/metaparse/entire_input.hpp>
14 using exp_parser2 = build_parser<entire_input<int_>>;
15 #include <boost/metaparse/token.hpp>
17 using exp_parser3 = build_parser<entire_input<token<int_>>>;
18 #include <boost/metaparse/lit_c.hpp>
20 #include <boost/metaparse/sequence.hpp>
22 using exp_parser4 = build_parser<sequence<token<int_>, token<lit_c<'+'>>, token<int_>>>;
24 #include <metashell/formatter.hpp>
26 using int_token = token<int_>;
28 using plus_token = token<lit_c<'+'>>;
30 using exp_parser5 = build_parser<sequence<int_token, plus_token, int_token>>;
31 #include <boost/metaparse/transform.hpp>
33 #include <boost/mpl/plus.hpp>
35 #include <boost/mpl/at.hpp>
37 template <class Vector>
40 typename boost::mpl::at_c<Vector, 0>::type,
41 typename boost::mpl::at_c<Vector, 2>::type
44 #include <boost/mpl/quote.hpp>
49 sequence<int_token, plus_token, int_token>,
50 boost::mpl::quote1<eval_plus>
53 #include <boost/metaparse/any.hpp>
58 int_token, /* The first <number> */
59 repeated<sequence<plus_token, int_token>> /* The "+ <number>" elements */
62 using temp_result = exp_parser7::apply<BOOST_METAPARSE_STRING("1 + 2 + 3 + 4")>::type;
63 #include <boost/mpl/fold.hpp>
65 using vector_of_numbers =
72 template <class Vector>
78 boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>
83 template <class Sum, class Item>
87 typename boost::mpl::at_c<Item, 1>::type
93 int_token, /* parse the first <number> */
95 repeated<sequence<plus_token, int_token>>, /* parse the "+ <number>" elements */
96 /* lambda expression summarising the "+ <number>" elements using fold */
98 /* The folding expression we have just created */
100 boost::mpl::_1, /* the argument of the lambda expression, the result */
101 /* of the repeated<...> parser */
103 boost::mpl::quote2<sum_items>
113 /* What we had so far */
117 repeated<sequence<plus_token, int_token>>,
122 boost::mpl::quote2<sum_items>
127 boost::mpl::quote1<sum_vector> /* summarise the vector of numbers */
130 #include <boost/metaparse/foldl.hpp>
138 sequence<plus_token, int_token>,
140 boost::mpl::quote2<sum_items>
143 boost::mpl::quote1<sum_vector>>
145 #include <boost/metaparse/foldl_start_with_parser.hpp>
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 */
155 using minus_token = token<lit_c<'-'>>;
157 #include <boost/metaparse/one_of.hpp>
161 foldl_start_with_parser<
162 sequence<one_of<plus_token, minus_token>, int_token>,
164 boost::mpl::quote2<sum_items>
167 #include <boost/mpl/minus.hpp>
169 template <class L, char Op, class R> struct eval_binary_op;
171 template <class L, class R> struct eval_binary_op<L, '+', R> : boost::mpl::plus<L, R>::type {};
173 template <class L, class R> struct eval_binary_op<L, '-', R> : boost::mpl::minus<L, R>::type {};
175 template <class S, class Item>
179 boost::mpl::at_c<Item, 0>::type::value,
180 typename boost::mpl::at_c<Item, 1>::type
186 foldl_start_with_parser<
187 sequence<one_of<plus_token, minus_token>, int_token>,
189 boost::mpl::quote2<binary_op>
192 #include <boost/mpl/times.hpp>
194 template <class L, class R> struct eval_binary_op<L, '*', R> : boost::mpl::times<L, R>::type {};
196 using times_token = token<lit_c<'*'>>;
200 foldl_start_with_parser<
201 sequence<one_of<plus_token, minus_token, times_token>, int_token>,
203 boost::mpl::quote2<binary_op>
206 using mult_exp1 = foldl_start_with_parser<sequence<times_token, int_token>, int_token, boost::mpl::quote2<binary_op>>;
210 foldl_start_with_parser<
211 sequence<one_of<plus_token, minus_token>, mult_exp1>,
213 boost::mpl::quote2<binary_op>
216 #include <boost/mpl/divides.hpp>
218 template <class L, class R> struct eval_binary_op<L, '/', R> : boost::mpl::divides<L, R>::type {};
220 using divides_token = token<lit_c<'/'>>;
223 foldl_start_with_parser<
224 sequence<one_of<times_token, divides_token>, int_token>,
226 boost::mpl::quote2<binary_op>
231 foldl_start_with_parser<
232 sequence<one_of<plus_token, minus_token>, mult_exp2>,
234 boost::mpl::quote2<binary_op>
237 template <class S, class Item>
238 struct reverse_binary_op :
240 typename boost::mpl::at_c<Item, 0>::type,
241 boost::mpl::at_c<Item, 1>::type::value,
246 #include <boost/metaparse/foldr_start_with_parser.hpp>
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 */
258 foldl_start_with_parser<
259 sequence<one_of<plus_token, minus_token>, mult_exp3>,
261 boost::mpl::quote2<binary_op>
264 #include <boost/mpl/negate.hpp>
267 foldr_start_with_parser<
270 boost::mpl::lambda<boost::mpl::negate<boost::mpl::_1>>::type
274 foldl_start_with_parser<
275 sequence<one_of<times_token, divides_token>, unary_exp1>,
277 boost::mpl::quote2<binary_op>
282 foldl_start_with_parser<
283 sequence<one_of<plus_token, minus_token>, mult_exp4>,
285 boost::mpl::quote2<binary_op>
288 using lparen_token = token<lit_c<'('>>;
290 using rparen_token = token<lit_c<')'>>;
293 foldl_start_with_parser<
294 sequence<one_of<plus_token, minus_token>, mult_exp4>,
296 boost::mpl::quote2<binary_op>
299 using paren_exp1 = sequence<lparen_token, plus_exp1, rparen_token>;
301 #include <boost/metaparse/middle_of.hpp>
303 using paren_exp2 = middle_of<lparen_token, plus_exp1, rparen_token>;
305 using primary_exp1 = one_of<int_token, paren_exp2>;
309 using paren_exp3 = middle_of<lparen_token, plus_exp2, rparen_token>;
311 using primary_exp2 = one_of<int_token, paren_exp2>;
314 foldr_start_with_parser<
317 boost::mpl::lambda<boost::mpl::negate<boost::mpl::_1>>::type
321 foldl_start_with_parser<
322 sequence<one_of<times_token, divides_token>, unary_exp2>,
324 boost::mpl::quote2<binary_op>
328 foldl_start_with_parser<
329 sequence<one_of<plus_token, minus_token>, mult_exp5>,
331 boost::mpl::quote2<binary_op>
334 using exp_parser19 = build_parser<plus_exp2>;
335 #include <boost/metaparse/define_error.hpp>
337 BOOST_METAPARSE_DEFINE_ERROR(missing_primary_expression, "Missing primary expression");
341 using paren_exp4 = middle_of<lparen_token, plus_exp3, rparen_token>;
343 #include <boost/metaparse/fail.hpp>
345 using primary_exp3 = one_of<int_token, paren_exp4, fail<missing_primary_expression>>;
348 foldr_start_with_parser<
351 boost::mpl::lambda<boost::mpl::negate<boost::mpl::_1>>::type
355 foldl_start_with_parser<
356 sequence<one_of<times_token, divides_token>, unary_exp3>,
358 boost::mpl::quote2<binary_op>
362 foldl_start_with_parser<
363 sequence<one_of<plus_token, minus_token>, mult_exp6>,
365 boost::mpl::quote2<binary_op>
368 using exp_parser20 = build_parser<plus_exp3>;
369 #include <boost/metaparse/fail_at_first_char_expected.hpp>
371 #include <boost/metaparse/first_of.hpp>
375 foldl_start_with_parser<
376 sequence<one_of<plus_token, minus_token>, mult_exp6>,
378 boost::mpl::quote2<binary_op>
380 fail_at_first_char_expected<
381 sequence<one_of<plus_token, minus_token>, mult_exp6>
385 using exp_parser21 = build_parser<plus_exp4>;