1[#before_5_2_4] 2['Definitions before section 5.2.4.] 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 146