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_KLEENE_STAR_HPP)
|
---|
12 | #define BOOST_SPIRIT_KLEENE_STAR_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 |
|
---|
19 | namespace boost { namespace spirit {
|
---|
20 |
|
---|
21 | ///////////////////////////////////////////////////////////////////////////
|
---|
22 | //
|
---|
23 | // kleene_star 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 zero (0) or more times.
|
---|
31 | //
|
---|
32 | ///////////////////////////////////////////////////////////////////////////
|
---|
33 | struct kleene_star_parser_gen;
|
---|
34 |
|
---|
35 | template <typename S>
|
---|
36 | struct kleene_star
|
---|
37 | : public unary<S, parser<kleene_star<S> > >
|
---|
38 | {
|
---|
39 | typedef kleene_star<S> self_t;
|
---|
40 | typedef unary_parser_category parser_category_t;
|
---|
41 | typedef kleene_star_parser_gen parser_generator_t;
|
---|
42 | typedef unary<S, parser<self_t> > base_t;
|
---|
43 |
|
---|
44 | kleene_star(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 = scan.empty_match();
|
---|
54 |
|
---|
55 | for (;;)
|
---|
56 | {
|
---|
57 | iterator_t save = scan.first;
|
---|
58 | if (result_t next = this->subject().parse(scan))
|
---|
59 | {
|
---|
60 | scan.concat_match(hit, next);
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | scan.first = save;
|
---|
65 | return hit;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 | };
|
---|
70 |
|
---|
71 | struct kleene_star_parser_gen
|
---|
72 | {
|
---|
73 | template <typename S>
|
---|
74 | struct result
|
---|
75 | {
|
---|
76 | typedef kleene_star<S> type;
|
---|
77 | };
|
---|
78 |
|
---|
79 | template <typename S>
|
---|
80 | static kleene_star<S>
|
---|
81 | generate(parser<S> const& a)
|
---|
82 | {
|
---|
83 | return kleene_star<S>(a.derived());
|
---|
84 | }
|
---|
85 | };
|
---|
86 |
|
---|
87 | //////////////////////////////////
|
---|
88 | template <typename S>
|
---|
89 | kleene_star<S>
|
---|
90 | operator*(parser<S> const& a);
|
---|
91 |
|
---|
92 | }} // namespace boost::spirit
|
---|
93 |
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | #include <boost/spirit/core/composite/impl/kleene_star.ipp>
|
---|