]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/metaparse/doc/foldl1.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / metaparse / doc / foldl1.qbk
1 [#foldl1]
2 [section foldl1]
3
4 [h1 Synopsis]
5
6 template <class P, class State, class ForwardOp>
7 struct foldl1;
8
9 This 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 `foldl1` 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`.
24
25 When `P` rejects the input for the first time, `foldl1` rejects it as well. At
26 least one successful application of `P` is required for `foldl1` to accept the
27 input.
28
29 [h1 Header]
30
31 #include <boost/metaparse/foldl1.hpp>
32
33 [h1 Expression semantics]
34
35 For any `p` parser, `t` class, `f` metafunction class taking two arguments the
36 following are equivalent:
37
38 foldl1<p, t, f>
39
40 last_of<look_ahead<p>, foldl<p, t, f>>
41
42 [h1 Example]
43
44 #include <boost/metaparse/foldl1.hpp>
45 #include <boost/metaparse/token.hpp>
46 #include <boost/metaparse/int_.hpp>
47 #include <boost/metaparse/string.hpp>
48 #include <boost/metaparse/start.hpp>
49 #include <boost/metaparse/get_result.hpp>
50 #include <boost/metaparse/is_error.hpp>
51
52 #include <boost/mpl/lambda.hpp>
53 #include <boost/mpl/plus.hpp>
54
55 using namespace boost::metaparse;
56
57 using int_token = token<int_>;
58 using sum_op =
59 boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type;
60
61 using ints = foldl1<int_token, boost::mpl::int_<0>, sum_op>;
62
63 static_assert(
64 get_result<
65 ints::apply<BOOST_METAPARSE_STRING("11 13 3 21"), start>
66 >::type::value == 48,
67 "ints should sum the numbers"
68 );
69
70 static_assert(
71 is_error<ints::apply<BOOST_METAPARSE_STRING(""), start>>::type::value,
72 "when no numbers are provided, it should be an error"
73 );
74
75 [endsect]
76