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