repeated.qbk 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. [#repeated]
  2. [section repeated]
  3. [h1 Synopsis]
  4. template <class P>
  5. struct repeated;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P`] [[link parser parser]]]
  10. ]
  11. [h1 Description]
  12. It applies `P` on the input string repeatedly as long as `P` accepts the input.
  13. The result of parsing is a sequence of the results of the individual
  14. applications of `P`.
  15. When `P` rejects the input for the first time, `repeated` still accepts the
  16. input and the result of parsing is an empty sequence.
  17. Here is a diagram showing how `repeated` works by example:
  18. using int_token = token<int_>;
  19. [$images/metaparse/repeated_diag1.png [width 70%]]
  20. Further details can be found in the [link repetition Repetition] section
  21. of the [link manual User Manual].
  22. [h1 Header]
  23. #include <boost/metaparse/repeated.hpp>
  24. [h1 Expression semantics]
  25. For any `p` parser the following are equivalent:
  26. repeated<p>
  27. foldl<
  28. p,
  29. /* unspecified empty sequence */,
  30. boost::mpl::push_back<_2, _1>
  31. >
  32. [h1 Example]
  33. #include <boost/metaparse/repeated.hpp>
  34. #include <boost/metaparse/digit_val.hpp>
  35. #include <boost/metaparse/start.hpp>
  36. #include <boost/metaparse/string.hpp>
  37. #include <boost/metaparse/get_result.hpp>
  38. #include <boost/mpl/equal.hpp>
  39. #include <boost/mpl/vector.hpp>
  40. #include <boost/mpl/int.hpp>
  41. using namespace boost::metaparse;
  42. using digits = repeated<digit_val>;
  43. static_assert(
  44. boost::mpl::equal<
  45. get_result<digits::apply<BOOST_METAPARSE_STRING("1234"), start>>::type,
  46. boost::mpl::vector<
  47. boost::mpl::int_<1>,
  48. boost::mpl::int_<2>,
  49. boost::mpl::int_<3>,
  50. boost::mpl::int_<4>
  51. >
  52. >::type::value,
  53. "the result of parsing should be the list of digit values"
  54. );
  55. static_assert(
  56. boost::mpl::equal<
  57. get_result<digits::apply<BOOST_METAPARSE_STRING("x"), start>>::type,
  58. boost::mpl::vector<>
  59. >::type::value,
  60. "repeated should accept the input when it can't parse anything with digit_val"
  61. );
  62. [endsect]