/*============================================================================= Copyright (c) 2011-2013 Bolero MURAKAMI https://github.com/bolero-MURAKAMI/Sprout 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 SPROUT_WEED_PARSER_DIRECTIVE_REPEAT_HPP #define SPROUT_WEED_PARSER_DIRECTIVE_REPEAT_HPP #include #include #include #include #include #include #include #include #include #include #include #include namespace sprout { namespace weed { // // repeat_p // template struct repeat_p : public sprout::weed::parser_base { public: template struct attribute; template struct result; private: template struct eval { private: typedef typename sprout::weed::traits::limit_of::type limit; typedef typename sprout::weed::traits::attribute_of::type attr_type; public: typedef typename repeat_p::template attribute::type attribute_type; typedef typename repeat_p::template result::type result_type; private: template SPROUT_CONSTEXPR typename std::enable_if< sizeof...(Attrs) + 1 == limit::value, result_type >::type call( repeat_p const& p, Iterator first, Result const& res, Attrs const&... attrs ) const { return res.success() ? result_type(true, res.current(), sprout::weed::attr_cnv::times(attrs..., res.attr())) : p.count_ <= sizeof...(Attrs) ? result_type(true, first, sprout::weed::attr_cnv::times(attrs...)) : result_type(false, first, attribute_type()) ; } template SPROUT_CONSTEXPR typename std::enable_if< sizeof...(Attrs) + 1 != limit::value, result_type >::type call( repeat_p const& p, Iterator first, Result const& res, Attrs const&... attrs ) const { return res.success() ? call(p, p.count_ <= sizeof...(Attrs) ? res.current() : first, sprout::weed::eval(p.expr_, res.ctx()), attrs..., res.attr()) : p.count_ <= sizeof...(Attrs) ? result_type(true, first, sprout::weed::attr_cnv::times(attrs...)) : result_type(false, first, attribute_type()) ; } template SPROUT_CONSTEXPR result_type call_inf( repeat_p const& p, Iterator first, Result const& res, std::size_t n ) const { return res.success() ? call_inf(p, p.count_ <= n ? res.current() : first, sprout::weed::eval(p.expr_, res.ctx()), n + 1) : result_type(p.count_ <= n || p.count_ == std::size_t(-1), first, attribute_type()) ; } template SPROUT_CONSTEXPR typename std::enable_if< Infinity, result_type >::type call( repeat_p const& p, Iterator first, Result const& res ) const { return res.success() ? call_inf( p, p.count_ <= 0 ? res.current() : first, sprout::weed::eval(p.expr_, res.ctx()), 1 ) : result_type(p.count_ <= 0 || p.count_ == std::size_t(-1), first, attribute_type()) ; } template SPROUT_CONSTEXPR typename std::enable_if< !Infinity, result_type >::type call( repeat_p const& p, Iterator first, Result const& res ) const { return res.success() ? call( p, p.count_ <= 0 ? res.current() : first, sprout::weed::eval(p.expr_, res.ctx()), res.attr() ) : result_type(p.count_ <= 0, first, attribute_type()) ; } public: SPROUT_CONSTEXPR result_type operator()( repeat_p const& p, Iterator first, Context const& ctx ) const { return call( p, first, sprout::weed::eval(p.expr_, ctx) ); } }; public: template struct attribute : public sprout::weed::attr_cnv::result_of::times< sprout::weed::traits::limit_of::value, typename sprout::weed::traits::attribute_of::type > {}; template struct result : public sprout::identity::type> > {}; private: typedef typename sprout::weed::traits::terminal_or_expr_of::type expr_type; private: expr_type expr_; std::size_t count_; public: repeat_p() = default; explicit SPROUT_CONSTEXPR repeat_p(Parser const& p, std::size_t count = -1) : expr_(sprout::weed::make_terminal_or_expr(p)) , count_(count) {} template SPROUT_CONSTEXPR typename result::type operator()( Iterator first, Iterator, Context const& ctx ) const { return eval()(*this, first, ctx); } }; // // repeat_g // struct repeat_g { private: std::size_t count_; public: explicit SPROUT_CONSTEXPR repeat_g(std::size_t count) : count_(count) {} template SPROUT_CONSTEXPR sprout::weed::repeat_p operator[](Parser const& p) const { return sprout::weed::repeat_p(p, count_); } }; // // repeat_d // struct repeat_d { public: template SPROUT_CONSTEXPR sprout::weed::repeat_p operator[](Parser const& p) const { return sprout::weed::repeat_p(p); } SPROUT_CONSTEXPR sprout::weed::repeat_g operator()(std::size_t count) const { return sprout::weed::repeat_g(count); } }; // // repeat // SPROUT_CONSTEXPR sprout::weed::repeat_d repeat = sprout::weed::repeat_d(); } // namespace weed } // namespace sprout #endif // #ifndef SPROUT_WEED_PARSER_DIRECTIVE_REPEAT_HPP