foldr_reject_incomplete.qbk 2.9 KB

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