source: NonGTP/Boost/boost/spirit/core/composite/positive.hpp @ 857

Revision 857, 3.0 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*=============================================================================
2    Copyright (c) 1998-2003 Joel de Guzman
3    Copyright (c) 2001 Daniel Nuffer
4    Copyright (c) 2002 Hartmut Kaiser
5    http://spirit.sourceforge.net/
6
7    Use, modification and distribution is subject to the Boost Software
8    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9    http://www.boost.org/LICENSE_1_0.txt)
10=============================================================================*/
11#if !defined(BOOST_SPIRIT_POSITIVE_HPP)
12#define BOOST_SPIRIT_POSITIVE_HPP
13
14#include <boost/spirit/core/parser.hpp>
15#include <boost/spirit/core/primitives/primitives.hpp>
16#include <boost/spirit/core/composite/composite.hpp>
17#include <boost/spirit/meta/as_parser.hpp>
18
19namespace boost { namespace spirit {
20
21    ///////////////////////////////////////////////////////////////////////////
22    //
23    //  positive class
24    //
25    //      Handles expressions of the form:
26    //
27    //          +a
28    //
29    //      where a is a parser. The expression returns a composite
30    //      parser that matches its subject one (1) or more times.
31    //
32    ///////////////////////////////////////////////////////////////////////////
33    struct positive_parser_gen;
34   
35    template <typename S>
36    struct positive
37    :   public unary<S, parser<positive<S> > >
38    {
39        typedef positive<S>                 self_t;
40        typedef unary_parser_category       parser_category_t;
41        typedef positive_parser_gen         parser_generator_t;
42        typedef unary<S, parser<self_t> >   base_t;
43   
44        positive(S const& a)
45        : base_t(a) {}
46   
47        template <typename ScannerT>
48        typename parser_result<self_t, ScannerT>::type
49        parse(ScannerT const& scan) const
50        {
51            typedef typename parser_result<self_t, ScannerT>::type result_t;
52            typedef typename ScannerT::iterator_t iterator_t;
53            result_t hit = this->subject().parse(scan);
54   
55            if (hit)
56            {
57                for (;;)
58                {
59                    iterator_t save = scan.first;
60                    if (result_t next = this->subject().parse(scan))
61                    {
62                        scan.concat_match(hit, next);
63                    }
64                    else
65                    {
66                        scan.first = save;
67                        break;
68                    }
69                }
70            }
71            return hit;
72        }
73    };
74   
75    struct positive_parser_gen
76    {
77        template <typename S>
78        struct result
79        {
80            typedef positive<S> type;
81        };
82   
83        template <typename S>
84        static positive<S>
85        generate(parser<S> const& a)
86        {
87            return positive<S>(a.derived());
88        }
89    };
90   
91    template <typename S>
92    inline positive<S>
93    operator+(parser<S> const& a);
94
95}} // namespace boost::spirit
96
97#endif
98
99#include <boost/spirit/core/composite/impl/positive.ipp>
Note: See TracBrowser for help on using the repository browser.