sequence.hpp 12 KB

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