Chris@16: /*============================================================================= Chris@16: Copyright (c) 2001-2011 Joel de Guzman Chris@16: Chris@16: Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: =============================================================================*/ Chris@16: #if !defined(SPIRIT_DIFFERENCE_FEBRUARY_11_2007_1250PM) Chris@16: #define SPIRIT_DIFFERENCE_FEBRUARY_11_2007_1250PM Chris@16: Chris@16: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace spirit Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Enablers Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template <> Chris@16: struct use_operator // enables - Chris@16: : mpl::true_ {}; Chris@16: }} Chris@16: Chris@16: namespace boost { namespace spirit { namespace qi Chris@16: { Chris@16: template Chris@16: struct difference : binary_parser > Chris@16: { Chris@16: typedef Left left_type; Chris@16: typedef Right right_type; Chris@16: Chris@16: template Chris@16: struct attribute Chris@16: { Chris@16: typedef typename Chris@16: traits::attribute_of::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: difference(Left const& left_, Right const& right_) Chris@16: : left(left_), right(right_) {} Chris@16: Chris@16: template Chris@16: bool parse(Iterator& first, Iterator const& last Chris@16: , Context& context, Skipper const& skipper Chris@16: , Attribute& attr_) const Chris@16: { Chris@16: // Unlike classic Spirit, with this version of difference, the rule Chris@16: // lit("policeman") - "police" will always fail to match. Chris@16: Chris@16: // Spirit2 does not count the matching chars while parsing and Chris@16: // there is no reliable and fast way to check if the LHS matches Chris@16: // more than the RHS. Chris@16: Chris@16: // Try RHS first Chris@16: Iterator start = first; Chris@16: if (right.parse(first, last, context, skipper, unused)) Chris@16: { Chris@16: // RHS succeeds, we fail. Chris@16: first = start; Chris@16: return false; Chris@16: } Chris@16: // RHS fails, now try LHS Chris@16: return left.parse(first, last, context, skipper, attr_); Chris@16: } Chris@16: Chris@16: template Chris@16: info what(Context& context) const Chris@16: { Chris@16: return info("difference", Chris@16: std::make_pair(left.what(context), right.what(context))); Chris@16: } Chris@16: Chris@16: Left left; Chris@16: Right right; Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Parser generators: make_xxx function (objects) Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct make_composite Chris@16: : make_binary_composite Chris@16: {}; Chris@16: }}} Chris@16: Chris@16: namespace boost { namespace spirit { namespace traits Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct has_semantic_action > Chris@16: : binary_has_semantic_action {}; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct handles_container, Attribute, Context Chris@16: , Iterator> Chris@16: : binary_handles_container {}; Chris@16: }}} Chris@16: Chris@16: #endif