1 /*=============================================================================
2 Copyright (c) 1998-2003 Joel de Guzman
3 Copyright (c) 2002 Raghavendra Satish
4 Copyright (c) 2002 Jeff Westfahl
5 http://spirit.sourceforge.net/
7 Distributed under the Boost Software License, Version 1.0. (See accompanying
8 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #if !defined(BOOST_SPIRIT_LOOPS_HPP)
11 #define BOOST_SPIRIT_LOOPS_HPP
13 ///////////////////////////////////////////////////////////////////////////////
14 #include <boost/spirit/home/classic/namespace.hpp>
15 #include <boost/spirit/home/classic/core/parser.hpp>
16 #include <boost/spirit/home/classic/core/composite/composite.hpp>
18 ///////////////////////////////////////////////////////////////////////////////
19 namespace boost { namespace spirit {
21 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
23 ///////////////////////////////////////////////////////////////////////////
27 // This class takes care of the construct:
29 // repeat_p (exact) [p]
31 // where 'p' is a parser and 'exact' is the number of times to
32 // repeat. The parser iterates over the input exactly 'exact' times.
33 // The parse function fails if the parser does not match the input
34 // exactly 'exact' times.
36 // This class is parametizable and can accept constant arguments
37 // (e.g. repeat_p (5) [p]) as well as references to variables (e.g.
38 // repeat_p (ref (n)) [p]).
40 ///////////////////////////////////////////////////////////////////////////
41 template <typename ParserT, typename ExactT>
43 : public unary<ParserT, parser <fixed_loop <ParserT, ExactT> > >
47 typedef fixed_loop<ParserT, ExactT> self_t;
48 typedef unary<ParserT, parser<self_t> > base_t;
50 fixed_loop (ParserT const & subject_, ExactT const & exact)
51 : base_t(subject_), m_exact(exact) {}
53 template <typename ScannerT>
54 typename parser_result <self_t, ScannerT>::type
55 parse (ScannerT const & scan) const
57 typedef typename parser_result<self_t, ScannerT>::type result_t;
58 result_t hit = scan.empty_match();
59 std::size_t n = m_exact;
61 for (std::size_t i = 0; i < n; ++i)
63 if (result_t next = this->subject().parse(scan))
65 scan.concat_match(hit, next);
69 return scan.no_match();
76 template <typename ScannerT>
79 typedef typename match_result<ScannerT, nil_t>::type type;
87 ///////////////////////////////////////////////////////////////////////////////
91 // This class takes care of the construct:
93 // repeat_p (min, max) [p]
95 // where 'p' is a parser, 'min' and 'max' specifies the minimum and
96 // maximum iterations over 'p'. The parser iterates over the input
97 // at least 'min' times and at most 'max' times. The parse function
98 // fails if the parser does not match the input at least 'min' times
99 // and at most 'max' times.
101 // This class is parametizable and can accept constant arguments
102 // (e.g. repeat_p (5, 10) [p]) as well as references to variables
103 // (e.g. repeat_p (ref (n1), ref (n2)) [p]).
105 ///////////////////////////////////////////////////////////////////////////////
106 template <typename ParserT, typename MinT, typename MaxT>
108 : public unary<ParserT, parser<finite_loop<ParserT, MinT, MaxT> > >
112 typedef finite_loop <ParserT, MinT, MaxT> self_t;
113 typedef unary<ParserT, parser<self_t> > base_t;
115 finite_loop (ParserT const & subject_, MinT const & min, MaxT const & max)
116 : base_t(subject_), m_min(min), m_max(max) {}
118 template <typename ScannerT>
119 typename parser_result <self_t, ScannerT>::type
120 parse(ScannerT const & scan) const
122 BOOST_SPIRIT_ASSERT(m_min <= m_max);
123 typedef typename parser_result<self_t, ScannerT>::type result_t;
124 result_t hit = scan.empty_match();
126 std::size_t n1 = m_min;
127 std::size_t n2 = m_max;
129 for (std::size_t i = 0; i < n2; ++i)
131 typename ScannerT::iterator_t save = scan.first;
132 result_t next = this->subject().parse(scan);
143 return scan.no_match();
147 scan.concat_match(hit, next);
153 template <typename ScannerT>
156 typedef typename match_result<ScannerT, nil_t>::type type;
165 ///////////////////////////////////////////////////////////////////////////////
167 // infinite_loop class
169 // This class takes care of the construct:
171 // repeat_p (min, more) [p]
173 // where 'p' is a parser, 'min' is the minimum iteration over 'p'
174 // and more specifies that the iteration should proceed
175 // indefinitely. The parser iterates over the input at least 'min'
176 // times and continues indefinitely until 'p' fails or all of the
177 // input is parsed. The parse function fails if the parser does not
178 // match the input at least 'min' times.
180 // This class is parametizable and can accept constant arguments
181 // (e.g. repeat_p (5, more) [p]) as well as references to variables
182 // (e.g. repeat_p (ref (n), more) [p]).
184 ///////////////////////////////////////////////////////////////////////////////
187 more_t const more = more_t ();
189 template <typename ParserT, typename MinT>
191 : public unary<ParserT, parser<infinite_loop<ParserT, MinT> > >
195 typedef infinite_loop <ParserT, MinT> self_t;
196 typedef unary<ParserT, parser<self_t> > base_t;
199 ParserT const& subject_,
203 : base_t(subject_), m_min(min) {}
205 template <typename ScannerT>
206 typename parser_result <self_t, ScannerT>::type
207 parse(ScannerT const & scan) const
209 typedef typename parser_result<self_t, ScannerT>::type result_t;
210 result_t hit = scan.empty_match();
211 std::size_t n = m_min;
213 for (std::size_t i = 0; ; ++i)
215 typename ScannerT::iterator_t save = scan.first;
216 result_t next = this->subject().parse(scan);
227 return scan.no_match();
231 scan.concat_match(hit, next);
237 template <typename ScannerT>
240 typedef typename match_result<ScannerT, nil_t>::type type;
248 template <typename ExactT>
249 struct fixed_loop_gen
251 fixed_loop_gen (ExactT const & exact)
254 template <typename ParserT>
255 fixed_loop <ParserT, ExactT>
256 operator[](parser <ParserT> const & subject_) const
258 return fixed_loop <ParserT, ExactT> (subject_.derived (), m_exact);
266 template <typename ParserT, typename MinT, typename MaxT>
269 typedef typename mpl::if_<
270 boost::is_same<MaxT, more_t>,
271 infinite_loop<ParserT, MinT>,
272 finite_loop<ParserT, MinT, MaxT>
278 template <typename MinT, typename MaxT>
279 struct nonfixed_loop_gen
281 nonfixed_loop_gen (MinT min, MaxT max)
282 : m_min (min), m_max (max) {}
284 template <typename ParserT>
285 typename impl::loop_traits<ParserT, MinT, MaxT>::type
286 operator[](parser <ParserT> const & subject_) const
288 typedef typename impl::loop_traits<ParserT, MinT, MaxT>::type ret_t;
299 template <typename ExactT>
300 fixed_loop_gen <ExactT>
301 repeat_p(ExactT const & exact)
303 return fixed_loop_gen <ExactT> (exact);
306 template <typename MinT, typename MaxT>
307 nonfixed_loop_gen <MinT, MaxT>
308 repeat_p(MinT const & min, MaxT const & max)
310 return nonfixed_loop_gen <MinT, MaxT> (min, max);
313 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
315 }} // namespace BOOST_SPIRIT_CLASSIC_NS
317 #endif // #if !defined(BOOST_SPIRIT_LOOPS_HPP)