1[#foldl] 2[section foldl] 3 4[h1 Synopsis] 5 6 template <class P, class State, class ForwardOp> 7 struct foldl; 8 9This is a [link parser_combinator parser combinator]. 10 11[table Arguments 12 [[Name] [Type]] 13 [[`P`] [[link parser parser]]] 14 [[`State`] [[link metaprogramming_value template metaprogramming value]]] 15 [[`ForwardOp`] [[link metafunction_class template metafunction class] taking two arguments]] 16] 17 18[h1 Description] 19 20`foldl` applies `P` on the input string repeatedly as long as `P` accepts the 21input. The result of parsing is equivalent to 22`boost::mpl::fold<Sequence, State, ForwardOp>`, where `Sequence` is the sequence 23of the results of the applications of `P`. 24 25When `P` rejects the input for the first time, `foldl` still accepts the input 26and the result of parsing is `State`. 27 28Here is a diagram showing how `foldl` works by example: 29 30 using int_token = token<int_>; 31 using sum_op = mpl::lambda<mpl::plus<mpl::_1, mpl::_2>>::type; 32 33[$images/metaparse/foldl_diag2.png [width 70%]] 34 35Further details can be found in the [link introducing-foldl Introducing foldl] 36section of the [link manual User Manual]. 37 38[h1 Header] 39 40 #include <boost/metaparse/foldl.hpp> 41 42[h1 Expression semantics] 43 44For any `p` parser, `t` class, `f` metafunction class taking two arguments, 45`s` compile-time string and `pos` source position 46 47 foldl<p, t, f>::apply<s, pos> 48 49is equivalent to 50 51 return_<t>::apply<s, pos> 52 53when `p::apply<s, pos>` returns an error. It is 54 55 foldl<p, f::apply<t, get_result<p::apply<s, pos>>::type>::type, f>::apply< 56 get_remaining<p::apply<s, pos>>, 57 get_position<p::apply<s, pos>> 58 > 59 60otherwise. 61 62[h1 Example] 63 64 #include <boost/metaparse/foldl.hpp> 65 #include <boost/metaparse/token.hpp> 66 #include <boost/metaparse/int_.hpp> 67 #include <boost/metaparse/string.hpp> 68 #include <boost/metaparse/start.hpp> 69 #include <boost/metaparse/get_result.hpp> 70 71 #include <boost/mpl/lambda.hpp> 72 #include <boost/mpl/plus.hpp> 73 74 using namespace boost::metaparse; 75 76 using int_token = token<int_>; 77 using sum_op = 78 boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type; 79 80 using ints = foldl<int_token, boost::mpl::int_<0>, sum_op>; 81 82 static_assert( 83 get_result< 84 ints::apply<BOOST_METAPARSE_STRING("11 13 3 21"), start> 85 >::type::value == 48, 86 "ints should sum the numbers" 87 ); 88 89 static_assert( 90 get_result< 91 ints::apply<BOOST_METAPARSE_STRING(""), start> 92 >::type::value == 0, 93 "the sum of no elements is 0" 94 ); 95 96[endsect] 97 98