foldr_start_with_parser.qbk 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. [#foldr_start_with_parser]
  2. [section foldr_start_with_parser]
  3. [h1 Synopsis]
  4. template <class P, class StateP, class BackwardOp>
  5. struct foldr_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. [[`BackwardOp`] [[link metafunction_class template metafunction class] taking two arguments]]
  12. ]
  13. [h1 Description]
  14. The same as [link foldr `foldr`], but after folding it applies a parser,
  15. `StateP` on the input. If `StateP` fails, `foldr_start_with_parser` fails. If it
  16. succeeds, the result of parsing is equivalent to
  17. `boost::reverse_fold<Sequence, State, BackwardOp>`, where `Sequence` is the
  18. sequence of the results of the applications of `P` and `State` is the result
  19. `StateP` returned ['after] the repeated application of `P` on the input.
  20. Here is a diagram showing how `foldr_start_with_parser` works by example:
  21. using int_token = token<int_>;
  22. using plus_token = token<lit_c<'+'>>;
  23. using int_plus = first_of<int_token, plus_token>;
  24. using sum_op = mpl::lambda<mpl::plus<mpl::_1, mpl::_2>>::type;
  25. [$images/metaparse/foldr_start_with_parser_diag1.png [width 70%]]
  26. Further details can be found in the
  27. [link introducing-foldr_start_with_parser Introducing foldr_start_with_parser]
  28. section of the [link manual User Manual].
  29. [h1 Header]
  30. #include <boost/metaparse/foldr_start_with_parser.hpp>
  31. [h1 Expression semantics]
  32. For any `p` parser, `pt` class, `f` metafunction class taking two arguments,
  33. `s` compile-time string and `pos` source position let `pos_` be the position
  34. where the repeated application of `p` on `s` fails for the first time. Let
  35. `s_` be the postfix of `s` starting at that position.
  36. foldr_start_with_parser<p, pt, f>::apply<s, pos>
  37. is equivalent to
  38. pt::apply<s_, pos_>
  39. when the above expression returns a parsing error. It is
  40. return_<
  41. foldr<p, get_result<pt::apply<s_, pos_>>::type, f>::apply<s, pos>
  42. >::apply<
  43. get_remaining<pt::apply<s_, pos_>>::type,
  44. get_position<pt::apply<s_, pos_>>::type
  45. >
  46. otherwise.
  47. [h1 Example]
  48. #include <boost/metaparse/foldr_start_with_parser.hpp>
  49. #include <boost/metaparse/lit_c.hpp>
  50. #include <boost/metaparse/first_of.hpp>
  51. #include <boost/metaparse/token.hpp>
  52. #include <boost/metaparse/int_.hpp>
  53. #include <boost/metaparse/string.hpp>
  54. #include <boost/metaparse/start.hpp>
  55. #include <boost/metaparse/get_result.hpp>
  56. #include <boost/metaparse/is_error.hpp>
  57. #include <boost/mpl/lambda.hpp>
  58. #include <boost/mpl/plus.hpp>
  59. using namespace boost::metaparse;
  60. using int_token = token<int_>;
  61. using plus_token = token<lit_c<'+'>>;
  62. using int_plus = first_of<int_token, plus_token>;
  63. using sum_op =
  64. boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type;
  65. using ints = foldr_start_with_parser<int_plus, int_token, sum_op>;
  66. static_assert(
  67. get_result<
  68. ints::apply<BOOST_METAPARSE_STRING("11 + 13 + 3 + 21"), start>
  69. >::type::value == 48,
  70. "ints should sum the numbers"
  71. );
  72. static_assert(
  73. is_error<ints::apply<BOOST_METAPARSE_STRING(""), start>>::type::value,
  74. "when no numbers are provided, it should be an error"
  75. );
  76. [endsect]