Release 1.39.0
[boost.git] / Boost_1_39_0 / boost / spirit / home / qi / operator / difference.hpp
blobb84738ff00d6106680d0504d3cd8a65e33a011c4
1 /*=============================================================================
2 Copyright (c) 2001-2007 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_FEB_11_2007_1250PM)
8 #define SPIRIT_DIFFERENCE_FEB_11_2007_1250PM
10 #include <boost/spirit/home/qi/domain.hpp>
11 #include <boost/spirit/home/support/component.hpp>
12 #include <boost/spirit/home/support/attribute_of.hpp>
13 #include <vector>
15 namespace boost { namespace spirit { namespace qi
17 struct difference
19 template <typename Component, typename Context, typename Iterator>
20 struct attribute
22 typedef typename
23 result_of::left<Component>::type
24 left_type;
26 typedef typename
27 traits::attribute_of<
28 qi::domain, left_type, Context, Iterator>::type
29 type;
32 template <
33 typename Component
34 , typename Iterator, typename Context
35 , typename Skipper, typename Attribute>
36 static bool parse(
37 Component const& component
38 , Iterator& first, Iterator const& last
39 , Context& context, Skipper const& skipper
40 , Attribute& attr)
42 typedef typename
43 result_of::left<Component>::type::director
44 ldirector;
46 typedef typename
47 result_of::right<Component>::type::director
48 rdirector;
50 // Unlike classic Spirit, with this version of difference, the rule
51 // lit("policeman") - "police" will always fail to match.
53 // Spirit2 does not count the matching chars while parsing and
54 // there is no reliable and fast way to check if the LHS matches
55 // more than the RHS.
57 // Try RHS first
58 Iterator start = first;
59 if (rdirector::parse(spirit::right(component), first, last, context,
60 skipper, unused))
62 // RHS succeeds, we fail.
63 first = start;
64 return false;
66 // RHS fails, now try LHS
67 return ldirector::parse(spirit::left(component), first, last,
68 context, skipper, attr);
71 template <typename Component, typename Context>
72 static std::string what(Component const& component, Context const& ctx)
74 std::string result = "difference[";
76 typedef typename
77 result_of::left<Component>::type::director
78 ldirector;
80 typedef typename
81 result_of::right<Component>::type::director
82 rdirector;
84 result += ldirector::what(spirit::left(component), ctx);
85 result += ", ";
86 result += rdirector::what(spirit::right(component), ctx);
87 result += "]";
88 return result;
91 }}}
93 #endif