repeat.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2001-2011 Joel de Guzman
  3. //
  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. #if !defined(SPIRIT_KARMA_REPEAT_MAY_18_2009_0926AM)
  7. #define SPIRIT_KARMA_REPEAT_MAY_18_2009_0926AM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/karma/meta_compiler.hpp>
  12. #include <boost/spirit/home/karma/detail/output_iterator.hpp>
  13. #include <boost/spirit/home/karma/detail/get_stricttag.hpp>
  14. #include <boost/spirit/home/karma/generator.hpp>
  15. #include <boost/spirit/home/karma/auxiliary/lazy.hpp>
  16. #include <boost/spirit/home/karma/operator/kleene.hpp>
  17. #include <boost/spirit/home/support/container.hpp>
  18. #include <boost/spirit/home/support/common_terminals.hpp>
  19. #include <boost/spirit/home/support/has_semantic_action.hpp>
  20. #include <boost/spirit/home/support/handles_container.hpp>
  21. #include <boost/spirit/home/karma/detail/attributes.hpp>
  22. #include <boost/spirit/home/support/info.hpp>
  23. #include <boost/fusion/include/at.hpp>
  24. namespace boost { namespace spirit
  25. {
  26. ///////////////////////////////////////////////////////////////////////////
  27. // Enablers
  28. ///////////////////////////////////////////////////////////////////////////
  29. template <>
  30. struct use_directive<karma::domain, tag::repeat> // enables repeat[p]
  31. : mpl::true_ {};
  32. template <typename T>
  33. struct use_directive<karma::domain
  34. , terminal_ex<tag::repeat // enables repeat(exact)[p]
  35. , fusion::vector1<T> >
  36. > : mpl::true_ {};
  37. template <typename T>
  38. struct use_directive<karma::domain
  39. , terminal_ex<tag::repeat // enables repeat(min, max)[p]
  40. , fusion::vector2<T, T> >
  41. > : mpl::true_ {};
  42. template <typename T>
  43. struct use_directive<karma::domain
  44. , terminal_ex<tag::repeat // enables repeat(min, inf)[p]
  45. , fusion::vector2<T, inf_type> >
  46. > : mpl::true_ {};
  47. template <> // enables *lazy* repeat(exact)[p]
  48. struct use_lazy_directive<
  49. karma::domain
  50. , tag::repeat
  51. , 1 // arity
  52. > : mpl::true_ {};
  53. template <> // enables *lazy* repeat(min, max)[p]
  54. struct use_lazy_directive< // and repeat(min, inf)[p]
  55. karma::domain
  56. , tag::repeat
  57. , 2 // arity
  58. > : mpl::true_ {};
  59. }}
  60. namespace boost { namespace spirit { namespace karma
  61. {
  62. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  63. using spirit::repeat;
  64. using spirit::inf;
  65. #endif
  66. using spirit::repeat_type;
  67. using spirit::inf_type;
  68. ///////////////////////////////////////////////////////////////////////////
  69. // handles repeat(exact)[p]
  70. template <typename T>
  71. struct exact_iterator
  72. {
  73. exact_iterator(T const exact)
  74. : exact(exact) {}
  75. typedef T type;
  76. T start() const { return 0; }
  77. bool got_max(T i) const { return i >= exact; }
  78. bool got_min(T i) const { return i >= exact; }
  79. T const exact;
  80. // silence MSVC warning C4512: assignment operator could not be generated
  81. BOOST_DELETED_FUNCTION(exact_iterator& operator= (exact_iterator const&))
  82. };
  83. // handles repeat(min, max)[p]
  84. template <typename T>
  85. struct finite_iterator
  86. {
  87. finite_iterator(T const min, T const max)
  88. : min BOOST_PREVENT_MACRO_SUBSTITUTION (min)
  89. , max BOOST_PREVENT_MACRO_SUBSTITUTION (max) {}
  90. typedef T type;
  91. T start() const { return 0; }
  92. bool got_max(T i) const { return i >= max; }
  93. bool got_min(T i) const { return i >= min; }
  94. T const min;
  95. T const max;
  96. // silence MSVC warning C4512: assignment operator could not be generated
  97. BOOST_DELETED_FUNCTION(finite_iterator& operator= (finite_iterator const&))
  98. };
  99. // handles repeat(min, inf)[p]
  100. template <typename T>
  101. struct infinite_iterator
  102. {
  103. infinite_iterator(T const min)
  104. : min BOOST_PREVENT_MACRO_SUBSTITUTION (min) {}
  105. typedef T type;
  106. T start() const { return 0; }
  107. bool got_max(T /*i*/) const { return false; }
  108. bool got_min(T i) const { return i >= min; }
  109. T const min;
  110. // silence MSVC warning C4512: assignment operator could not be generated
  111. BOOST_DELETED_FUNCTION(infinite_iterator& operator= (infinite_iterator const&))
  112. };
  113. ///////////////////////////////////////////////////////////////////////////
  114. template <typename Subject, typename LoopIter, typename Strict
  115. , typename Derived>
  116. struct base_repeat_generator : unary_generator<Derived>
  117. {
  118. private:
  119. // iterate over the given container until its exhausted or the embedded
  120. // generator succeeds
  121. template <typename F, typename Attribute>
  122. bool generate_subject(F f, Attribute const&, mpl::false_) const
  123. {
  124. // Failing subject generators are just skipped. This allows to
  125. // selectively generate items in the provided attribute.
  126. while (!f.is_at_end())
  127. {
  128. bool r = !f(subject);
  129. if (r)
  130. return true;
  131. if (!f.is_at_end())
  132. f.next();
  133. }
  134. return false;
  135. }
  136. template <typename F, typename Attribute>
  137. bool generate_subject(F f, Attribute const&, mpl::true_) const
  138. {
  139. return !f(subject);
  140. }
  141. // There is no way to distinguish a failed generator from a
  142. // generator to be skipped. We assume the user takes responsibility
  143. // for ending the loop if no attribute is specified.
  144. template <typename F>
  145. bool generate_subject(F f, unused_type, mpl::false_) const
  146. {
  147. return !f(subject);
  148. }
  149. public:
  150. typedef Subject subject_type;
  151. typedef mpl::int_<subject_type::properties::value> properties;
  152. // Build a std::vector from the subject's attribute. Note
  153. // that build_std_vector may return unused_type if the
  154. // subject's attribute is an unused_type.
  155. template <typename Context, typename Iterator>
  156. struct attribute
  157. : traits::build_std_vector<
  158. typename traits::attribute_of<Subject, Context, Iterator>::type
  159. >
  160. {};
  161. base_repeat_generator(Subject const& subject, LoopIter const& iter)
  162. : subject(subject), iter(iter) {}
  163. template <typename OutputIterator, typename Context, typename Delimiter
  164. , typename Attribute>
  165. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  166. , Attribute const& attr) const
  167. {
  168. typedef detail::fail_function<
  169. OutputIterator, Context, Delimiter
  170. > fail_function;
  171. typedef typename traits::container_iterator<
  172. typename add_const<Attribute>::type
  173. >::type iterator_type;
  174. typedef
  175. typename traits::make_indirect_iterator<iterator_type>::type
  176. indirect_iterator_type;
  177. typedef detail::pass_container<
  178. fail_function, Attribute, indirect_iterator_type, mpl::false_>
  179. pass_container;
  180. iterator_type it = traits::begin(attr);
  181. iterator_type end = traits::end(attr);
  182. pass_container pass(fail_function(sink, ctx, d),
  183. indirect_iterator_type(it), indirect_iterator_type(end));
  184. // generate the minimal required amount of output
  185. typename LoopIter::type i = iter.start();
  186. for (/**/; !pass.is_at_end() && !iter.got_min(i); ++i)
  187. {
  188. if (!generate_subject(pass, attr, Strict()))
  189. {
  190. // if we fail before reaching the minimum iteration
  191. // required, do not output anything and return false
  192. return false;
  193. }
  194. }
  195. if (pass.is_at_end() && !iter.got_min(i))
  196. return false; // insufficient attribute elements
  197. // generate some more up to the maximum specified
  198. for (/**/; !pass.is_at_end() && !iter.got_max(i); ++i)
  199. {
  200. if (!generate_subject(pass, attr, Strict()))
  201. break;
  202. }
  203. return detail::sink_is_good(sink);
  204. }
  205. template <typename Context>
  206. info what(Context& context) const
  207. {
  208. return info("repeat", subject.what(context));
  209. }
  210. Subject subject;
  211. LoopIter iter;
  212. };
  213. template <typename Subject, typename LoopIter>
  214. struct repeat_generator
  215. : base_repeat_generator<
  216. Subject, LoopIter, mpl::false_
  217. , repeat_generator<Subject, LoopIter> >
  218. {
  219. typedef base_repeat_generator<
  220. Subject, LoopIter, mpl::false_, repeat_generator
  221. > base_repeat_generator_;
  222. repeat_generator(Subject const& subject, LoopIter const& iter)
  223. : base_repeat_generator_(subject, iter) {}
  224. };
  225. template <typename Subject, typename LoopIter>
  226. struct strict_repeat_generator
  227. : base_repeat_generator<
  228. Subject, LoopIter, mpl::true_
  229. , strict_repeat_generator<Subject, LoopIter> >
  230. {
  231. typedef base_repeat_generator<
  232. Subject, LoopIter, mpl::true_, strict_repeat_generator
  233. > base_repeat_generator_;
  234. strict_repeat_generator(Subject const& subject, LoopIter const& iter)
  235. : base_repeat_generator_(subject, iter) {}
  236. };
  237. ///////////////////////////////////////////////////////////////////////////
  238. // Generator generators: make_xxx function (objects)
  239. ///////////////////////////////////////////////////////////////////////////
  240. template <typename Subject, typename Modifiers>
  241. struct make_directive<tag::repeat, Subject, Modifiers>
  242. {
  243. typedef typename mpl::if_<
  244. detail::get_stricttag<Modifiers>
  245. , strict_kleene<Subject>, kleene<Subject>
  246. >::type result_type;
  247. result_type operator()(unused_type, Subject const& subject
  248. , unused_type) const
  249. {
  250. return result_type(subject);
  251. }
  252. };
  253. template <typename T, typename Subject, typename Modifiers>
  254. struct make_directive<
  255. terminal_ex<tag::repeat, fusion::vector1<T> >, Subject, Modifiers>
  256. {
  257. typedef exact_iterator<T> iterator_type;
  258. typedef typename mpl::if_<
  259. detail::get_stricttag<Modifiers>
  260. , strict_repeat_generator<Subject, iterator_type>
  261. , repeat_generator<Subject, iterator_type>
  262. >::type result_type;
  263. template <typename Terminal>
  264. result_type operator()(
  265. Terminal const& term, Subject const& subject, unused_type) const
  266. {
  267. return result_type(subject, fusion::at_c<0>(term.args));
  268. }
  269. };
  270. template <typename T, typename Subject, typename Modifiers>
  271. struct make_directive<
  272. terminal_ex<tag::repeat, fusion::vector2<T, T> >, Subject, Modifiers>
  273. {
  274. typedef finite_iterator<T> iterator_type;
  275. typedef typename mpl::if_<
  276. detail::get_stricttag<Modifiers>
  277. , strict_repeat_generator<Subject, iterator_type>
  278. , repeat_generator<Subject, iterator_type>
  279. >::type result_type;
  280. template <typename Terminal>
  281. result_type operator()(
  282. Terminal const& term, Subject const& subject, unused_type) const
  283. {
  284. return result_type(subject,
  285. iterator_type(
  286. fusion::at_c<0>(term.args)
  287. , fusion::at_c<1>(term.args)
  288. )
  289. );
  290. }
  291. };
  292. template <typename T, typename Subject, typename Modifiers>
  293. struct make_directive<
  294. terminal_ex<tag::repeat
  295. , fusion::vector2<T, inf_type> >, Subject, Modifiers>
  296. {
  297. typedef infinite_iterator<T> iterator_type;
  298. typedef typename mpl::if_<
  299. detail::get_stricttag<Modifiers>
  300. , strict_repeat_generator<Subject, iterator_type>
  301. , repeat_generator<Subject, iterator_type>
  302. >::type result_type;
  303. template <typename Terminal>
  304. result_type operator()(
  305. Terminal const& term, Subject const& subject, unused_type) const
  306. {
  307. return result_type(subject, fusion::at_c<0>(term.args));
  308. }
  309. };
  310. }}}
  311. namespace boost { namespace spirit { namespace traits
  312. {
  313. ///////////////////////////////////////////////////////////////////////////
  314. template <typename Subject, typename LoopIter>
  315. struct has_semantic_action<karma::repeat_generator<Subject, LoopIter> >
  316. : unary_has_semantic_action<Subject> {};
  317. template <typename Subject, typename LoopIter>
  318. struct has_semantic_action<karma::strict_repeat_generator<Subject, LoopIter> >
  319. : unary_has_semantic_action<Subject> {};
  320. ///////////////////////////////////////////////////////////////////////////
  321. template <typename Subject, typename LoopIter, typename Attribute
  322. , typename Context, typename Iterator>
  323. struct handles_container<
  324. karma::repeat_generator<Subject, LoopIter>, Attribute
  325. , Context, Iterator>
  326. : mpl::true_ {};
  327. template <typename Subject, typename LoopIter, typename Attribute
  328. , typename Context, typename Iterator>
  329. struct handles_container<
  330. karma::strict_repeat_generator<Subject, LoopIter>, Attribute
  331. , Context, Iterator>
  332. : mpl::true_ {};
  333. }}}
  334. #endif