123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- // Copyright (c) 2001-2011 Hartmut Kaiser
- // Copyright (c) 2001-2011 Joel de Guzman
- //
- // 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)
- #ifndef BOOST_SPIRIT_KARMA_DIRECTIVE_REPEAT_HPP
- #define BOOST_SPIRIT_KARMA_DIRECTIVE_REPEAT_HPP
- #if defined(_MSC_VER)
- #pragma once
- #endif
- #include <boost/spirit/home/karma/meta_compiler.hpp>
- #include <boost/spirit/home/karma/detail/output_iterator.hpp>
- #include <boost/spirit/home/karma/detail/get_stricttag.hpp>
- #include <boost/spirit/home/karma/generator.hpp>
- #include <boost/spirit/home/karma/auxiliary/lazy.hpp>
- #include <boost/spirit/home/karma/operator/kleene.hpp>
- #include <boost/spirit/home/support/container.hpp>
- #include <boost/spirit/home/support/common_terminals.hpp>
- #include <boost/spirit/home/support/has_semantic_action.hpp>
- #include <boost/spirit/home/support/handles_container.hpp>
- #include <boost/spirit/home/karma/detail/attributes.hpp>
- #include <boost/spirit/home/support/info.hpp>
- #include <boost/fusion/include/at.hpp>
- namespace boost { namespace spirit
- {
- ///////////////////////////////////////////////////////////////////////////
- // Enablers
- ///////////////////////////////////////////////////////////////////////////
- template <>
- struct use_directive<karma::domain, tag::repeat> // enables repeat[p]
- : mpl::true_ {};
- template <typename T>
- struct use_directive<karma::domain
- , terminal_ex<tag::repeat // enables repeat(exact)[p]
- , fusion::vector1<T> >
- > : mpl::true_ {};
- template <typename T>
- struct use_directive<karma::domain
- , terminal_ex<tag::repeat // enables repeat(min, max)[p]
- , fusion::vector2<T, T> >
- > : mpl::true_ {};
- template <typename T>
- struct use_directive<karma::domain
- , terminal_ex<tag::repeat // enables repeat(min, inf)[p]
- , fusion::vector2<T, inf_type> >
- > : mpl::true_ {};
- template <> // enables *lazy* repeat(exact)[p]
- struct use_lazy_directive<
- karma::domain
- , tag::repeat
- , 1 // arity
- > : mpl::true_ {};
- template <> // enables *lazy* repeat(min, max)[p]
- struct use_lazy_directive< // and repeat(min, inf)[p]
- karma::domain
- , tag::repeat
- , 2 // arity
- > : mpl::true_ {};
- }}
- namespace boost { namespace spirit { namespace karma
- {
- #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
- using spirit::repeat;
- using spirit::inf;
- #endif
- using spirit::repeat_type;
- using spirit::inf_type;
- ///////////////////////////////////////////////////////////////////////////
- // handles repeat(exact)[p]
- template <typename T>
- struct exact_iterator
- {
- exact_iterator(T const exact)
- : exact(exact) {}
- typedef T type;
- T start() const { return 0; }
- bool got_max(T i) const { return i >= exact; }
- bool got_min(T i) const { return i >= exact; }
- T const exact;
- // silence MSVC warning C4512: assignment operator could not be generated
- BOOST_DELETED_FUNCTION(exact_iterator& operator= (exact_iterator const&))
- };
- // handles repeat(min, max)[p]
- template <typename T>
- struct finite_iterator
- {
- finite_iterator(T const min, T const max)
- : min BOOST_PREVENT_MACRO_SUBSTITUTION (min)
- , max BOOST_PREVENT_MACRO_SUBSTITUTION (max) {}
- typedef T type;
- T start() const { return 0; }
- bool got_max(T i) const { return i >= max; }
- bool got_min(T i) const { return i >= min; }
- T const min;
- T const max;
- // silence MSVC warning C4512: assignment operator could not be generated
- BOOST_DELETED_FUNCTION(finite_iterator& operator= (finite_iterator const&))
- };
- // handles repeat(min, inf)[p]
- template <typename T>
- struct infinite_iterator
- {
- infinite_iterator(T const min)
- : min BOOST_PREVENT_MACRO_SUBSTITUTION (min) {}
- typedef T type;
- T start() const { return 0; }
- bool got_max(T /*i*/) const { return false; }
- bool got_min(T i) const { return i >= min; }
- T const min;
- // silence MSVC warning C4512: assignment operator could not be generated
- BOOST_DELETED_FUNCTION(infinite_iterator& operator= (infinite_iterator const&))
- };
- ///////////////////////////////////////////////////////////////////////////
- template <typename Subject, typename LoopIter, typename Strict
- , typename Derived>
- struct base_repeat_generator : unary_generator<Derived>
- {
- private:
- // iterate over the given container until its exhausted or the embedded
- // generator succeeds
- template <typename F, typename Attribute>
- bool generate_subject(F f, Attribute const&, mpl::false_) const
- {
- // Failing subject generators are just skipped. This allows to
- // selectively generate items in the provided attribute.
- while (!f.is_at_end())
- {
- bool r = !f(subject);
- if (r)
- return true;
- if (!f.is_at_end())
- f.next();
- }
- return false;
- }
- template <typename F, typename Attribute>
- bool generate_subject(F f, Attribute const&, mpl::true_) const
- {
- return !f(subject);
- }
- // There is no way to distinguish a failed generator from a
- // generator to be skipped. We assume the user takes responsibility
- // for ending the loop if no attribute is specified.
- template <typename F>
- bool generate_subject(F f, unused_type, mpl::false_) const
- {
- return !f(subject);
- }
- public:
- typedef Subject subject_type;
- typedef mpl::int_<subject_type::properties::value> properties;
- // Build a std::vector from the subject's attribute. Note
- // that build_std_vector may return unused_type if the
- // subject's attribute is an unused_type.
- template <typename Context, typename Iterator>
- struct attribute
- : traits::build_std_vector<
- typename traits::attribute_of<Subject, Context, Iterator>::type
- >
- {};
- base_repeat_generator(Subject const& subject, LoopIter const& iter)
- : subject(subject), iter(iter) {}
- template <typename OutputIterator, typename Context, typename Delimiter
- , typename Attribute>
- bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
- , Attribute const& attr) const
- {
- typedef detail::fail_function<
- OutputIterator, Context, Delimiter
- > fail_function;
- typedef typename traits::container_iterator<
- typename add_const<Attribute>::type
- >::type iterator_type;
- typedef
- typename traits::make_indirect_iterator<iterator_type>::type
- indirect_iterator_type;
- typedef detail::pass_container<
- fail_function, Attribute, indirect_iterator_type, mpl::false_>
- pass_container;
- iterator_type it = traits::begin(attr);
- iterator_type end = traits::end(attr);
- pass_container pass(fail_function(sink, ctx, d),
- indirect_iterator_type(it), indirect_iterator_type(end));
- // generate the minimal required amount of output
- typename LoopIter::type i = iter.start();
- for (/**/; !pass.is_at_end() && !iter.got_min(i); ++i)
- {
- if (!generate_subject(pass, attr, Strict()))
- {
- // if we fail before reaching the minimum iteration
- // required, do not output anything and return false
- return false;
- }
- }
- if (pass.is_at_end() && !iter.got_min(i))
- return false; // insufficient attribute elements
- // generate some more up to the maximum specified
- for (/**/; !pass.is_at_end() && !iter.got_max(i); ++i)
- {
- if (!generate_subject(pass, attr, Strict()))
- break;
- }
- return detail::sink_is_good(sink);
- }
- template <typename Context>
- info what(Context& context) const
- {
- return info("repeat", subject.what(context));
- }
- Subject subject;
- LoopIter iter;
- };
- template <typename Subject, typename LoopIter>
- struct repeat_generator
- : base_repeat_generator<
- Subject, LoopIter, mpl::false_
- , repeat_generator<Subject, LoopIter> >
- {
- typedef base_repeat_generator<
- Subject, LoopIter, mpl::false_, repeat_generator
- > base_repeat_generator_;
- repeat_generator(Subject const& subject, LoopIter const& iter)
- : base_repeat_generator_(subject, iter) {}
- };
- template <typename Subject, typename LoopIter>
- struct strict_repeat_generator
- : base_repeat_generator<
- Subject, LoopIter, mpl::true_
- , strict_repeat_generator<Subject, LoopIter> >
- {
- typedef base_repeat_generator<
- Subject, LoopIter, mpl::true_, strict_repeat_generator
- > base_repeat_generator_;
- strict_repeat_generator(Subject const& subject, LoopIter const& iter)
- : base_repeat_generator_(subject, iter) {}
- };
- ///////////////////////////////////////////////////////////////////////////
- // Generator generators: make_xxx function (objects)
- ///////////////////////////////////////////////////////////////////////////
- template <typename Subject, typename Modifiers>
- struct make_directive<tag::repeat, Subject, Modifiers>
- {
- typedef typename mpl::if_<
- detail::get_stricttag<Modifiers>
- , strict_kleene<Subject>, kleene<Subject>
- >::type result_type;
- result_type operator()(unused_type, Subject const& subject
- , unused_type) const
- {
- return result_type(subject);
- }
- };
- template <typename T, typename Subject, typename Modifiers>
- struct make_directive<
- terminal_ex<tag::repeat, fusion::vector1<T> >, Subject, Modifiers>
- {
- typedef exact_iterator<T> iterator_type;
- typedef typename mpl::if_<
- detail::get_stricttag<Modifiers>
- , strict_repeat_generator<Subject, iterator_type>
- , repeat_generator<Subject, iterator_type>
- >::type result_type;
- template <typename Terminal>
- result_type operator()(
- Terminal const& term, Subject const& subject, unused_type) const
- {
- return result_type(subject, fusion::at_c<0>(term.args));
- }
- };
- template <typename T, typename Subject, typename Modifiers>
- struct make_directive<
- terminal_ex<tag::repeat, fusion::vector2<T, T> >, Subject, Modifiers>
- {
- typedef finite_iterator<T> iterator_type;
- typedef typename mpl::if_<
- detail::get_stricttag<Modifiers>
- , strict_repeat_generator<Subject, iterator_type>
- , repeat_generator<Subject, iterator_type>
- >::type result_type;
- template <typename Terminal>
- result_type operator()(
- Terminal const& term, Subject const& subject, unused_type) const
- {
- return result_type(subject,
- iterator_type(
- fusion::at_c<0>(term.args)
- , fusion::at_c<1>(term.args)
- )
- );
- }
- };
- template <typename T, typename Subject, typename Modifiers>
- struct make_directive<
- terminal_ex<tag::repeat
- , fusion::vector2<T, inf_type> >, Subject, Modifiers>
- {
- typedef infinite_iterator<T> iterator_type;
- typedef typename mpl::if_<
- detail::get_stricttag<Modifiers>
- , strict_repeat_generator<Subject, iterator_type>
- , repeat_generator<Subject, iterator_type>
- >::type result_type;
- template <typename Terminal>
- result_type operator()(
- Terminal const& term, Subject const& subject, unused_type) const
- {
- return result_type(subject, fusion::at_c<0>(term.args));
- }
- };
- }}}
- namespace boost { namespace spirit { namespace traits
- {
- ///////////////////////////////////////////////////////////////////////////
- template <typename Subject, typename LoopIter>
- struct has_semantic_action<karma::repeat_generator<Subject, LoopIter> >
- : unary_has_semantic_action<Subject> {};
- template <typename Subject, typename LoopIter>
- struct has_semantic_action<karma::strict_repeat_generator<Subject, LoopIter> >
- : unary_has_semantic_action<Subject> {};
- ///////////////////////////////////////////////////////////////////////////
- template <typename Subject, typename LoopIter, typename Attribute
- , typename Context, typename Iterator>
- struct handles_container<
- karma::repeat_generator<Subject, LoopIter>, Attribute
- , Context, Iterator>
- : mpl::true_ {};
- template <typename Subject, typename LoopIter, typename Attribute
- , typename Context, typename Iterator>
- struct handles_container<
- karma::strict_repeat_generator<Subject, LoopIter>, Attribute
- , Context, Iterator>
- : mpl::true_ {};
- }}}
- #endif
|