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
7#ifndef BOOST_SPIRIT_KARMA_OPERATOR_ALTERNATIVE_HPP
8#define BOOST_SPIRIT_KARMA_OPERATOR_ALTERNATIVE_HPP
9
10#if defined(_MSC_VER)
11#pragma once
12#endif
13
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/proto/tags.hpp>
30#include <boost/config.hpp>
31
32namespace boost { namespace spirit
33{
34 ///////////////////////////////////////////////////////////////////////////
35 // Enablers
36 ///////////////////////////////////////////////////////////////////////////
37 template <>
38 struct use_operator<karma::domain, proto::tag::bitwise_or> // enables |
39 : mpl::true_ {};
40
41 template <>
42 struct flatten_tree<karma::domain, proto::tag::bitwise_or> // flattens |
43 : mpl::true_ {};
44
45}}
46
47///////////////////////////////////////////////////////////////////////////////
48namespace boost { namespace spirit { namespace traits
49{
50 // specialization for sequences
51 template <typename Elements>
52 struct alternative_properties
53 {
54 struct element_properties
55 {
56 template <typename T>
57 struct result;
58
59 template <typename F, typename Element>
60 struct result<F(Element)>
61 {
62 typedef properties_of<Element> type;
63 };
64
65 // never called, but needed for decltype-based result_of (C++0x)
66#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
67 template <typename Element>
68 typename result<element_properties(Element)>::type
69 operator()(Element&&) const;
70#endif
71 };
72
73 typedef typename mpl::accumulate<
74 typename fusion::result_of::transform<
75 Elements, element_properties>::type
76 , mpl::int_<karma::generator_properties::countingbuffer>
77 , mpl::bitor_<mpl::_2, mpl::_1>
78 >::type type;
79 };
80
81}}}
82
83namespace boost { namespace spirit { namespace karma
84{
85 template <typename Elements, typename Strict, typename Derived>
86 struct base_alternative : nary_generator<Derived>
87 {
88 typedef typename traits::alternative_properties<Elements>::type
89 properties;
90
91 template <typename Context, typename Iterator = unused_type>
92 struct attribute
93 {
94 // Put all the element attributes in a tuple
95 typedef typename traits::build_attribute_sequence<
96 Elements, Context, traits::alternative_attribute_transform
97 , Iterator, karma::domain
98 >::type all_attributes;
99
100 // Ok, now make a variant over the attribute sequence. Note that
101 // build_variant makes sure that 1) all attributes in the variant
102 // are unique 2) puts the unused attribute, if there is any, to
103 // the front and 3) collapses single element variants, variant<T>
104 // to T.
105 typedef typename traits::build_variant<all_attributes>::type type;
106 };
107
108 base_alternative(Elements const& elements)
109 : elements(elements) {}
110
111 template <
112 typename OutputIterator, typename Context, typename Delimiter
113 , typename Attribute>
114 bool generate(OutputIterator& sink, Context& ctx
115 , Delimiter const& d, Attribute const& attr) const
116 {
117 typedef detail::alternative_generate_function<
118 OutputIterator, Context, Delimiter, Attribute, Strict
119 > functor;
120
121 // f return true if *any* of the parser succeeds
122 functor f (sink, ctx, d, attr);
123 return fusion::any(elements, f);
124 }
125
126 template <typename Context>
127 info what(Context& context) const
128 {
129 info result("alternative");
130 fusion::for_each(elements,
131 spirit::detail::what_function<Context>(result, context));
132 return result;
133 }
134
135 Elements elements;
136 };
137
138 template <typename Elements>
139 struct alternative
140 : base_alternative<Elements, mpl::false_, alternative<Elements> >
141 {
142 typedef base_alternative<Elements, mpl::false_, alternative>
143 base_alternative_;
144
145 alternative(Elements const& elements)
146 : base_alternative_(elements) {}
147 };
148
149 template <typename Elements>
150 struct strict_alternative
151 : base_alternative<Elements, mpl::true_, strict_alternative<Elements> >
152 {
153 typedef base_alternative<Elements, mpl::true_, strict_alternative>
154 base_alternative_;
155
156 strict_alternative(Elements const& elements)
157 : base_alternative_(elements) {}
158 };
159
160 ///////////////////////////////////////////////////////////////////////////
161 // Generator generators: make_xxx function (objects)
162 ///////////////////////////////////////////////////////////////////////////
163 namespace detail
164 {
165 template <typename Elements, bool strict_mode = false>
166 struct make_alternative
167 : make_nary_composite<Elements, alternative>
168 {};
169
170 template <typename Elements>
171 struct make_alternative<Elements, true>
172 : make_nary_composite<Elements, strict_alternative>
173 {};
174 }
175
176 template <typename Elements, typename Modifiers>
177 struct make_composite<proto::tag::bitwise_or, Elements, Modifiers>
178 : detail::make_alternative<Elements
179 , detail::get_stricttag<Modifiers>::value>
180 {};
181
182}}}
183
184namespace boost { namespace spirit { namespace traits
185{
186 ///////////////////////////////////////////////////////////////////////////
187 template <typename Elements>
188 struct has_semantic_action<karma::alternative<Elements> >
189 : nary_has_semantic_action<Elements> {};
190
191 template <typename Elements>
192 struct has_semantic_action<karma::strict_alternative<Elements> >
193 : nary_has_semantic_action<Elements> {};
194
195 ///////////////////////////////////////////////////////////////////////////
196 template <typename Elements, typename Attribute, typename Context
197 , typename Iterator>
198 struct handles_container<karma::alternative<Elements>
199 , Attribute, Context, Iterator>
200 : nary_handles_container<Elements, Attribute, Context, Iterator> {};
201
202 template <typename Elements, typename Attribute, typename Context
203 , typename Iterator>
204 struct handles_container<karma::strict_alternative<Elements>
205 , Attribute, Context, Iterator>
206 : nary_handles_container<Elements, Attribute, Context, Iterator> {};
207}}}
208
209#endif
210

source code of boost/libs/spirit/include/boost/spirit/home/karma/operator/alternative.hpp