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