repeat.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #ifndef BOOST_SPIRIT_QI_DIRECTIVE_REPEAT_HPP
  8. #define BOOST_SPIRIT_QI_DIRECTIVE_REPEAT_HPP
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/spirit/home/qi/meta_compiler.hpp>
  13. #include <boost/spirit/home/qi/parser.hpp>
  14. #include <boost/spirit/home/qi/auxiliary/lazy.hpp>
  15. #include <boost/spirit/home/qi/operator/kleene.hpp>
  16. #include <boost/spirit/home/support/container.hpp>
  17. #include <boost/spirit/home/support/common_terminals.hpp>
  18. #include <boost/spirit/home/qi/detail/attributes.hpp>
  19. #include <boost/spirit/home/qi/detail/fail_function.hpp>
  20. #include <boost/spirit/home/qi/detail/pass_container.hpp>
  21. #include <boost/spirit/home/support/info.hpp>
  22. #include <boost/spirit/home/support/has_semantic_action.hpp>
  23. #include <boost/spirit/home/support/handles_container.hpp>
  24. #include <boost/fusion/include/at.hpp>
  25. #include <vector>
  26. namespace boost { namespace spirit
  27. {
  28. ///////////////////////////////////////////////////////////////////////////
  29. // Enablers
  30. ///////////////////////////////////////////////////////////////////////////
  31. template <>
  32. struct use_directive<qi::domain, tag::repeat> // enables repeat[p]
  33. : mpl::true_ {};
  34. template <typename T>
  35. struct use_directive<qi::domain
  36. , terminal_ex<tag::repeat // enables repeat(exact)[p]
  37. , fusion::vector1<T> >
  38. > : mpl::true_ {};
  39. template <typename T>
  40. struct use_directive<qi::domain
  41. , terminal_ex<tag::repeat // enables repeat(min, max)[p]
  42. , fusion::vector2<T, T> >
  43. > : mpl::true_ {};
  44. template <typename T>
  45. struct use_directive<qi::domain
  46. , terminal_ex<tag::repeat // enables repeat(min, inf)[p]
  47. , fusion::vector2<T, inf_type> >
  48. > : mpl::true_ {};
  49. template <> // enables *lazy* repeat(exact)[p]
  50. struct use_lazy_directive<
  51. qi::domain
  52. , tag::repeat
  53. , 1 // arity
  54. > : mpl::true_ {};
  55. template <> // enables *lazy* repeat(min, max)[p]
  56. struct use_lazy_directive< // and repeat(min, inf)[p]
  57. qi::domain
  58. , tag::repeat
  59. , 2 // arity
  60. > : mpl::true_ {};
  61. }}
  62. namespace boost { namespace spirit { namespace qi
  63. {
  64. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  65. using spirit::repeat;
  66. using spirit::inf;
  67. #endif
  68. using spirit::repeat_type;
  69. using spirit::inf_type;
  70. #ifdef _MSC_VER
  71. # pragma warning(push)
  72. # pragma warning(disable: 4512) // assignment operator could not be generated.
  73. #endif
  74. template <typename T>
  75. struct exact_iterator // handles repeat(exact)[p]
  76. {
  77. exact_iterator(T const exact_)
  78. : exact(exact_) {}
  79. typedef T type;
  80. T start() const { return 0; }
  81. bool got_max(T i) const { return i >= exact; }
  82. bool got_min(T i) const { return i >= exact; }
  83. T const exact;
  84. };
  85. template <typename T>
  86. struct finite_iterator // handles repeat(min, max)[p]
  87. {
  88. finite_iterator(T const min_, T const max_)
  89. : min BOOST_PREVENT_MACRO_SUBSTITUTION (min_)
  90. , max BOOST_PREVENT_MACRO_SUBSTITUTION (max_) {}
  91. typedef T type;
  92. T start() const { return 0; }
  93. bool got_max(T i) const { return i >= max; }
  94. bool got_min(T i) const { return i >= min; }
  95. T const min;
  96. T const max;
  97. };
  98. template <typename T>
  99. struct infinite_iterator // handles repeat(min, inf)[p]
  100. {
  101. infinite_iterator(T const min_)
  102. : min BOOST_PREVENT_MACRO_SUBSTITUTION (min_) {}
  103. typedef T type;
  104. T start() const { return 0; }
  105. bool got_max(T /*i*/) const { return false; }
  106. bool got_min(T i) const { return i >= min; }
  107. T const min;
  108. };
  109. #ifdef _MSC_VER
  110. # pragma warning(pop)
  111. #endif
  112. template <typename Subject, typename LoopIter>
  113. struct repeat_parser : unary_parser<repeat_parser<Subject, LoopIter> >
  114. {
  115. typedef Subject subject_type;
  116. template <typename Context, typename Iterator>
  117. struct attribute
  118. {
  119. // Build a std::vector from the subject's attribute. Note
  120. // that build_std_vector may return unused_type if the
  121. // subject's attribute is an unused_type.
  122. typedef typename
  123. traits::build_std_vector<
  124. typename traits::attribute_of<
  125. Subject, Context, Iterator>::type
  126. >::type
  127. type;
  128. };
  129. repeat_parser(Subject const& subject_, LoopIter const& iter_)
  130. : subject(subject_), iter(iter_) {}
  131. template <typename F>
  132. bool parse_container(F f) const
  133. {
  134. typename LoopIter::type i = iter.start();
  135. for (/**/; !iter.got_min(i); ++i)
  136. {
  137. if (f (subject))
  138. return false;
  139. }
  140. // parse some more up to the maximum specified
  141. typename F::iterator_type save = f.f.first;
  142. for (/**/; !iter.got_max(i); ++i)
  143. {
  144. if (f (subject))
  145. break;
  146. save = f.f.first;
  147. }
  148. f.f.first = save;
  149. return true;
  150. }
  151. template <typename Iterator, typename Context
  152. , typename Skipper, typename Attribute>
  153. bool parse(Iterator& first, Iterator const& last
  154. , Context& context, Skipper const& skipper
  155. , Attribute& attr_) const
  156. {
  157. typedef detail::fail_function<Iterator, Context, Skipper>
  158. fail_function;
  159. // ensure the attribute is actually a container type
  160. traits::make_container(attr_);
  161. Iterator iter_local = first;
  162. fail_function f(iter_local, last, context, skipper);
  163. if (!parse_container(detail::make_pass_container(f, attr_)))
  164. return false;
  165. first = f.first;
  166. return true;
  167. }
  168. template <typename Context>
  169. info what(Context& context) const
  170. {
  171. return info("repeat", subject.what(context));
  172. }
  173. Subject subject;
  174. LoopIter iter;
  175. };
  176. ///////////////////////////////////////////////////////////////////////////
  177. // Parser generators: make_xxx function (objects)
  178. ///////////////////////////////////////////////////////////////////////////
  179. template <typename Subject, typename Modifiers>
  180. struct make_directive<tag::repeat, Subject, Modifiers>
  181. {
  182. typedef kleene<Subject> result_type;
  183. result_type operator()(unused_type, Subject const& subject, unused_type) const
  184. {
  185. return result_type(subject);
  186. }
  187. };
  188. template <typename T, typename Subject, typename Modifiers>
  189. struct make_directive<
  190. terminal_ex<tag::repeat, fusion::vector1<T> >, Subject, Modifiers>
  191. {
  192. typedef exact_iterator<T> iterator_type;
  193. typedef repeat_parser<Subject, iterator_type> result_type;
  194. template <typename Terminal>
  195. result_type operator()(
  196. Terminal const& term, Subject const& subject, unused_type) const
  197. {
  198. return result_type(subject, fusion::at_c<0>(term.args));
  199. }
  200. };
  201. template <typename T, typename Subject, typename Modifiers>
  202. struct make_directive<
  203. terminal_ex<tag::repeat, fusion::vector2<T, T> >, Subject, Modifiers>
  204. {
  205. typedef finite_iterator<T> iterator_type;
  206. typedef repeat_parser<Subject, iterator_type> result_type;
  207. template <typename Terminal>
  208. result_type operator()(
  209. Terminal const& term, Subject const& subject, unused_type) const
  210. {
  211. return result_type(subject,
  212. iterator_type(
  213. fusion::at_c<0>(term.args)
  214. , fusion::at_c<1>(term.args)
  215. )
  216. );
  217. }
  218. };
  219. template <typename T, typename Subject, typename Modifiers>
  220. struct make_directive<
  221. terminal_ex<tag::repeat
  222. , fusion::vector2<T, inf_type> >, Subject, Modifiers>
  223. {
  224. typedef infinite_iterator<T> iterator_type;
  225. typedef repeat_parser<Subject, iterator_type> result_type;
  226. template <typename Terminal>
  227. result_type operator()(
  228. Terminal const& term, Subject const& subject, unused_type) const
  229. {
  230. return result_type(subject, fusion::at_c<0>(term.args));
  231. }
  232. };
  233. }}}
  234. namespace boost { namespace spirit { namespace traits
  235. {
  236. ///////////////////////////////////////////////////////////////////////////
  237. template <typename Subject, typename LoopIter>
  238. struct has_semantic_action<qi::repeat_parser<Subject, LoopIter> >
  239. : unary_has_semantic_action<Subject> {};
  240. ///////////////////////////////////////////////////////////////////////////
  241. template <typename Subject, typename LoopIter, typename Attribute
  242. , typename Context, typename Iterator>
  243. struct handles_container<qi::repeat_parser<Subject, LoopIter>
  244. , Attribute, Context, Iterator>
  245. : mpl::true_ {};
  246. }}}
  247. #endif