2 ['Definitions before section 9.]
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>