sequence_apply.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2015.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/metaparse/sequence_apply.hpp>
  6. #include <boost/metaparse/is_error.hpp>
  7. #include <boost/metaparse/start.hpp>
  8. #include <boost/metaparse/get_result.hpp>
  9. #include <boost/metaparse/always.hpp>
  10. #include <boost/metaparse/one_char.hpp>
  11. #include "common.hpp"
  12. #include <boost/mpl/equal_to.hpp>
  13. #include <boost/mpl/list.hpp>
  14. #include <boost/mpl/at.hpp>
  15. #include <boost/mpl/vector_c.hpp>
  16. #include <boost/mpl/vector.hpp>
  17. #include <boost/mpl/assert.hpp>
  18. #include <boost/mpl/char.hpp>
  19. #include <boost/type_traits/is_same.hpp>
  20. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  21. #include <boost/preprocessor/repetition/enum_params.hpp>
  22. #include <boost/preprocessor/repetition/enum.hpp>
  23. #include <boost/preprocessor/tuple/eat.hpp>
  24. #include <boost/preprocessor/cat.hpp>
  25. #include "test_case.hpp"
  26. namespace
  27. {
  28. #ifdef BOOST_METAPARSE_C_VALUE
  29. # error BOOST_METAPARSE_C_VALUE already defined
  30. #endif
  31. #define BOOST_METAPARSE_C_VALUE(z, n, unused) BOOST_PP_CAT(C, n)::value
  32. #ifdef BOOST_METAPARSE_TEMPLATE
  33. # error BOOST_METAPARSE_TEMPLATE already defined
  34. #endif
  35. #define BOOST_METAPARSE_TEMPLATE(z, n, unused) \
  36. template <BOOST_PP_ENUM(n, char BOOST_PP_TUPLE_EAT(3), ~)> \
  37. struct BOOST_PP_CAT(template_c, n) \
  38. { \
  39. typedef BOOST_PP_CAT(template_c, n) type; \
  40. }; \
  41. \
  42. template <BOOST_PP_ENUM_PARAMS(n, class C)> \
  43. struct BOOST_PP_CAT(template, n) \
  44. { \
  45. typedef \
  46. BOOST_PP_CAT(template_c, n)< \
  47. BOOST_PP_ENUM(n, BOOST_METAPARSE_C_VALUE, ~) \
  48. > \
  49. type; \
  50. };
  51. BOOST_PP_REPEAT_FROM_TO(1, 4, BOOST_METAPARSE_TEMPLATE, ~)
  52. #undef BOOST_METAPARSE_TEMPLATE
  53. #undef BOOST_METAPARSE_C_VALUE
  54. template <class T> struct has_no_type {};
  55. // "is_same<T::type::type, double_eval<T>::type>" - helper tool to avoid
  56. // writing type::type (which is interpreted as the constructor of ::type by
  57. // msvc-7.1)
  58. template <class T> struct double_eval : T::type {};
  59. }
  60. BOOST_METAPARSE_TEST_CASE(sequence_apply)
  61. {
  62. using boost::metaparse::get_result;
  63. using boost::metaparse::sequence_apply1;
  64. using boost::metaparse::sequence_apply2;
  65. using boost::metaparse::sequence_apply3;
  66. using boost::metaparse::start;
  67. using boost::metaparse::is_error;
  68. using boost::metaparse::always;
  69. using boost::metaparse::one_char;
  70. using boost::mpl::list;
  71. using boost::mpl::equal_to;
  72. using boost::mpl::at_c;
  73. using boost::mpl::vector_c;
  74. using boost::mpl::vector;
  75. using boost::mpl::char_;
  76. using boost::is_same;
  77. typedef always<one_char, int> always_int;
  78. // test_one_parser
  79. BOOST_MPL_ASSERT((
  80. is_same<
  81. template_c1<'h'>,
  82. double_eval<
  83. get_result<
  84. sequence_apply1<template1, lit_h>::apply<str_hello, start>
  85. >
  86. >::type
  87. >
  88. ));
  89. // test_one_failing_parser
  90. BOOST_MPL_ASSERT((
  91. is_error<sequence_apply1<template1, lit_e>::apply<str_hello, start> >
  92. ));
  93. // test_two_chars
  94. BOOST_MPL_ASSERT((
  95. is_same<
  96. template_c2<'h', 'e'>,
  97. double_eval<
  98. get_result<
  99. sequence_apply2<template2, lit_h, lit_e>::apply<str_hello, start>
  100. >
  101. >::type
  102. >
  103. ));
  104. // test_first_fails
  105. BOOST_MPL_ASSERT((
  106. is_error<sequence_apply2<template2, lit_x, lit_e>::apply<str_hello, start> >
  107. ));
  108. // test_second_fails
  109. BOOST_MPL_ASSERT((
  110. is_error<sequence_apply2<template2, lit_h, lit_x>::apply<str_hello, start> >
  111. ));
  112. // test_empty_input
  113. BOOST_MPL_ASSERT((
  114. is_error<sequence_apply2<template2, lit_h, lit_e>::apply<str_,start> >
  115. ));
  116. // test_three_chars
  117. BOOST_MPL_ASSERT((
  118. is_same<
  119. template_c3<'h', 'e', 'l'>,
  120. double_eval<
  121. get_result<
  122. sequence_apply3<template3, lit_h, lit_e, lit_l>
  123. ::apply<str_hello, start>
  124. >
  125. >::type
  126. >
  127. ));
  128. // test_no_extra_evaluation
  129. BOOST_MPL_ASSERT((
  130. is_same<
  131. has_no_type<int>,
  132. get_result<
  133. sequence_apply1<has_no_type, always_int>::apply<str_ca, start>
  134. >::type
  135. >
  136. ));
  137. }