1 // Copyright (c) 2011 Aaron Graham
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #if !defined(BOOST_SPIRIT_REPOSITORY_QI_ADVANCE_JAN_23_2011_1203PM)
7 #define BOOST_SPIRIT_REPOSITORY_QI_ADVANCE_JAN_23_2011_1203PM
9 #include <boost/spirit/include/qi_parse.hpp>
11 ///////////////////////////////////////////////////////////////////////////////
12 // definition the place holder
13 namespace boost { namespace spirit { namespace repository { namespace qi
15 BOOST_SPIRIT_TERMINAL_EX(advance);
18 ///////////////////////////////////////////////////////////////////////////////
19 // implementation the enabler
20 namespace boost { namespace spirit
22 template <typename A0>
23 struct use_terminal<qi::domain
24 , terminal_ex<repository::qi::tag::advance, fusion::vector1<A0> > >
25 : mpl::or_<is_integral<A0>, is_enum<A0> >
29 struct use_lazy_terminal<qi::domain, repository::qi::tag::advance, 1>
34 ///////////////////////////////////////////////////////////////////////////////
35 // implementation of the parser
36 namespace boost { namespace spirit { namespace repository { namespace qi
38 template <typename Int>
40 : boost::spirit::qi::primitive_parser< advance_parser<Int> >
42 // Define the attribute type exposed by this parser component
43 template <typename Context, typename Iterator>
46 typedef boost::spirit::unused_type type;
49 advance_parser(Int dist)
53 // This function is called during the actual parsing process
54 template <typename Iterator, typename Context
55 , typename Skipper, typename Attribute>
56 bool parse(Iterator& first, Iterator const& last
57 , Context&, Skipper&, Attribute&) const
59 // This series of checks is designed to fail parsing on negative
60 // values, without generating a "expression always evaluates true"
61 // warning on unsigned types.
62 if (dist == Int(0)) return true;
63 if (dist < Int(1)) return false;
65 typedef typename std::iterator_traits<Iterator>::iterator_category
67 return advance(first, last, iterator_category());
70 // This function is called during error handling to create
71 // a human readable string for the error context.
72 template <typename Context>
73 boost::spirit::info what(Context&) const
75 return boost::spirit::info("advance");
79 // this is the general implementation used by most iterator categories
80 template <typename Iterator, typename IteratorCategory>
81 bool advance(Iterator& first, Iterator const& last
82 , IteratorCategory) const
88 if (i == last) return false;
96 // this is a specialization for random access iterators
97 template <typename Iterator>
98 bool advance(Iterator& first, Iterator const& last
99 , std::random_access_iterator_tag) const
101 Iterator const it = first + dist;
102 if (it > last) return false;
111 ///////////////////////////////////////////////////////////////////////////////
112 // instantiation of the parser
113 namespace boost { namespace spirit { namespace qi
115 template <typename Modifiers, typename A0>
116 struct make_primitive<
117 terminal_ex<repository::qi::tag::advance, fusion::vector1<A0> >
120 typedef repository::qi::advance_parser<A0> result_type;
122 template <typename Terminal>
123 result_type operator()(Terminal const& term, unused_type) const
125 return result_type(fusion::at_c<0>(term.args));