repeat.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Copyright (c) 2014 Thomas Bernard
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #if !defined(SPIRIT_X3_REPEAT_APRIL_16_2014_0848AM)
  9. #define SPIRIT_X3_REPEAT_APRIL_16_2014_0848AM
  10. #include <boost/spirit/home/x3/core/parser.hpp>
  11. #include <boost/spirit/home/x3/operator/kleene.hpp>
  12. namespace boost { namespace spirit { namespace x3 { namespace detail
  13. {
  14. template <typename T>
  15. struct exact_count // handles repeat(exact)[p]
  16. {
  17. typedef T type;
  18. bool got_max(T i) const { return i >= exact_value; }
  19. bool got_min(T i) const { return i >= exact_value; }
  20. T const exact_value;
  21. };
  22. template <typename T>
  23. struct finite_count // handles repeat(min, max)[p]
  24. {
  25. typedef T type;
  26. bool got_max(T i) const { return i >= max_value; }
  27. bool got_min(T i) const { return i >= min_value; }
  28. T const min_value;
  29. T const max_value;
  30. };
  31. template <typename T>
  32. struct infinite_count // handles repeat(min, inf)[p]
  33. {
  34. typedef T type;
  35. bool got_max(T /*i*/) const { return false; }
  36. bool got_min(T i) const { return i >= min_value; }
  37. T const min_value;
  38. };
  39. }}}}
  40. namespace boost { namespace spirit { namespace x3
  41. {
  42. template<typename Subject, typename RepeatCountLimit>
  43. struct repeat_directive : unary_parser<Subject, repeat_directive<Subject,RepeatCountLimit>>
  44. {
  45. typedef unary_parser<Subject, repeat_directive<Subject,RepeatCountLimit>> base_type;
  46. static bool const is_pass_through_unary = true;
  47. static bool const handles_container = true;
  48. repeat_directive(Subject const& subject, RepeatCountLimit const& repeat_limit_)
  49. : base_type(subject)
  50. , repeat_limit(repeat_limit_)
  51. {}
  52. template<typename Iterator, typename Context
  53. , typename RContext, typename Attribute>
  54. bool parse(
  55. Iterator& first, Iterator const& last
  56. , Context const& context, RContext& rcontext, Attribute& attr) const
  57. {
  58. Iterator local_iterator = first;
  59. typename RepeatCountLimit::type i{};
  60. for (/**/; !repeat_limit.got_min(i); ++i)
  61. {
  62. if (!detail::parse_into_container(
  63. this->subject, local_iterator, last, context, rcontext, attr))
  64. return false;
  65. }
  66. first = local_iterator;
  67. // parse some more up to the maximum specified
  68. for (/**/; !repeat_limit.got_max(i); ++i)
  69. {
  70. if (!detail::parse_into_container(
  71. this->subject, first, last, context, rcontext, attr))
  72. break;
  73. }
  74. return true;
  75. }
  76. RepeatCountLimit repeat_limit;
  77. };
  78. // Infinite loop tag type
  79. struct inf_type {};
  80. const inf_type inf = inf_type();
  81. struct repeat_gen
  82. {
  83. template<typename Subject>
  84. auto operator[](Subject const& subject) const
  85. {
  86. return *as_parser(subject);
  87. }
  88. template <typename T>
  89. struct repeat_gen_lvl1
  90. {
  91. repeat_gen_lvl1(T&& repeat_limit_)
  92. : repeat_limit(repeat_limit_)
  93. {}
  94. template<typename Subject>
  95. repeat_directive< typename extension::as_parser<Subject>::value_type, T>
  96. operator[](Subject const& subject) const
  97. {
  98. return { as_parser(subject),repeat_limit };
  99. }
  100. T repeat_limit;
  101. };
  102. template <typename T>
  103. repeat_gen_lvl1<detail::exact_count<T>>
  104. operator()(T const exact) const
  105. {
  106. return { detail::exact_count<T>{exact} };
  107. }
  108. template <typename T>
  109. repeat_gen_lvl1<detail::finite_count<T>>
  110. operator()(T const min_val, T const max_val) const
  111. {
  112. return { detail::finite_count<T>{min_val,max_val} };
  113. }
  114. template <typename T>
  115. repeat_gen_lvl1<detail::infinite_count<T>>
  116. operator()(T const min_val, inf_type const &) const
  117. {
  118. return { detail::infinite_count<T>{min_val} };
  119. }
  120. };
  121. auto const repeat = repeat_gen{};
  122. }}}
  123. namespace boost { namespace spirit { namespace x3 { namespace traits
  124. {
  125. template <typename Subject, typename RepeatCountLimit, typename Context>
  126. struct attribute_of<x3::repeat_directive<Subject,RepeatCountLimit>, Context>
  127. : build_container<typename attribute_of<Subject, Context>::type> {};
  128. }}}}
  129. #endif