• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[#foldr1]
2[section foldr1]
3
4[h1 Synopsis]
5
6  template <class P, class State, class BackwardOp>
7  struct foldr1;
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  [[`BackwardOp`] [[link metafunction_class template metafunction class] taking two arguments]]
16]
17
18[h1 Description]
19
20`foldr1` applies `P` on the input string repeatedly as long as `P` accepts the
21input. The result of parsing is equivalent to
22`boost::reverse_fold<Sequence, State, BackwardOp>`, where `Sequence` is the
23sequence of the results of the applications of `P`.
24
25When `P` rejects the input for the first time, `foldr1` rejects it as well. At
26least one successful application of `P` is required for `foldr1` to accept the
27input.
28
29[h1 Header]
30
31  #include <boost/metaparse/foldr1.hpp>
32
33[h1 Expression semantics]
34
35For any `p` parser, `t` class, `f` metafunction class taking two arguments the
36following are equivalent:
37
38  foldr1<p, t, f>
39
40  last_of<look_ahead<p>, foldr<p, t, f>>
41
42[h1 Example]
43
44  #include <boost/metaparse/foldr1.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 = foldr1<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
77