~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/boost/spirit/core/composite/operators.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=============================================================================
 
2
    Spirit v1.6.1
 
3
    Copyright (c) 1998-2003 Joel de Guzman
 
4
    Copyright (c) 2001 Daniel Nuffer
 
5
    http://spirit.sourceforge.net/
 
6
 
 
7
    Permission to copy, use, modify, sell and distribute this software is
 
8
    granted provided this copyright notice appears in all copies. This
 
9
    software is provided "as is" without express or implied warranty, and
 
10
    with no claim as to its suitability for any purpose.
 
11
=============================================================================*/
 
12
#if !defined(BOOST_SPIRIT_OPERATORS_HPP)
 
13
#define BOOST_SPIRIT_OPERATORS_HPP
 
14
 
 
15
///////////////////////////////////////////////////////////////////////////////
 
16
#include <algorithm>
 
17
 
 
18
#include "boost/spirit/core/parser.hpp"
 
19
#include "boost/spirit/core/primitives/primitives.hpp"
 
20
#include "boost/spirit/core/composite/composite.hpp"
 
21
#include "boost/spirit/core/meta/impl/parser_type.hpp"
 
22
 
 
23
///////////////////////////////////////////////////////////////////////////////
 
24
namespace boost { namespace spirit {
 
25
 
 
26
///////////////////////////////////////////////////////////////////////////////
 
27
//
 
28
//  sequence class
 
29
//
 
30
//      Handles expressions of the form:
 
31
//
 
32
//          a >> b
 
33
//
 
34
//      where a and b are parsers. The expression returns a composite
 
35
//      parser that matches a and b in sequence. One (not both) of the
 
36
//      operands may be a literal char, wchar_t or a primitive string
 
37
//      char const*, wchar_t const*.
 
38
//
 
39
///////////////////////////////////////////////////////////////////////////////
 
40
struct sequence_parser_gen;
 
41
 
 
42
template <typename A, typename B>
 
43
struct sequence : public binary<A, B, parser<sequence<A, B> > >
 
44
{
 
45
    typedef sequence<A, B>                  self_t;
 
46
    typedef binary_parser_category          parser_category_t;
 
47
    typedef sequence_parser_gen             parser_generator_t;
 
48
    typedef binary<A, B, parser<self_t> >   base_t;
 
49
 
 
50
    sequence()
 
51
    : base_t(A(), B()) {}
 
52
    sequence(A const& a, B const& b)
 
53
    : base_t(a, b) {}
 
54
 
 
55
    template <typename ScannerT>
 
56
    typename parser_result<self_t, ScannerT>::type
 
57
    parse(ScannerT const& scan) const
 
58
    {
 
59
        typedef typename parser_result<self_t, ScannerT>::type result_t;
 
60
        if (result_t ma = this->left().parse(scan))
 
61
            if (result_t mb = this->right().parse(scan))
 
62
            {
 
63
                scan.concat_match(ma, mb);
 
64
                return ma;
 
65
            }
 
66
        return scan.no_match();
 
67
    }
 
68
};
 
69
 
 
70
//////////////////////////////////
 
71
struct sequence_parser_gen
 
72
{
 
73
    template <typename A, typename B>
 
74
    struct result {
 
75
 
 
76
        typedef
 
77
            sequence<typename as_parser<A>::type, typename as_parser<B>::type>
 
78
            type;
 
79
    };
 
80
 
 
81
    template <typename A, typename B>
 
82
    static sequence<typename as_parser<A>::type, typename as_parser<B>::type>
 
83
    generate(A const& a, B const& b)
 
84
    {
 
85
        return sequence<BOOST_SPIRIT_TYPENAME as_parser<A>::type,
 
86
            BOOST_SPIRIT_TYPENAME as_parser<B>::type>
 
87
                (as_parser<A>::convert(a), as_parser<B>::convert(b));
 
88
    }
 
89
};
 
90
 
 
91
//////////////////////////////////
 
92
template <typename A, typename B>
 
93
sequence<A, B>
 
94
operator>>(parser<A> const& a, parser<B> const& b);
 
95
 
 
96
//////////////////////////////////
 
97
template <typename A>
 
98
sequence<A, chlit<char> >
 
99
operator>>(parser<A> const& a, char b);
 
100
 
 
101
//////////////////////////////////
 
102
template <typename B>
 
103
sequence<chlit<char>, B>
 
104
operator>>(char a, parser<B> const& b);
 
105
 
 
106
//////////////////////////////////
 
107
template <typename A>
 
108
sequence<A, strlit<char const*> >
 
109
operator>>(parser<A> const& a, char const* b);
 
110
 
 
111
//////////////////////////////////
 
112
template <typename B>
 
113
sequence<strlit<char const*>, B>
 
114
operator>>(char const* a, parser<B> const& b);
 
115
 
 
116
//////////////////////////////////
 
117
template <typename A>
 
118
sequence<A, chlit<wchar_t> >
 
119
operator>>(parser<A> const& a, wchar_t b);
 
120
 
 
121
//////////////////////////////////
 
122
template <typename B>
 
123
sequence<chlit<wchar_t>, B>
 
124
operator>>(wchar_t a, parser<B> const& b);
 
125
 
 
126
//////////////////////////////////
 
127
template <typename A>
 
128
sequence<A, strlit<wchar_t const*> >
 
129
operator>>(parser<A> const& a, wchar_t const* b);
 
130
 
 
131
//////////////////////////////////
 
132
template <typename B>
 
133
sequence<strlit<wchar_t const*>, B>
 
134
operator>>(wchar_t const* a, parser<B> const& b);
 
135
 
 
136
///////////////////////////////////////////////////////////////////////////////
 
137
//
 
138
//  sequential-and operators
 
139
//
 
140
//      Handles expressions of the form:
 
141
//
 
142
//          a && b
 
143
//
 
144
//      Same as a >> b.
 
145
//
 
146
///////////////////////////////////////////////////////////////////////////////
 
147
template <typename A, typename B>
 
148
sequence<A, B>
 
149
operator&&(parser<A> const& a, parser<B> const& b);
 
150
 
 
151
//////////////////////////////////
 
152
template <typename A>
 
153
sequence<A, chlit<char> >
 
154
operator&&(parser<A> const& a, char b);
 
155
 
 
156
//////////////////////////////////
 
157
template <typename B>
 
158
sequence<chlit<char>, B>
 
159
operator&&(char a, parser<B> const& b);
 
160
 
 
161
//////////////////////////////////
 
162
template <typename A>
 
163
sequence<A, strlit<char const*> >
 
164
operator&&(parser<A> const& a, char const* b);
 
165
 
 
166
//////////////////////////////////
 
167
template <typename B>
 
168
sequence<strlit<char const*>, B>
 
169
operator&&(char const* a, parser<B> const& b);
 
170
 
 
171
//////////////////////////////////
 
172
template <typename A>
 
173
sequence<A, chlit<wchar_t> >
 
174
operator&&(parser<A> const& a, wchar_t b);
 
175
 
 
176
//////////////////////////////////
 
177
template <typename B>
 
178
sequence<chlit<wchar_t>, B>
 
179
operator&&(wchar_t a, parser<B> const& b);
 
180
 
 
181
//////////////////////////////////
 
182
template <typename A>
 
183
sequence<A, strlit<wchar_t const*> >
 
184
operator&&(parser<A> const& a, wchar_t const* b);
 
185
 
 
186
//////////////////////////////////
 
187
template <typename B>
 
188
sequence<strlit<wchar_t const*>, B>
 
189
operator&&(wchar_t const* a, parser<B> const& b);
 
190
 
 
191
///////////////////////////////////////////////////////////////////////////////
 
192
//
 
193
//  sequential-or class
 
194
//
 
195
//      Handles expressions of the form:
 
196
//
 
197
//          a || b
 
198
//
 
199
//      Equivalent to
 
200
//
 
201
//          a | b | a >> b;
 
202
//
 
203
//      where a and b are parsers. The expression returns a composite
 
204
//      parser that matches matches a or b in sequence. One (not both) of
 
205
//      the operands may be a literal char, wchar_t or a primitive string
 
206
//      char const*, wchar_t const*.
 
207
//
 
208
///////////////////////////////////////////////////////////////////////////////
 
209
struct sequential_or_parser_gen;
 
210
 
 
211
template <typename A, typename B>
 
212
struct sequential_or : public binary<A, B, parser<sequential_or<A, B> > >
 
213
{
 
214
    typedef sequential_or<A, B>             self_t;
 
215
    typedef binary_parser_category          parser_category_t;
 
216
    typedef sequential_or_parser_gen        parser_generator_t;
 
217
    typedef binary<A, B, parser<self_t> >   base_t;
 
218
 
 
219
    sequential_or()
 
220
    : base_t(A(), B()) {}
 
221
    sequential_or(A const& a, B const& b)
 
222
    : base_t(a, b) {}
 
223
 
 
224
    template <typename ScannerT>
 
225
    typename parser_result<self_t, ScannerT>::type
 
226
    parse(ScannerT const& scan) const
 
227
    {
 
228
        typedef typename parser_result<self_t, ScannerT>::type result_t;
 
229
        typedef typename ScannerT::iterator_t iterator_t;
 
230
        { // scope for save
 
231
            iterator_t save = scan.first;
 
232
            if (result_t ma = this->left().parse(scan))
 
233
            {
 
234
                save = scan.first;
 
235
                if (result_t mb = this->right().parse(scan))
 
236
                {
 
237
                    // matched a b
 
238
                    scan.concat_match(ma, mb);
 
239
                    return ma;
 
240
                }
 
241
                else
 
242
                {
 
243
                    // matched a
 
244
                    scan.first = save;
 
245
                    return ma;
 
246
                }
 
247
            }
 
248
            scan.first = save;
 
249
        }
 
250
 
 
251
        // matched b
 
252
        return this->right().parse(scan);
 
253
    }
 
254
};
 
255
 
 
256
//////////////////////////////////
 
257
struct sequential_or_parser_gen
 
258
{
 
259
    template <typename A, typename B>
 
260
    struct result {
 
261
 
 
262
        typedef sequential_or<
 
263
                    typename as_parser<A>::type, typename as_parser<B>::type
 
264
                > type;
 
265
    };
 
266
 
 
267
    template <typename A, typename B>
 
268
    static sequential_or
 
269
    <
 
270
        typename as_parser<A>::type,
 
271
        typename as_parser<B>::type
 
272
    >
 
273
    generate(A const& a, B const& b)
 
274
    {
 
275
        return sequential_or<BOOST_SPIRIT_TYPENAME as_parser<A>::type,
 
276
            BOOST_SPIRIT_TYPENAME as_parser<B>::type>
 
277
                (as_parser<A>::convert(a), as_parser<B>::convert(b));
 
278
    }
 
279
};
 
280
 
 
281
//////////////////////////////////
 
282
template <typename A, typename B>
 
283
sequential_or<A, B>
 
284
operator||(parser<A> const& a, parser<B> const& b);
 
285
 
 
286
//////////////////////////////////
 
287
template <typename A>
 
288
sequential_or<A, chlit<char> >
 
289
operator||(parser<A> const& a, char b);
 
290
 
 
291
//////////////////////////////////
 
292
template <typename B>
 
293
sequential_or<chlit<char>, B>
 
294
operator||(char a, parser<B> const& b);
 
295
 
 
296
//////////////////////////////////
 
297
template <typename A>
 
298
sequential_or<A, strlit<char const*> >
 
299
operator||(parser<A> const& a, char const* b);
 
300
 
 
301
//////////////////////////////////
 
302
template <typename B>
 
303
sequential_or<strlit<char const*>, B>
 
304
operator||(char const* a, parser<B> const& b);
 
305
//////////////////////////////////
 
306
template <typename A>
 
307
sequential_or<A, chlit<wchar_t> >
 
308
operator||(parser<A> const& a, wchar_t b);
 
309
 
 
310
//////////////////////////////////
 
311
template <typename B>
 
312
sequential_or<chlit<wchar_t>, B>
 
313
operator||(wchar_t a, parser<B> const& b);
 
314
 
 
315
//////////////////////////////////
 
316
template <typename A>
 
317
sequential_or<A, strlit<wchar_t const*> >
 
318
operator||(parser<A> const& a, wchar_t const* b);
 
319
 
 
320
//////////////////////////////////
 
321
template <typename B>
 
322
sequential_or<strlit<wchar_t const*>, B>
 
323
operator||(wchar_t const* a, parser<B> const& b);
 
324
 
 
325
///////////////////////////////////////////////////////////////////////////////
 
326
//
 
327
//  alternative class
 
328
//
 
329
//      Handles expressions of the form:
 
330
//
 
331
//          a | b
 
332
//
 
333
//      where a and b are parsers. The expression returns a composite
 
334
//      parser that matches a or b. One (not both) of the operands may
 
335
//      be a literal char, wchar_t or a primitive string char const*,
 
336
//      wchar_t const*.
 
337
//
 
338
//      The expression is short circuit evaluated. b is never touched
 
339
//      when a is returns a successful match.
 
340
//
 
341
///////////////////////////////////////////////////////////////////////////////
 
342
struct alternative_parser_gen;
 
343
 
 
344
template <typename A, typename B>
 
345
struct alternative
 
346
:   public binary<A, B, parser<alternative<A, B> > >
 
347
{
 
348
    typedef alternative<A, B>               self_t;
 
349
    typedef binary_parser_category          parser_category_t;
 
350
    typedef alternative_parser_gen          parser_generator_t;
 
351
    typedef binary<A, B, parser<self_t> >   base_t;
 
352
 
 
353
    alternative()
 
354
    : base_t(A(), B()) {}
 
355
    alternative(A const& a, B const& b)
 
356
    : base_t(a, b) {}
 
357
 
 
358
    template <typename ScannerT>
 
359
    typename parser_result<self_t, ScannerT>::type
 
360
    parse(ScannerT const& scan) const
 
361
    {
 
362
        typedef typename parser_result<self_t, ScannerT>::type result_t;
 
363
        typedef typename ScannerT::iterator_t iterator_t;
 
364
        { // scope for save
 
365
            iterator_t save = scan.first;
 
366
            if (result_t hit = this->left().parse(scan))
 
367
                return hit;
 
368
            scan.first = save;
 
369
        }
 
370
        return this->right().parse(scan);
 
371
    }
 
372
};
 
373
 
 
374
//////////////////////////////////
 
375
struct alternative_parser_gen
 
376
{
 
377
    template <typename A, typename B>
 
378
    struct result {
 
379
 
 
380
        typedef alternative<
 
381
                    typename as_parser<A>::type, typename as_parser<B>::type
 
382
                > type;
 
383
    };
 
384
 
 
385
    template <typename A, typename B>
 
386
    static alternative
 
387
    <
 
388
        typename as_parser<A>::type,
 
389
        typename as_parser<B>::type
 
390
    >
 
391
    generate(A const& a, B const& b)
 
392
    {
 
393
        return alternative<BOOST_SPIRIT_TYPENAME as_parser<A>::type,
 
394
            BOOST_SPIRIT_TYPENAME as_parser<B>::type>
 
395
                (as_parser<A>::convert(a), as_parser<B>::convert(b));
 
396
    }
 
397
};
 
398
 
 
399
//////////////////////////////////
 
400
template <typename A, typename B>
 
401
alternative<A, B>
 
402
operator|(parser<A> const& a, parser<B> const& b);
 
403
 
 
404
//////////////////////////////////
 
405
template <typename A>
 
406
alternative<A, chlit<char> >
 
407
operator|(parser<A> const& a, char b);
 
408
 
 
409
//////////////////////////////////
 
410
template <typename B>
 
411
alternative<chlit<char>, B>
 
412
operator|(char a, parser<B> const& b);
 
413
 
 
414
//////////////////////////////////
 
415
template <typename A>
 
416
alternative<A, strlit<char const*> >
 
417
operator|(parser<A> const& a, char const* b);
 
418
 
 
419
//////////////////////////////////
 
420
template <typename B>
 
421
alternative<strlit<char const*>, B>
 
422
operator|(char const* a, parser<B> const& b);
 
423
 
 
424
//////////////////////////////////
 
425
template <typename A>
 
426
alternative<A, chlit<wchar_t> >
 
427
operator|(parser<A> const& a, wchar_t b);
 
428
 
 
429
//////////////////////////////////
 
430
template <typename B>
 
431
alternative<chlit<wchar_t>, B>
 
432
operator|(wchar_t a, parser<B> const& b);
 
433
 
 
434
//////////////////////////////////
 
435
template <typename A>
 
436
alternative<A, strlit<wchar_t const*> >
 
437
operator|(parser<A> const& a, wchar_t const* b);
 
438
 
 
439
//////////////////////////////////
 
440
template <typename B>
 
441
alternative<strlit<wchar_t const*>, B>
 
442
operator|(wchar_t const* a, parser<B> const& b);
 
443
 
 
444
///////////////////////////////////////////////////////////////////////////////
 
445
//
 
446
//  intersection class
 
447
//
 
448
//      Handles expressions of the form:
 
449
//
 
450
//          a & b
 
451
//
 
452
//      where a and b are parsers. The expression returns a composite
 
453
//      parser that matches a and b. One (not both) of the operands may
 
454
//      be a literal char, wchar_t or a primitive string char const*,
 
455
//      wchar_t const*.
 
456
//
 
457
//      The expression is short circuit evaluated. b is never touched
 
458
//      when a is returns a no-match.
 
459
//
 
460
///////////////////////////////////////////////////////////////////////////////
 
461
struct intersection_parser_gen;
 
462
 
 
463
template <typename A, typename B>
 
464
struct intersection
 
465
:   public binary<A, B, parser<intersection<A, B> > >
 
466
{
 
467
    typedef intersection<A, B>              self_t;
 
468
    typedef binary_parser_category          parser_category_t;
 
469
    typedef intersection_parser_gen         parser_generator_t;
 
470
    typedef binary<A, B, parser<self_t> >   base_t;
 
471
 
 
472
    intersection()
 
473
    : base_t(A(), B()) {}
 
474
    intersection(A const& a, B const& b)
 
475
    : base_t(a, b) {}
 
476
 
 
477
    template <typename ScannerT>
 
478
    typename parser_result<self_t, ScannerT>::type
 
479
    parse(ScannerT const& scan) const
 
480
    {
 
481
        typedef typename parser_result<self_t, ScannerT>::type result_t;
 
482
        typedef typename ScannerT::iterator_t iterator_t;
 
483
        iterator_t save = scan.first;
 
484
        if (result_t hl = this->left().parse(scan))
 
485
        {
 
486
            ScannerT bscan(scan.first, scan.first);
 
487
            scan.first = save;
 
488
            result_t hr = this->right().parse(bscan);
 
489
            if (hl.length() == hr.length())
 
490
                return hl;
 
491
        }
 
492
 
 
493
        return scan.no_match();
 
494
    }
 
495
};
 
496
 
 
497
//////////////////////////////////
 
498
struct intersection_parser_gen
 
499
{
 
500
    template <typename A, typename B>
 
501
    struct result {
 
502
 
 
503
        typedef intersection<
 
504
                    typename as_parser<A>::type, typename as_parser<B>::type
 
505
                > type;
 
506
    };
 
507
 
 
508
    template <typename A, typename B>
 
509
    static intersection
 
510
    <
 
511
        typename as_parser<A>::type,
 
512
        typename as_parser<B>::type
 
513
    >
 
514
    generate(A const& a, B const& b)
 
515
    {
 
516
        return intersection<BOOST_SPIRIT_TYPENAME as_parser<A>::type,
 
517
            BOOST_SPIRIT_TYPENAME as_parser<B>::type>
 
518
                (as_parser<A>::convert(a), as_parser<B>::convert(b));
 
519
    }
 
520
};
 
521
 
 
522
//////////////////////////////////
 
523
template <typename A, typename B>
 
524
intersection<A, B>
 
525
operator&(parser<A> const& a, parser<B> const& b);
 
526
 
 
527
//////////////////////////////////
 
528
template <typename A>
 
529
intersection<A, chlit<char> >
 
530
operator&(parser<A> const& a, char b);
 
531
 
 
532
//////////////////////////////////
 
533
template <typename B>
 
534
intersection<chlit<char>, B>
 
535
operator&(char a, parser<B> const& b);
 
536
 
 
537
//////////////////////////////////
 
538
template <typename A>
 
539
intersection<A, strlit<char const*> >
 
540
operator&(parser<A> const& a, char const* b);
 
541
 
 
542
//////////////////////////////////
 
543
template <typename B>
 
544
intersection<strlit<char const*>, B>
 
545
operator&(char const* a, parser<B> const& b);
 
546
 
 
547
//////////////////////////////////
 
548
template <typename A>
 
549
intersection<A, chlit<wchar_t> >
 
550
operator&(parser<A> const& a, wchar_t b);
 
551
 
 
552
//////////////////////////////////
 
553
template <typename B>
 
554
intersection<chlit<wchar_t>, B>
 
555
operator&(wchar_t a, parser<B> const& b);
 
556
 
 
557
//////////////////////////////////
 
558
template <typename A>
 
559
intersection<A, strlit<wchar_t const*> >
 
560
operator&(parser<A> const& a, wchar_t const* b);
 
561
 
 
562
//////////////////////////////////
 
563
template <typename B>
 
564
intersection<strlit<wchar_t const*>, B>
 
565
operator&(wchar_t const* a, parser<B> const& b);
 
566
 
 
567
///////////////////////////////////////////////////////////////////////////////
 
568
//
 
569
//  difference: a - b; Matches a but not b
 
570
//
 
571
//      Handles expressions of the form:
 
572
//
 
573
//          a - b
 
574
//
 
575
//      where a and b are parsers. The expression returns a composite
 
576
//      parser that matches a but not b. One (not both) of the operands
 
577
//      may be a literal char, wchar_t or a primitive string char const*,
 
578
//      wchar_t const*.
 
579
//
 
580
///////////////////////////////////////////////////////////////////////////////
 
581
struct difference_parser_gen;
 
582
 
 
583
template <typename A, typename B>
 
584
struct difference
 
585
:   public binary<A, B, parser<difference<A, B> > >
 
586
{
 
587
    typedef difference<A, B>                self_t;
 
588
    typedef binary_parser_category          parser_category_t;
 
589
    typedef difference_parser_gen           parser_generator_t;
 
590
    typedef binary<A, B, parser<self_t> >   base_t;
 
591
 
 
592
    difference()
 
593
    : base_t(A(), B()) {}
 
594
    difference(A const& a, B const& b)
 
595
    : base_t(a, b) {}
 
596
 
 
597
    template <typename ScannerT>
 
598
    typename parser_result<self_t, ScannerT>::type
 
599
    parse(ScannerT const& scan) const
 
600
    {
 
601
        typedef typename parser_result<self_t, ScannerT>::type result_t;
 
602
        typedef typename ScannerT::iterator_t iterator_t;
 
603
        iterator_t save = scan.first;
 
604
        if (result_t hl = this->left().parse(scan))
 
605
        {
 
606
            std::swap(save, scan.first);
 
607
            result_t hr = this->right().parse(scan);
 
608
            if (!hr || (hr.length() < hl.length()))
 
609
            {
 
610
                scan.first = save;
 
611
                return hl;
 
612
            }
 
613
        }
 
614
 
 
615
        return scan.no_match();
 
616
    }
 
617
};
 
618
 
 
619
//////////////////////////////////
 
620
struct difference_parser_gen
 
621
{
 
622
    template <typename A, typename B>
 
623
    struct result {
 
624
 
 
625
        typedef difference<
 
626
                    typename as_parser<A>::type, typename as_parser<B>::type
 
627
                > type;
 
628
    };
 
629
 
 
630
    template <typename A, typename B>
 
631
    static difference
 
632
    <
 
633
        typename as_parser<A>::type,
 
634
        typename as_parser<B>::type
 
635
    >
 
636
    generate(A const& a, B const& b)
 
637
    {
 
638
        return difference<BOOST_SPIRIT_TYPENAME as_parser<A>::type,
 
639
            BOOST_SPIRIT_TYPENAME as_parser<B>::type>
 
640
                (as_parser<A>::convert(a), as_parser<B>::convert(b));
 
641
    }
 
642
};
 
643
 
 
644
//////////////////////////////////
 
645
template <typename A, typename B>
 
646
difference<A, B>
 
647
operator-(parser<A> const& a, parser<B> const& b);
 
648
 
 
649
//////////////////////////////////
 
650
template <typename A>
 
651
difference<A, chlit<char> >
 
652
operator-(parser<A> const& a, char b);
 
653
 
 
654
//////////////////////////////////
 
655
template <typename B>
 
656
difference<chlit<char>, B>
 
657
operator-(char a, parser<B> const& b);
 
658
 
 
659
//////////////////////////////////
 
660
template <typename A>
 
661
difference<A, strlit<char const*> >
 
662
operator-(parser<A> const& a, char const* b);
 
663
 
 
664
//////////////////////////////////
 
665
template <typename B>
 
666
difference<strlit<char const*>, B>
 
667
operator-(char const* a, parser<B> const& b);
 
668
 
 
669
//////////////////////////////////
 
670
template <typename A>
 
671
difference<A, chlit<wchar_t> >
 
672
operator-(parser<A> const& a, wchar_t b);
 
673
 
 
674
//////////////////////////////////
 
675
template <typename B>
 
676
difference<chlit<wchar_t>, B>
 
677
operator-(wchar_t a, parser<B> const& b);
 
678
 
 
679
//////////////////////////////////
 
680
template <typename A>
 
681
difference<A, strlit<wchar_t const*> >
 
682
operator-(parser<A> const& a, wchar_t const* b);
 
683
 
 
684
//////////////////////////////////
 
685
template <typename B>
 
686
difference<strlit<wchar_t const*>, B>
 
687
operator-(wchar_t const* a, parser<B> const& b);
 
688
 
 
689
///////////////////////////////////////////////////////////////////////////////
 
690
//
 
691
//  exclusive_or class
 
692
//
 
693
//      Handles expressions of the form:
 
694
//
 
695
//          a ^ b
 
696
//
 
697
//      where a and b are parsers. The expression returns a composite
 
698
//      parser that matches a or b but not both. One (not both) of the
 
699
//      operands may be a literal char, wchar_t or a primitive string
 
700
//      char const*, wchar_t const*.
 
701
//
 
702
///////////////////////////////////////////////////////////////////////////////
 
703
struct exclusive_or_parser_gen;
 
704
 
 
705
template <typename A, typename B>
 
706
struct exclusive_or
 
707
:   public binary<A, B, parser<exclusive_or<A, B> > >
 
708
{
 
709
    typedef exclusive_or<A, B>              self_t;
 
710
    typedef binary_parser_category          parser_category_t;
 
711
    typedef exclusive_or_parser_gen         parser_generator_t;
 
712
    typedef binary<A, B, parser<self_t> >   base_t;
 
713
 
 
714
    exclusive_or()
 
715
    : base_t(A(), B()) {}
 
716
    exclusive_or(A const& a, B const& b)
 
717
    : base_t(a, b) {}
 
718
 
 
719
    template <typename ScannerT>
 
720
    typename parser_result<self_t, ScannerT>::type
 
721
    parse(ScannerT const& scan) const
 
722
    {
 
723
        typedef typename parser_result<self_t, ScannerT>::type result_t;
 
724
        typedef typename ScannerT::iterator_t iterator_t;
 
725
 
 
726
        iterator_t save = scan.first;
 
727
        result_t l = this->left().parse(scan);
 
728
        std::swap(save, scan.first);
 
729
        result_t r = this->right().parse(scan);
 
730
 
 
731
        if (bool(l) ^ bool(r))
 
732
        {
 
733
            if (l)
 
734
                scan.first = save;
 
735
            return bool(l) ? l : r;
 
736
        }
 
737
 
 
738
        return scan.no_match();
 
739
    }
 
740
};
 
741
 
 
742
//////////////////////////////////
 
743
struct exclusive_or_parser_gen
 
744
{
 
745
    template <typename A, typename B>
 
746
    struct result {
 
747
 
 
748
        typedef exclusive_or<
 
749
                    typename as_parser<A>::type, typename as_parser<B>::type
 
750
                > type;
 
751
    };
 
752
 
 
753
    template <typename A, typename B>
 
754
    static exclusive_or
 
755
    <
 
756
        typename as_parser<A>::type,
 
757
        typename as_parser<B>::type
 
758
    >
 
759
    generate(A const& a, B const& b)
 
760
    {
 
761
        return exclusive_or<BOOST_SPIRIT_TYPENAME as_parser<A>::type,
 
762
            BOOST_SPIRIT_TYPENAME as_parser<B>::type>
 
763
                (as_parser<A>::convert(a), as_parser<B>::convert(b));
 
764
    }
 
765
};
 
766
 
 
767
//////////////////////////////////
 
768
template <typename A, typename B>
 
769
exclusive_or<A, B>
 
770
operator^(parser<A> const& a, parser<B> const& b);
 
771
 
 
772
//////////////////////////////////
 
773
template <typename A>
 
774
exclusive_or<A, chlit<char> >
 
775
operator^(parser<A> const& a, char b);
 
776
 
 
777
//////////////////////////////////
 
778
template <typename B>
 
779
exclusive_or<chlit<char>, B>
 
780
operator^(char a, parser<B> const& b);
 
781
 
 
782
//////////////////////////////////
 
783
template <typename A>
 
784
exclusive_or<A, strlit<char const*> >
 
785
operator^(parser<A> const& a, char const* b);
 
786
 
 
787
//////////////////////////////////
 
788
template <typename B>
 
789
exclusive_or<strlit<char const*>, B>
 
790
operator^(char const* a, parser<B> const& b);
 
791
 
 
792
//////////////////////////////////
 
793
template <typename A>
 
794
exclusive_or<A, chlit<wchar_t> >
 
795
operator^(parser<A> const& a, wchar_t b);
 
796
 
 
797
//////////////////////////////////
 
798
template <typename B>
 
799
exclusive_or<chlit<wchar_t>, B>
 
800
operator^(wchar_t a, parser<B> const& b);
 
801
 
 
802
//////////////////////////////////
 
803
template <typename A>
 
804
exclusive_or<A, strlit<wchar_t const*> >
 
805
operator^(parser<A> const& a, wchar_t const* b);
 
806
 
 
807
//////////////////////////////////
 
808
template <typename B>
 
809
exclusive_or<strlit<wchar_t const*>, B>
 
810
operator^(wchar_t const* a, parser<B> const& b);
 
811
 
 
812
///////////////////////////////////////////////////////////////////////////////
 
813
//
 
814
//  optional class
 
815
//
 
816
//      Handles expressions of the form:
 
817
//
 
818
//          !a
 
819
//
 
820
//      where a is a parser. The expression returns a composite
 
821
//      parser that matches its subject zero (0) or one (1) time.
 
822
//
 
823
///////////////////////////////////////////////////////////////////////////////
 
824
struct optional_parser_gen;
 
825
 
 
826
template <typename S>
 
827
struct optional
 
828
:   public unary<S, parser<optional<S> > >
 
829
{
 
830
    typedef optional<S>                 self_t;
 
831
    typedef unary_parser_category       parser_category_t;
 
832
    typedef optional_parser_gen         parser_generator_t;
 
833
    typedef unary<S, parser<self_t> >   base_t;
 
834
 
 
835
    optional()
 
836
    : base_t(S()) {}
 
837
    optional(S const& a)
 
838
    : base_t(a) {}
 
839
 
 
840
    template <typename ScannerT>
 
841
    typename parser_result<self_t, ScannerT>::type
 
842
    parse(ScannerT const& scan) const
 
843
    {
 
844
        typedef typename parser_result<self_t, ScannerT>::type result_t;
 
845
        typedef typename ScannerT::iterator_t iterator_t;
 
846
        iterator_t save = scan.first;
 
847
        if (result_t r = this->subject().parse(scan))
 
848
        {
 
849
            return r;
 
850
        }
 
851
        else
 
852
        {
 
853
            scan.first = save;
 
854
            return scan.empty_match();
 
855
        }
 
856
    }
 
857
};
 
858
 
 
859
//////////////////////////////////
 
860
struct optional_parser_gen
 
861
{
 
862
    template <typename S>
 
863
    struct result {
 
864
 
 
865
        typedef optional<S> type;
 
866
    };
 
867
 
 
868
    template <typename S>
 
869
    static optional<S>
 
870
    generate(parser<S> const& a)
 
871
    {
 
872
        return optional<S>(a.derived());
 
873
    }
 
874
};
 
875
 
 
876
//////////////////////////////////
 
877
template <typename S>
 
878
optional<S>
 
879
operator!(parser<S> const& a);
 
880
 
 
881
///////////////////////////////////////////////////////////////////////////////
 
882
//
 
883
//  kleene_star class
 
884
//
 
885
//      Handles expressions of the form:
 
886
//
 
887
//          *a
 
888
//
 
889
//      where a is a parser. The expression returns a composite
 
890
//      parser that matches its subject zero (0) or more times.
 
891
//
 
892
///////////////////////////////////////////////////////////////////////////////
 
893
struct kleene_star_parser_gen;
 
894
 
 
895
template <typename S>
 
896
struct kleene_star
 
897
:   public unary<S, parser<kleene_star<S> > >
 
898
{
 
899
    typedef kleene_star<S>              self_t;
 
900
    typedef unary_parser_category       parser_category_t;
 
901
    typedef kleene_star_parser_gen      parser_generator_t;
 
902
    typedef unary<S, parser<self_t> >   base_t;
 
903
 
 
904
    kleene_star()
 
905
    : base_t(S()) {}
 
906
    kleene_star(S const& a)
 
907
    : base_t(a) {}
 
908
 
 
909
    template <typename ScannerT>
 
910
    typename parser_result<self_t, ScannerT>::type
 
911
    parse(ScannerT const& scan) const
 
912
    {
 
913
        typedef typename parser_result<self_t, ScannerT>::type result_t;
 
914
        typedef typename ScannerT::iterator_t iterator_t;
 
915
        result_t hit = scan.empty_match();
 
916
 
 
917
        for (;;)
 
918
        {
 
919
            iterator_t save = scan.first;
 
920
            if (result_t next = this->subject().parse(scan))
 
921
            {
 
922
                scan.concat_match(hit, next);
 
923
            }
 
924
            else
 
925
            {
 
926
                scan.first = save;
 
927
                return hit;
 
928
            }
 
929
        }
 
930
    }
 
931
};
 
932
 
 
933
//////////////////////////////////
 
934
struct kleene_star_parser_gen
 
935
{
 
936
    template <typename S>
 
937
    struct result {
 
938
 
 
939
        typedef kleene_star<S> type;
 
940
    };
 
941
 
 
942
    template <typename S>
 
943
    static kleene_star<S>
 
944
    generate(parser<S> const& a)
 
945
    {
 
946
        return kleene_star<S>(a.derived());
 
947
    }
 
948
};
 
949
 
 
950
//////////////////////////////////
 
951
template <typename S>
 
952
kleene_star<S>
 
953
operator*(parser<S> const& a);
 
954
 
 
955
///////////////////////////////////////////////////////////////////////////////
 
956
//
 
957
//  positive class
 
958
//
 
959
//      Handles expressions of the form:
 
960
//
 
961
//          +a
 
962
//
 
963
//      where a is a parser. The expression returns a composite
 
964
//      parser that matches its subject one (1) or more times.
 
965
//
 
966
///////////////////////////////////////////////////////////////////////////////
 
967
struct positive_parser_gen;
 
968
 
 
969
template <typename S>
 
970
struct positive
 
971
:   public unary<S, parser<positive<S> > >
 
972
{
 
973
    typedef positive<S>                 self_t;
 
974
    typedef unary_parser_category       parser_category_t;
 
975
    typedef positive_parser_gen         parser_generator_t;
 
976
    typedef unary<S, parser<self_t> >   base_t;
 
977
 
 
978
    positive()
 
979
    : base_t(S()) {}
 
980
    positive(S const& a)
 
981
    : base_t(a) {}
 
982
 
 
983
    template <typename ScannerT>
 
984
    typename parser_result<self_t, ScannerT>::type
 
985
    parse(ScannerT const& scan) const
 
986
    {
 
987
        typedef typename parser_result<self_t, ScannerT>::type result_t;
 
988
        typedef typename ScannerT::iterator_t iterator_t;
 
989
        result_t hit = this->subject().parse(scan);
 
990
 
 
991
        if (hit)
 
992
        {
 
993
            for (;;)
 
994
            {
 
995
                iterator_t save = scan.first;
 
996
                if (result_t next = this->subject().parse(scan))
 
997
                {
 
998
                    scan.concat_match(hit, next);
 
999
                }
 
1000
                else
 
1001
                {
 
1002
                    scan.first = save;
 
1003
                    break;
 
1004
                }
 
1005
            }
 
1006
        }
 
1007
        return hit;
 
1008
    }
 
1009
};
 
1010
 
 
1011
//////////////////////////////////
 
1012
struct positive_parser_gen
 
1013
{
 
1014
    template <typename S>
 
1015
    struct result {
 
1016
 
 
1017
        typedef positive<S> type;
 
1018
    };
 
1019
 
 
1020
    template <typename S>
 
1021
    static positive<S>
 
1022
    generate(parser<S> const& a)
 
1023
    {
 
1024
        return positive<S>(a.derived());
 
1025
    }
 
1026
};
 
1027
 
 
1028
//////////////////////////////////
 
1029
template <typename S>
 
1030
inline positive<S>
 
1031
operator + (parser<S> const& a);
 
1032
 
 
1033
///////////////////////////////////////////////////////////////////////////////
 
1034
//
 
1035
//  operator% is defined as:
 
1036
//  a % b ---> a >> *(b >> a)
 
1037
//
 
1038
///////////////////////////////////////////////////////////////////////////////
 
1039
template <typename A, typename B>
 
1040
sequence<A, kleene_star<sequence<B, A> > >
 
1041
operator%(parser<A> const& a, parser<B> const& b);
 
1042
 
 
1043
//////////////////////////////////
 
1044
template <typename A>
 
1045
sequence<A, kleene_star<sequence<chlit<char>, A> > >
 
1046
operator%(parser<A> const& a, char b);
 
1047
 
 
1048
//////////////////////////////////
 
1049
template <typename B>
 
1050
sequence<chlit<char>, kleene_star<sequence<B, chlit<char> > > >
 
1051
operator%(char a, parser<B> const& b);
 
1052
 
 
1053
//////////////////////////////////
 
1054
template <typename A>
 
1055
sequence<A, kleene_star<sequence<strlit<char const*>, A> > >
 
1056
operator%(parser<A> const& a, char const* b);
 
1057
 
 
1058
//////////////////////////////////
 
1059
template <typename B>
 
1060
sequence<strlit<char const*>,
 
1061
    kleene_star<sequence<B, strlit<char const*> > > >
 
1062
operator%(char const* a, parser<B> const& b);
 
1063
 
 
1064
//////////////////////////////////
 
1065
template <typename A>
 
1066
sequence<A, kleene_star<sequence<chlit<wchar_t>, A> > >
 
1067
operator%(parser<A> const& a, wchar_t b);
 
1068
 
 
1069
//////////////////////////////////
 
1070
template <typename B>
 
1071
sequence<chlit<wchar_t>, kleene_star<sequence<B, chlit<wchar_t> > > >
 
1072
operator%(wchar_t a, parser<B> const& b);
 
1073
 
 
1074
//////////////////////////////////
 
1075
template <typename A>
 
1076
sequence<A, kleene_star<sequence<strlit<wchar_t const*>, A> > >
 
1077
operator%(parser<A> const& a, wchar_t const* b);
 
1078
 
 
1079
//////////////////////////////////
 
1080
template <typename B>
 
1081
sequence<strlit<wchar_t const*>,
 
1082
    kleene_star<sequence<B, strlit<wchar_t const*> > > >
 
1083
operator%(wchar_t const* a, parser<B> const& b);
 
1084
 
 
1085
///////////////////////////////////////////////////////////////////////////////
 
1086
}} // namespace boost::spirit
 
1087
 
 
1088
#endif
 
1089
 
 
1090
#include "boost/spirit/core/composite/impl/operators.ipp"