foldl_reject_incomplete.qbk 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. [#foldl_reject_incomplete]
  2. [section foldl_reject_incomplete]
  3. [h1 Synopsis]
  4. template <class P, class State, class ForwardOp>
  5. struct foldl_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. [[`ForwardOp`] [[link metafunction_class template metafunction class] taking two arguments]]
  12. ]
  13. [h1 Description]
  14. The same as [link foldl `foldl`], but once `P` rejects the input,
  15. `foldl_reject_incomplete` checks if `P` consumes any characters before rejecting
  16. the input. If so, `foldl_reject_incomplete` rejects the input with the same
  17. error message this last application of `P` returned. Otherwise
  18. `foldl_reject_incomplete` accepts the input and gives the same result as
  19. [link foldl `foldl`].
  20. Here is a diagram showing how `foldl_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/foldl_reject_incomplete_diag1.png [width 70%]]
  26. [h1 Header]
  27. #include <boost/metaparse/foldl_reject_incomplete.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. foldl_reject_incomplete<p, t, f>::apply<s, pos>
  32. is equivalent to
  33. first_of<foldl<p, t, f>, fail_at_first_char_expected<p> >::apply<s, pos>
  34. [h1 Example]
  35. #include <boost/metaparse/foldl_reject_incomplete.hpp>
  36. #include <boost/metaparse/lit_c.hpp>
  37. #include <boost/metaparse/last_of.hpp>
  38. #include <boost/metaparse/token.hpp>
  39. #include <boost/metaparse/int_.hpp>
  40. #include <boost/metaparse/string.hpp>
  41. #include <boost/metaparse/start.hpp>
  42. #include <boost/metaparse/get_result.hpp>
  43. #include <boost/metaparse/is_error.hpp>
  44. #include <boost/mpl/lambda.hpp>
  45. #include <boost/mpl/plus.hpp>
  46. #include <boost/mpl/int.hpp>
  47. using namespace boost::metaparse;
  48. using int_token = token<int_>;
  49. using plus_token = token<lit_c<'+'>>;
  50. using plus_int = last_of<plus_token, int_token>;
  51. using sum_op =
  52. boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type;
  53. using ints = foldl_reject_incomplete<plus_int, boost::mpl::int_<11>, sum_op>;
  54. static_assert(
  55. get_result<
  56. ints::apply<BOOST_METAPARSE_STRING("+ 13 + 3 + 21"), start>
  57. >::type::value == 48,
  58. "ints should sum the numbers"
  59. );
  60. static_assert(
  61. is_error<
  62. ints::apply<BOOST_METAPARSE_STRING("+ 13 + 3 +"), start>
  63. >::type::value,
  64. "when the last number is missing, it should be an error"
  65. );
  66. [endsect]