foldl_start_with_parser.qbk 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. [#foldl_start_with_parser]
  2. [section foldl_start_with_parser]
  3. [h1 Synopsis]
  4. template <class P, class StateP, class ForwardOp>
  5. struct foldl_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 `foldl`], but before folding it applies a parser,
  15. `StateP` on the input. If it fails, `foldl_start_with_parser` fails. If it
  16. succeeds, `foldl_start_with_parser` works as `foldl` taking the result of
  17. `StateP` as the initial state.
  18. Here is a diagram showing how `foldl_start_with_parser` works by example:
  19. using int_token = token<int_>;
  20. using plus_token = token<lit_c<'+'>>;
  21. using plus_int = last_of<plus_token, int_token>;
  22. using sum_op = mpl::lambda<mpl::plus<mpl::_1, mpl::_2>>::type;
  23. [$images/metaparse/foldl_start_with_parser_diag1.png [width 70%]]
  24. Further details can be found in the
  25. [link introducing-foldl_start_with_parser Introducing foldl_start_with_parser]
  26. section of the [link manual User Manual].
  27. [h1 Header]
  28. #include <boost/metaparse/foldl_start_with_parser.hpp>
  29. [h1 Expression semantics]
  30. For any `p` parser, `pt` class, `f` metafunction class taking two arguments,
  31. `s` compile-time string and `pos` source position
  32. foldl_start_with_parser<p, pt, f>::apply<s, pos>
  33. is equivalent to
  34. pt::apply<s, pos>
  35. when the above expression returns a parsing error. It is
  36. foldl<p, get_result<pt::apply<s, pos>>::type, f>::apply<
  37. get_remaining<pt::apply<s, pos>>::type,
  38. get_position<pt::apply<s, pos>>::type
  39. >
  40. otherwise.
  41. [h1 Example]
  42. #include <boost/metaparse/foldl_start_with_parser.hpp>
  43. #include <boost/metaparse/lit_c.hpp>
  44. #include <boost/metaparse/last_of.hpp>
  45. #include <boost/metaparse/token.hpp>
  46. #include <boost/metaparse/int_.hpp>
  47. #include <boost/metaparse/string.hpp>
  48. #include <boost/metaparse/start.hpp>
  49. #include <boost/metaparse/get_result.hpp>
  50. #include <boost/metaparse/is_error.hpp>
  51. #include <boost/mpl/lambda.hpp>
  52. #include <boost/mpl/plus.hpp>
  53. using namespace boost::metaparse;
  54. using int_token = token<int_>;
  55. using plus_token = token<lit_c<'+'>>;
  56. using plus_int = last_of<plus_token, int_token>;
  57. using sum_op =
  58. boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type;
  59. using ints = foldl_start_with_parser<plus_int, int_token, sum_op>;
  60. static_assert(
  61. get_result<
  62. ints::apply<BOOST_METAPARSE_STRING("11 + 13 + 3 + 21"), start>
  63. >::type::value == 48,
  64. "ints should sum the numbers"
  65. );
  66. static_assert(
  67. is_error<ints::apply<BOOST_METAPARSE_STRING(""), start>>::type::value,
  68. "when no numbers are provided, it should be an error"
  69. );
  70. [endsect]