foldr.qbk 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. [#foldr]
  2. [section foldr]
  3. [h1 Synopsis]
  4. template <class P, class State, class BackwardOp>
  5. struct foldr;
  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. `foldr` 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, `foldr` still accepts the input
  19. and the result of parsing is `State`.
  20. Here is a diagram showing how `foldr` works by example:
  21. using int_token = token<int_>;
  22. using sum_op = mpl::lambda<mpl::plus<mpl::_1, mpl::_2>>::type;
  23. [$images/metaparse/foldr_diag1.png [width 70%]]
  24. Further details can be found in the [link introducing-foldr Introducing foldr]
  25. section of the [link manual User Manual].
  26. [h1 Header]
  27. #include <boost/metaparse/foldr.hpp>
  28. [h1 Expression semantics]
  29. For any `p` parser, `t` class, `f` metafunction class taking two arguments,
  30. `s` compile-time string and `pos` source position
  31. foldr<p, t, f>::apply<s, pos>
  32. is equivalent to
  33. return_<t>::apply<s, pos>
  34. when `p::apply<s, pos>` returns an error. It is
  35. f::apply<
  36. get_result<
  37. foldr<p, t, f>::apply<
  38. get_remaining<p::apply<s, pos>>,
  39. get_position<p::apply<s, pos>>
  40. >
  41. >::type,
  42. get_result<p::apply<s, pos>>::type
  43. >
  44. otherwise.
  45. [h1 Example]
  46. #include <boost/metaparse/foldr.hpp>
  47. #include <boost/metaparse/token.hpp>
  48. #include <boost/metaparse/int_.hpp>
  49. #include <boost/metaparse/string.hpp>
  50. #include <boost/metaparse/start.hpp>
  51. #include <boost/metaparse/get_result.hpp>
  52. #include <boost/mpl/lambda.hpp>
  53. #include <boost/mpl/plus.hpp>
  54. using namespace boost::metaparse;
  55. using int_token = token<int_>;
  56. using sum_op =
  57. boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type;
  58. using ints = foldr<int_token, boost::mpl::int_<0>, sum_op>;
  59. static_assert(
  60. get_result<
  61. ints::apply<BOOST_METAPARSE_STRING("11 13 3 21"), start>
  62. >::type::value == 48,
  63. "ints should sum the numbers"
  64. );
  65. static_assert(
  66. get_result<
  67. ints::apply<BOOST_METAPARSE_STRING(""), start>
  68. >::type::value == 0,
  69. "the sum of no elements is 0"
  70. );
  71. [endsect]