Chris@16: /*============================================================================= Chris@16: Copyright (c) 2002-2003 Martin Wille Chris@16: http://spirit.sourceforge.net/ Chris@16: Chris@16: Use, modification and distribution is subject to the Boost Software Chris@16: License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: http://www.boost.org/LICENSE_1_0.txt) Chris@16: =============================================================================*/ Chris@16: #ifndef BOOST_SPIRIT_CONDITIONS_IPP Chris@16: #define BOOST_SPIRIT_CONDITIONS_IPP Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: #include Chris@16: #include Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: namespace boost { namespace spirit { Chris@16: Chris@16: BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN Chris@16: Chris@16: namespace impl { Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // condition evaluation Chris@16: // Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: ////////////////////////////////// Chris@16: // condition_parser_selector, decides which parser to use for a condition Chris@16: // If the template argument is a parser then that parser is used. Chris@16: // If the template argument is a functor then a condition parser using Chris@16: // the functor is chosen Chris@16: Chris@16: template struct embed_t_accessor Chris@16: { Chris@16: typedef typename T::embed_t type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct condition_parser_selector Chris@16: { Chris@16: typedef Chris@16: typename mpl::if_< Chris@16: is_parser, Chris@16: ConditionT, Chris@16: condition_parser Chris@16: >::type Chris@16: type; Chris@16: Chris@16: typedef typename embed_t_accessor::type embed_t; Chris@16: }; Chris@16: Chris@16: ////////////////////////////////// Chris@16: // condition_evaluator, uses a parser to check wether a condition is met Chris@16: // takes a parser or a functor that can be evaluated in boolean context Chris@16: // as template parameter. Chris@16: Chris@16: // JDG 4-15-03 refactored Chris@16: template Chris@16: struct condition_evaluator Chris@16: { Chris@16: typedef condition_parser_selector selector_t; Chris@16: typedef typename selector_t::type selected_t; Chris@16: typedef typename selector_t::embed_t cond_embed_t; Chris@16: Chris@16: typedef typename boost::call_traits::param_type Chris@16: param_t; Chris@16: Chris@16: condition_evaluator(param_t s) : cond(s) {} Chris@16: Chris@16: ///////////////////////////// Chris@16: // evaluate, checks wether condition is met Chris@16: // returns length of a match or a negative number for no-match Chris@16: template Chris@16: std::ptrdiff_t Chris@16: evaluate(ScannerT const &scan) const Chris@16: { Chris@16: typedef typename ScannerT::iterator_t iterator_t; Chris@16: typedef typename parser_result::type cres_t; Chris@16: iterator_t save(scan.first); Chris@16: cres_t result = cond.parse(scan); Chris@16: if (!result) // reset the position if evaluation Chris@16: scan.first = save; // fails. Chris@16: return result.length(); Chris@16: } Chris@16: Chris@16: cond_embed_t cond; Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: } // namespace impl Chris@16: Chris@16: BOOST_SPIRIT_CLASSIC_NAMESPACE_END Chris@16: Chris@16: }} // namespace boost::spirit Chris@16: Chris@16: #endif