1 /*=============================================================================
2 Copyright (c) 2001-2003 Joel de Guzman
3 http://spirit.sourceforge.net/
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #ifndef BOOST_SPIRIT_CHSET_OPERATORS_IPP
10 #define BOOST_SPIRIT_CHSET_OPERATORS_IPP
12 ///////////////////////////////////////////////////////////////////////////////
13 #include <boost/limits.hpp>
15 ///////////////////////////////////////////////////////////////////////////////
16 namespace boost { namespace spirit {
18 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
20 ///////////////////////////////////////////////////////////////////////////////
22 // chset free operators implementation
24 ///////////////////////////////////////////////////////////////////////////////
25 template <typename CharT>
27 operator|(chset<CharT> const& a, chset<CharT> const& b)
29 return chset<CharT>(a) |= b;
32 //////////////////////////////////
33 template <typename CharT>
35 operator-(chset<CharT> const& a, chset<CharT> const& b)
37 return chset<CharT>(a) -= b;
40 //////////////////////////////////
41 template <typename CharT>
43 operator~(chset<CharT> const& a)
45 return chset<CharT>(a).inverse();
48 //////////////////////////////////
49 template <typename CharT>
51 operator&(chset<CharT> const& a, chset<CharT> const& b)
53 return chset<CharT>(a) &= b;
56 //////////////////////////////////
57 template <typename CharT>
59 operator^(chset<CharT> const& a, chset<CharT> const& b)
61 return chset<CharT>(a) ^= b;
64 ///////////////////////////////////////////////////////////////////////////////
66 // range <--> chset free operators implementation
68 ///////////////////////////////////////////////////////////////////////////////
69 template <typename CharT>
71 operator|(chset<CharT> const& a, range<CharT> const& b)
78 //////////////////////////////////
79 template <typename CharT>
81 operator&(chset<CharT> const& a, range<CharT> const& b)
84 if(b.first != (std::numeric_limits<CharT>::min)()) {
85 a_.clear(range<CharT>((std::numeric_limits<CharT>::min)(), b.first - 1));
87 if(b.last != (std::numeric_limits<CharT>::max)()) {
88 a_.clear(range<CharT>(b.last + 1, (std::numeric_limits<CharT>::max)()));
93 //////////////////////////////////
94 template <typename CharT>
96 operator-(chset<CharT> const& a, range<CharT> const& b)
103 //////////////////////////////////
104 template <typename CharT>
106 operator^(chset<CharT> const& a, range<CharT> const& b)
108 return a ^ chset<CharT>(b);
111 //////////////////////////////////
112 template <typename CharT>
114 operator|(range<CharT> const& a, chset<CharT> const& b)
121 //////////////////////////////////
122 template <typename CharT>
124 operator&(range<CharT> const& a, chset<CharT> const& b)
127 if(a.first != (std::numeric_limits<CharT>::min)()) {
128 b_.clear(range<CharT>((std::numeric_limits<CharT>::min)(), a.first - 1));
130 if(a.last != (std::numeric_limits<CharT>::max)()) {
131 b_.clear(range<CharT>(a.last + 1, (std::numeric_limits<CharT>::max)()));
136 //////////////////////////////////
137 template <typename CharT>
139 operator-(range<CharT> const& a, chset<CharT> const& b)
141 return chset<CharT>(a) - b;
144 //////////////////////////////////
145 template <typename CharT>
147 operator^(range<CharT> const& a, chset<CharT> const& b)
149 return chset<CharT>(a) ^ b;
152 ///////////////////////////////////////////////////////////////////////////////
154 // literal primitives <--> chset free operators implementation
156 ///////////////////////////////////////////////////////////////////////////////
157 template <typename CharT>
159 operator|(chset<CharT> const& a, CharT b)
161 return a | chset<CharT>(b);
164 //////////////////////////////////
165 template <typename CharT>
167 operator&(chset<CharT> const& a, CharT b)
169 return a & chset<CharT>(b);
172 //////////////////////////////////
173 template <typename CharT>
175 operator-(chset<CharT> const& a, CharT b)
177 return a - chset<CharT>(b);
180 //////////////////////////////////
181 template <typename CharT>
183 operator^(chset<CharT> const& a, CharT b)
185 return a ^ chset<CharT>(b);
188 //////////////////////////////////
189 template <typename CharT>
191 operator|(CharT a, chset<CharT> const& b)
193 return chset<CharT>(a) | b;
196 //////////////////////////////////
197 template <typename CharT>
199 operator&(CharT a, chset<CharT> const& b)
201 return chset<CharT>(a) & b;
204 //////////////////////////////////
205 template <typename CharT>
207 operator-(CharT a, chset<CharT> const& b)
209 return chset<CharT>(a) - b;
212 //////////////////////////////////
213 template <typename CharT>
215 operator^(CharT a, chset<CharT> const& b)
217 return chset<CharT>(a) ^ b;
220 ///////////////////////////////////////////////////////////////////////////////
222 // chlit <--> chset free operators implementation
224 ///////////////////////////////////////////////////////////////////////////////
225 template <typename CharT>
227 operator|(chset<CharT> const& a, chlit<CharT> const& b)
229 return a | chset<CharT>(b.ch);
232 //////////////////////////////////
233 template <typename CharT>
235 operator&(chset<CharT> const& a, chlit<CharT> const& b)
237 return a & chset<CharT>(b.ch);
240 //////////////////////////////////
241 template <typename CharT>
243 operator-(chset<CharT> const& a, chlit<CharT> const& b)
245 return a - chset<CharT>(b.ch);
248 //////////////////////////////////
249 template <typename CharT>
251 operator^(chset<CharT> const& a, chlit<CharT> const& b)
253 return a ^ chset<CharT>(b.ch);
256 //////////////////////////////////
257 template <typename CharT>
259 operator|(chlit<CharT> const& a, chset<CharT> const& b)
261 return chset<CharT>(a.ch) | b;
264 //////////////////////////////////
265 template <typename CharT>
267 operator&(chlit<CharT> const& a, chset<CharT> const& b)
269 return chset<CharT>(a.ch) & b;
272 //////////////////////////////////
273 template <typename CharT>
275 operator-(chlit<CharT> const& a, chset<CharT> const& b)
277 return chset<CharT>(a.ch) - b;
280 //////////////////////////////////
281 template <typename CharT>
283 operator^(chlit<CharT> const& a, chset<CharT> const& b)
285 return chset<CharT>(a.ch) ^ b;
288 ///////////////////////////////////////////////////////////////////////////////
290 // negated_char_parser<range> <--> chset free operators implementation
292 ///////////////////////////////////////////////////////////////////////////////
293 template <typename CharT>
295 operator|(chset<CharT> const& a, negated_char_parser<range<CharT> > const& b)
297 return a | chset<CharT>(b);
300 //////////////////////////////////
301 template <typename CharT>
303 operator&(chset<CharT> const& a, negated_char_parser<range<CharT> > const& b)
305 return a & chset<CharT>(b);
308 //////////////////////////////////
309 template <typename CharT>
311 operator-(chset<CharT> const& a, negated_char_parser<range<CharT> > const& b)
313 return a - chset<CharT>(b);
316 //////////////////////////////////
317 template <typename CharT>
319 operator^(chset<CharT> const& a, negated_char_parser<range<CharT> > const& b)
321 return a ^ chset<CharT>(b);
324 //////////////////////////////////
325 template <typename CharT>
327 operator|(negated_char_parser<range<CharT> > const& a, chset<CharT> const& b)
329 return chset<CharT>(a) | b;
332 //////////////////////////////////
333 template <typename CharT>
335 operator&(negated_char_parser<range<CharT> > const& a, chset<CharT> const& b)
337 return chset<CharT>(a) & b;
340 //////////////////////////////////
341 template <typename CharT>
343 operator-(negated_char_parser<range<CharT> > const& a, chset<CharT> const& b)
345 return chset<CharT>(a) - b;
348 //////////////////////////////////
349 template <typename CharT>
351 operator^(negated_char_parser<range<CharT> > const& a, chset<CharT> const& b)
353 return chset<CharT>(a) ^ b;
356 ///////////////////////////////////////////////////////////////////////////////
358 // negated_char_parser<chlit> <--> chset free operators implementation
360 ///////////////////////////////////////////////////////////////////////////////
361 template <typename CharT>
363 operator|(chset<CharT> const& a, negated_char_parser<chlit<CharT> > const& b)
365 return a | chset<CharT>(b);
368 //////////////////////////////////
369 template <typename CharT>
371 operator&(chset<CharT> const& a, negated_char_parser<chlit<CharT> > const& b)
373 return a & chset<CharT>(b);
376 //////////////////////////////////
377 template <typename CharT>
379 operator-(chset<CharT> const& a, negated_char_parser<chlit<CharT> > const& b)
381 return a - chset<CharT>(b);
384 //////////////////////////////////
385 template <typename CharT>
387 operator^(chset<CharT> const& a, negated_char_parser<chlit<CharT> > const& b)
389 return a ^ chset<CharT>(b);
392 //////////////////////////////////
393 template <typename CharT>
395 operator|(negated_char_parser<chlit<CharT> > const& a, chset<CharT> const& b)
397 return chset<CharT>(a) | b;
400 //////////////////////////////////
401 template <typename CharT>
403 operator&(negated_char_parser<chlit<CharT> > const& a, chset<CharT> const& b)
405 return chset<CharT>(a) & b;
408 //////////////////////////////////
409 template <typename CharT>
411 operator-(negated_char_parser<chlit<CharT> > const& a, chset<CharT> const& b)
413 return chset<CharT>(a) - b;
416 //////////////////////////////////
417 template <typename CharT>
419 operator^(negated_char_parser<chlit<CharT> > const& a, chset<CharT> const& b)
421 return chset<CharT>(a) ^ b;
424 ///////////////////////////////////////////////////////////////////////////////
426 // anychar_parser <--> chset free operators
428 // Where a is chset and b is a anychar_parser, and vice-versa, implements:
430 // a | b, a & b, a - b, a ^ b
432 ///////////////////////////////////////////////////////////////////////////////
435 template <typename CharT>
436 inline BOOST_SPIRIT_CLASSIC_NS::range<CharT> const&
439 static BOOST_SPIRIT_CLASSIC_NS::range<CharT> full_(
440 (std::numeric_limits<CharT>::min)(),
441 (std::numeric_limits<CharT>::max)());
445 template <typename CharT>
446 inline BOOST_SPIRIT_CLASSIC_NS::range<CharT> const&
449 static BOOST_SPIRIT_CLASSIC_NS::range<CharT> empty_;
454 //////////////////////////////////
455 template <typename CharT>
457 operator|(chset<CharT> const&, anychar_parser)
459 return chset<CharT>(impl::full<CharT>());
462 //////////////////////////////////
463 template <typename CharT>
465 operator&(chset<CharT> const& a, anychar_parser)
470 //////////////////////////////////
471 template <typename CharT>
473 operator-(chset<CharT> const&, anychar_parser)
475 return chset<CharT>();
478 //////////////////////////////////
479 template <typename CharT>
481 operator^(chset<CharT> const& a, anychar_parser)
486 //////////////////////////////////
487 template <typename CharT>
489 operator|(anychar_parser, chset<CharT> const& /*b*/)
491 return chset<CharT>(impl::full<CharT>());
494 //////////////////////////////////
495 template <typename CharT>
497 operator&(anychar_parser, chset<CharT> const& b)
502 //////////////////////////////////
503 template <typename CharT>
505 operator-(anychar_parser, chset<CharT> const& b)
510 //////////////////////////////////
511 template <typename CharT>
513 operator^(anychar_parser, chset<CharT> const& b)
518 ///////////////////////////////////////////////////////////////////////////////
520 // nothing_parser <--> chset free operators implementation
522 ///////////////////////////////////////////////////////////////////////////////
523 template <typename CharT>
525 operator|(chset<CharT> const& a, nothing_parser)
530 //////////////////////////////////
531 template <typename CharT>
533 operator&(chset<CharT> const& /*a*/, nothing_parser)
535 return impl::empty<CharT>();
538 //////////////////////////////////
539 template <typename CharT>
541 operator-(chset<CharT> const& a, nothing_parser)
546 //////////////////////////////////
547 template <typename CharT>
549 operator^(chset<CharT> const& a, nothing_parser)
554 //////////////////////////////////
555 template <typename CharT>
557 operator|(nothing_parser, chset<CharT> const& b)
562 //////////////////////////////////
563 template <typename CharT>
565 operator&(nothing_parser, chset<CharT> const& /*b*/)
567 return impl::empty<CharT>();
570 //////////////////////////////////
571 template <typename CharT>
573 operator-(nothing_parser, chset<CharT> const& /*b*/)
575 return impl::empty<CharT>();
578 //////////////////////////////////
579 template <typename CharT>
581 operator^(nothing_parser, chset<CharT> const& b)
586 ///////////////////////////////////////////////////////////////////////////////
587 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
589 }} // namespace boost::spirit