1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #if !defined(SPIRIT_DIFFERENCE_FEBRUARY_11_2007_1250PM)
8 #define SPIRIT_DIFFERENCE_FEBRUARY_11_2007_1250PM
14 #include <boost/spirit/home/qi/domain.hpp>
15 #include <boost/spirit/home/qi/meta_compiler.hpp>
16 #include <boost/spirit/home/qi/parser.hpp>
17 #include <boost/spirit/home/qi/detail/attributes.hpp>
18 #include <boost/spirit/home/support/info.hpp>
19 #include <boost/spirit/home/support/has_semantic_action.hpp>
20 #include <boost/spirit/home/support/handles_container.hpp>
21 #include <boost/fusion/include/at.hpp>
23 namespace boost { namespace spirit
25 ///////////////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////////////
29 struct use_operator<qi::domain, proto::tag::minus> // enables -
33 namespace boost { namespace spirit { namespace qi
35 template <typename Left, typename Right>
36 struct difference : binary_parser<difference<Left, Right> >
38 typedef Left left_type;
39 typedef Right right_type;
41 template <typename Context, typename Iterator>
45 traits::attribute_of<left_type, Context, Iterator>::type
49 difference(Left const& left_, Right const& right_)
50 : left(left_), right(right_) {}
52 template <typename Iterator, typename Context
53 , typename Skipper, typename Attribute>
54 bool parse(Iterator& first, Iterator const& last
55 , Context& context, Skipper const& skipper
56 , Attribute& attr_) const
58 // Unlike classic Spirit, with this version of difference, the rule
59 // lit("policeman") - "police" will always fail to match.
61 // Spirit2 does not count the matching chars while parsing and
62 // there is no reliable and fast way to check if the LHS matches
66 Iterator start = first;
67 if (right.parse(first, last, context, skipper, unused))
69 // RHS succeeds, we fail.
73 // RHS fails, now try LHS
74 return left.parse(first, last, context, skipper, attr_);
77 template <typename Context>
78 info what(Context& context) const
80 return info("difference",
81 std::make_pair(left.what(context), right.what(context)));
88 ///////////////////////////////////////////////////////////////////////////
89 // Parser generators: make_xxx function (objects)
90 ///////////////////////////////////////////////////////////////////////////
91 template <typename Elements, typename Modifiers>
92 struct make_composite<proto::tag::minus, Elements, Modifiers>
93 : make_binary_composite<Elements, difference>
97 namespace boost { namespace spirit { namespace traits
99 ///////////////////////////////////////////////////////////////////////////
100 template <typename Left, typename Right>
101 struct has_semantic_action<qi::difference<Left, Right> >
102 : binary_has_semantic_action<Left, Right> {};
104 ///////////////////////////////////////////////////////////////////////////
105 template <typename Left, typename Right, typename Attribute
106 , typename Context, typename Iterator>
107 struct handles_container<qi::difference<Left, Right>, Attribute, Context
109 : binary_handles_container<Left, Right, Attribute, Context, Iterator> {};