repeat.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. #ifndef BOOST_SPIRIT_KARMA_DIRECTIVE_REPEAT_HPP
  7. #define BOOST_SPIRIT_KARMA_DIRECTIVE_REPEAT_HPP
  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. #ifdef _MSC_VER
  70. # pragma warning(push)
  71. # pragma warning(disable: 4512) // assignment operator could not be generated.
  72. #endif
  73. // handles repeat(exact)[p]
  74. template <typename T>
  75. struct exact_iterator
  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. // handles repeat(min, max)[p]
  86. template <typename T>
  87. struct finite_iterator
  88. {
  89. finite_iterator(T const min, T const max)
  90. : min BOOST_PREVENT_MACRO_SUBSTITUTION (min)
  91. , max BOOST_PREVENT_MACRO_SUBSTITUTION (max) {}
  92. typedef T type;
  93. T start() const { return 0; }
  94. bool got_max(T i) const { return i >= max; }
  95. bool got_min(T i) const { return i >= min; }
  96. T const min;
  97. T const max;
  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. };
  111. #ifdef _MSC_VER
  112. # pragma warning(pop)
  113. #endif
  114. ///////////////////////////////////////////////////////////////////////////
  115. template <typename Subject, typename LoopIter, typename Strict
  116. , typename Derived>
  117. struct base_repeat_generator : unary_generator<Derived>
  118. {
  119. private:
  120. // iterate over the given container until its exhausted or the embedded
  121. // generator succeeds
  122. template <typename F, typename Attribute>
  123. bool generate_subject(F f, Attribute const&, mpl::false_) const
  124. {
  125. // Failing subject generators are just skipped. This allows to
  126. // selectively generate items in the provided attribute.
  127. while (!f.is_at_end())
  128. {
  129. bool r = !f(subject);
  130. if (r)
  131. return true;
  132. if (!f.is_at_end())
  133. f.next();
  134. }
  135. return false;
  136. }
  137. template <typename F, typename Attribute>
  138. bool generate_subject(F f, Attribute const&, mpl::true_) const
  139. {
  140. return !f(subject);
  141. }
  142. // There is no way to distinguish a failed generator from a
  143. // generator to be skipped. We assume the user takes responsibility
  144. // for ending the loop if no attribute is specified.
  145. template <typename F>
  146. bool generate_subject(F f, unused_type, mpl::false_) const
  147. {
  148. return !f(subject);
  149. }
  150. public:
  151. typedef Subject subject_type;
  152. typedef mpl::int_<subject_type::properties::value> properties;
  153. // Build a std::vector from the subject's attribute. Note
  154. // that build_std_vector may return unused_type if the
  155. // subject's attribute is an unused_type.
  156. template <typename Context, typename Iterator>
  157. struct attribute
  158. : traits::build_std_vector<
  159. typename traits::attribute_of<Subject, Context, Iterator>::type
  160. >
  161. {};
  162. base_repeat_generator(Subject const& subject, LoopIter const& iter)
  163. : subject(subject), iter(iter) {}
  164. template <typename OutputIterator, typename Context, typename Delimiter
  165. , typename Attribute>
  166. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  167. , Attribute const& attr) const
  168. {
  169. typedef detail::fail_function<
  170. OutputIterator, Context, Delimiter
  171. > fail_function;
  172. typedef typename traits::container_iterator<
  173. typename add_const<Attribute>::type
  174. >::type iterator_type;
  175. typedef
  176. typename traits::make_indirect_iterator<iterator_type>::type
  177. indirect_iterator_type;
  178. typedef detail::pass_container<
  179. fail_function, Attribute, indirect_iterator_type, mpl::false_>
  180. pass_container;
  181. iterator_type it = traits::begin(attr);
  182. iterator_type end = traits::end(attr);
  183. pass_container pass(fail_function(sink, ctx, d),
  184. indirect_iterator_type(it), indirect_iterator_type(end));
  185. // generate the minimal required amount of output
  186. typename LoopIter::type i = iter.start();
  187. for (/**/; !pass.is_at_end() && !iter.got_min(i); ++i)
  188. {
  189. if (!generate_subject(pass, attr, Strict()))
  190. {
  191. // if we fail before reaching the minimum iteration
  192. // required, do not output anything and return false
  193. return false;
  194. }
  195. }
  196. if (pass.is_at_end() && !iter.got_min(i))
  197. return false; // insufficient attribute elements
  198. // generate some more up to the maximum specified
  199. for (/**/; !pass.is_at_end() && !iter.got_max(i); ++i)
  200. {
  201. if (!generate_subject(pass, attr, Strict()))
  202. break;
  203. }
  204. return detail::sink_is_good(sink);
  205. }
  206. template <typename Context>
  207. info what(Context& context) const
  208. {
  209. return info("repeat", subject.what(context));
  210. }
  211. Subject subject;
  212. LoopIter iter;
  213. };
  214. template <typename Subject, typename LoopIter>
  215. struct repeat_generator
  216. : base_repeat_generator<
  217. Subject, LoopIter, mpl::false_
  218. , repeat_generator<Subject, LoopIter> >
  219. {
  220. typedef base_repeat_generator<
  221. Subject, LoopIter, mpl::false_, repeat_generator
  222. > base_repeat_generator_;
  223. repeat_generator(Subject const& subject, LoopIter const& iter)
  224. : base_repeat_generator_(subject, iter) {}
  225. };
  226. template <typename Subject, typename LoopIter>
  227. struct strict_repeat_generator
  228. : base_repeat_generator<
  229. Subject, LoopIter, mpl::true_
  230. , strict_repeat_generator<Subject, LoopIter> >
  231. {
  232. typedef base_repeat_generator<
  233. Subject, LoopIter, mpl::true_, strict_repeat_generator
  234. > base_repeat_generator_;
  235. strict_repeat_generator(Subject const& subject, LoopIter const& iter)
  236. : base_repeat_generator_(subject, iter) {}
  237. };
  238. ///////////////////////////////////////////////////////////////////////////
  239. // Generator generators: make_xxx function (objects)
  240. ///////////////////////////////////////////////////////////////////////////
  241. template <typename Subject, typename Modifiers>
  242. struct make_directive<tag::repeat, Subject, Modifiers>
  243. {
  244. typedef typename mpl::if_<
  245. detail::get_stricttag<Modifiers>
  246. , strict_kleene<Subject>, kleene<Subject>
  247. >::type result_type;
  248. result_type operator()(unused_type, Subject const& subject
  249. , unused_type) const
  250. {
  251. return result_type(subject);
  252. }
  253. };
  254. template <typename T, typename Subject, typename Modifiers>
  255. struct make_directive<
  256. terminal_ex<tag::repeat, fusion::vector1<T> >, Subject, Modifiers>
  257. {
  258. typedef exact_iterator<T> iterator_type;
  259. typedef typename mpl::if_<
  260. detail::get_stricttag<Modifiers>
  261. , strict_repeat_generator<Subject, iterator_type>
  262. , repeat_generator<Subject, iterator_type>
  263. >::type result_type;
  264. template <typename Terminal>
  265. result_type operator()(
  266. Terminal const& term, Subject const& subject, unused_type) const
  267. {
  268. return result_type(subject, fusion::at_c<0>(term.args));
  269. }
  270. };
  271. template <typename T, typename Subject, typename Modifiers>
  272. struct make_directive<
  273. terminal_ex<tag::repeat, fusion::vector2<T, T> >, Subject, Modifiers>
  274. {
  275. typedef finite_iterator<T> iterator_type;
  276. typedef typename mpl::if_<
  277. detail::get_stricttag<Modifiers>
  278. , strict_repeat_generator<Subject, iterator_type>
  279. , repeat_generator<Subject, iterator_type>
  280. >::type result_type;
  281. template <typename Terminal>
  282. result_type operator()(
  283. Terminal const& term, Subject const& subject, unused_type) const
  284. {
  285. return result_type(subject,
  286. iterator_type(
  287. fusion::at_c<0>(term.args)
  288. , fusion::at_c<1>(term.args)
  289. )
  290. );
  291. }
  292. };
  293. template <typename T, typename Subject, typename Modifiers>
  294. struct make_directive<
  295. terminal_ex<tag::repeat
  296. , fusion::vector2<T, inf_type> >, Subject, Modifiers>
  297. {
  298. typedef infinite_iterator<T> iterator_type;
  299. typedef typename mpl::if_<
  300. detail::get_stricttag<Modifiers>
  301. , strict_repeat_generator<Subject, iterator_type>
  302. , repeat_generator<Subject, iterator_type>
  303. >::type result_type;
  304. template <typename Terminal>
  305. result_type operator()(
  306. Terminal const& term, Subject const& subject, unused_type) const
  307. {
  308. return result_type(subject, fusion::at_c<0>(term.args));
  309. }
  310. };
  311. }}}
  312. namespace boost { namespace spirit { namespace traits
  313. {
  314. ///////////////////////////////////////////////////////////////////////////
  315. template <typename Subject, typename LoopIter>
  316. struct has_semantic_action<karma::repeat_generator<Subject, LoopIter> >
  317. : unary_has_semantic_action<Subject> {};
  318. template <typename Subject, typename LoopIter>
  319. struct has_semantic_action<karma::strict_repeat_generator<Subject, LoopIter> >
  320. : unary_has_semantic_action<Subject> {};
  321. ///////////////////////////////////////////////////////////////////////////
  322. template <typename Subject, typename LoopIter, typename Attribute
  323. , typename Context, typename Iterator>
  324. struct handles_container<
  325. karma::repeat_generator<Subject, LoopIter>, Attribute
  326. , Context, Iterator>
  327. : mpl::true_ {};
  328. template <typename Subject, typename LoopIter, typename Attribute
  329. , typename Context, typename Iterator>
  330. struct handles_container<
  331. karma::strict_repeat_generator<Subject, LoopIter>, Attribute
  332. , Context, Iterator>
  333. : mpl::true_ {};
  334. }}}
  335. #endif