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_REPEAT_MAY_18_2009_0926AM)
|
Chris@16
|
8 #define SPIRIT_KARMA_REPEAT_MAY_18_2009_0926AM
|
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/meta_compiler.hpp>
|
Chris@16
|
15 #include <boost/spirit/home/karma/detail/output_iterator.hpp>
|
Chris@16
|
16 #include <boost/spirit/home/karma/detail/get_stricttag.hpp>
|
Chris@16
|
17 #include <boost/spirit/home/karma/generator.hpp>
|
Chris@16
|
18 #include <boost/spirit/home/karma/auxiliary/lazy.hpp>
|
Chris@16
|
19 #include <boost/spirit/home/karma/operator/kleene.hpp>
|
Chris@16
|
20 #include <boost/spirit/home/support/container.hpp>
|
Chris@16
|
21 #include <boost/spirit/home/support/common_terminals.hpp>
|
Chris@16
|
22 #include <boost/spirit/home/support/has_semantic_action.hpp>
|
Chris@16
|
23 #include <boost/spirit/home/support/handles_container.hpp>
|
Chris@16
|
24 #include <boost/spirit/home/karma/detail/attributes.hpp>
|
Chris@16
|
25 #include <boost/spirit/home/support/info.hpp>
|
Chris@16
|
26 #include <boost/fusion/include/at.hpp>
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost { namespace spirit
|
Chris@16
|
29 {
|
Chris@16
|
30 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
31 // Enablers
|
Chris@16
|
32 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
33 template <>
|
Chris@16
|
34 struct use_directive<karma::domain, tag::repeat> // enables repeat[p]
|
Chris@16
|
35 : mpl::true_ {};
|
Chris@16
|
36
|
Chris@16
|
37 template <typename T>
|
Chris@16
|
38 struct use_directive<karma::domain
|
Chris@16
|
39 , terminal_ex<tag::repeat // enables repeat(exact)[p]
|
Chris@16
|
40 , fusion::vector1<T> >
|
Chris@16
|
41 > : mpl::true_ {};
|
Chris@16
|
42
|
Chris@16
|
43 template <typename T>
|
Chris@16
|
44 struct use_directive<karma::domain
|
Chris@16
|
45 , terminal_ex<tag::repeat // enables repeat(min, max)[p]
|
Chris@16
|
46 , fusion::vector2<T, T> >
|
Chris@16
|
47 > : mpl::true_ {};
|
Chris@16
|
48
|
Chris@16
|
49 template <typename T>
|
Chris@16
|
50 struct use_directive<karma::domain
|
Chris@16
|
51 , terminal_ex<tag::repeat // enables repeat(min, inf)[p]
|
Chris@16
|
52 , fusion::vector2<T, inf_type> >
|
Chris@16
|
53 > : mpl::true_ {};
|
Chris@16
|
54
|
Chris@16
|
55 template <> // enables *lazy* repeat(exact)[p]
|
Chris@16
|
56 struct use_lazy_directive<
|
Chris@16
|
57 karma::domain
|
Chris@16
|
58 , tag::repeat
|
Chris@16
|
59 , 1 // arity
|
Chris@16
|
60 > : mpl::true_ {};
|
Chris@16
|
61
|
Chris@16
|
62 template <> // enables *lazy* repeat(min, max)[p]
|
Chris@16
|
63 struct use_lazy_directive< // and repeat(min, inf)[p]
|
Chris@16
|
64 karma::domain
|
Chris@16
|
65 , tag::repeat
|
Chris@16
|
66 , 2 // arity
|
Chris@16
|
67 > : mpl::true_ {};
|
Chris@16
|
68 }}
|
Chris@16
|
69
|
Chris@16
|
70 namespace boost { namespace spirit { namespace karma
|
Chris@16
|
71 {
|
Chris@16
|
72 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
|
Chris@16
|
73 using spirit::repeat;
|
Chris@16
|
74 using spirit::inf;
|
Chris@16
|
75 #endif
|
Chris@16
|
76 using spirit::repeat_type;
|
Chris@16
|
77 using spirit::inf_type;
|
Chris@16
|
78
|
Chris@16
|
79 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
80 // handles repeat(exact)[p]
|
Chris@16
|
81 template <typename T>
|
Chris@16
|
82 struct exact_iterator
|
Chris@16
|
83 {
|
Chris@16
|
84 exact_iterator(T const exact)
|
Chris@16
|
85 : exact(exact) {}
|
Chris@16
|
86
|
Chris@16
|
87 typedef T type;
|
Chris@16
|
88 T start() const { return 0; }
|
Chris@16
|
89 bool got_max(T i) const { return i >= exact; }
|
Chris@16
|
90 bool got_min(T i) const { return i >= exact; }
|
Chris@16
|
91
|
Chris@16
|
92 T const exact;
|
Chris@16
|
93
|
Chris@16
|
94 private:
|
Chris@16
|
95 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
96 exact_iterator& operator= (exact_iterator const&);
|
Chris@16
|
97 };
|
Chris@16
|
98
|
Chris@16
|
99 // handles repeat(min, max)[p]
|
Chris@16
|
100 template <typename T>
|
Chris@16
|
101 struct finite_iterator
|
Chris@16
|
102 {
|
Chris@16
|
103 finite_iterator(T const min, T const max)
|
Chris@16
|
104 : min BOOST_PREVENT_MACRO_SUBSTITUTION (min)
|
Chris@16
|
105 , max BOOST_PREVENT_MACRO_SUBSTITUTION (max) {}
|
Chris@16
|
106
|
Chris@16
|
107 typedef T type;
|
Chris@16
|
108 T start() const { return 0; }
|
Chris@16
|
109 bool got_max(T i) const { return i >= max; }
|
Chris@16
|
110 bool got_min(T i) const { return i >= min; }
|
Chris@16
|
111
|
Chris@16
|
112 T const min;
|
Chris@16
|
113 T const max;
|
Chris@16
|
114
|
Chris@16
|
115 private:
|
Chris@16
|
116 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
117 finite_iterator& operator= (finite_iterator const&);
|
Chris@16
|
118 };
|
Chris@16
|
119
|
Chris@16
|
120 // handles repeat(min, inf)[p]
|
Chris@16
|
121 template <typename T>
|
Chris@16
|
122 struct infinite_iterator
|
Chris@16
|
123 {
|
Chris@16
|
124 infinite_iterator(T const min)
|
Chris@16
|
125 : min BOOST_PREVENT_MACRO_SUBSTITUTION (min) {}
|
Chris@16
|
126
|
Chris@16
|
127 typedef T type;
|
Chris@16
|
128 T start() const { return 0; }
|
Chris@16
|
129 bool got_max(T /*i*/) const { return false; }
|
Chris@16
|
130 bool got_min(T i) const { return i >= min; }
|
Chris@16
|
131
|
Chris@16
|
132 T const min;
|
Chris@16
|
133
|
Chris@16
|
134 private:
|
Chris@16
|
135 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
136 infinite_iterator& operator= (infinite_iterator const&);
|
Chris@16
|
137 };
|
Chris@16
|
138
|
Chris@16
|
139 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
140 template <typename Subject, typename LoopIter, typename Strict
|
Chris@16
|
141 , typename Derived>
|
Chris@16
|
142 struct base_repeat_generator : unary_generator<Derived>
|
Chris@16
|
143 {
|
Chris@16
|
144 private:
|
Chris@16
|
145 // iterate over the given container until its exhausted or the embedded
|
Chris@16
|
146 // generator succeeds
|
Chris@16
|
147 template <typename F, typename Attribute>
|
Chris@16
|
148 bool generate_subject(F f, Attribute const&, mpl::false_) const
|
Chris@16
|
149 {
|
Chris@16
|
150 // Failing subject generators are just skipped. This allows to
|
Chris@16
|
151 // selectively generate items in the provided attribute.
|
Chris@16
|
152 while (!f.is_at_end())
|
Chris@16
|
153 {
|
Chris@16
|
154 bool r = !f(subject);
|
Chris@16
|
155 if (r)
|
Chris@16
|
156 return true;
|
Chris@16
|
157 if (!f.is_at_end())
|
Chris@16
|
158 f.next();
|
Chris@16
|
159 }
|
Chris@16
|
160 return false;
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 template <typename F, typename Attribute>
|
Chris@16
|
164 bool generate_subject(F f, Attribute const&, mpl::true_) const
|
Chris@16
|
165 {
|
Chris@16
|
166 return !f(subject);
|
Chris@16
|
167 }
|
Chris@16
|
168
|
Chris@16
|
169 // There is no way to distinguish a failed generator from a
|
Chris@16
|
170 // generator to be skipped. We assume the user takes responsibility
|
Chris@16
|
171 // for ending the loop if no attribute is specified.
|
Chris@16
|
172 template <typename F>
|
Chris@16
|
173 bool generate_subject(F f, unused_type, mpl::false_) const
|
Chris@16
|
174 {
|
Chris@16
|
175 return !f(subject);
|
Chris@16
|
176 }
|
Chris@16
|
177
|
Chris@16
|
178 public:
|
Chris@16
|
179 typedef Subject subject_type;
|
Chris@16
|
180
|
Chris@16
|
181 typedef mpl::int_<subject_type::properties::value> properties;
|
Chris@16
|
182
|
Chris@16
|
183 // Build a std::vector from the subject's attribute. Note
|
Chris@16
|
184 // that build_std_vector may return unused_type if the
|
Chris@16
|
185 // subject's attribute is an unused_type.
|
Chris@16
|
186 template <typename Context, typename Iterator>
|
Chris@16
|
187 struct attribute
|
Chris@16
|
188 : traits::build_std_vector<
|
Chris@16
|
189 typename traits::attribute_of<Subject, Context, Iterator>::type
|
Chris@16
|
190 >
|
Chris@16
|
191 {};
|
Chris@16
|
192
|
Chris@16
|
193 base_repeat_generator(Subject const& subject, LoopIter const& iter)
|
Chris@16
|
194 : subject(subject), iter(iter) {}
|
Chris@16
|
195
|
Chris@16
|
196 template <typename OutputIterator, typename Context, typename Delimiter
|
Chris@16
|
197 , typename Attribute>
|
Chris@16
|
198 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
|
Chris@16
|
199 , Attribute const& attr) const
|
Chris@16
|
200 {
|
Chris@16
|
201 typedef detail::fail_function<
|
Chris@16
|
202 OutputIterator, Context, Delimiter
|
Chris@16
|
203 > fail_function;
|
Chris@16
|
204
|
Chris@16
|
205 typedef typename traits::container_iterator<
|
Chris@16
|
206 typename add_const<Attribute>::type
|
Chris@16
|
207 >::type iterator_type;
|
Chris@16
|
208
|
Chris@16
|
209 typedef
|
Chris@16
|
210 typename traits::make_indirect_iterator<iterator_type>::type
|
Chris@16
|
211 indirect_iterator_type;
|
Chris@16
|
212
|
Chris@16
|
213 typedef detail::pass_container<
|
Chris@16
|
214 fail_function, Attribute, indirect_iterator_type, mpl::false_>
|
Chris@16
|
215 pass_container;
|
Chris@16
|
216
|
Chris@16
|
217 iterator_type it = traits::begin(attr);
|
Chris@16
|
218 iterator_type end = traits::end(attr);
|
Chris@16
|
219
|
Chris@16
|
220 pass_container pass(fail_function(sink, ctx, d),
|
Chris@16
|
221 indirect_iterator_type(it), indirect_iterator_type(end));
|
Chris@16
|
222
|
Chris@16
|
223 // generate the minimal required amount of output
|
Chris@16
|
224 typename LoopIter::type i = iter.start();
|
Chris@16
|
225 for (/**/; !pass.is_at_end() && !iter.got_min(i); ++i)
|
Chris@16
|
226 {
|
Chris@16
|
227 if (!generate_subject(pass, attr, Strict()))
|
Chris@16
|
228 {
|
Chris@16
|
229 // if we fail before reaching the minimum iteration
|
Chris@16
|
230 // required, do not output anything and return false
|
Chris@16
|
231 return false;
|
Chris@16
|
232 }
|
Chris@16
|
233 }
|
Chris@16
|
234
|
Chris@16
|
235 if (pass.is_at_end() && !iter.got_min(i))
|
Chris@16
|
236 return false; // insufficient attribute elements
|
Chris@16
|
237
|
Chris@16
|
238 // generate some more up to the maximum specified
|
Chris@16
|
239 for (/**/; !pass.is_at_end() && !iter.got_max(i); ++i)
|
Chris@16
|
240 {
|
Chris@16
|
241 if (!generate_subject(pass, attr, Strict()))
|
Chris@16
|
242 break;
|
Chris@16
|
243 }
|
Chris@16
|
244 return detail::sink_is_good(sink);
|
Chris@16
|
245 }
|
Chris@16
|
246
|
Chris@16
|
247 template <typename Context>
|
Chris@16
|
248 info what(Context& context) const
|
Chris@16
|
249 {
|
Chris@16
|
250 return info("repeat", subject.what(context));
|
Chris@16
|
251 }
|
Chris@16
|
252
|
Chris@16
|
253 Subject subject;
|
Chris@16
|
254 LoopIter iter;
|
Chris@16
|
255 };
|
Chris@16
|
256
|
Chris@16
|
257 template <typename Subject, typename LoopIter>
|
Chris@16
|
258 struct repeat_generator
|
Chris@16
|
259 : base_repeat_generator<
|
Chris@16
|
260 Subject, LoopIter, mpl::false_
|
Chris@16
|
261 , repeat_generator<Subject, LoopIter> >
|
Chris@16
|
262 {
|
Chris@16
|
263 typedef base_repeat_generator<
|
Chris@16
|
264 Subject, LoopIter, mpl::false_, repeat_generator
|
Chris@16
|
265 > base_repeat_generator_;
|
Chris@16
|
266
|
Chris@16
|
267 repeat_generator(Subject const& subject, LoopIter const& iter)
|
Chris@16
|
268 : base_repeat_generator_(subject, iter) {}
|
Chris@16
|
269 };
|
Chris@16
|
270
|
Chris@16
|
271 template <typename Subject, typename LoopIter>
|
Chris@16
|
272 struct strict_repeat_generator
|
Chris@16
|
273 : base_repeat_generator<
|
Chris@16
|
274 Subject, LoopIter, mpl::true_
|
Chris@16
|
275 , strict_repeat_generator<Subject, LoopIter> >
|
Chris@16
|
276 {
|
Chris@16
|
277 typedef base_repeat_generator<
|
Chris@16
|
278 Subject, LoopIter, mpl::true_, strict_repeat_generator
|
Chris@16
|
279 > base_repeat_generator_;
|
Chris@16
|
280
|
Chris@16
|
281 strict_repeat_generator(Subject const& subject, LoopIter const& iter)
|
Chris@16
|
282 : base_repeat_generator_(subject, iter) {}
|
Chris@16
|
283 };
|
Chris@16
|
284
|
Chris@16
|
285 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
286 // Generator generators: make_xxx function (objects)
|
Chris@16
|
287 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
288 template <typename Subject, typename Modifiers>
|
Chris@16
|
289 struct make_directive<tag::repeat, Subject, Modifiers>
|
Chris@16
|
290 {
|
Chris@16
|
291 typedef typename mpl::if_<
|
Chris@16
|
292 detail::get_stricttag<Modifiers>
|
Chris@16
|
293 , strict_kleene<Subject>, kleene<Subject>
|
Chris@16
|
294 >::type result_type;
|
Chris@16
|
295
|
Chris@16
|
296 result_type operator()(unused_type, Subject const& subject
|
Chris@16
|
297 , unused_type) const
|
Chris@16
|
298 {
|
Chris@16
|
299 return result_type(subject);
|
Chris@16
|
300 }
|
Chris@16
|
301 };
|
Chris@16
|
302
|
Chris@16
|
303 template <typename T, typename Subject, typename Modifiers>
|
Chris@16
|
304 struct make_directive<
|
Chris@16
|
305 terminal_ex<tag::repeat, fusion::vector1<T> >, Subject, Modifiers>
|
Chris@16
|
306 {
|
Chris@16
|
307 typedef exact_iterator<T> iterator_type;
|
Chris@16
|
308
|
Chris@16
|
309 typedef typename mpl::if_<
|
Chris@16
|
310 detail::get_stricttag<Modifiers>
|
Chris@16
|
311 , strict_repeat_generator<Subject, iterator_type>
|
Chris@16
|
312 , repeat_generator<Subject, iterator_type>
|
Chris@16
|
313 >::type result_type;
|
Chris@16
|
314
|
Chris@16
|
315 template <typename Terminal>
|
Chris@16
|
316 result_type operator()(
|
Chris@16
|
317 Terminal const& term, Subject const& subject, unused_type) const
|
Chris@16
|
318 {
|
Chris@16
|
319 return result_type(subject, fusion::at_c<0>(term.args));
|
Chris@16
|
320 }
|
Chris@16
|
321 };
|
Chris@16
|
322
|
Chris@16
|
323 template <typename T, typename Subject, typename Modifiers>
|
Chris@16
|
324 struct make_directive<
|
Chris@16
|
325 terminal_ex<tag::repeat, fusion::vector2<T, T> >, Subject, Modifiers>
|
Chris@16
|
326 {
|
Chris@16
|
327 typedef finite_iterator<T> iterator_type;
|
Chris@16
|
328
|
Chris@16
|
329 typedef typename mpl::if_<
|
Chris@16
|
330 detail::get_stricttag<Modifiers>
|
Chris@16
|
331 , strict_repeat_generator<Subject, iterator_type>
|
Chris@16
|
332 , repeat_generator<Subject, iterator_type>
|
Chris@16
|
333 >::type result_type;
|
Chris@16
|
334
|
Chris@16
|
335 template <typename Terminal>
|
Chris@16
|
336 result_type operator()(
|
Chris@16
|
337 Terminal const& term, Subject const& subject, unused_type) const
|
Chris@16
|
338 {
|
Chris@16
|
339 return result_type(subject,
|
Chris@16
|
340 iterator_type(
|
Chris@16
|
341 fusion::at_c<0>(term.args)
|
Chris@16
|
342 , fusion::at_c<1>(term.args)
|
Chris@16
|
343 )
|
Chris@16
|
344 );
|
Chris@16
|
345 }
|
Chris@16
|
346 };
|
Chris@16
|
347
|
Chris@16
|
348 template <typename T, typename Subject, typename Modifiers>
|
Chris@16
|
349 struct make_directive<
|
Chris@16
|
350 terminal_ex<tag::repeat
|
Chris@16
|
351 , fusion::vector2<T, inf_type> >, Subject, Modifiers>
|
Chris@16
|
352 {
|
Chris@16
|
353 typedef infinite_iterator<T> iterator_type;
|
Chris@16
|
354
|
Chris@16
|
355 typedef typename mpl::if_<
|
Chris@16
|
356 detail::get_stricttag<Modifiers>
|
Chris@16
|
357 , strict_repeat_generator<Subject, iterator_type>
|
Chris@16
|
358 , repeat_generator<Subject, iterator_type>
|
Chris@16
|
359 >::type result_type;
|
Chris@16
|
360
|
Chris@16
|
361 template <typename Terminal>
|
Chris@16
|
362 result_type operator()(
|
Chris@16
|
363 Terminal const& term, Subject const& subject, unused_type) const
|
Chris@16
|
364 {
|
Chris@16
|
365 return result_type(subject, fusion::at_c<0>(term.args));
|
Chris@16
|
366 }
|
Chris@16
|
367 };
|
Chris@16
|
368 }}}
|
Chris@16
|
369
|
Chris@16
|
370 namespace boost { namespace spirit { namespace traits
|
Chris@16
|
371 {
|
Chris@16
|
372 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
373 template <typename Subject, typename LoopIter>
|
Chris@16
|
374 struct has_semantic_action<karma::repeat_generator<Subject, LoopIter> >
|
Chris@16
|
375 : unary_has_semantic_action<Subject> {};
|
Chris@16
|
376
|
Chris@16
|
377 template <typename Subject, typename LoopIter>
|
Chris@16
|
378 struct has_semantic_action<karma::strict_repeat_generator<Subject, LoopIter> >
|
Chris@16
|
379 : unary_has_semantic_action<Subject> {};
|
Chris@16
|
380
|
Chris@16
|
381 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
382 template <typename Subject, typename LoopIter, typename Attribute
|
Chris@16
|
383 , typename Context, typename Iterator>
|
Chris@16
|
384 struct handles_container<
|
Chris@16
|
385 karma::repeat_generator<Subject, LoopIter>, Attribute
|
Chris@16
|
386 , Context, Iterator>
|
Chris@16
|
387 : mpl::true_ {};
|
Chris@16
|
388
|
Chris@16
|
389 template <typename Subject, typename LoopIter, typename Attribute
|
Chris@16
|
390 , typename Context, typename Iterator>
|
Chris@16
|
391 struct handles_container<
|
Chris@16
|
392 karma::strict_repeat_generator<Subject, LoopIter>, Attribute
|
Chris@16
|
393 , Context, Iterator>
|
Chris@16
|
394 : mpl::true_ {};
|
Chris@16
|
395 }}}
|
Chris@16
|
396
|
Chris@16
|
397 #endif
|