/*============================================================================= Copyright (c) 2011-2015 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_REPLACE_HPP #define SPROUT_WEED_PARSER_DIRECTIVE_REPLACE_HPP #include #include #include #include #include #include #include #include #include #include namespace sprout { namespace weed { // // replace_p // template struct replace_p : public sprout::weed::parser_base { public: typedef T value_type; template struct attribute : public sprout::identity {}; template struct result : public sprout::identity::type> > {}; private: typedef typename sprout::weed::traits::terminal_or_expr_of::type expr_type; private: expr_type expr_; value_type value_; private: template SPROUT_CONSTEXPR typename result::type call( Iterator first, Result const& res ) const { typedef typename result::type result_type; return res.success() ? result_type(true, res.current(), value_) : result_type(false, first, value_) ; } public: SPROUT_CONSTEXPR replace_p() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL SPROUT_CONSTEXPR replace_p( Parser const& p, value_type const& value ) : expr_(sprout::weed::make_terminal_or_expr(p)) , value_(value) {} template SPROUT_CONSTEXPR typename result::type operator()( Iterator first, Iterator, Context const& ctx ) const { return call(first, sprout::weed::eval(expr_, ctx)); } }; // // replace_d // template struct replace_d { public: typedef T value_type; private: value_type value_; public: explicit SPROUT_CONSTEXPR replace_d(value_type const& value) : value_(value) {} template SPROUT_CONSTEXPR sprout::weed::replace_p operator[](Parser const& p) const { return sprout::weed::replace_p(p, value_); } }; // // replace_g // struct replace_g { public: template SPROUT_CONSTEXPR sprout::weed::replace_d operator()(T const& value) const { return sprout::weed::replace_d(value); } }; // // replace // SPROUT_CONSTEXPR sprout::weed::replace_g replace = sprout::weed::replace_g(); } // namespace weed } // namespace sprout #endif // #ifndef SPROUT_WEED_PARSER_DIRECTIVE_REPLACE_HPP