source: NonGTP/Boost/boost/spirit/utility/impl/chset_operators.ipp @ 857

Revision 857, 16.5 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*=============================================================================
2    Copyright (c) 2001-2003 Joel de Guzman
3    http://spirit.sourceforge.net/
4
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
11
12///////////////////////////////////////////////////////////////////////////////
13#include <boost/limits.hpp>
14
15///////////////////////////////////////////////////////////////////////////////
16namespace boost { namespace spirit {
17
18///////////////////////////////////////////////////////////////////////////////
19//
20//  chset free operators implementation
21//
22///////////////////////////////////////////////////////////////////////////////
23template <typename CharT>
24inline chset<CharT>
25operator|(chset<CharT> const& a, chset<CharT> const& b)
26{
27    return chset<CharT>(a) |= b;
28}
29
30//////////////////////////////////
31template <typename CharT>
32inline chset<CharT>
33operator-(chset<CharT> const& a, chset<CharT> const& b)
34{
35    return chset<CharT>(a) -= b;
36}
37
38//////////////////////////////////
39template <typename CharT>
40inline chset<CharT>
41operator~(chset<CharT> const& a)
42{
43    return chset<CharT>(a).inverse();
44}
45
46//////////////////////////////////
47template <typename CharT>
48inline chset<CharT>
49operator&(chset<CharT> const& a, chset<CharT> const& b)
50{
51    return chset<CharT>(a) &= b;
52}
53
54//////////////////////////////////
55template <typename CharT>
56inline chset<CharT>
57operator^(chset<CharT> const& a, chset<CharT> const& b)
58{
59    return chset<CharT>(a) ^= b;
60}
61
62///////////////////////////////////////////////////////////////////////////////
63//
64//  range <--> chset free operators implementation
65//
66///////////////////////////////////////////////////////////////////////////////
67template <typename CharT>
68inline chset<CharT>
69operator|(chset<CharT> const& a, range<CharT> const& b)
70{
71    chset<CharT> a_(a);
72    a_.set(b);
73    return a_;
74}
75
76//////////////////////////////////
77template <typename CharT>
78inline chset<CharT>
79operator&(chset<CharT> const& a, range<CharT> const& b)
80{
81    chset<CharT> a_(a);
82    if(b.first != (std::numeric_limits<CharT>::min)()) {
83        a_.clear(range<CharT>((std::numeric_limits<CharT>::min)(), b.first - 1));
84    }
85    if(b.last != (std::numeric_limits<CharT>::max)()) {
86        a_.clear(range<CharT>(b.last + 1, (std::numeric_limits<CharT>::max)()));
87    }
88    return a_;
89}
90
91//////////////////////////////////
92template <typename CharT>
93inline chset<CharT>
94operator-(chset<CharT> const& a, range<CharT> const& b)
95{
96    chset<CharT> a_(a);
97    a_.clear(b);
98    return a_;
99}
100
101//////////////////////////////////
102template <typename CharT>
103inline chset<CharT>
104operator^(chset<CharT> const& a, range<CharT> const& b)
105{
106    return a ^ chset<CharT>(b);
107}
108
109//////////////////////////////////
110template <typename CharT>
111inline chset<CharT>
112operator|(range<CharT> const& a, chset<CharT> const& b)
113{
114    chset<CharT> b_(b);
115    b_.set(a);
116    return b_;
117}
118
119//////////////////////////////////
120template <typename CharT>
121inline chset<CharT>
122operator&(range<CharT> const& a, chset<CharT> const& b)
123{
124    chset<CharT> b_(b);
125    if(a.first != (std::numeric_limits<CharT>::min)()) {
126        b_.clear(range<CharT>((std::numeric_limits<CharT>::min)(), a.first - 1));
127    }
128    if(a.last != (std::numeric_limits<CharT>::max)()) {
129        b_.clear(range<CharT>(a.last + 1, (std::numeric_limits<CharT>::max)()));
130    }
131    return b_;
132}
133
134//////////////////////////////////
135template <typename CharT>
136inline chset<CharT>
137operator-(range<CharT> const& a, chset<CharT> const& b)
138{
139    return chset<CharT>(a) - b;
140}
141
142//////////////////////////////////
143template <typename CharT>
144inline chset<CharT>
145operator^(range<CharT> const& a, chset<CharT> const& b)
146{
147    return chset<CharT>(a) ^ b;
148}
149
150///////////////////////////////////////////////////////////////////////////////
151//
152//  literal primitives <--> chset free operators implementation
153//
154///////////////////////////////////////////////////////////////////////////////
155template <typename CharT>
156inline chset<CharT>
157operator|(chset<CharT> const& a, CharT b)
158{
159    return a | chset<CharT>(b);
160}
161
162//////////////////////////////////
163template <typename CharT>
164inline chset<CharT>
165operator&(chset<CharT> const& a, CharT b)
166{
167    return a & chset<CharT>(b);
168}
169
170//////////////////////////////////
171template <typename CharT>
172inline chset<CharT>
173operator-(chset<CharT> const& a, CharT b)
174{
175    return a - chset<CharT>(b);
176}
177
178//////////////////////////////////
179template <typename CharT>
180inline chset<CharT>
181operator^(chset<CharT> const& a, CharT b)
182{
183    return a ^ chset<CharT>(b);
184}
185
186//////////////////////////////////
187template <typename CharT>
188inline chset<CharT>
189operator|(CharT a, chset<CharT> const& b)
190{
191    return chset<CharT>(a) | b;
192}
193
194//////////////////////////////////
195template <typename CharT>
196inline chset<CharT>
197operator&(CharT a, chset<CharT> const& b)
198{
199    return chset<CharT>(a) & b;
200}
201
202//////////////////////////////////
203template <typename CharT>
204inline chset<CharT>
205operator-(CharT a, chset<CharT> const& b)
206{
207    return chset<CharT>(a) - b;
208}
209
210//////////////////////////////////
211template <typename CharT>
212inline chset<CharT>
213operator^(CharT a, chset<CharT> const& b)
214{
215    return chset<CharT>(a) ^ b;
216}
217
218///////////////////////////////////////////////////////////////////////////////
219//
220//  chlit <--> chset free operators implementation
221//
222///////////////////////////////////////////////////////////////////////////////
223template <typename CharT>
224inline chset<CharT>
225operator|(chset<CharT> const& a, chlit<CharT> const& b)
226{
227    return a | chset<CharT>(b.ch);
228}
229
230//////////////////////////////////
231template <typename CharT>
232inline chset<CharT>
233operator&(chset<CharT> const& a, chlit<CharT> const& b)
234{
235    return a & chset<CharT>(b.ch);
236}
237
238//////////////////////////////////
239template <typename CharT>
240inline chset<CharT>
241operator-(chset<CharT> const& a, chlit<CharT> const& b)
242{
243    return a - chset<CharT>(b.ch);
244}
245
246//////////////////////////////////
247template <typename CharT>
248inline chset<CharT>
249operator^(chset<CharT> const& a, chlit<CharT> const& b)
250{
251    return a ^ chset<CharT>(b.ch);
252}
253
254//////////////////////////////////
255template <typename CharT>
256inline chset<CharT>
257operator|(chlit<CharT> const& a, chset<CharT> const& b)
258{
259    return chset<CharT>(a.ch) | b;
260}
261
262//////////////////////////////////
263template <typename CharT>
264inline chset<CharT>
265operator&(chlit<CharT> const& a, chset<CharT> const& b)
266{
267    return chset<CharT>(a.ch) & b;
268}
269
270//////////////////////////////////
271template <typename CharT>
272inline chset<CharT>
273operator-(chlit<CharT> const& a, chset<CharT> const& b)
274{
275    return chset<CharT>(a.ch) - b;
276}
277
278//////////////////////////////////
279template <typename CharT>
280inline chset<CharT>
281operator^(chlit<CharT> const& a, chset<CharT> const& b)
282{
283    return chset<CharT>(a.ch) ^ b;
284}
285
286#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
287
288///////////////////////////////////////////////////////////////////////////////
289//
290//  negated_char_parser <--> chset free operators implementation
291//
292///////////////////////////////////////////////////////////////////////////////
293template <typename CharT, typename ParserT>
294inline chset<CharT>
295operator|(chset<CharT> const& a, negated_char_parser<ParserT> const& b)
296{
297    return a | chset<CharT>(b);
298}
299
300//////////////////////////////////
301template <typename CharT, typename ParserT>
302inline chset<CharT>
303operator&(chset<CharT> const& a, negated_char_parser<ParserT> const& b)
304{
305    return a & chset<CharT>(b);
306}
307
308//////////////////////////////////
309template <typename CharT, typename ParserT>
310inline chset<CharT>
311operator-(chset<CharT> const& a, negated_char_parser<ParserT> const& b)
312{
313    return a - chset<CharT>(b);
314}
315
316//////////////////////////////////
317template <typename CharT, typename ParserT>
318inline chset<CharT>
319operator^(chset<CharT> const& a, negated_char_parser<ParserT> const& b)
320{
321    return a ^ chset<CharT>(b);
322}
323
324//////////////////////////////////
325template <typename CharT, typename ParserT>
326inline chset<CharT>
327operator|(negated_char_parser<ParserT> const& a, chset<CharT> const& b)
328{
329    return chset<CharT>(a) | b;
330}
331
332//////////////////////////////////
333template <typename CharT, typename ParserT>
334inline chset<CharT>
335operator&(negated_char_parser<ParserT> const& a, chset<CharT> const& b)
336{
337    return chset<CharT>(a) & b;
338}
339
340//////////////////////////////////
341template <typename CharT, typename ParserT>
342inline chset<CharT>
343operator-(negated_char_parser<ParserT> const& a, chset<CharT> const& b)
344{
345    return chset<CharT>(a) - b;
346}
347
348//////////////////////////////////
349template <typename CharT, typename ParserT>
350inline chset<CharT>
351operator^(negated_char_parser<ParserT> const& a, chset<CharT> const& b)
352{
353    return chset<CharT>(a) ^ b;
354}
355
356#else // BOOST_WORKAROUND(BOOST_MSVC, < 1300)
357
358///////////////////////////////////////////////////////////////////////////////
359//
360//  negated_char_parser<range> <--> chset free operators implementation
361//
362///////////////////////////////////////////////////////////////////////////////
363template <typename CharT>
364inline chset<CharT>
365operator|(chset<CharT> const& a, negated_char_parser<range<CharT> > const& b)
366{
367    return a | chset<CharT>(b);
368}
369
370//////////////////////////////////
371template <typename CharT>
372inline chset<CharT>
373operator&(chset<CharT> const& a, negated_char_parser<range<CharT> > const& b)
374{
375    return a & chset<CharT>(b);
376}
377
378//////////////////////////////////
379template <typename CharT>
380inline chset<CharT>
381operator-(chset<CharT> const& a, negated_char_parser<range<CharT> > const& b)
382{
383    return a - chset<CharT>(b);
384}
385
386//////////////////////////////////
387template <typename CharT>
388inline chset<CharT>
389operator^(chset<CharT> const& a, negated_char_parser<range<CharT> > const& b)
390{
391    return a ^ chset<CharT>(b);
392}
393
394//////////////////////////////////
395template <typename CharT>
396inline chset<CharT>
397operator|(negated_char_parser<range<CharT> > const& a, chset<CharT> const& b)
398{
399    return chset<CharT>(a) | b;
400}
401
402//////////////////////////////////
403template <typename CharT>
404inline chset<CharT>
405operator&(negated_char_parser<range<CharT> > const& a, chset<CharT> const& b)
406{
407    return chset<CharT>(a) & b;
408}
409
410//////////////////////////////////
411template <typename CharT>
412inline chset<CharT>
413operator-(negated_char_parser<range<CharT> > const& a, chset<CharT> const& b)
414{
415    return chset<CharT>(a) - b;
416}
417
418//////////////////////////////////
419template <typename CharT>
420inline chset<CharT>
421operator^(negated_char_parser<range<CharT> > const& a, chset<CharT> const& b)
422{
423    return chset<CharT>(a) ^ b;
424}
425
426///////////////////////////////////////////////////////////////////////////////
427//
428//  negated_char_parser<chlit> <--> chset free operators implementation
429//
430///////////////////////////////////////////////////////////////////////////////
431template <typename CharT>
432inline chset<CharT>
433operator|(chset<CharT> const& a, negated_char_parser<chlit<CharT> > const& b)
434{
435    return a | chset<CharT>(b);
436}
437
438//////////////////////////////////
439template <typename CharT>
440inline chset<CharT>
441operator&(chset<CharT> const& a, negated_char_parser<chlit<CharT> > const& b)
442{
443    return a & chset<CharT>(b);
444}
445
446//////////////////////////////////
447template <typename CharT>
448inline chset<CharT>
449operator-(chset<CharT> const& a, negated_char_parser<chlit<CharT> > const& b)
450{
451    return a - chset<CharT>(b);
452}
453
454//////////////////////////////////
455template <typename CharT>
456inline chset<CharT>
457operator^(chset<CharT> const& a, negated_char_parser<chlit<CharT> > const& b)
458{
459    return a ^ chset<CharT>(b);
460}
461
462//////////////////////////////////
463template <typename CharT>
464inline chset<CharT>
465operator|(negated_char_parser<chlit<CharT> > const& a, chset<CharT> const& b)
466{
467    return chset<CharT>(a) | b;
468}
469
470//////////////////////////////////
471template <typename CharT>
472inline chset<CharT>
473operator&(negated_char_parser<chlit<CharT> > const& a, chset<CharT> const& b)
474{
475    return chset<CharT>(a) & b;
476}
477
478//////////////////////////////////
479template <typename CharT>
480inline chset<CharT>
481operator-(negated_char_parser<chlit<CharT> > const& a, chset<CharT> const& b)
482{
483    return chset<CharT>(a) - b;
484}
485
486//////////////////////////////////
487template <typename CharT>
488inline chset<CharT>
489operator^(negated_char_parser<chlit<CharT> > const& a, chset<CharT> const& b)
490{
491    return chset<CharT>(a) ^ b;
492}
493
494#endif // BOOST_WORKAROUND(BOOST_MSVC, < 1300)
495
496///////////////////////////////////////////////////////////////////////////////
497//
498//  anychar_parser <--> chset free operators
499//
500//      Where a is chset and b is a anychar_parser, and vice-versa, implements:
501//
502//          a | b, a & b, a - b, a ^ b
503//
504///////////////////////////////////////////////////////////////////////////////
505namespace impl {
506
507    template <typename CharT>
508    inline boost::spirit::range<CharT> const&
509    full()
510    {
511        static boost::spirit::range<CharT> full_(
512            (std::numeric_limits<CharT>::min)(),
513            (std::numeric_limits<CharT>::max)());
514        return full_;
515    }
516
517    template <typename CharT>
518    inline boost::spirit::range<CharT> const&
519    empty()
520    {
521        static boost::spirit::range<CharT> empty_;
522        return empty_;
523    }
524}
525
526//////////////////////////////////
527template <typename CharT>
528inline chset<CharT>
529operator|(chset<CharT> const&, anychar_parser)
530{
531    return chset<CharT>(impl::full<CharT>());
532}
533
534//////////////////////////////////
535template <typename CharT>
536inline chset<CharT>
537operator&(chset<CharT> const& a, anychar_parser)
538{
539    return a;
540}
541
542//////////////////////////////////
543template <typename CharT>
544inline chset<CharT>
545operator-(chset<CharT> const&, anychar_parser)
546{
547    return chset<CharT>();
548}
549
550//////////////////////////////////
551template <typename CharT>
552inline chset<CharT>
553operator^(chset<CharT> const& a, anychar_parser)
554{
555    return ~a;
556}
557
558//////////////////////////////////
559template <typename CharT>
560inline chset<CharT>
561operator|(anychar_parser, chset<CharT> const& /*b*/)
562{
563    return chset<CharT>(impl::full<CharT>());
564}
565
566//////////////////////////////////
567template <typename CharT>
568inline chset<CharT>
569operator&(anychar_parser, chset<CharT> const& b)
570{
571    return b;
572}
573
574//////////////////////////////////
575template <typename CharT>
576inline chset<CharT>
577operator-(anychar_parser, chset<CharT> const& b)
578{
579    return ~b;
580}
581
582//////////////////////////////////
583template <typename CharT>
584inline chset<CharT>
585operator^(anychar_parser, chset<CharT> const& b)
586{
587    return ~b;
588}
589
590///////////////////////////////////////////////////////////////////////////////
591//
592//  nothing_parser <--> chset free operators implementation
593//
594///////////////////////////////////////////////////////////////////////////////
595template <typename CharT>
596inline chset<CharT>
597operator|(chset<CharT> const& a, nothing_parser)
598{
599    return a;
600}
601
602//////////////////////////////////
603template <typename CharT>
604inline chset<CharT>
605operator&(chset<CharT> const& /*a*/, nothing_parser)
606{
607    return impl::empty<CharT>();
608}
609
610//////////////////////////////////
611template <typename CharT>
612inline chset<CharT>
613operator-(chset<CharT> const& a, nothing_parser)
614{
615    return a;
616}
617
618//////////////////////////////////
619template <typename CharT>
620inline chset<CharT>
621operator^(chset<CharT> const& a, nothing_parser)
622{
623    return a;
624}
625
626//////////////////////////////////
627template <typename CharT>
628inline chset<CharT>
629operator|(nothing_parser, chset<CharT> const& b)
630{
631    return b;
632}
633
634//////////////////////////////////
635template <typename CharT>
636inline chset<CharT>
637operator&(nothing_parser, chset<CharT> const& /*b*/)
638{
639    return impl::empty<CharT>();
640}
641
642//////////////////////////////////
643template <typename CharT>
644inline chset<CharT>
645operator-(nothing_parser, chset<CharT> const& /*b*/)
646{
647    return impl::empty<CharT>();
648}
649
650//////////////////////////////////
651template <typename CharT>
652inline chset<CharT>
653operator^(nothing_parser, chset<CharT> const& b)
654{
655    return b;
656}
657
658///////////////////////////////////////////////////////////////////////////////
659}} // namespace boost::spirit
660
661#endif
662
Note: See TracBrowser for help on using the repository browser.