• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[#foldr_reject_incomplete]
2[section foldr_reject_incomplete]
3
4[h1 Synopsis]
5
6  template <class P, class State, class BackwardOp>
7  struct foldr_reject_incomplete;
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
20The same as [link foldr `foldr`], but once `P` rejects the input,
21`foldr_reject_incomplete` checks if `P` consumes any characters before rejecting
22the input. If so, `foldr_reject_incomplete` rejects the input with the same
23error message this last application of `P` returned. Otherwise
24`foldr_reject_incomplete` accepts the input and gives the same result as
25[link foldr `foldr`].
26
27Here is a diagram showing how `foldr_reject_incomplete` works by example:
28
29  using int_token = token<int_>;
30  using plus_token = token<lit_c<'+'>>;
31  using plus_int = last_of<plus_token, int_token>;
32  using sum_op = mpl::lambda<mpl::plus<mpl::_1, mpl::_2>>::type;
33
34[$images/metaparse/foldr_reject_incomplete_diag1.png [width 70%]]
35
36Note that `foldr_reject_incomplete` folds from right to left and therefore does
37not start folding until it reaches the end of the sequence. Since at the end of
38the sequence it finds an error, folding does not happen at all.
39
40[h1 Header]
41
42  #include <boost/metaparse/foldr_reject_incomplete.hpp>
43
44[h1 Expression semantics]
45
46For any `p` parser, `t` class, `f` metafunction class taking two arguments,
47`s` compile-time string and `pos` source position
48
49  foldr_reject_incomplete<p, t, f>::apply<s, pos>
50
51is equivalent to
52
53  first_of<foldr<p, t, f>, fail_at_first_char_expected<p> >::apply<s, pos>
54
55[h1 Example]
56
57  #include <boost/metaparse/foldr_reject_incomplete.hpp>
58  #include <boost/metaparse/lit_c.hpp>
59  #include <boost/metaparse/last_of.hpp>
60  #include <boost/metaparse/token.hpp>
61  #include <boost/metaparse/int_.hpp>
62  #include <boost/metaparse/string.hpp>
63  #include <boost/metaparse/start.hpp>
64  #include <boost/metaparse/get_result.hpp>
65  #include <boost/metaparse/is_error.hpp>
66
67  #include <boost/mpl/lambda.hpp>
68  #include <boost/mpl/plus.hpp>
69  #include <boost/mpl/int.hpp>
70
71  using namespace boost::metaparse;
72
73  using int_token = token<int_>;
74  using plus_token = token<lit_c<'+'>>;
75  using plus_int = last_of<plus_token, int_token>;
76  using sum_op =
77    boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type;
78
79  using ints = foldr_reject_incomplete<plus_int, boost::mpl::int_<11>, sum_op>;
80
81  static_assert(
82    get_result<
83      ints::apply<BOOST_METAPARSE_STRING("+ 13 + 3 + 21"), start>
84    >::type::value == 48,
85    "ints should sum the numbers"
86  );
87
88  static_assert(
89    is_error<
90      ints::apply<BOOST_METAPARSE_STRING("+ 13 + 3 +"), start>
91    >::type::value,
92    "when the last number is missing, it should be an error"
93  );
94
95[endsect]
96
97