1 | /*=============================================================================
|
---|
2 | Copyright (c) 1998-2003 Joel de Guzman
|
---|
3 | Copyright (c) 2001 Daniel Nuffer
|
---|
4 | Copyright (c) 2002 Hartmut Kaiser
|
---|
5 | http://spirit.sourceforge.net/
|
---|
6 |
|
---|
7 | Use, modification and distribution is subject to the Boost Software
|
---|
8 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
---|
9 | http://www.boost.org/LICENSE_1_0.txt)
|
---|
10 | =============================================================================*/
|
---|
11 | #if !defined(BOOST_SPIRIT_EXCLUSIVE_OR_HPP)
|
---|
12 | #define BOOST_SPIRIT_EXCLUSIVE_OR_HPP
|
---|
13 |
|
---|
14 | #include <boost/spirit/core/parser.hpp>
|
---|
15 | #include <boost/spirit/core/primitives/primitives.hpp>
|
---|
16 | #include <boost/spirit/core/composite/composite.hpp>
|
---|
17 | #include <boost/spirit/meta/as_parser.hpp>
|
---|
18 |
|
---|
19 | namespace boost { namespace spirit {
|
---|
20 |
|
---|
21 | ///////////////////////////////////////////////////////////////////////////
|
---|
22 | //
|
---|
23 | // exclusive_or class
|
---|
24 | //
|
---|
25 | // Handles expressions of the form:
|
---|
26 | //
|
---|
27 | // a ^ b
|
---|
28 | //
|
---|
29 | // where a and b are parsers. The expression returns a composite
|
---|
30 | // parser that matches a or b but not both. One (not both) of the
|
---|
31 | // operands may be a literal char, wchar_t or a primitive string
|
---|
32 | // char const*, wchar_t const*.
|
---|
33 | //
|
---|
34 | ///////////////////////////////////////////////////////////////////////////
|
---|
35 | struct exclusive_or_parser_gen;
|
---|
36 |
|
---|
37 | template <typename A, typename B>
|
---|
38 | struct exclusive_or
|
---|
39 | : public binary<A, B, parser<exclusive_or<A, B> > >
|
---|
40 | {
|
---|
41 | typedef exclusive_or<A, B> self_t;
|
---|
42 | typedef binary_parser_category parser_category_t;
|
---|
43 | typedef exclusive_or_parser_gen parser_generator_t;
|
---|
44 | typedef binary<A, B, parser<self_t> > base_t;
|
---|
45 |
|
---|
46 | exclusive_or(A const& a, B const& b)
|
---|
47 | : base_t(a, b) {}
|
---|
48 |
|
---|
49 | template <typename ScannerT>
|
---|
50 | typename parser_result<self_t, ScannerT>::type
|
---|
51 | parse(ScannerT const& scan) const
|
---|
52 | {
|
---|
53 | typedef typename parser_result<self_t, ScannerT>::type result_t;
|
---|
54 | typedef typename ScannerT::iterator_t iterator_t;
|
---|
55 |
|
---|
56 | iterator_t save = scan.first;
|
---|
57 | result_t l = this->left().parse(scan);
|
---|
58 | std::swap(save, scan.first);
|
---|
59 | result_t r = this->right().parse(scan);
|
---|
60 |
|
---|
61 | if (l ? !bool(r) : bool(r))
|
---|
62 | {
|
---|
63 | if (l)
|
---|
64 | scan.first = save;
|
---|
65 | return l ? l : r;
|
---|
66 | }
|
---|
67 |
|
---|
68 | return scan.no_match();
|
---|
69 | }
|
---|
70 | };
|
---|
71 |
|
---|
72 | struct exclusive_or_parser_gen
|
---|
73 | {
|
---|
74 | template <typename A, typename B>
|
---|
75 | struct result
|
---|
76 | {
|
---|
77 | typedef
|
---|
78 | exclusive_or<
|
---|
79 | typename as_parser<A>::type
|
---|
80 | , typename as_parser<B>::type
|
---|
81 | >
|
---|
82 | type;
|
---|
83 | };
|
---|
84 |
|
---|
85 | template <typename A, typename B>
|
---|
86 | static exclusive_or<
|
---|
87 | typename as_parser<A>::type
|
---|
88 | , typename as_parser<B>::type
|
---|
89 | >
|
---|
90 | generate(A const& a, B const& b)
|
---|
91 | {
|
---|
92 | return exclusive_or<BOOST_DEDUCED_TYPENAME as_parser<A>::type,
|
---|
93 | BOOST_DEDUCED_TYPENAME as_parser<B>::type>
|
---|
94 | (as_parser<A>::convert(a), as_parser<B>::convert(b));
|
---|
95 | }
|
---|
96 | };
|
---|
97 |
|
---|
98 | template <typename A, typename B>
|
---|
99 | exclusive_or<A, B>
|
---|
100 | operator^(parser<A> const& a, parser<B> const& b);
|
---|
101 |
|
---|
102 | template <typename A>
|
---|
103 | exclusive_or<A, chlit<char> >
|
---|
104 | operator^(parser<A> const& a, char b);
|
---|
105 |
|
---|
106 | template <typename B>
|
---|
107 | exclusive_or<chlit<char>, B>
|
---|
108 | operator^(char a, parser<B> const& b);
|
---|
109 |
|
---|
110 | template <typename A>
|
---|
111 | exclusive_or<A, strlit<char const*> >
|
---|
112 | operator^(parser<A> const& a, char const* b);
|
---|
113 |
|
---|
114 | template <typename B>
|
---|
115 | exclusive_or<strlit<char const*>, B>
|
---|
116 | operator^(char const* a, parser<B> const& b);
|
---|
117 |
|
---|
118 | template <typename A>
|
---|
119 | exclusive_or<A, chlit<wchar_t> >
|
---|
120 | operator^(parser<A> const& a, wchar_t b);
|
---|
121 |
|
---|
122 | template <typename B>
|
---|
123 | exclusive_or<chlit<wchar_t>, B>
|
---|
124 | operator^(wchar_t a, parser<B> const& b);
|
---|
125 |
|
---|
126 | template <typename A>
|
---|
127 | exclusive_or<A, strlit<wchar_t const*> >
|
---|
128 | operator^(parser<A> const& a, wchar_t const* b);
|
---|
129 |
|
---|
130 | template <typename B>
|
---|
131 | exclusive_or<strlit<wchar_t const*>, B>
|
---|
132 | operator^(wchar_t const* a, parser<B> const& b);
|
---|
133 |
|
---|
134 | }} // namespace boost::spirit
|
---|
135 |
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | #include <boost/spirit/core/composite/impl/exclusive_or.ipp>
|
---|