1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 // Copyright (c) 2001-2011 Joel de Guzman
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)
7 #if !defined(SPIRIT_KARMA_ALTERNATIVE_MAR_01_2007_1117AM)
8 #define SPIRIT_KARMA_ALTERNATIVE_MAR_01_2007_1117AM
14 #include <boost/spirit/home/karma/detail/alternative_function.hpp>
15 #include <boost/spirit/home/karma/detail/get_stricttag.hpp>
16 #include <boost/spirit/home/karma/domain.hpp>
17 #include <boost/spirit/home/karma/generator.hpp>
18 #include <boost/spirit/home/karma/meta_compiler.hpp>
19 #include <boost/spirit/home/support/info.hpp>
20 #include <boost/spirit/home/support/unused.hpp>
21 #include <boost/spirit/home/support/has_semantic_action.hpp>
22 #include <boost/spirit/home/support/handles_container.hpp>
23 #include <boost/spirit/home/support/detail/what_function.hpp>
24 #include <boost/fusion/include/any.hpp>
25 #include <boost/fusion/include/mpl.hpp>
26 #include <boost/fusion/include/for_each.hpp>
27 #include <boost/mpl/accumulate.hpp>
28 #include <boost/mpl/bitor.hpp>
29 #include <boost/config.hpp>
31 namespace boost { namespace spirit
33 ///////////////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////
37 struct use_operator<karma::domain, proto::tag::bitwise_or> // enables |
41 struct flatten_tree<karma::domain, proto::tag::bitwise_or> // flattens |
46 ///////////////////////////////////////////////////////////////////////////////
47 namespace boost { namespace spirit { namespace traits
49 // specialization for sequences
50 template <typename Elements>
51 struct alternative_properties
53 struct element_properties
58 template <typename F, typename Element>
59 struct result<F(Element)>
61 typedef properties_of<Element> type;
64 // never called, but needed for decltype-based result_of (C++0x)
65 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
66 template <typename Element>
67 typename result<element_properties(Element)>::type
68 operator()(Element&&) const;
72 typedef typename mpl::accumulate<
73 typename fusion::result_of::transform<
74 Elements, element_properties>::type
75 , mpl::int_<karma::generator_properties::countingbuffer>
76 , mpl::bitor_<mpl::_2, mpl::_1>
82 namespace boost { namespace spirit { namespace karma
84 template <typename Elements, typename Strict, typename Derived>
85 struct base_alternative : nary_generator<Derived>
87 typedef typename traits::alternative_properties<Elements>::type
90 template <typename Context, typename Iterator = unused_type>
93 // Put all the element attributes in a tuple
94 typedef typename traits::build_attribute_sequence<
95 Elements, Context, traits::alternative_attribute_transform
96 , Iterator, karma::domain
97 >::type all_attributes;
99 // Ok, now make a variant over the attribute sequence. Note that
100 // build_variant makes sure that 1) all attributes in the variant
101 // are unique 2) puts the unused attribute, if there is any, to
102 // the front and 3) collapses single element variants, variant<T>
104 typedef typename traits::build_variant<all_attributes>::type type;
107 base_alternative(Elements const& elements)
108 : elements(elements) {}
111 typename OutputIterator, typename Context, typename Delimiter
112 , typename Attribute>
113 bool generate(OutputIterator& sink, Context& ctx
114 , Delimiter const& d, Attribute const& attr) const
116 typedef detail::alternative_generate_function<
117 OutputIterator, Context, Delimiter, Attribute, Strict
120 // f return true if *any* of the parser succeeds
121 functor f (sink, ctx, d, attr);
122 return fusion::any(elements, f);
125 template <typename Context>
126 info what(Context& context) const
128 info result("alternative");
129 fusion::for_each(elements,
130 spirit::detail::what_function<Context>(result, context));
137 template <typename Elements>
139 : base_alternative<Elements, mpl::false_, alternative<Elements> >
141 typedef base_alternative<Elements, mpl::false_, alternative>
144 alternative(Elements const& elements)
145 : base_alternative_(elements) {}
148 template <typename Elements>
149 struct strict_alternative
150 : base_alternative<Elements, mpl::true_, strict_alternative<Elements> >
152 typedef base_alternative<Elements, mpl::true_, strict_alternative>
155 strict_alternative(Elements const& elements)
156 : base_alternative_(elements) {}
159 ///////////////////////////////////////////////////////////////////////////
160 // Generator generators: make_xxx function (objects)
161 ///////////////////////////////////////////////////////////////////////////
164 template <typename Elements, bool strict_mode = false>
165 struct make_alternative
166 : make_nary_composite<Elements, alternative>
169 template <typename Elements>
170 struct make_alternative<Elements, true>
171 : make_nary_composite<Elements, strict_alternative>
175 template <typename Elements, typename Modifiers>
176 struct make_composite<proto::tag::bitwise_or, Elements, Modifiers>
177 : detail::make_alternative<Elements
178 , detail::get_stricttag<Modifiers>::value>
183 namespace boost { namespace spirit { namespace traits
185 ///////////////////////////////////////////////////////////////////////////
186 template <typename Elements>
187 struct has_semantic_action<karma::alternative<Elements> >
188 : nary_has_semantic_action<Elements> {};
190 template <typename Elements>
191 struct has_semantic_action<karma::strict_alternative<Elements> >
192 : nary_has_semantic_action<Elements> {};
194 ///////////////////////////////////////////////////////////////////////////
195 template <typename Elements, typename Attribute, typename Context
197 struct handles_container<karma::alternative<Elements>
198 , Attribute, Context, Iterator>
199 : nary_handles_container<Elements, Attribute, Context, Iterator> {};
201 template <typename Elements, typename Attribute, typename Context
203 struct handles_container<karma::strict_alternative<Elements>
204 , Attribute, Context, Iterator>
205 : nary_handles_container<Elements, Attribute, Context, Iterator> {};