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; }
94 // silence MSVC warning C4512: assignment operator could not be generated
95 BOOST_DELETED_FUNCTION(exact_iterator& operator= (exact_iterator const&))
99 struct finite_iterator // handles repeat(min, max)[p]
101 finite_iterator(T const min_, T const max_)
102 : min BOOST_PREVENT_MACRO_SUBSTITUTION (min_)
103 , max BOOST_PREVENT_MACRO_SUBSTITUTION (max_) {}
106 T start() const { return 0; }
107 bool got_max(T i) const { return i >= max; }
108 bool got_min(T i) const { return i >= min; }
113 // silence MSVC warning C4512: assignment operator could not be generated
114 BOOST_DELETED_FUNCTION(finite_iterator& operator= (finite_iterator const&))
117 template <typename T>
118 struct infinite_iterator // handles repeat(min, inf)[p]
120 infinite_iterator(T const min_)
121 : min BOOST_PREVENT_MACRO_SUBSTITUTION (min_) {}
124 T start() const { return 0; }
125 bool got_max(T /*i*/) const { return false; }
126 bool got_min(T i) const { return i >= min; }
130 // silence MSVC warning C4512: assignment operator could not be generated
131 BOOST_DELETED_FUNCTION(infinite_iterator& operator= (infinite_iterator const&))
134 template <typename Subject, typename LoopIter>
135 struct repeat_parser : unary_parser<repeat_parser<Subject, LoopIter> >
137 typedef Subject subject_type;
139 template <typename Context, typename Iterator>
142 // Build a std::vector from the subject's attribute. Note
143 // that build_std_vector may return unused_type if the
144 // subject's attribute is an unused_type.
146 traits::build_std_vector<
147 typename traits::attribute_of<
148 Subject, Context, Iterator>::type
153 repeat_parser(Subject const& subject_, LoopIter const& iter_)
154 : subject(subject_), iter(iter_) {}
156 template <typename F>
157 bool parse_container(F f) const
159 typename LoopIter::type i = iter.start();
160 for (/**/; !iter.got_min(i); ++i)
166 // parse some more up to the maximum specified
167 typename F::iterator_type save = f.f.first;
168 for (/**/; !iter.got_max(i); ++i)
179 template <typename Iterator, typename Context
180 , typename Skipper, typename Attribute>
181 bool parse(Iterator& first, Iterator const& last
182 , Context& context, Skipper const& skipper
183 , Attribute& attr_) const
185 typedef detail::fail_function<Iterator, Context, Skipper>
188 // ensure the attribute is actually a container type
189 traits::make_container(attr_);
191 Iterator iter_local = first;
192 fail_function f(iter_local, last, context, skipper);
193 if (!parse_container(detail::make_pass_container(f, attr_)))
200 template <typename Context>
201 info what(Context& context) const
203 return info("repeat", subject.what(context));
209 // silence MSVC warning C4512: assignment operator could not be generated
210 BOOST_DELETED_FUNCTION(repeat_parser& operator= (repeat_parser const&))
213 ///////////////////////////////////////////////////////////////////////////
214 // Parser generators: make_xxx function (objects)
215 ///////////////////////////////////////////////////////////////////////////
216 template <typename Subject, typename Modifiers>
217 struct make_directive<tag::repeat, Subject, Modifiers>
219 typedef kleene<Subject> result_type;
220 result_type operator()(unused_type, Subject const& subject, unused_type) const
222 return result_type(subject);
226 template <typename T, typename Subject, typename Modifiers>
227 struct make_directive<
228 terminal_ex<tag::repeat, fusion::vector1<T> >, Subject, Modifiers>
230 typedef exact_iterator<T> iterator_type;
231 typedef repeat_parser<Subject, iterator_type> result_type;
233 template <typename Terminal>
234 result_type operator()(
235 Terminal const& term, Subject const& subject, unused_type) const
237 return result_type(subject, fusion::at_c<0>(term.args));
241 template <typename T, typename Subject, typename Modifiers>
242 struct make_directive<
243 terminal_ex<tag::repeat, fusion::vector2<T, T> >, Subject, Modifiers>
245 typedef finite_iterator<T> iterator_type;
246 typedef repeat_parser<Subject, iterator_type> result_type;
248 template <typename Terminal>
249 result_type operator()(
250 Terminal const& term, Subject const& subject, unused_type) const
252 return result_type(subject,
254 fusion::at_c<0>(term.args)
255 , fusion::at_c<1>(term.args)
261 template <typename T, typename Subject, typename Modifiers>
262 struct make_directive<
263 terminal_ex<tag::repeat
264 , fusion::vector2<T, inf_type> >, Subject, Modifiers>
266 typedef infinite_iterator<T> iterator_type;
267 typedef repeat_parser<Subject, iterator_type> result_type;
269 template <typename Terminal>
270 result_type operator()(
271 Terminal const& term, Subject const& subject, unused_type) const
273 return result_type(subject, fusion::at_c<0>(term.args));
278 namespace boost { namespace spirit { namespace traits
280 ///////////////////////////////////////////////////////////////////////////
281 template <typename Subject, typename LoopIter>
282 struct has_semantic_action<qi::repeat_parser<Subject, LoopIter> >
283 : unary_has_semantic_action<Subject> {};
285 ///////////////////////////////////////////////////////////////////////////
286 template <typename Subject, typename LoopIter, typename Attribute
287 , typename Context, typename Iterator>
288 struct handles_container<qi::repeat_parser<Subject, LoopIter>
289 , Attribute, Context, Iterator>