foldl1.qbk
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
[#foldl1]
[section foldl1]
[h1 Synopsis]
template <class P, class State, class ForwardOp>
struct foldl1;
This is a [link parser_combinator parser combinator].
[table Arguments
[[Name] [Type]]
[[`P`] [[link parser parser]]]
[[`State`] [[link metaprogramming_value template metaprogramming value]]]
[[`ForwardOp`] [[link metafunction_class template metafunction class] taking two arguments]]
]
[h1 Description]
`foldl1` applies `P` on the input string repeatedly as long as `P` accepts the
input. The result of parsing is equivalent to
`boost::mpl::fold<Sequence, State, ForwardOp>`, where `Sequence` is the sequence
of the results of the applications of `P`.
When `P` rejects the input for the first time, `foldl1` rejects it as well. At
least one successful application of `P` is required for `foldl1` to accept the
input.
[h1 Header]
#include <boost/metaparse/foldl1.hpp>
[h1 Expression semantics]
For any `p` parser, `t` class, `f` metafunction class taking two arguments the
following are equivalent:
foldl1<p, t, f>
last_of<look_ahead<p>, foldl<p, t, f>>
[h1 Example]
#include <boost/metaparse/foldl1.hpp>
#include <boost/metaparse/token.hpp>
#include <boost/metaparse/int_.hpp>
#include <boost/metaparse/string.hpp>
#include <boost/metaparse/start.hpp>
#include <boost/metaparse/get_result.hpp>
#include <boost/metaparse/is_error.hpp>
#include <boost/mpl/lambda.hpp>
#include <boost/mpl/plus.hpp>
using namespace boost::metaparse;
using int_token = token<int_>;
using sum_op =
boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type;
using ints = foldl1<int_token, boost::mpl::int_<0>, sum_op>;
static_assert(
get_result<
ints::apply<BOOST_METAPARSE_STRING("11 13 3 21"), start>
>::type::value == 48,
"ints should sum the numbers"
);
static_assert(
is_error<ints::apply<BOOST_METAPARSE_STRING(""), start>>::type::value,
"when no numbers are provided, it should be an error"
);
[endsect]