foldr_reject_incomplete1.qbk 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. [#foldr_reject_incomplete1]
  2. [section foldr_reject_incomplete1]
  3. [h1 Synopsis]
  4. template <class P, class State, class BackwardOp>
  5. struct foldr_reject_incomplete1;
  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 foldr1 `foldr1`], but once `P` rejects the input,
  15. `foldr_reject_incomplete1` checks if `P` consumes any characters before
  16. rejecting the input. If so, `foldr_reject_incomplete1` rejects the input with
  17. the same error message this last application of `P` returned. Otherwise
  18. `foldr_reject_incomplete1` accepts the input and gives the same result as
  19. [link foldr1 `foldr1`].
  20. [h1 Header]
  21. #include <boost/metaparse/foldr_reject_incomplete1.hpp>
  22. [h1 Expression semantics]
  23. For any `p` parser, `t` class, `f` metafunction class taking two arguments,
  24. `s` compile-time string and `pos` source position
  25. foldr_reject_incomplete1<p, t, f>::apply<s, pos>
  26. is equivalent to
  27. first_of<foldr1<p, t, f>, fail_at_first_char_expected<p> >::apply<s, pos>
  28. [h1 Example]
  29. #include <boost/metaparse/foldr_reject_incomplete1.hpp>
  30. #include <boost/metaparse/lit_c.hpp>
  31. #include <boost/metaparse/last_of.hpp>
  32. #include <boost/metaparse/token.hpp>
  33. #include <boost/metaparse/int_.hpp>
  34. #include <boost/metaparse/string.hpp>
  35. #include <boost/metaparse/start.hpp>
  36. #include <boost/metaparse/get_result.hpp>
  37. #include <boost/metaparse/is_error.hpp>
  38. #include <boost/mpl/lambda.hpp>
  39. #include <boost/mpl/plus.hpp>
  40. #include <boost/mpl/int.hpp>
  41. using namespace boost::metaparse;
  42. using int_token = token<int_>;
  43. using plus_token = token<lit_c<'+'>>;
  44. using plus_int = last_of<plus_token, int_token>;
  45. using sum_op =
  46. boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type;
  47. using ints = foldr_reject_incomplete1<plus_int, boost::mpl::int_<11>, sum_op>;
  48. static_assert(
  49. get_result<
  50. ints::apply<BOOST_METAPARSE_STRING("+ 13 + 3 + 21"), start>
  51. >::type::value == 48,
  52. "ints should sum the numbers"
  53. );
  54. static_assert(
  55. is_error<
  56. ints::apply<BOOST_METAPARSE_STRING("+ 13 + 3 +"), start>
  57. >::type::value,
  58. "when the last number is missing, it should be an error"
  59. );
  60. static_assert(
  61. is_error<ints::apply<BOOST_METAPARSE_STRING(""), start>>::type::value,
  62. "when no numbers are provided, it should be an error"
  63. );
  64. [endsect]