foldl_reject_incomplete_start_with_parser.qbk 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. [#foldl_reject_incomplete_start_with_parser]
  2. [section foldl_reject_incomplete_start_with_parser]
  3. [h1 Synopsis]
  4. template <class P, class StateP, class ForwardOp>
  5. struct foldl_reject_incomplete_start_with_parser;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P`] [[link parser parser]]]
  10. [[`StateP`] [[link parser parser]]]
  11. [[`ForwardOp`] [[link metafunction_class template metafunction class] taking two arguments]]
  12. ]
  13. [h1 Description]
  14. The same as [link foldl_start_with_parser `foldl_start_with_parser`], but once
  15. `P` rejects the input, `foldl_reject_incomplete_start_with_parser` checks if `P`
  16. consumes any characters before rejecting the input. If so,
  17. `foldl_reject_incomplete_start_with_parser` rejects the input with the same
  18. error message this last application of `P` returned. Otherwise
  19. `foldl_reject_incomplete_start_with_parser` accepts the input and gives the same
  20. result as [link foldl_start_with_parser `foldl_start_with_parser`].
  21. Here is a diagram showing how `foldl_reject_incomplete_start_with_parser` works
  22. by example:
  23. using int_token = token<int_>;
  24. using plus_token = token<lit_c<'+'>>;
  25. using plus_int = last_of<plus_token, int_token>;
  26. using sum_op = mpl::lambda<mpl::plus<mpl::_1, mpl::_2>>::type;
  27. [$images/metaparse/foldl_reject_incomplete_start_with_parser_diag1.png [width 70%]]
  28. Further details can be found in the
  29. [link introducing-foldl_reject_incomplete_start_with_parser Introducing foldl_reject_incomplete_start_with_parser]
  30. section of the [link manual User Manual].
  31. [h1 Header]
  32. #include <boost/metaparse/foldl_reject_incomplete_start_with_parser.hpp>
  33. [h1 Expression semantics]
  34. For any `p` parser, `pt` class, `f` metafunction class taking two arguments,
  35. `s` compile-time string and `pos` source position
  36. foldl_reject_incomplete_start_with_parser<p, pt, f>::apply<s, pos>
  37. is equivalent to
  38. first_of<
  39. foldl_start_with_parser<p, pt, f>,
  40. fail_at_first_char_expected<p>
  41. >::apply<s, pos>
  42. [h1 Example]
  43. #include <boost/metaparse/foldl_reject_incomplete_start_with_parser.hpp>
  44. #include <boost/metaparse/lit_c.hpp>
  45. #include <boost/metaparse/last_of.hpp>
  46. #include <boost/metaparse/token.hpp>
  47. #include <boost/metaparse/int_.hpp>
  48. #include <boost/metaparse/string.hpp>
  49. #include <boost/metaparse/start.hpp>
  50. #include <boost/metaparse/get_result.hpp>
  51. #include <boost/metaparse/is_error.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 plus_token = token<lit_c<'+'>>;
  57. using plus_int = last_of<plus_token, int_token>;
  58. using sum_op =
  59. boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type;
  60. using ints =
  61. foldl_reject_incomplete_start_with_parser<plus_int, int_token, sum_op>;
  62. static_assert(
  63. get_result<
  64. ints::apply<BOOST_METAPARSE_STRING("11 + 13 + 3 + 21"), start>
  65. >::type::value == 48,
  66. "ints should sum the numbers"
  67. );
  68. static_assert(
  69. is_error<
  70. ints::apply<BOOST_METAPARSE_STRING("11 + 13 + 3 +"), start>
  71. >::type::value,
  72. "when the last number is missing, it should be an error"
  73. );
  74. [endsect]