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/home/support/terminal.hpp>
10 #include <boost/spirit/include/qi_parse.hpp>
12 ///////////////////////////////////////////////////////////////////////////////
13 // definition the place holder
14 namespace boost { namespace spirit { namespace repository { namespace qi
16 BOOST_SPIRIT_TERMINAL_EX(advance)
19 ///////////////////////////////////////////////////////////////////////////////
20 // implementation the enabler
21 namespace boost { namespace spirit
23 template <typename A0>
24 struct use_terminal<qi::domain
25 , terminal_ex<repository::qi::tag::advance, fusion::vector1<A0> > >
26 : mpl::or_<is_integral<A0>, is_enum<A0> >
30 struct use_lazy_terminal<qi::domain, repository::qi::tag::advance, 1>
35 ///////////////////////////////////////////////////////////////////////////////
36 // implementation of the parser
37 namespace boost { namespace spirit { namespace repository { namespace qi
39 template <typename Int>
41 : boost::spirit::qi::primitive_parser< advance_parser<Int> >
43 // Define the attribute type exposed by this parser component
44 template <typename Context, typename Iterator>
47 typedef boost::spirit::unused_type type;
50 advance_parser(Int dist)
54 // This function is called during the actual parsing process
55 template <typename Iterator, typename Context
56 , typename Skipper, typename Attribute>
57 bool parse(Iterator& first, Iterator const& last
58 , Context&, Skipper&, Attribute&) const
60 // This series of checks is designed to fail parsing on negative
61 // values, without generating a "expression always evaluates true"
62 // warning on unsigned types.
63 if (dist == Int(0)) return true;
64 if (dist < Int(1)) return false;
66 typedef typename std::iterator_traits<Iterator>::iterator_category
68 return advance(first, last, iterator_category());
71 // This function is called during error handling to create
72 // a human readable string for the error context.
73 template <typename Context>
74 boost::spirit::info what(Context&) const
76 return boost::spirit::info("advance");
80 // this is the general implementation used by most iterator categories
81 template <typename Iterator, typename IteratorCategory>
82 bool advance(Iterator& first, Iterator const& last
83 , IteratorCategory) const
89 if (i == last) return false;
97 // this is a specialization for random access iterators
98 template <typename Iterator>
99 bool advance(Iterator& first, Iterator const& last
100 , std::random_access_iterator_tag) const
102 Iterator const it = first + dist;
103 if (it > last) return false;
112 ///////////////////////////////////////////////////////////////////////////////
113 // instantiation of the parser
114 namespace boost { namespace spirit { namespace qi
116 template <typename Modifiers, typename A0>
117 struct make_primitive<
118 terminal_ex<repository::qi::tag::advance, fusion::vector1<A0> >
121 typedef repository::qi::advance_parser<A0> result_type;
123 template <typename Terminal>
124 result_type operator()(Terminal const& term, unused_type) const
126 return result_type(fusion::at_c<0>(term.args));