foldr1.qbk 2.0 KB

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