1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #if !defined(SPIRIT_REPEAT_NOVEMBER_14_2008_1148AM)
9 #define SPIRIT_REPEAT_NOVEMBER_14_2008_1148AM
15 #include <boost/spirit/home/qi/meta_compiler.hpp>
16 #include <boost/spirit/home/qi/parser.hpp>
17 #include <boost/spirit/home/qi/auxiliary/lazy.hpp>
18 #include <boost/spirit/home/qi/operator/kleene.hpp>
19 #include <boost/spirit/home/support/container.hpp>
20 #include <boost/spirit/home/support/common_terminals.hpp>
21 #include <boost/spirit/home/qi/detail/attributes.hpp>
22 #include <boost/spirit/home/qi/detail/fail_function.hpp>
23 #include <boost/spirit/home/qi/detail/pass_container.hpp>
24 #include <boost/spirit/home/support/info.hpp>
25 #include <boost/spirit/home/support/has_semantic_action.hpp>
26 #include <boost/spirit/home/support/handles_container.hpp>
27 #include <boost/fusion/include/at.hpp>
30 namespace boost { namespace spirit
32 ///////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////
36 struct use_directive<qi::domain, tag::repeat> // enables repeat[p]
40 struct use_directive<qi::domain
41 , terminal_ex<tag::repeat // enables repeat(exact)[p]
42 , fusion::vector1<T> >
46 struct use_directive<qi::domain
47 , terminal_ex<tag::repeat // enables repeat(min, max)[p]
48 , fusion::vector2<T, T> >
52 struct use_directive<qi::domain
53 , terminal_ex<tag::repeat // enables repeat(min, inf)[p]
54 , fusion::vector2<T, inf_type> >
57 template <> // enables *lazy* repeat(exact)[p]
58 struct use_lazy_directive<
64 template <> // enables *lazy* repeat(min, max)[p]
65 struct use_lazy_directive< // and repeat(min, inf)[p]
72 namespace boost { namespace spirit { namespace qi
74 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
78 using spirit::repeat_type;
79 using spirit::inf_type;
82 struct exact_iterator // handles repeat(exact)[p]
84 exact_iterator(T const exact_)
88 T start() const { return 0; }
89 bool got_max(T i) const { return i >= exact; }
90 bool got_min(T i) const { return i >= exact; }
95 // silence MSVC warning C4512: assignment operator could not be generated
96 exact_iterator& operator= (exact_iterator const&);
100 struct finite_iterator // handles repeat(min, max)[p]
102 finite_iterator(T const min_, T const max_)
103 : min BOOST_PREVENT_MACRO_SUBSTITUTION (min_)
104 , max BOOST_PREVENT_MACRO_SUBSTITUTION (max_) {}
107 T start() const { return 0; }
108 bool got_max(T i) const { return i >= max; }
109 bool got_min(T i) const { return i >= min; }
115 // silence MSVC warning C4512: assignment operator could not be generated
116 finite_iterator& operator= (finite_iterator const&);
119 template <typename T>
120 struct infinite_iterator // handles repeat(min, inf)[p]
122 infinite_iterator(T const min_)
123 : min BOOST_PREVENT_MACRO_SUBSTITUTION (min_) {}
126 T start() const { return 0; }
127 bool got_max(T /*i*/) const { return false; }
128 bool got_min(T i) const { return i >= min; }
133 // silence MSVC warning C4512: assignment operator could not be generated
134 infinite_iterator& operator= (infinite_iterator const&);
137 template <typename Subject, typename LoopIter>
138 struct repeat_parser : unary_parser<repeat_parser<Subject, LoopIter> >
140 typedef Subject subject_type;
142 template <typename Context, typename Iterator>
145 // Build a std::vector from the subject's attribute. Note
146 // that build_std_vector may return unused_type if the
147 // subject's attribute is an unused_type.
149 traits::build_std_vector<
150 typename traits::attribute_of<
151 Subject, Context, Iterator>::type
156 repeat_parser(Subject const& subject_, LoopIter const& iter_)
157 : subject(subject_), iter(iter_) {}
159 template <typename F>
160 bool parse_container(F f) const
162 typename LoopIter::type i = iter.start();
163 for (/**/; !iter.got_min(i); ++i)
169 // parse some more up to the maximum specified
170 typename F::iterator_type save = f.f.first;
171 for (/**/; !iter.got_max(i); ++i)
182 template <typename Iterator, typename Context
183 , typename Skipper, typename Attribute>
184 bool parse(Iterator& first, Iterator const& last
185 , Context& context, Skipper const& skipper
186 , Attribute& attr_) const
188 typedef detail::fail_function<Iterator, Context, Skipper>
191 // ensure the attribute is actually a container type
192 traits::make_container(attr_);
194 Iterator iter_local = first;
195 fail_function f(iter_local, last, context, skipper);
196 if (!parse_container(detail::make_pass_container(f, attr_)))
203 template <typename Context>
204 info what(Context& context) const
206 return info("repeat", subject.what(context));
213 // silence MSVC warning C4512: assignment operator could not be generated
214 repeat_parser& operator= (repeat_parser const&);
217 ///////////////////////////////////////////////////////////////////////////
218 // Parser generators: make_xxx function (objects)
219 ///////////////////////////////////////////////////////////////////////////
220 template <typename Subject, typename Modifiers>
221 struct make_directive<tag::repeat, Subject, Modifiers>
223 typedef kleene<Subject> result_type;
224 result_type operator()(unused_type, Subject const& subject, unused_type) const
226 return result_type(subject);
230 template <typename T, typename Subject, typename Modifiers>
231 struct make_directive<
232 terminal_ex<tag::repeat, fusion::vector1<T> >, Subject, Modifiers>
234 typedef exact_iterator<T> iterator_type;
235 typedef repeat_parser<Subject, iterator_type> result_type;
237 template <typename Terminal>
238 result_type operator()(
239 Terminal const& term, Subject const& subject, unused_type) const
241 return result_type(subject, fusion::at_c<0>(term.args));
245 template <typename T, typename Subject, typename Modifiers>
246 struct make_directive<
247 terminal_ex<tag::repeat, fusion::vector2<T, T> >, Subject, Modifiers>
249 typedef finite_iterator<T> iterator_type;
250 typedef repeat_parser<Subject, iterator_type> result_type;
252 template <typename Terminal>
253 result_type operator()(
254 Terminal const& term, Subject const& subject, unused_type) const
256 return result_type(subject,
258 fusion::at_c<0>(term.args)
259 , fusion::at_c<1>(term.args)
265 template <typename T, typename Subject, typename Modifiers>
266 struct make_directive<
267 terminal_ex<tag::repeat
268 , fusion::vector2<T, inf_type> >, Subject, Modifiers>
270 typedef infinite_iterator<T> iterator_type;
271 typedef repeat_parser<Subject, iterator_type> result_type;
273 template <typename Terminal>
274 result_type operator()(
275 Terminal const& term, Subject const& subject, unused_type) const
277 return result_type(subject, fusion::at_c<0>(term.args));
282 namespace boost { namespace spirit { namespace traits
284 ///////////////////////////////////////////////////////////////////////////
285 template <typename Subject, typename LoopIter>
286 struct has_semantic_action<qi::repeat_parser<Subject, LoopIter> >
287 : unary_has_semantic_action<Subject> {};
289 ///////////////////////////////////////////////////////////////////////////
290 template <typename Subject, typename LoopIter, typename Attribute
291 , typename Context, typename Iterator>
292 struct handles_container<qi::repeat_parser<Subject, LoopIter>
293 , Attribute, Context, Iterator>