From e731c3caf48e9aa51f9b48dc8dfe0216dd63121a Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Tue, 10 Jan 2012 23:57:46 +0900 Subject: [PATCH] =?UTF-8?q?sprout/weed/parser/directive/as=5Farray.hpp=20?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=20sprout/weed/parser/directive/as=5Ftuple.hp?= =?UTF-8?q?p=20=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sprout/utility/pack.hpp | 18 ++++- sprout/weed/parser/directive.hpp | 2 + sprout/weed/parser/directive/as_array.hpp | 91 +++++++++++++++++++++++ sprout/weed/parser/directive/as_tuple.hpp | 91 +++++++++++++++++++++++ 4 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 sprout/weed/parser/directive/as_array.hpp create mode 100644 sprout/weed/parser/directive/as_tuple.hpp diff --git a/sprout/utility/pack.hpp b/sprout/utility/pack.hpp index 4c8e71c4..7aa7e85b 100644 --- a/sprout/utility/pack.hpp +++ b/sprout/utility/pack.hpp @@ -13,15 +13,29 @@ namespace sprout { // namespace detail { template - struct tppack_at_impl { + struct tppack_at_impl_1 { public: typedef typename sprout::detail::tppack_at_impl::type type; }; template - struct tppack_at_impl<0, Head, Tail...> { + struct tppack_at_impl_1<0, Head, Tail...> { public: typedef Head type; }; + template< + std::size_t N, + typename... Args, + typename std::enable_if<(N < sizeof...(Args))>::type*& = sprout::enabler + > + struct tppack_at_impl + : public sprout::detail::tppack_at_impl_1 + {}; + template< + std::size_t N, + typename... Args, + typename std::enable_if<(N >= sizeof...(Args))>::type*& = sprout::enabler + > + struct tppack_at_impl {}; } // namespace detail template struct tppack_at diff --git a/sprout/weed/parser/directive.hpp b/sprout/weed/parser/directive.hpp index f547631e..127caa8e 100644 --- a/sprout/weed/parser/directive.hpp +++ b/sprout/weed/parser/directive.hpp @@ -4,5 +4,7 @@ #include #include #include +#include +#include #endif // #ifndef SPROUT_WEED_PARSER_DIRECTIVE_HPP diff --git a/sprout/weed/parser/directive/as_array.hpp b/sprout/weed/parser/directive/as_array.hpp new file mode 100644 index 00000000..7294f37a --- /dev/null +++ b/sprout/weed/parser/directive/as_array.hpp @@ -0,0 +1,91 @@ +#ifndef SPROUT_WEED_PARSER_DIRECTIVE_AS_ARRAY_HPP +#define SPROUT_WEED_PARSER_DIRECTIVE_AS_ARRAY_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace weed { + // + // as_array_p + // + template + struct as_array_p + : public sprout::weed::parser_base + { + public: + template + struct attribute { + private: + typedef typename sprout::weed::traits::attribute_of::type attr_type; + private: + static_assert(!sprout::weed::traits::is_unused::value, "invalid attribute : unused"); + public: + typedef sprout::array type; + }; + template + struct result { + public: + typedef sprout::weed::parser_result::type> type; + }; + private: + typedef typename sprout::weed::traits::terminal_or_expr_of::type expr_type; + private: + expr_type expr_; + private: + template + SPROUT_CONSTEXPR typename result::type call( + Iterator first, + Result const& res + ) const + { + typedef typename result::type result_type; + typedef typename attribute::type attribute_type; + return res.success() + ? result_type(true, res.current(), attribute_type{res.attr()}) + : result_type(false, first, attribute_type()) + ; + } + public: + as_array_p() = default; + SPROUT_CONSTEXPR explicit as_array_p( + Parser const& p + ) + : expr_(sprout::weed::make_terminal_or_expr(p)) + {} + template + SPROUT_CONSTEXPR typename result::type operator()( + Iterator first, + Iterator last, + Context const& ctx + ) const + { + return call(first, sprout::weed::eval(expr_, ctx)); + } + }; + // + // as_array_d + // + struct as_array_d { + public: + template + SPROUT_CONSTEXPR sprout::weed::as_array_p operator[](Parser const& p) const { + return sprout::weed::as_array_p(p); + } + }; + // + // as_array + // + SPROUT_CONSTEXPR sprout::weed::as_array_d as_array = sprout::weed::as_array_d(); + } // namespace weed +} // namespace sprout + +#endif // #ifndef SPROUT_WEED_PARSER_DIRECTIVE_AS_ARRAY_HPP diff --git a/sprout/weed/parser/directive/as_tuple.hpp b/sprout/weed/parser/directive/as_tuple.hpp new file mode 100644 index 00000000..9e82362b --- /dev/null +++ b/sprout/weed/parser/directive/as_tuple.hpp @@ -0,0 +1,91 @@ +#ifndef SPROUT_WEED_PARSER_DIRECTIVE_AS_TUPLE_HPP +#define SPROUT_WEED_PARSER_DIRECTIVE_AS_TUPLE_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace weed { + // + // as_tuple_p + // + template + struct as_tuple_p + : public sprout::weed::parser_base + { + public: + template + struct attribute { + private: + typedef typename sprout::weed::traits::attribute_of::type attr_type; + private: + static_assert(!sprout::weed::traits::is_unused::value, "invalid attribute : unused"); + public: + typedef sprout::tuples::tuple type; + }; + template + struct result { + public: + typedef sprout::weed::parser_result::type> type; + }; + private: + typedef typename sprout::weed::traits::terminal_or_expr_of::type expr_type; + private: + expr_type expr_; + private: + template + SPROUT_CONSTEXPR typename result::type call( + Iterator first, + Result const& res + ) const + { + typedef typename result::type result_type; + typedef typename attribute::type attribute_type; + return res.success() + ? result_type(true, res.current(), attribute_type(res.attr())) + : result_type(false, first, attribute_type()) + ; + } + public: + as_tuple_p() = default; + SPROUT_CONSTEXPR explicit as_tuple_p( + Parser const& p + ) + : expr_(sprout::weed::make_terminal_or_expr(p)) + {} + template + SPROUT_CONSTEXPR typename result::type operator()( + Iterator first, + Iterator last, + Context const& ctx + ) const + { + return call(first, sprout::weed::eval(expr_, ctx)); + } + }; + // + // as_tuple_d + // + struct as_tuple_d { + public: + template + SPROUT_CONSTEXPR sprout::weed::as_tuple_p operator[](Parser const& p) const { + return sprout::weed::as_tuple_p(p); + } + }; + // + // as_tuple + // + SPROUT_CONSTEXPR sprout::weed::as_tuple_d as_tuple = sprout::weed::as_tuple_d(); + } // namespace weed +} // namespace sprout + +#endif // #ifndef SPROUT_WEED_PARSER_DIRECTIVE_AS_TUPLE_HPP