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