6 template <class P, class State, class ForwardOp>
9 This is a [link parser_combinator parser combinator].
13 [[`P`] [[link parser parser]]]
14 [[`State`] [[link metaprogramming_value template metaprogramming value]]]
15 [[`ForwardOp`] [[link metafunction_class template metafunction class] taking two arguments]]
20 `foldl` applies `P` on the input string repeatedly as long as `P` accepts the
21 input. The result of parsing is equivalent to
22 `boost::mpl::fold<Sequence, State, ForwardOp>`, where `Sequence` is the sequence
23 of the results of the applications of `P`.
25 When `P` rejects the input for the first time, `foldl` still accepts the input
26 and the result of parsing is `State`.
28 Here is a diagram showing how `foldl` works by example:
30 using int_token = token<int_>;
31 using sum_op = mpl::lambda<mpl::plus<mpl::_1, mpl::_2>>::type;
33 [$images/metaparse/foldl_diag2.png [width 70%]]
35 Further details can be found in the [link introducing-foldl Introducing foldl]
36 section of the [link manual User Manual].
40 #include <boost/metaparse/foldl.hpp>
42 [h1 Expression semantics]
44 For any `p` parser, `t` class, `f` metafunction class taking two arguments,
45 `s` compile-time string and `pos` source position
47 foldl<p, t, f>::apply<s, pos>
51 return_<t>::apply<s, pos>
53 when `p::apply<s, pos>` returns an error. It is
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>>
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>
71 #include <boost/mpl/lambda.hpp>
72 #include <boost/mpl/plus.hpp>
74 using namespace boost::metaparse;
76 using int_token = token<int_>;
78 boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type;
80 using ints = foldl<int_token, boost::mpl::int_<0>, sum_op>;
84 ints::apply<BOOST_METAPARSE_STRING("11 13 3 21"), start>
86 "ints should sum the numbers"
91 ints::apply<BOOST_METAPARSE_STRING(""), start>
93 "the sum of no elements is 0"