Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2001-2011 Joel de Guzman
|
Chris@16
|
3 Copyright (c) 2001-2011 Hartmut Kaiser
|
Chris@16
|
4
|
Chris@16
|
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 =============================================================================*/
|
Chris@16
|
8 #if !defined(SPIRIT_PASS_CONTAINER_JANUARY_06_2009_0802PM)
|
Chris@16
|
9 #define SPIRIT_PASS_CONTAINER_JANUARY_06_2009_0802PM
|
Chris@16
|
10
|
Chris@16
|
11 #if defined(_MSC_VER)
|
Chris@16
|
12 #pragma once
|
Chris@16
|
13 #endif
|
Chris@16
|
14
|
Chris@16
|
15 #include <boost/spirit/home/qi/detail/attributes.hpp>
|
Chris@16
|
16 #include <boost/spirit/home/support/container.hpp>
|
Chris@16
|
17 #include <boost/spirit/home/support/handles_container.hpp>
|
Chris@16
|
18 #include <boost/type_traits/is_base_of.hpp>
|
Chris@16
|
19 #include <boost/type_traits/is_convertible.hpp>
|
Chris@16
|
20 #include <boost/mpl/bool.hpp>
|
Chris@16
|
21 #include <boost/mpl/and.hpp>
|
Chris@16
|
22 #include <boost/mpl/or.hpp>
|
Chris@16
|
23 #include <boost/preprocessor/cat.hpp>
|
Chris@16
|
24 #include <boost/preprocessor/repetition/repeat.hpp>
|
Chris@16
|
25
|
Chris@16
|
26 namespace boost { namespace spirit { namespace qi { namespace detail
|
Chris@16
|
27 {
|
Chris@16
|
28 // Helper meta-function allowing to evaluate weak substitutability and
|
Chris@16
|
29 // negate the result if the predicate (Sequence) is not true
|
Chris@16
|
30 template <typename Sequence, typename Attribute, typename ValueType>
|
Chris@16
|
31 struct negate_weak_substitute_if_not
|
Chris@16
|
32 : mpl::if_<
|
Chris@16
|
33 Sequence
|
Chris@16
|
34 , typename traits::is_weak_substitute<Attribute, ValueType>::type
|
Chris@16
|
35 , typename mpl::not_<
|
Chris@16
|
36 traits::is_weak_substitute<Attribute, ValueType>
|
Chris@16
|
37 >::type>
|
Chris@16
|
38 {};
|
Chris@16
|
39
|
Chris@16
|
40 // pass_through_container: utility to check decide whether a provided
|
Chris@16
|
41 // container attribute needs to be passed through to the current component
|
Chris@16
|
42 // or of we need to split the container by passing along instances of its
|
Chris@16
|
43 // value type
|
Chris@16
|
44
|
Chris@16
|
45 // if the expected attribute of the current component is neither a Fusion
|
Chris@16
|
46 // sequence nor a container, we will pass through the provided container
|
Chris@16
|
47 // only if its value type is not compatible with the component
|
Chris@16
|
48 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
49 , typename Sequence, typename Enable = void>
|
Chris@16
|
50 struct pass_through_container_base
|
Chris@16
|
51 : negate_weak_substitute_if_not<Sequence, Attribute, ValueType>
|
Chris@16
|
52 {};
|
Chris@16
|
53
|
Chris@16
|
54 // Specialization for fusion sequences, in this case we check whether all
|
Chris@16
|
55 // the types in the sequence are convertible to the lhs attribute.
|
Chris@16
|
56 //
|
Chris@16
|
57 // We return false if the rhs attribute itself is a fusion sequence, which
|
Chris@16
|
58 // is compatible with the LHS sequence (we want to pass through this
|
Chris@16
|
59 // attribute without it being split apart).
|
Chris@16
|
60 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
61 , typename Sequence = mpl::true_>
|
Chris@16
|
62 struct not_compatible_element
|
Chris@16
|
63 : mpl::and_<
|
Chris@16
|
64 negate_weak_substitute_if_not<Sequence, Attribute, Container>
|
Chris@16
|
65 , negate_weak_substitute_if_not<Sequence, Attribute, ValueType> >
|
Chris@16
|
66 {};
|
Chris@16
|
67
|
Chris@16
|
68 // If the value type of the container is not a Fusion sequence, we pass
|
Chris@16
|
69 // through the container if each of the elements of the Attribute
|
Chris@16
|
70 // sequence is compatible with either the container or its value type.
|
Chris@16
|
71 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
72 , typename Sequence
|
Chris@16
|
73 , bool IsSequence = fusion::traits::is_sequence<ValueType>::value>
|
Chris@16
|
74 struct pass_through_container_fusion_sequence
|
Chris@16
|
75 {
|
Chris@16
|
76 typedef typename mpl::find_if<
|
Chris@16
|
77 Attribute, not_compatible_element<Container, ValueType, mpl::_1>
|
Chris@16
|
78 >::type iter;
|
Chris@16
|
79 typedef typename mpl::end<Attribute>::type end;
|
Chris@16
|
80
|
Chris@16
|
81 typedef typename is_same<iter, end>::type type;
|
Chris@16
|
82 };
|
Chris@16
|
83
|
Chris@16
|
84 // If both, the Attribute and the value type of the provided container
|
Chris@16
|
85 // are Fusion sequences, we pass the container only if the two
|
Chris@16
|
86 // sequences are not compatible.
|
Chris@16
|
87 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
88 , typename Sequence>
|
Chris@16
|
89 struct pass_through_container_fusion_sequence<
|
Chris@16
|
90 Container, ValueType, Attribute, Sequence, true>
|
Chris@16
|
91 {
|
Chris@16
|
92 typedef typename mpl::find_if<
|
Chris@16
|
93 Attribute
|
Chris@16
|
94 , not_compatible_element<Container, ValueType, mpl::_1, Sequence>
|
Chris@16
|
95 >::type iter;
|
Chris@16
|
96 typedef typename mpl::end<Attribute>::type end;
|
Chris@16
|
97
|
Chris@16
|
98 typedef typename is_same<iter, end>::type type;
|
Chris@16
|
99 };
|
Chris@16
|
100
|
Chris@16
|
101 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
102 , typename Sequence>
|
Chris@16
|
103 struct pass_through_container_base<Container, ValueType, Attribute
|
Chris@16
|
104 , Sequence
|
Chris@16
|
105 , typename enable_if<fusion::traits::is_sequence<Attribute> >::type>
|
Chris@16
|
106 : pass_through_container_fusion_sequence<
|
Chris@16
|
107 Container, ValueType, Attribute, Sequence>
|
Chris@16
|
108 {};
|
Chris@16
|
109
|
Chris@16
|
110 // Specialization for containers
|
Chris@16
|
111 //
|
Chris@16
|
112 // If the value type of the attribute of the current component is not
|
Chris@16
|
113 // a Fusion sequence, we have to pass through the provided container if
|
Chris@16
|
114 // both are compatible.
|
Chris@16
|
115 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
116 , typename Sequence, typename AttributeValueType
|
Chris@16
|
117 , bool IsSequence = fusion::traits::is_sequence<AttributeValueType>::value>
|
Chris@16
|
118 struct pass_through_container_container
|
Chris@16
|
119 : mpl::or_<
|
Chris@16
|
120 traits::is_weak_substitute<Attribute, Container>
|
Chris@16
|
121 , traits::is_weak_substitute<AttributeValueType, Container> >
|
Chris@16
|
122 {};
|
Chris@16
|
123
|
Chris@16
|
124 // If the value type of the exposed container attribute is a Fusion
|
Chris@16
|
125 // sequence, we use the already existing logic for those.
|
Chris@16
|
126 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
127 , typename Sequence, typename AttributeValueType>
|
Chris@16
|
128 struct pass_through_container_container<
|
Chris@16
|
129 Container, ValueType, Attribute, Sequence, AttributeValueType, true>
|
Chris@16
|
130 : pass_through_container_fusion_sequence<
|
Chris@16
|
131 Container, ValueType, AttributeValueType, Sequence>
|
Chris@16
|
132 {};
|
Chris@16
|
133
|
Chris@16
|
134 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
135 , typename Sequence>
|
Chris@16
|
136 struct pass_through_container_base<
|
Chris@16
|
137 Container, ValueType, Attribute, Sequence
|
Chris@16
|
138 , typename enable_if<traits::is_container<Attribute> >::type>
|
Chris@16
|
139 : detail::pass_through_container_container<
|
Chris@16
|
140 Container, ValueType, Attribute, Sequence
|
Chris@16
|
141 , typename traits::container_value<Attribute>::type>
|
Chris@16
|
142 {};
|
Chris@16
|
143
|
Chris@16
|
144 // Specialization for exposed optional attributes
|
Chris@16
|
145 //
|
Chris@16
|
146 // If the type embedded in the exposed optional is not a Fusion
|
Chris@16
|
147 // sequence we pass through the container attribute if it is compatible
|
Chris@16
|
148 // either to the optionals embedded type or to the containers value
|
Chris@16
|
149 // type.
|
Chris@16
|
150 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
151 , typename Sequence
|
Chris@16
|
152 , bool IsSequence = fusion::traits::is_sequence<Attribute>::value>
|
Chris@16
|
153 struct pass_through_container_optional
|
Chris@16
|
154 : mpl::or_<
|
Chris@16
|
155 traits::is_weak_substitute<Attribute, Container>
|
Chris@16
|
156 , traits::is_weak_substitute<Attribute, ValueType> >
|
Chris@16
|
157 {};
|
Chris@16
|
158
|
Chris@16
|
159 // If the embedded type of the exposed optional attribute is a Fusion
|
Chris@16
|
160 // sequence, we use the already existing logic for those.
|
Chris@16
|
161 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
162 , typename Sequence>
|
Chris@16
|
163 struct pass_through_container_optional<
|
Chris@16
|
164 Container, ValueType, Attribute, Sequence, true>
|
Chris@16
|
165 : pass_through_container_fusion_sequence<
|
Chris@16
|
166 Container, ValueType, Attribute, Sequence>
|
Chris@16
|
167 {};
|
Chris@16
|
168
|
Chris@16
|
169 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
170 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
171 , typename Sequence>
|
Chris@16
|
172 struct pass_through_container
|
Chris@16
|
173 : pass_through_container_base<Container, ValueType, Attribute, Sequence>
|
Chris@16
|
174 {};
|
Chris@16
|
175
|
Chris@16
|
176 // Handle optional attributes
|
Chris@16
|
177 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
178 , typename Sequence>
|
Chris@16
|
179 struct pass_through_container<
|
Chris@16
|
180 Container, ValueType, boost::optional<Attribute>, Sequence>
|
Chris@16
|
181 : pass_through_container_optional<
|
Chris@16
|
182 Container, ValueType, Attribute, Sequence>
|
Chris@16
|
183 {};
|
Chris@16
|
184
|
Chris@16
|
185 // If both, the containers value type and the exposed attribute type are
|
Chris@16
|
186 // optionals we are allowed to pass through the the container only if the
|
Chris@16
|
187 // embedded types of those optionals are not compatible.
|
Chris@16
|
188 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
189 , typename Sequence>
|
Chris@16
|
190 struct pass_through_container<
|
Chris@16
|
191 Container, boost::optional<ValueType>, boost::optional<Attribute>
|
Chris@16
|
192 , Sequence>
|
Chris@16
|
193 : mpl::not_<traits::is_weak_substitute<Attribute, ValueType> >
|
Chris@16
|
194 {};
|
Chris@16
|
195
|
Chris@16
|
196 // Specialization for exposed variant attributes
|
Chris@16
|
197 //
|
Chris@16
|
198 // We pass through the container attribute if at least one of the embedded
|
Chris@16
|
199 // types in the variant requires to pass through the attribute
|
Chris@16
|
200
|
Chris@16
|
201 #define BOOST_SPIRIT_PASS_THROUGH_CONTAINER(z, N, _) \
|
Chris@16
|
202 pass_through_container<Container, ValueType, \
|
Chris@16
|
203 BOOST_PP_CAT(T, N), Sequence>::type::value || \
|
Chris@16
|
204 /***/
|
Chris@16
|
205
|
Chris@16
|
206 // make sure unused variant parameters do not affect the outcome
|
Chris@16
|
207 template <typename Container, typename ValueType, typename Sequence>
|
Chris@16
|
208 struct pass_through_container<Container, ValueType
|
Chris@16
|
209 , boost::detail::variant::void_, Sequence>
|
Chris@16
|
210 : mpl::false_
|
Chris@16
|
211 {};
|
Chris@16
|
212
|
Chris@16
|
213 template <typename Container, typename ValueType, typename Sequence
|
Chris@16
|
214 , BOOST_VARIANT_ENUM_PARAMS(typename T)>
|
Chris@16
|
215 struct pass_through_container<Container, ValueType
|
Chris@16
|
216 , boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Sequence>
|
Chris@16
|
217 : mpl::bool_<BOOST_PP_REPEAT(BOOST_VARIANT_LIMIT_TYPES
|
Chris@16
|
218 , BOOST_SPIRIT_PASS_THROUGH_CONTAINER, _) false>
|
Chris@16
|
219 {};
|
Chris@16
|
220
|
Chris@16
|
221 #undef BOOST_SPIRIT_PASS_THROUGH_CONTAINER
|
Chris@16
|
222 }}}}
|
Chris@16
|
223
|
Chris@16
|
224 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
225 namespace boost { namespace spirit { namespace traits
|
Chris@16
|
226 {
|
Chris@16
|
227 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
228 // forwarding customization point for domain qi::domain
|
Chris@16
|
229 template <typename Container, typename ValueType, typename Attribute
|
Chris@16
|
230 , typename Sequence>
|
Chris@16
|
231 struct pass_through_container<
|
Chris@16
|
232 Container, ValueType, Attribute, Sequence, qi::domain>
|
Chris@16
|
233 : qi::detail::pass_through_container<
|
Chris@16
|
234 Container, ValueType, Attribute, Sequence>
|
Chris@16
|
235 {};
|
Chris@16
|
236 }}}
|
Chris@16
|
237
|
Chris@16
|
238 namespace boost { namespace spirit { namespace qi { namespace detail
|
Chris@16
|
239 {
|
Chris@16
|
240 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
241 // This function handles the case where the attribute (Attr) given
|
Chris@16
|
242 // the sequence is an STL container. This is a wrapper around F.
|
Chris@16
|
243 // The function F does the actual parsing.
|
Chris@16
|
244 template <typename F, typename Attr, typename Sequence>
|
Chris@16
|
245 struct pass_container
|
Chris@16
|
246 {
|
Chris@16
|
247 typedef typename F::context_type context_type;
|
Chris@16
|
248 typedef typename F::iterator_type iterator_type;
|
Chris@16
|
249
|
Chris@16
|
250 pass_container(F const& f_, Attr& attr_)
|
Chris@16
|
251 : f(f_), attr(attr_) {}
|
Chris@16
|
252
|
Chris@16
|
253 // this is for the case when the current element exposes an attribute
|
Chris@16
|
254 // which is pushed back onto the container
|
Chris@16
|
255 template <typename Component>
|
Chris@16
|
256 bool dispatch_container(Component const& component, mpl::false_) const
|
Chris@16
|
257 {
|
Chris@16
|
258 // synthesized attribute needs to be default constructed
|
Chris@16
|
259 typename traits::container_value<Attr>::type val =
|
Chris@16
|
260 typename traits::container_value<Attr>::type();
|
Chris@16
|
261
|
Chris@16
|
262 iterator_type save = f.first;
|
Chris@16
|
263 bool r = f(component, val);
|
Chris@16
|
264 if (!r)
|
Chris@16
|
265 {
|
Chris@16
|
266 // push the parsed value into our attribute
|
Chris@16
|
267 r = !traits::push_back(attr, val);
|
Chris@16
|
268 if (r)
|
Chris@16
|
269 f.first = save;
|
Chris@16
|
270 }
|
Chris@16
|
271 return r;
|
Chris@16
|
272 }
|
Chris@16
|
273
|
Chris@16
|
274 // this is for the case when the current element is able to handle an
|
Chris@16
|
275 // attribute which is a container itself, this element will push its
|
Chris@16
|
276 // data directly into the attribute container
|
Chris@16
|
277 template <typename Component>
|
Chris@16
|
278 bool dispatch_container(Component const& component, mpl::true_) const
|
Chris@16
|
279 {
|
Chris@16
|
280 return f(component, attr);
|
Chris@16
|
281 }
|
Chris@16
|
282
|
Chris@16
|
283 ///////////////////////////////////////////////////////////////////////
|
Chris@16
|
284 // this is for the case when the current element doesn't expect an
|
Chris@16
|
285 // attribute
|
Chris@16
|
286 template <typename Component>
|
Chris@16
|
287 bool dispatch_attribute(Component const& component, mpl::false_) const
|
Chris@16
|
288 {
|
Chris@16
|
289 return f(component, unused);
|
Chris@16
|
290 }
|
Chris@16
|
291
|
Chris@16
|
292 // the current element expects an attribute
|
Chris@16
|
293 template <typename Component>
|
Chris@16
|
294 bool dispatch_attribute(Component const& component, mpl::true_) const
|
Chris@16
|
295 {
|
Chris@16
|
296 typedef typename traits::container_value<Attr>::type value_type;
|
Chris@16
|
297 typedef typename traits::attribute_of<
|
Chris@16
|
298 Component, context_type, iterator_type>::type
|
Chris@16
|
299 rhs_attribute;
|
Chris@16
|
300
|
Chris@16
|
301 // this predicate detects, whether the attribute of the current
|
Chris@16
|
302 // element is a substitute for the value type of the container
|
Chris@16
|
303 // attribute
|
Chris@16
|
304 typedef mpl::and_<
|
Chris@16
|
305 traits::handles_container<
|
Chris@16
|
306 Component, Attr, context_type, iterator_type>
|
Chris@16
|
307 , traits::pass_through_container<
|
Chris@16
|
308 Attr, value_type, rhs_attribute, Sequence, qi::domain>
|
Chris@16
|
309 > predicate;
|
Chris@16
|
310
|
Chris@16
|
311 return dispatch_container(component, predicate());
|
Chris@16
|
312 }
|
Chris@16
|
313
|
Chris@16
|
314 // Dispatches to dispatch_main depending on the attribute type
|
Chris@16
|
315 // of the Component
|
Chris@16
|
316 template <typename Component>
|
Chris@16
|
317 bool operator()(Component const& component) const
|
Chris@16
|
318 {
|
Chris@16
|
319 // we need to dispatch depending on the type of the attribute
|
Chris@16
|
320 // of the current element (component). If this is has no attribute
|
Chris@16
|
321 // we shouldn't pass an attribute at all.
|
Chris@16
|
322 typedef typename traits::not_is_unused<
|
Chris@16
|
323 typename traits::attribute_of<
|
Chris@16
|
324 Component, context_type, iterator_type
|
Chris@16
|
325 >::type
|
Chris@16
|
326 >::type predicate;
|
Chris@16
|
327
|
Chris@16
|
328 // ensure the attribute is actually a container type
|
Chris@16
|
329 traits::make_container(attr);
|
Chris@16
|
330
|
Chris@16
|
331 return dispatch_attribute(component, predicate());
|
Chris@16
|
332 }
|
Chris@16
|
333
|
Chris@16
|
334 F f;
|
Chris@16
|
335 Attr& attr;
|
Chris@16
|
336
|
Chris@16
|
337 private:
|
Chris@16
|
338 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
339 pass_container& operator= (pass_container const&);
|
Chris@16
|
340 };
|
Chris@16
|
341
|
Chris@16
|
342 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
343 // Utility function to make a pass_container for container components
|
Chris@16
|
344 // (kleene, list, plus, repeat)
|
Chris@16
|
345 template <typename F, typename Attr>
|
Chris@16
|
346 inline pass_container<F, Attr, mpl::false_>
|
Chris@16
|
347 make_pass_container(F const& f, Attr& attr)
|
Chris@16
|
348 {
|
Chris@16
|
349 return pass_container<F, Attr, mpl::false_>(f, attr);
|
Chris@16
|
350 }
|
Chris@16
|
351
|
Chris@16
|
352 // Utility function to make a pass_container for sequences
|
Chris@16
|
353 template <typename F, typename Attr>
|
Chris@16
|
354 inline pass_container<F, Attr, mpl::true_>
|
Chris@16
|
355 make_sequence_pass_container(F const& f, Attr& attr)
|
Chris@16
|
356 {
|
Chris@16
|
357 return pass_container<F, Attr, mpl::true_>(f, attr);
|
Chris@16
|
358 }
|
Chris@16
|
359 }}}}
|
Chris@16
|
360
|
Chris@16
|
361 #endif
|
Chris@16
|
362
|