sequence.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_SEQUENCE_FEB_28_2007_0247PM)
  7. #define SPIRIT_KARMA_SEQUENCE_FEB_28_2007_0247PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/karma/domain.hpp>
  12. #include <boost/spirit/home/karma/generator.hpp>
  13. #include <boost/spirit/home/karma/meta_compiler.hpp>
  14. #include <boost/spirit/home/karma/detail/fail_function.hpp>
  15. #include <boost/spirit/home/karma/detail/pass_container.hpp>
  16. #include <boost/spirit/home/karma/detail/get_stricttag.hpp>
  17. #include <boost/spirit/home/support/info.hpp>
  18. #include <boost/spirit/home/support/detail/what_function.hpp>
  19. #include <boost/spirit/home/karma/detail/attributes.hpp>
  20. #include <boost/spirit/home/karma/detail/indirect_iterator.hpp>
  21. #include <boost/spirit/home/support/algorithm/any_if.hpp>
  22. #include <boost/spirit/home/support/unused.hpp>
  23. #include <boost/spirit/home/support/sequence_base_id.hpp>
  24. #include <boost/spirit/home/support/has_semantic_action.hpp>
  25. #include <boost/spirit/home/support/handles_container.hpp>
  26. #include <boost/spirit/home/support/attributes.hpp>
  27. #include <boost/fusion/include/vector.hpp>
  28. #include <boost/fusion/include/as_vector.hpp>
  29. #include <boost/fusion/include/for_each.hpp>
  30. #include <boost/type_traits/is_same.hpp>
  31. #include <boost/mpl/bitor.hpp>
  32. #include <boost/mpl/int.hpp>
  33. #include <boost/mpl/and.hpp>
  34. #include <boost/mpl/not.hpp>
  35. #include <boost/fusion/include/transform.hpp>
  36. #include <boost/mpl/accumulate.hpp>
  37. #include <boost/config.hpp>
  38. ///////////////////////////////////////////////////////////////////////////////
  39. namespace boost { namespace spirit
  40. {
  41. ///////////////////////////////////////////////////////////////////////////
  42. // Enablers
  43. ///////////////////////////////////////////////////////////////////////////
  44. template <>
  45. struct use_operator<karma::domain, proto::tag::shift_left> // enables <<
  46. : mpl::true_ {};
  47. template <>
  48. struct flatten_tree<karma::domain, proto::tag::shift_left> // flattens <<
  49. : mpl::true_ {};
  50. }}
  51. ///////////////////////////////////////////////////////////////////////////////
  52. namespace boost { namespace spirit { namespace traits
  53. {
  54. // specialization for sequences
  55. template <typename Elements>
  56. struct sequence_properties
  57. {
  58. struct element_properties
  59. {
  60. template <typename T>
  61. struct result;
  62. template <typename F, typename Element>
  63. struct result<F(Element)>
  64. {
  65. typedef properties_of<Element> type;
  66. };
  67. // never called, but needed for decltype-based result_of (C++0x)
  68. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  69. template <typename Element>
  70. typename result<element_properties(Element)>::type
  71. operator()(Element&&) const;
  72. #endif
  73. };
  74. typedef typename mpl::accumulate<
  75. typename fusion::result_of::transform<
  76. Elements, element_properties>::type
  77. , mpl::int_<karma::generator_properties::no_properties>
  78. , mpl::bitor_<mpl::_2, mpl::_1>
  79. >::type type;
  80. };
  81. }}}
  82. ///////////////////////////////////////////////////////////////////////////////
  83. namespace boost { namespace spirit { namespace karma
  84. {
  85. template <typename Elements, typename Strict, typename Derived>
  86. struct base_sequence : nary_generator<Derived>
  87. {
  88. typedef typename traits::sequence_properties<Elements>::type properties;
  89. base_sequence(Elements const& elements)
  90. : elements(elements) {}
  91. typedef Elements elements_type;
  92. struct sequence_base_id;
  93. template <typename Context, typename Iterator = unused_type>
  94. struct attribute
  95. {
  96. // Put all the element attributes in a tuple
  97. typedef typename traits::build_attribute_sequence<
  98. Elements, Context, traits::sequence_attribute_transform
  99. , Iterator, karma::domain
  100. >::type all_attributes;
  101. // Now, build a fusion vector over the attributes. Note
  102. // that build_fusion_vector 1) removes all unused attributes
  103. // and 2) may return unused_type if all elements have
  104. // unused_type(s).
  105. typedef typename
  106. traits::build_fusion_vector<all_attributes>::type
  107. type_;
  108. // Finally, strip single element vectors into its
  109. // naked form: vector1<T> --> T
  110. typedef typename
  111. traits::strip_single_element_vector<type_>::type
  112. type;
  113. };
  114. // standard case. Attribute is a fusion tuple
  115. template <
  116. typename OutputIterator, typename Context, typename Delimiter
  117. , typename Attribute, typename Pred1, typename Pred2>
  118. bool generate_impl(OutputIterator& sink, Context& ctx
  119. , Delimiter const& d, Attribute& attr_, Pred1, Pred2) const
  120. {
  121. typedef detail::fail_function<
  122. OutputIterator, Context, Delimiter> fail_function;
  123. typedef traits::attribute_not_unused<Context> predicate;
  124. // wrap the attribute in a tuple if it is not a tuple or if the
  125. // attribute of this sequence is a single element tuple
  126. typedef typename attribute<Context>::type_ attr_type_;
  127. typename traits::wrap_if_not_tuple<Attribute
  128. , typename mpl::and_<
  129. traits::one_element_sequence<attr_type_>
  130. , mpl::not_<traits::one_element_sequence<Attribute> >
  131. >::type
  132. >::type attr(attr_);
  133. // return false if *any* of the generators fail
  134. bool r = spirit::any_if(elements, attr
  135. , fail_function(sink, ctx, d), predicate());
  136. typedef typename traits::attribute_size<Attribute>::type size_type;
  137. // fail generating if sequences have not the same (logical) length
  138. return !r && (!Strict::value ||
  139. // This ignores container element count (which is not good),
  140. // but allows valid attributes to succeed. This will lead to
  141. // false positives (failing generators, even if they shouldn't)
  142. // if the embedded component is restricting the number of
  143. // container elements it consumes (i.e. repeat). This solution
  144. // is not optimal but much better than letting _all_ repetitive
  145. // components fail.
  146. Pred1::value ||
  147. size_type(traits::sequence_size<attr_type_>::value) == traits::size(attr_));
  148. }
  149. // Special case when Attribute is an stl container and the sequence's
  150. // attribute is not a one element sequence
  151. template <
  152. typename OutputIterator, typename Context, typename Delimiter
  153. , typename Attribute>
  154. bool generate_impl(OutputIterator& sink, Context& ctx
  155. , Delimiter const& d, Attribute const& attr_
  156. , mpl::true_, mpl::false_) const
  157. {
  158. // return false if *any* of the generators fail
  159. typedef detail::fail_function<
  160. OutputIterator, Context, Delimiter> fail_function;
  161. typedef typename traits::container_iterator<
  162. typename add_const<Attribute>::type
  163. >::type iterator_type;
  164. typedef
  165. typename traits::make_indirect_iterator<iterator_type>::type
  166. indirect_iterator_type;
  167. typedef detail::pass_container<
  168. fail_function, Attribute, indirect_iterator_type, mpl::true_>
  169. pass_container;
  170. iterator_type begin = traits::begin(attr_);
  171. iterator_type end = traits::end(attr_);
  172. pass_container pass(fail_function(sink, ctx, d),
  173. indirect_iterator_type(begin), indirect_iterator_type(end));
  174. bool r = fusion::any(elements, pass);
  175. // fail generating if sequences have not the same (logical) length
  176. return !r && (!Strict::value || begin == end);
  177. }
  178. // main generate function. Dispatches to generate_impl depending
  179. // on the Attribute type.
  180. template <
  181. typename OutputIterator, typename Context, typename Delimiter
  182. , typename Attribute>
  183. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  184. , Attribute const& attr) const
  185. {
  186. typedef typename traits::is_container<Attribute>::type
  187. is_container;
  188. typedef typename attribute<Context>::type_ attr_type_;
  189. typedef typename traits::one_element_sequence<attr_type_>::type
  190. is_one_element_sequence;
  191. return generate_impl(sink, ctx, d, attr, is_container()
  192. , is_one_element_sequence());
  193. }
  194. template <typename Context>
  195. info what(Context& context) const
  196. {
  197. info result("sequence");
  198. fusion::for_each(elements,
  199. spirit::detail::what_function<Context>(result, context));
  200. return result;
  201. }
  202. Elements elements;
  203. };
  204. template <typename Elements>
  205. struct sequence
  206. : base_sequence<Elements, mpl::false_, sequence<Elements> >
  207. {
  208. typedef base_sequence<Elements, mpl::false_, sequence> base_sequence_;
  209. sequence(Elements const& subject)
  210. : base_sequence_(subject) {}
  211. };
  212. template <typename Elements>
  213. struct strict_sequence
  214. : base_sequence<Elements, mpl::true_, strict_sequence<Elements> >
  215. {
  216. typedef base_sequence<Elements, mpl::true_, strict_sequence>
  217. base_sequence_;
  218. strict_sequence(Elements const& subject)
  219. : base_sequence_(subject) {}
  220. };
  221. ///////////////////////////////////////////////////////////////////////////
  222. // Generator generators: make_xxx function (objects)
  223. ///////////////////////////////////////////////////////////////////////////
  224. namespace detail
  225. {
  226. template <typename Elements, bool strict_mode = false>
  227. struct make_sequence
  228. : make_nary_composite<Elements, sequence>
  229. {};
  230. template <typename Elements>
  231. struct make_sequence<Elements, true>
  232. : make_nary_composite<Elements, strict_sequence>
  233. {};
  234. }
  235. template <typename Elements, typename Modifiers>
  236. struct make_composite<proto::tag::shift_left, Elements, Modifiers>
  237. : detail::make_sequence<Elements, detail::get_stricttag<Modifiers>::value>
  238. {};
  239. ///////////////////////////////////////////////////////////////////////////
  240. // Helper template allowing to get the required container type for a rule
  241. // attribute, which is part of a sequence.
  242. template <typename Iterator>
  243. struct make_sequence_iterator_range
  244. {
  245. typedef iterator_range<detail::indirect_iterator<Iterator> > type;
  246. };
  247. }}}
  248. namespace boost { namespace spirit { namespace traits
  249. {
  250. ///////////////////////////////////////////////////////////////////////////
  251. template <typename Elements>
  252. struct has_semantic_action<karma::sequence<Elements> >
  253. : nary_has_semantic_action<Elements> {};
  254. template <typename Elements>
  255. struct has_semantic_action<karma::strict_sequence<Elements> >
  256. : nary_has_semantic_action<Elements> {};
  257. ///////////////////////////////////////////////////////////////////////////
  258. template <typename Elements, typename Attribute, typename Context
  259. , typename Iterator>
  260. struct handles_container<karma::sequence<Elements>, Attribute, Context
  261. , Iterator>
  262. : mpl::true_ {};
  263. template <typename Elements, typename Attribute, typename Context
  264. , typename Iterator>
  265. struct handles_container<karma::strict_sequence<Elements>, Attribute
  266. , Context, Iterator>
  267. : mpl::true_ {};
  268. }}}
  269. #endif