iterate_c.qbk 2.0 KB

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