// Copyright (c) 2011 Aaron Graham // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #if !defined(BOOST_SPIRIT_REPOSITORY_QI_ADVANCE_JAN_23_2011_1203PM) #define BOOST_SPIRIT_REPOSITORY_QI_ADVANCE_JAN_23_2011_1203PM #include /////////////////////////////////////////////////////////////////////////////// // definition the place holder namespace boost { namespace spirit { namespace repository { namespace qi { BOOST_SPIRIT_TERMINAL_EX(advance); }}}} /////////////////////////////////////////////////////////////////////////////// // implementation the enabler namespace boost { namespace spirit { template struct use_terminal > > : mpl::or_, is_enum > {}; template <> struct use_lazy_terminal : mpl::true_ {}; }} /////////////////////////////////////////////////////////////////////////////// // implementation of the parser namespace boost { namespace spirit { namespace repository { namespace qi { template struct advance_parser : boost::spirit::qi::primitive_parser< advance_parser > { // Define the attribute type exposed by this parser component template struct attribute { typedef boost::spirit::unused_type type; }; advance_parser(Int dist) : dist(dist) {} // This function is called during the actual parsing process template bool parse(Iterator& first, Iterator const& last , Context&, Skipper&, Attribute&) const { // This series of checks is designed to fail parsing on negative // values, without generating a "expression always evaluates true" // warning on unsigned types. if (dist == Int(0)) return true; if (dist < Int(1)) return false; typedef typename std::iterator_traits::iterator_category iterator_category; return advance(first, last, iterator_category()); } // This function is called during error handling to create // a human readable string for the error context. template boost::spirit::info what(Context&) const { return boost::spirit::info("advance"); } private: // this is the general implementation used by most iterator categories template bool advance(Iterator& first, Iterator const& last , IteratorCategory) const { Int n = dist; Iterator i = first; while (n) { if (i == last) return false; ++i; --n; } first = i; return true; } // this is a specialization for random access iterators template bool advance(Iterator& first, Iterator const& last , std::random_access_iterator_tag) const { Iterator const it = first + dist; if (it > last) return false; first = it; return true; } Int const dist; }; }}}} /////////////////////////////////////////////////////////////////////////////// // instantiation of the parser namespace boost { namespace spirit { namespace qi { template struct make_primitive< terminal_ex > , Modifiers> { typedef repository::qi::advance_parser result_type; template result_type operator()(Terminal const& term, unused_type) const { return result_type(fusion::at_c<0>(term.args)); } }; }}} #endif