[857] | 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_DIFFERENCE_HPP)
|
---|
| 12 | #define BOOST_SPIRIT_DIFFERENCE_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 | // difference: a - b; Matches a but not b
|
---|
| 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 but not b. One (not both) of the operands
|
---|
| 31 | // may be a literal char, wchar_t or a primitive string char const*,
|
---|
| 32 | // wchar_t const*.
|
---|
| 33 | //
|
---|
| 34 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 35 | struct difference_parser_gen;
|
---|
| 36 |
|
---|
| 37 | template <typename A, typename B>
|
---|
| 38 | struct difference
|
---|
| 39 | : public binary<A, B, parser<difference<A, B> > >
|
---|
| 40 | {
|
---|
| 41 | typedef difference<A, B> self_t;
|
---|
| 42 | typedef binary_parser_category parser_category_t;
|
---|
| 43 | typedef difference_parser_gen parser_generator_t;
|
---|
| 44 | typedef binary<A, B, parser<self_t> > base_t;
|
---|
| 45 |
|
---|
| 46 | difference(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 | iterator_t save = scan.first;
|
---|
| 56 | if (result_t hl = this->left().parse(scan))
|
---|
| 57 | {
|
---|
| 58 | std::swap(save, scan.first);
|
---|
| 59 | result_t hr = this->right().parse(scan);
|
---|
| 60 | if (!hr || (hr.length() < hl.length()))
|
---|
| 61 | {
|
---|
| 62 | scan.first = save;
|
---|
| 63 | return hl;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | return scan.no_match();
|
---|
| 68 | }
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 | struct difference_parser_gen
|
---|
| 72 | {
|
---|
| 73 | template <typename A, typename B>
|
---|
| 74 | struct result
|
---|
| 75 | {
|
---|
| 76 | typedef
|
---|
| 77 | difference<
|
---|
| 78 | typename as_parser<A>::type
|
---|
| 79 | , typename as_parser<B>::type
|
---|
| 80 | >
|
---|
| 81 | type;
|
---|
| 82 | };
|
---|
| 83 |
|
---|
| 84 | template <typename A, typename B>
|
---|
| 85 | static difference<
|
---|
| 86 | typename as_parser<A>::type
|
---|
| 87 | , typename as_parser<B>::type
|
---|
| 88 | >
|
---|
| 89 | generate(A const& a, B const& b)
|
---|
| 90 | {
|
---|
| 91 | return difference<BOOST_DEDUCED_TYPENAME as_parser<A>::type,
|
---|
| 92 | BOOST_DEDUCED_TYPENAME as_parser<B>::type>
|
---|
| 93 | (as_parser<A>::convert(a), as_parser<B>::convert(b));
|
---|
| 94 | }
|
---|
| 95 | };
|
---|
| 96 |
|
---|
| 97 | template <typename A, typename B>
|
---|
| 98 | difference<A, B>
|
---|
| 99 | operator-(parser<A> const& a, parser<B> const& b);
|
---|
| 100 |
|
---|
| 101 | template <typename A>
|
---|
| 102 | difference<A, chlit<char> >
|
---|
| 103 | operator-(parser<A> const& a, char b);
|
---|
| 104 |
|
---|
| 105 | template <typename B>
|
---|
| 106 | difference<chlit<char>, B>
|
---|
| 107 | operator-(char a, parser<B> const& b);
|
---|
| 108 |
|
---|
| 109 | template <typename A>
|
---|
| 110 | difference<A, strlit<char const*> >
|
---|
| 111 | operator-(parser<A> const& a, char const* b);
|
---|
| 112 |
|
---|
| 113 | template <typename B>
|
---|
| 114 | difference<strlit<char const*>, B>
|
---|
| 115 | operator-(char const* a, parser<B> const& b);
|
---|
| 116 |
|
---|
| 117 | template <typename A>
|
---|
| 118 | difference<A, chlit<wchar_t> >
|
---|
| 119 | operator-(parser<A> const& a, wchar_t b);
|
---|
| 120 |
|
---|
| 121 | template <typename B>
|
---|
| 122 | difference<chlit<wchar_t>, B>
|
---|
| 123 | operator-(wchar_t a, parser<B> const& b);
|
---|
| 124 |
|
---|
| 125 | template <typename A>
|
---|
| 126 | difference<A, strlit<wchar_t const*> >
|
---|
| 127 | operator-(parser<A> const& a, wchar_t const* b);
|
---|
| 128 |
|
---|
| 129 | template <typename B>
|
---|
| 130 | difference<strlit<wchar_t const*>, B>
|
---|
| 131 | operator-(wchar_t const* a, parser<B> const& b);
|
---|
| 132 |
|
---|
| 133 | }} // namespace boost::spirit
|
---|
| 134 |
|
---|
| 135 | #endif
|
---|
| 136 |
|
---|
| 137 | #include <boost/spirit/core/composite/impl/difference.ipp>
|
---|