iterate.qbk 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. [#iterate]
  2. [section iterate]
  3. [h1 Synopsis]
  4. template <class P, class N>
  5. struct iterate;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P`] [[link parser parser]]]
  10. [[`N`] [[link boxed_value boxed] non-negative integer value]]
  11. ]
  12. [h1 Description]
  13. It applies `P` on the input string `N` times. The result of parsing is a
  14. sequence of the results of the individual applications of `P`. `P` has to accept
  15. the input `N` times for `iterate` to accept it.
  16. [h1 Header]
  17. #include <boost/metaparse/iterate.hpp>
  18. [h1 Expression semantics]
  19. For any `p` parser, `n` wrapped integer the following are equivalent:
  20. iterate<p, n>
  21. iterate_c<p, n::type::value>
  22. [h1 Example]
  23. #include <boost/metaparse/iterate.hpp>
  24. #include <boost/metaparse/digit.hpp>
  25. #include <boost/metaparse/string.hpp>
  26. #include <boost/metaparse/start.hpp>
  27. #include <boost/metaparse/get_result.hpp>
  28. #include <boost/metaparse/is_error.hpp>
  29. #include <boost/mpl/vector.hpp>
  30. #include <boost/mpl/equal.hpp>
  31. #include <boost/mpl/char.hpp>
  32. #include <type_traits>
  33. using namespace boost::metaparse;
  34. static_assert(
  35. boost::mpl::equal<
  36. boost::mpl::vector<
  37. boost::mpl::char_<'1'>,
  38. boost::mpl::char_<'2'>,
  39. boost::mpl::char_<'3'>
  40. >,
  41. get_result<
  42. iterate<digit, std::integral_constant<int, 3>>::apply<
  43. BOOST_METAPARSE_STRING("123"),
  44. start
  45. >
  46. >::type
  47. >::type::value,
  48. "the result should be the sequence of the individual applications of digit"
  49. );
  50. static_assert(
  51. boost::mpl::equal<
  52. boost::mpl::vector<
  53. boost::mpl::char_<'1'>,
  54. boost::mpl::char_<'2'>,
  55. boost::mpl::char_<'3'>
  56. >,
  57. get_result<
  58. iterate<digit, std::integral_constant<int, 3>>::apply<
  59. BOOST_METAPARSE_STRING("1234"),
  60. start
  61. >
  62. >::type
  63. >::type::value,
  64. "only three iterations should be made"
  65. );
  66. static_assert(
  67. is_error<
  68. iterate<digit, std::integral_constant<int, 3>>::apply<
  69. BOOST_METAPARSE_STRING("12"),
  70. start
  71. >
  72. >::type::value,
  73. "it should fail when digit can not be applied three times"
  74. );
  75. [endsect]