1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 Copyright (c) 2014 Thomas Bernard
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 ==============================================================================*/
9 #if !defined(SPIRIT_X3_REPEAT_APRIL_16_2014_0848AM)
10 #define SPIRIT_X3_REPEAT_APRIL_16_2014_0848AM
12 #include <boost/spirit/home/x3/core/parser.hpp>
13 #include <boost/spirit/home/x3/operator/kleene.hpp>
15 namespace boost { namespace spirit { namespace x3 { namespace detail
18 struct exact_count // handles repeat(exact)[p]
21 bool got_max(T i) const { return i >= exact_value; }
22 bool got_min(T i) const { return i >= exact_value; }
28 struct finite_count // handles repeat(min, max)[p]
31 bool got_max(T i) const { return i >= max_value; }
32 bool got_min(T i) const { return i >= min_value; }
39 struct infinite_count // handles repeat(min, inf)[p]
42 bool got_max(T /*i*/) const { return false; }
43 bool got_min(T i) const { return i >= min_value; }
49 namespace boost { namespace spirit { namespace x3
51 template<typename Subject, typename RepeatCountLimit>
52 struct repeat_directive : unary_parser<Subject, repeat_directive<Subject,RepeatCountLimit>>
54 typedef unary_parser<Subject, repeat_directive<Subject,RepeatCountLimit>> base_type;
55 static bool const is_pass_through_unary = true;
56 static bool const handles_container = true;
58 repeat_directive(Subject const& subject, RepeatCountLimit const& repeat_limit_)
60 , repeat_limit(repeat_limit_)
63 template<typename Iterator, typename Context
64 , typename RContext, typename Attribute>
66 Iterator& first, Iterator const& last
67 , Context const& context, RContext& rcontext, Attribute& attr) const
69 Iterator local_iterator = first;
70 typename RepeatCountLimit::type i{};
71 for (/**/; !repeat_limit.got_min(i); ++i)
73 if (!detail::parse_into_container(
74 this->subject, local_iterator, last, context, rcontext, attr))
78 first = local_iterator;
79 // parse some more up to the maximum specified
80 for (/**/; !repeat_limit.got_max(i); ++i)
82 if (!detail::parse_into_container(
83 this->subject, first, last, context, rcontext, attr))
89 RepeatCountLimit repeat_limit;
92 // Infinite loop tag type
94 const inf_type inf = inf_type();
98 template<typename Subject>
99 auto operator[](Subject const& subject) const
101 return *as_parser(subject);
104 template <typename T>
105 struct repeat_gen_lvl1
107 repeat_gen_lvl1(T&& repeat_limit_)
108 : repeat_limit(repeat_limit_)
111 template<typename Subject>
112 repeat_directive< typename extension::as_parser<Subject>::value_type, T>
113 operator[](Subject const& subject) const
115 return { as_parser(subject),repeat_limit };
121 template <typename T>
122 repeat_gen_lvl1<detail::exact_count<T>>
123 operator()(T const exact) const
125 return { detail::exact_count<T>{exact} };
128 template <typename T>
129 repeat_gen_lvl1<detail::finite_count<T>>
130 operator()(T const min_val, T const max_val) const
132 return { detail::finite_count<T>{min_val,max_val} };
135 template <typename T>
136 repeat_gen_lvl1<detail::infinite_count<T>>
137 operator()(T const min_val, inf_type const &) const
139 return { detail::infinite_count<T>{min_val} };
143 auto const repeat = repeat_gen{};
146 namespace boost { namespace spirit { namespace x3 { namespace traits
148 template <typename Subject, typename RepeatCountLimit, typename Context>
149 struct attribute_of<x3::repeat_directive<Subject,RepeatCountLimit>, Context>
150 : build_container<typename attribute_of<Subject, Context>::type> {};