PageRenderTime 34ms CodeModel.GetById 11ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

/src/contrib/boost/spirit/home/qi/directive/repeat.hpp

http://pythonocc.googlecode.com/
C++ Header | 327 lines | 254 code | 43 blank | 30 comment | 7 complexity | 5cf9f871db06d197ca7e113f6265e05f MD5 | raw file
  1/*=============================================================================
  2    Copyright (c) 2001-2010 Joel de Guzman
  3
  4    Distributed under the Boost Software License, Version 1.0. (See accompanying
  5    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6=============================================================================*/
  7#if !defined(SPIRIT_REPEAT_NOVEMBER_14_2008_1148AM)
  8#define SPIRIT_REPEAT_NOVEMBER_14_2008_1148AM
  9
 10#if defined(_MSC_VER)
 11#pragma once
 12#endif
 13
 14#include <boost/spirit/home/qi/meta_compiler.hpp>
 15#include <boost/spirit/home/qi/parser.hpp>
 16#include <boost/spirit/home/qi/auxiliary/lazy.hpp>
 17#include <boost/spirit/home/qi/operator/kleene.hpp>
 18#include <boost/spirit/home/support/container.hpp>
 19#include <boost/spirit/home/support/common_terminals.hpp>
 20#include <boost/spirit/home/support/attributes.hpp>
 21#include <boost/spirit/home/support/info.hpp>
 22#include <boost/fusion/include/at.hpp>
 23#include <boost/foreach.hpp>
 24#include <vector>
 25
 26namespace boost { namespace spirit
 27{
 28    ///////////////////////////////////////////////////////////////////////////
 29    // Enablers
 30    ///////////////////////////////////////////////////////////////////////////
 31    template <>
 32    struct use_directive<qi::domain, tag::repeat>   // enables repeat[p]
 33      : mpl::true_ {};
 34
 35    template <typename T>
 36    struct use_directive<qi::domain
 37      , terminal_ex<tag::repeat                     // enables repeat(exact)[p]
 38        , fusion::vector1<T> >
 39    > : mpl::true_ {};
 40
 41    template <typename T>
 42    struct use_directive<qi::domain
 43      , terminal_ex<tag::repeat                     // enables repeat(min, max)[p]
 44        , fusion::vector2<T, T> >
 45    > : mpl::true_ {};
 46
 47    template <typename T>
 48    struct use_directive<qi::domain
 49      , terminal_ex<tag::repeat                     // enables repeat(min, inf)[p]
 50        , fusion::vector2<T, inf_type> >
 51    > : mpl::true_ {};
 52
 53    template <>                                     // enables *lazy* repeat(exact)[p]
 54    struct use_lazy_directive<
 55        qi::domain
 56      , tag::repeat
 57      , 1 // arity
 58    > : mpl::true_ {};
 59
 60    template <>                                     // enables *lazy* repeat(min, max)[p]
 61    struct use_lazy_directive<                      // and repeat(min, inf)[p]
 62        qi::domain
 63      , tag::repeat
 64      , 2 // arity
 65    > : mpl::true_ {};
 66}}
 67
 68namespace boost { namespace spirit { namespace qi
 69{
 70    using spirit::repeat;
 71    using spirit::repeat_type;
 72    using spirit::inf;
 73    using spirit::inf_type;
 74
 75    template <typename T>
 76    struct exact_iterator // handles repeat(exact)[p]
 77    {
 78        exact_iterator(T const exact)
 79          : exact(exact) {}
 80
 81        typedef T type;
 82        T start() const { return 0; }
 83        bool got_max(T i) const { return i >= exact; }
 84        bool got_min(T i) const { return i >= exact; }
 85
 86        T const exact;
 87
 88    private:
 89        // silence MSVC warning C4512: assignment operator could not be generated
 90        exact_iterator& operator= (exact_iterator const&);
 91    };
 92
 93    template <typename T>
 94    struct finite_iterator // handles repeat(min, max)[p]
 95    {
 96        finite_iterator(T const min, T const max)
 97          : min BOOST_PREVENT_MACRO_SUBSTITUTION (min)
 98          , max BOOST_PREVENT_MACRO_SUBSTITUTION (max) {}
 99
100        typedef T type;
101        T start() const { return 0; }
102        bool got_max(T i) const { return i >= max; }
103        bool got_min(T i) const { return i >= min; }
104
105        T const min;
106        T const max;
107
108    private:
109        // silence MSVC warning C4512: assignment operator could not be generated
110        finite_iterator& operator= (finite_iterator const&);
111    };
112
113    template <typename T>
114    struct infinite_iterator // handles repeat(min, inf)[p]
115    {
116        infinite_iterator(T const min)
117          : min BOOST_PREVENT_MACRO_SUBSTITUTION (min) {}
118
119        typedef T type;
120        T start() const { return 0; }
121        bool got_max(T /*i*/) const { return false; }
122        bool got_min(T i) const { return i >= min; }
123
124        T const min;
125
126    private:
127        // silence MSVC warning C4512: assignment operator could not be generated
128        infinite_iterator& operator= (infinite_iterator const&);
129    };
130
131    template <typename Subject, typename LoopIter>
132    struct repeat_parser : unary_parser<repeat_parser<Subject, LoopIter> >
133    {
134        typedef Subject subject_type;
135
136        template <typename Context, typename Iterator>
137        struct attribute
138        {
139            // Build a std::vector from the subject's attribute. Note
140            // that build_std_vector may return unused_type if the
141            // subject's attribute is an unused_type.
142            typedef typename
143                traits::build_std_vector<
144                    typename traits::attribute_of<
145                        Subject, Context, Iterator>::type
146                >::type
147            type;
148        };
149
150        repeat_parser(Subject const& subject, LoopIter const& iter)
151          : subject(subject), iter(iter) {}
152
153        template <typename Iterator, typename Context
154          , typename Skipper, typename ValueType, typename Attribute
155          , typename LoopVar>
156        bool parse_minimal(Iterator &first, Iterator const& last
157          , Context& context, Skipper const& skipper
158          , Attribute& attr, ValueType& val, LoopVar& i) const
159        {
160            // this scope allows save and required_attr to be reclaimed 
161            // immediately after we're done with the required minimum 
162            // iteration.
163            Iterator save = first;
164            std::vector<ValueType> required_attr;
165            for (; !iter.got_min(i); ++i)
166            {
167                if (!subject.parse(save, last, context, skipper, val) ||
168                    !traits::push_back(required_attr, val))
169                {
170                    return false;
171                }
172
173                first = save;
174                traits::clear(val);
175            }
176
177            // if we got the required number of items, these are copied 
178            // over (appended) to the 'real' attribute
179            BOOST_FOREACH(ValueType const& v, required_attr)
180            {
181                traits::push_back(attr, v);
182            }
183            return true;
184        }
185
186        template <typename Iterator, typename Context
187          , typename Skipper, typename LoopVar>
188        bool parse_minimal(Iterator &first, Iterator const& last
189          , Context& context, Skipper const& skipper
190          , unused_type, unused_type, LoopVar& i) const
191        {
192            // this scope allows save and required_attr to be reclaimed 
193            // immediately after we're done with the required minimum 
194            // iteration.
195            Iterator save = first;
196            for (; !iter.got_min(i); ++i)
197            {
198                if (!subject.parse(save, last, context, skipper, unused))
199                {
200                    return false;
201                }
202                first = save;
203            }
204            return true;
205        }
206
207        template <typename Iterator, typename Context
208          , typename Skipper, typename Attribute>
209        bool parse(Iterator& first, Iterator const& last
210          , Context& context, Skipper const& skipper
211          , Attribute& attr) const
212        {
213            // create a local value if Attribute is not unused_type
214            typedef typename traits::container_value<Attribute>::type 
215                value_type;
216            value_type val = value_type();
217            typename LoopIter::type i = iter.start();
218
219            // parse the minimum required
220            if (!iter.got_min(i) &&
221                !parse_minimal(first, last, context, skipper, attr, val, i))
222            {
223                return false;
224            }
225
226            // parse some more up to the maximum specified
227            Iterator save = first;
228            for (; !iter.got_max(i); ++i)
229            {
230                if (!subject.parse(save, last, context, skipper, val) ||
231                    !traits::push_back(attr, val))
232                {
233                    break;
234                }
235                first = save;
236                traits::clear(val);
237            }
238            return true;
239        }
240
241        template <typename Context>
242        info what(Context& context) const
243        {
244            return info("repeat", subject.what(context));
245        }
246
247        Subject subject;
248        LoopIter iter;
249
250    private:
251        // silence MSVC warning C4512: assignment operator could not be generated
252        repeat_parser& operator= (repeat_parser const&);
253    };
254
255    ///////////////////////////////////////////////////////////////////////////
256    // Parser generators: make_xxx function (objects)
257    ///////////////////////////////////////////////////////////////////////////
258    template <typename Subject, typename Modifiers>
259    struct make_directive<tag::repeat, Subject, Modifiers>
260    {
261        typedef kleene<Subject> result_type;
262        result_type operator()(unused_type, Subject const& subject, unused_type) const
263        {
264            return result_type(subject);
265        }
266    };
267
268    template <typename T, typename Subject, typename Modifiers>
269    struct make_directive<
270        terminal_ex<tag::repeat, fusion::vector1<T> >, Subject, Modifiers>
271    {
272        typedef exact_iterator<T> iterator_type;
273        typedef repeat_parser<Subject, iterator_type> result_type;
274
275        template <typename Terminal>
276        result_type operator()(
277            Terminal const& term, Subject const& subject, unused_type) const
278        {
279            return result_type(subject, fusion::at_c<0>(term.args));
280        }
281    };
282
283    template <typename T, typename Subject, typename Modifiers>
284    struct make_directive<
285        terminal_ex<tag::repeat, fusion::vector2<T, T> >, Subject, Modifiers>
286    {
287        typedef finite_iterator<T> iterator_type;
288        typedef repeat_parser<Subject, iterator_type> result_type;
289
290        template <typename Terminal>
291        result_type operator()(
292            Terminal const& term, Subject const& subject, unused_type) const
293        {
294            return result_type(subject,
295                iterator_type(
296                    fusion::at_c<0>(term.args)
297                  , fusion::at_c<1>(term.args)
298                )
299            );
300        }
301    };
302
303    template <typename T, typename Subject, typename Modifiers>
304    struct make_directive<
305        terminal_ex<tag::repeat
306        , fusion::vector2<T, inf_type> >, Subject, Modifiers>
307    {
308        typedef infinite_iterator<T> iterator_type;
309        typedef repeat_parser<Subject, iterator_type> result_type;
310
311        template <typename Terminal>
312        result_type operator()(
313            Terminal const& term, Subject const& subject, unused_type) const
314        {
315            return result_type(subject, fusion::at_c<0>(term.args));
316        }
317    };
318}}}
319
320namespace boost { namespace spirit { namespace traits
321{
322    template <typename Subject, typename LoopIter>
323    struct has_semantic_action<qi::repeat_parser<Subject, LoopIter> >
324      : unary_has_semantic_action<Subject> {};
325}}}
326
327#endif