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/
7 Distributed under the Boost Software License, Version 1.0. (See accompanying
8 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #if !defined(BOOST_SPIRIT_DIFFERENCE_HPP)
11 #define BOOST_SPIRIT_DIFFERENCE_HPP
13 #include <boost/spirit/home/classic/namespace.hpp>
14 #include <boost/spirit/home/classic/core/parser.hpp>
15 #include <boost/spirit/home/classic/core/primitives/primitives.hpp>
16 #include <boost/spirit/home/classic/core/composite/composite.hpp>
17 #include <boost/spirit/home/classic/meta/as_parser.hpp>
19 namespace boost { namespace spirit {
21 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
23 ///////////////////////////////////////////////////////////////////////////
25 // difference: a - b; Matches a but not b
27 // Handles expressions of the form:
31 // where a and b are parsers. The expression returns a composite
32 // parser that matches a but not b. One (not both) of the operands
33 // may be a literal char, wchar_t or a primitive string char const*,
36 ///////////////////////////////////////////////////////////////////////////
37 struct difference_parser_gen;
39 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
41 #pragma warning(disable:4512) //assignment operator could not be generated
44 template <typename A, typename B>
46 : public binary<A, B, parser<difference<A, B> > >
48 typedef difference<A, B> self_t;
49 typedef binary_parser_category parser_category_t;
50 typedef difference_parser_gen parser_generator_t;
51 typedef binary<A, B, parser<self_t> > base_t;
53 difference(A const& a, B const& b)
56 template <typename ScannerT>
57 typename parser_result<self_t, ScannerT>::type
58 parse(ScannerT const& scan) const
60 typedef typename parser_result<self_t, ScannerT>::type result_t;
61 typedef typename ScannerT::iterator_t iterator_t;
62 iterator_t save = scan.first;
63 if (result_t hl = this->left().parse(scan))
65 std::swap(save, scan.first);
66 result_t hr = this->right().parse(scan);
67 if (!hr || (hr.length() < hl.length()))
74 return scan.no_match();
78 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
82 struct difference_parser_gen
84 template <typename A, typename B>
89 typename as_parser<A>::type
90 , typename as_parser<B>::type
95 template <typename A, typename B>
97 typename as_parser<A>::type
98 , typename as_parser<B>::type
100 generate(A const& a, B const& b)
102 return difference<BOOST_DEDUCED_TYPENAME as_parser<A>::type,
103 BOOST_DEDUCED_TYPENAME as_parser<B>::type>
104 (as_parser<A>::convert(a), as_parser<B>::convert(b));
108 template <typename A, typename B>
110 operator-(parser<A> const& a, parser<B> const& b);
112 template <typename A>
113 difference<A, chlit<char> >
114 operator-(parser<A> const& a, char b);
116 template <typename B>
117 difference<chlit<char>, B>
118 operator-(char a, parser<B> const& b);
120 template <typename A>
121 difference<A, strlit<char const*> >
122 operator-(parser<A> const& a, char const* b);
124 template <typename B>
125 difference<strlit<char const*>, B>
126 operator-(char const* a, parser<B> const& b);
128 template <typename A>
129 difference<A, chlit<wchar_t> >
130 operator-(parser<A> const& a, wchar_t b);
132 template <typename B>
133 difference<chlit<wchar_t>, B>
134 operator-(wchar_t a, parser<B> const& b);
136 template <typename A>
137 difference<A, strlit<wchar_t const*> >
138 operator-(parser<A> const& a, wchar_t const* b);
140 template <typename B>
141 difference<strlit<wchar_t const*>, B>
142 operator-(wchar_t const* a, parser<B> const& b);
144 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
146 }} // namespace BOOST_SPIRIT_CLASSIC_NS
150 #include <boost/spirit/home/classic/core/composite/impl/difference.ipp>