Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2002-2003 Joel de Guzman
|
Chris@16
|
3 Copyright (c) 2002-2003 Martin Wille
|
Chris@16
|
4 http://spirit.sourceforge.net/
|
Chris@16
|
5
|
Chris@16
|
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 =============================================================================*/
|
Chris@16
|
9 #ifndef BOOST_SPIRIT_WHILE_HPP
|
Chris@16
|
10 #define BOOST_SPIRIT_WHILE_HPP
|
Chris@16
|
11
|
Chris@16
|
12 #include <boost/spirit/home/classic/namespace.hpp>
|
Chris@16
|
13 #include <boost/spirit/home/classic/core/parser.hpp>
|
Chris@16
|
14 #include <boost/spirit/home/classic/core/composite/composite.hpp>
|
Chris@16
|
15 #include <boost/spirit/home/classic/dynamic/impl/conditions.ipp>
|
Chris@16
|
16
|
Chris@16
|
17 ////////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
18 namespace boost { namespace spirit {
|
Chris@16
|
19
|
Chris@16
|
20 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
|
Chris@16
|
21
|
Chris@16
|
22 namespace impl {
|
Chris@16
|
23
|
Chris@16
|
24 //////////////////////////////////
|
Chris@16
|
25 // while parser
|
Chris@16
|
26 // object are created by while_parser_gen and do_parser_gen
|
Chris@16
|
27 template <typename ParsableT, typename CondT, bool is_do_parser>
|
Chris@16
|
28 struct while_parser
|
Chris@16
|
29 : public condition_evaluator< typename as_parser<CondT>::type >
|
Chris@16
|
30 , public unary // the parent stores a copy of the body parser
|
Chris@16
|
31 <
|
Chris@16
|
32 typename as_parser<ParsableT>::type,
|
Chris@16
|
33 parser<while_parser<ParsableT, CondT, is_do_parser> >
|
Chris@16
|
34 >
|
Chris@16
|
35 {
|
Chris@16
|
36 typedef while_parser<ParsableT, CondT, is_do_parser> self_t;
|
Chris@16
|
37
|
Chris@16
|
38 typedef as_parser<ParsableT> as_parser_t;
|
Chris@16
|
39 typedef typename as_parser_t::type parser_t;
|
Chris@16
|
40 typedef as_parser<CondT> cond_as_parser_t;
|
Chris@16
|
41 typedef typename cond_as_parser_t::type condition_t;
|
Chris@16
|
42
|
Chris@16
|
43 typedef unary<parser_t, parser<self_t> > base_t;
|
Chris@16
|
44 typedef condition_evaluator<condition_t> eval_t;
|
Chris@16
|
45
|
Chris@16
|
46
|
Chris@16
|
47 //////////////////////////////
|
Chris@16
|
48 // constructor, saves condition and body parser
|
Chris@16
|
49 while_parser(ParsableT const &body, CondT const &cond)
|
Chris@16
|
50 : eval_t(cond_as_parser_t::convert(cond))
|
Chris@16
|
51 , base_t(as_parser_t::convert(body))
|
Chris@16
|
52 {}
|
Chris@16
|
53
|
Chris@16
|
54 //////////////////////////////
|
Chris@16
|
55 // result type computer.
|
Chris@16
|
56 template <typename ScannerT>
|
Chris@16
|
57 struct result
|
Chris@16
|
58 {
|
Chris@16
|
59 typedef typename match_result
|
Chris@16
|
60 <ScannerT, nil_t>::type type;
|
Chris@16
|
61 };
|
Chris@16
|
62
|
Chris@16
|
63 //////////////////////////////
|
Chris@16
|
64 // parse member function
|
Chris@16
|
65 template <typename ScannerT>
|
Chris@16
|
66 typename parser_result<self_t, ScannerT>::type
|
Chris@16
|
67 parse(ScannerT const& scan) const
|
Chris@16
|
68 {
|
Chris@16
|
69 typedef typename parser_result<parser_t, ScannerT>::type sresult_t;
|
Chris@16
|
70 typedef typename ScannerT::iterator_t iterator_t;
|
Chris@16
|
71
|
Chris@16
|
72 iterator_t save(scan.first);
|
Chris@16
|
73 std::size_t length = 0;
|
Chris@16
|
74 int eval_length = 0;
|
Chris@16
|
75
|
Chris@16
|
76 bool dont_check_condition = is_do_parser;
|
Chris@16
|
77
|
Chris@16
|
78 while (dont_check_condition || (eval_length=this->evaluate(scan))>=0)
|
Chris@16
|
79 {
|
Chris@16
|
80 dont_check_condition = false;
|
Chris@16
|
81 length += eval_length;
|
Chris@16
|
82 sresult_t tmp(this->subject().parse(scan));
|
Chris@16
|
83 if (tmp)
|
Chris@16
|
84 {
|
Chris@16
|
85 length+=tmp.length();
|
Chris@16
|
86 }
|
Chris@16
|
87 else
|
Chris@16
|
88 {
|
Chris@16
|
89 return scan.no_match();
|
Chris@16
|
90 }
|
Chris@16
|
91 }
|
Chris@16
|
92 return scan.create_match(length, nil_t(), save, scan.first);
|
Chris@16
|
93 }
|
Chris@16
|
94 };
|
Chris@16
|
95
|
Chris@16
|
96 //////////////////////////////////
|
Chris@16
|
97 // while-parser generator, takes the body-parser in brackets
|
Chris@16
|
98 // and returns the actual while-parser.
|
Chris@16
|
99 template <typename CondT>
|
Chris@16
|
100 struct while_parser_gen
|
Chris@16
|
101 {
|
Chris@16
|
102 //////////////////////////////
|
Chris@16
|
103 // constructor, saves the condition for use by operator[]
|
Chris@16
|
104 while_parser_gen(CondT const& cond_) : cond(cond_) {}
|
Chris@16
|
105
|
Chris@16
|
106 //////////////////////////////
|
Chris@16
|
107 // operator[] returns the actual while-parser object
|
Chris@16
|
108 template <typename ParsableT>
|
Chris@16
|
109 while_parser<ParsableT, CondT, false>
|
Chris@16
|
110 operator[](ParsableT const &subject) const
|
Chris@16
|
111 {
|
Chris@16
|
112 return while_parser<ParsableT, CondT, false>(subject, cond);
|
Chris@16
|
113 }
|
Chris@16
|
114 private:
|
Chris@16
|
115
|
Chris@16
|
116 //////////////////////////////
|
Chris@16
|
117 // the condition is stored by reference here.
|
Chris@16
|
118 // this should not cause any harm since object of type
|
Chris@16
|
119 // while_parser_gen<> are only used as temporaries
|
Chris@16
|
120 // the while-parser object constructed by the operator[]
|
Chris@16
|
121 // stores a copy of the condition.
|
Chris@16
|
122 CondT const &cond;
|
Chris@16
|
123 };
|
Chris@16
|
124
|
Chris@16
|
125 //////////////////////////////////
|
Chris@16
|
126 // do-while-parser generator, takes the condition as
|
Chris@16
|
127 // parameter to while_p member function and returns the
|
Chris@16
|
128 // actual do-while-parser.
|
Chris@16
|
129 template <typename ParsableT>
|
Chris@16
|
130 struct do_while_parser_gen
|
Chris@16
|
131 {
|
Chris@16
|
132 //////////////////////////////
|
Chris@16
|
133 // constructor. saves the body parser for use by while_p.
|
Chris@16
|
134 explicit do_while_parser_gen(ParsableT const &body_parser)
|
Chris@16
|
135 : body(body_parser)
|
Chris@16
|
136 {}
|
Chris@16
|
137
|
Chris@16
|
138 //////////////////////////////
|
Chris@16
|
139 // while_p returns the actual while-parser object
|
Chris@16
|
140 template <typename CondT>
|
Chris@16
|
141 while_parser<ParsableT, CondT, true>
|
Chris@16
|
142 while_p(CondT cond) const
|
Chris@16
|
143 {
|
Chris@16
|
144 return while_parser<ParsableT, CondT, true>(body, cond);
|
Chris@16
|
145 }
|
Chris@16
|
146 private:
|
Chris@16
|
147
|
Chris@16
|
148 //////////////////////////////
|
Chris@16
|
149 // the body is stored by reference here
|
Chris@16
|
150 // this should not cause any harm since object of type
|
Chris@16
|
151 // do_while_parser_gen<> are only used as temporaries
|
Chris@16
|
152 // the while-parser object constructed by the while_p
|
Chris@16
|
153 // member function stores a copy of the body parser.
|
Chris@16
|
154 ParsableT const &body;
|
Chris@16
|
155 };
|
Chris@16
|
156
|
Chris@16
|
157 struct do_parser_gen
|
Chris@16
|
158 {
|
Chris@16
|
159 inline do_parser_gen() {}
|
Chris@16
|
160
|
Chris@16
|
161 template <typename ParsableT>
|
Chris@16
|
162 impl::do_while_parser_gen<ParsableT>
|
Chris@16
|
163 operator[](ParsableT const& body) const
|
Chris@16
|
164 {
|
Chris@16
|
165 return impl::do_while_parser_gen<ParsableT>(body);
|
Chris@16
|
166 }
|
Chris@16
|
167 };
|
Chris@16
|
168 } // namespace impl
|
Chris@16
|
169
|
Chris@16
|
170 //////////////////////////////////
|
Chris@16
|
171 // while_p function, while-parser generator
|
Chris@16
|
172 // Usage: spirit::while_p(Condition)[Body]
|
Chris@16
|
173 template <typename CondT>
|
Chris@16
|
174 impl::while_parser_gen<CondT>
|
Chris@16
|
175 while_p(CondT const& cond)
|
Chris@16
|
176 {
|
Chris@16
|
177 return impl::while_parser_gen<CondT>(cond);
|
Chris@16
|
178 }
|
Chris@16
|
179
|
Chris@16
|
180 //////////////////////////////////
|
Chris@16
|
181 // do_p functor, do-while-parser generator
|
Chris@16
|
182 // Usage: spirit::do_p[Body].while_p(Condition)
|
Chris@16
|
183 impl::do_parser_gen const do_p = impl::do_parser_gen();
|
Chris@16
|
184
|
Chris@16
|
185 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
|
Chris@16
|
186
|
Chris@16
|
187 }} // namespace BOOST_SPIRIT_CLASSIC_NS
|
Chris@16
|
188
|
Chris@16
|
189 #endif // BOOST_SPIRIT_WHILE_HPP
|