Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

foldr1

Synopsis

template <class P, class State, class BackwardOp>
struct foldr1;

This is a parser combinator.

Table 26. Arguments

Name

Type

P

parser

State

template metaprogramming value

BackwardOp

template metafunction class taking two arguments


Description

foldr1 applies P on the input string repeatedly as long as P accepts the input. The result of parsing is equivalent to boost::reverse_fold<Sequence, State, BackwardOp>, where Sequence is the sequence of the results of the applications of P.

When P rejects the input for the first time, foldr1 rejects it as well. At least one successful application of P is required for foldr1 to accept the input.

Header

#include <boost/metaparse/foldr1.hpp>

Expression semantics

For any p parser, t class, f metafunction class taking two arguments the following are equivalent:

foldr1<p, t, f>

last_of<look_ahead<p>, foldr<p, t, f>>

Example

#include <boost/metaparse/foldr1.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 = foldr1<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"
);

PrevUpHomeNext