/*============================================================================= Copyright (c) 2003 Hartmut Kaiser http://spirit.sourceforge.net/ Use, modification and distribution is subject to 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_SWITCH_IPP #define BOOST_SPIRIT_SWITCH_IPP #include #include #include #include #include #include #include #include #include #include #include #include #include /////////////////////////////////////////////////////////////////////////////// namespace boost { namespace spirit { // forward declaration template struct case_parser; /////////////////////////////////////////////////////////////////////////////// namespace impl { /////////////////////////////////////////////////////////////////////////////// // parse helper functions template inline typename parser_result::type delegate_parse(ParserT const &p, ScannerT const &scan, typename ScannerT::iterator_t const save) { typedef typename parser_result::type result_t; result_t result (p.subject().parse(scan)); if (!result) scan.first = save; return result; } /////////////////////////////////////////////////////////////////////////////// // General default case handling (no default_p case branch given). // First try to match the current parser node (if the condition value is // matched) and, if that fails, return a no_match template struct default_delegate_parse { template < typename ParserT, typename DefaultT, typename ValueT, typename ScannerT > static typename parser_result::type parse (ValueT const &value, ParserT const &p, DefaultT const &, ScannerT const &scan, typename ScannerT::iterator_t const save) { if (value == N) return delegate_parse(p, scan, save); return scan.no_match(); } }; // The current case parser node is the default parser. // Ignore the given case value and try to match the given default parser. template struct default_delegate_parse { template < typename ParserT, typename DefaultT, typename ValueT, typename ScannerT > static typename parser_result::type parse (ValueT const& /*value*/, ParserT const &, DefaultT const &d, ScannerT const &scan, typename ScannerT::iterator_t const save) { // Since there is a default_p case branch defined, the corresponding // parser shouldn't be the nothing_parser BOOST_STATIC_ASSERT((!boost::is_same::value)); return delegate_parse(d, scan, save); } }; // The current case parser node is not the default parser, but there is a // default_p branch given inside the switch_p parser. // First try to match the current parser node (if the condition value is // matched) and, if that fails, match the given default_p parser. template struct default_delegate_parse { template < typename ParserT, typename DefaultT, typename ValueT, typename ScannerT > static typename parser_result::type parse (ValueT const &value, ParserT const &p, DefaultT const &d, ScannerT const &scan, typename ScannerT::iterator_t const save) { // Since there is a default_p case branch defined, the corresponding // parser shouldn't be the nothing_parser BOOST_STATIC_ASSERT((!boost::is_same::value)); if (value == N) return delegate_parse(p, scan, save); return delegate_parse(d, scan, save); } }; /////////////////////////////////////////////////////////////////////////////// // Look through the case parser chain to test, if there is a default case // branch defined (returned by 'value'). template struct default_case; //////////////////////////////////////// template struct get_default_parser { template static ResultT get(parser const &p) { return default_case:: get(p.derived().left()); } }; template struct get_default_parser { template static ResultT get(parser const &p) { return p.derived().right(); } }; //////////////////////////////////////// template struct default_case { // The 'value' constant is true, if the current case_parser or one of its // left siblings is a default_p generated case_parser. BOOST_STATIC_CONSTANT(bool, value = (CaseT::is_default || default_case::value)); // The 'is_epsilon' constant is true, if the current case_parser or one of // its left siblings is a default_p generated parser with an attached // epsilon_p (this is generated by the plain default_p). BOOST_STATIC_CONSTANT(bool, is_epsilon = ( (CaseT::is_default && CaseT::is_epsilon) || default_case::is_epsilon )); // The computed 'type' represents the type of the default case branch // parser (if there is one) or nothing_parser (if there isn't any default // case branch). typedef typename boost::mpl::if_c< CaseT::is_default, typename CaseT::right_embed_t, typename default_case::type >::type type; // The get function returns the parser attached to the default case branch // (if there is one) or an instance of a nothing_parser (if there isn't // any default case branch). template static type get(parser const &p) { return get_default_parser::get(p); } }; //////////////////////////////////////// template struct get_default_parser_simple { template static ResultT get(parser const &p) { return p.derived(); } }; template struct get_default_parser_simple { template static nothing_parser get(parser const &) { return nothing_p; } }; //////////////////////////////////////// // Specialization of the default_case template for the last (leftmost) element // of the case parser chain. template struct default_case { // The 'value' and 'is_epsilon' constant, the 'type' type and the function // 'get' are described above. BOOST_STATIC_CONSTANT(bool, value = CaseT::is_default); BOOST_STATIC_CONSTANT(bool, is_epsilon = ( CaseT::is_default && CaseT::is_epsilon )); typedef typename boost::mpl::if_c< CaseT::is_default, CaseT, nothing_parser >::type type; template static type get(parser const &p) { return get_default_parser_simple::get(p); } }; /////////////////////////////////////////////////////////////////////////////// // The case_chain template calculates recursivly the depth of the left // subchain of the given case branch node. template struct case_chain { BOOST_STATIC_CONSTANT(int, depth = ( case_chain::depth + 1 )); }; template struct case_chain { BOOST_STATIC_CONSTANT(int, depth = 0); }; /////////////////////////////////////////////////////////////////////////////// // The chain_parser template is used to extract the type and the instance of // a left or a right parser, burried arbitrary deep inside the case parser // chain. template struct chain_parser { typedef typename CaseT::left_t our_left_t; typedef typename chain_parser::left_t left_t; typedef typename chain_parser::right_t right_t; static left_t left(CaseT const &p) { return chain_parser::left(p.left()); } static right_t right(CaseT const &p) { return chain_parser::right(p.left()); } }; template struct chain_parser<1, CaseT> { typedef typename CaseT::left_t left_t; typedef typename CaseT::right_t right_t; static left_t left(CaseT const &p) { return p.left(); } static right_t right(CaseT const &p) { return p.right(); } }; template struct chain_parser<0, CaseT>; // shouldn't be instantiated /////////////////////////////////////////////////////////////////////////////// // Type computing meta function for calculating the type of the return value // of the used conditional switch expression template struct condition_result { typedef typename TargetT::template result::type type; }; /////////////////////////////////////////////////////////////////////////////// template struct compound_case_parser : public binary > > { typedef compound_case_parser self_t; typedef binary_parser_category parser_category_t; typedef binary > base_t; BOOST_STATIC_CONSTANT(int, value = RightT::value); BOOST_STATIC_CONSTANT(bool, is_default = IsDefault); BOOST_STATIC_CONSTANT(bool, is_simple = false); BOOST_STATIC_CONSTANT(bool, is_epsilon = ( is_default && boost::is_same::value )); compound_case_parser(parser const &lhs, parser const &rhs) : base_t(lhs.derived(), rhs.derived()) {} template struct result { typedef typename match_result::type type; }; template typename parser_result::type parse(ScannerT const& scan, CondT const &cond) const; template compound_case_parser< self_t, case_parser, IsDefault1 > operator, (case_parser const &p) const { // If the following compile time assertion fires, you've probably used // more than one default_p case inside the switch_p parser construct. BOOST_STATIC_ASSERT(!default_case::value || !IsDefault1); // If this compile time assertion fires, you've probably want to use // more case_p/default_p case branches, than possible. BOOST_STATIC_ASSERT( case_chain::depth < BOOST_SPIRIT_SWITCH_CASE_LIMIT ); typedef case_parser right_t; return compound_case_parser(*this, p); } }; /////////////////////////////////////////////////////////////////////////////// // The parse_switch::do_ functions dispatch to the correct parser, which is // selected through the given conditional switch value. template struct parse_switch; /////////////////////////////////////////////////////////////////////////////// // // The following generates a couple of parse_switch template specializations // with an increasing number of handled case branches (for 1..N). // // template // struct parse_switch { // // template // static typename parser_result::type // do_(ParserT const &p, ScannerT const &scan, long cond_value, // typename ScannerT::iterator_t const &save) // { // typedef ParserT left_t0; // typedef typename left_t0::left left_t1; // ... // // switch (cond_value) { // case left_tN::value: // return delegate_parse(chain_parser< // case_chain::depth, ParserT // >::left(p), scan, save); // ... // case left_t1::value: // return delegate_parse(chain_parser< // 1, left_t1 // >::right(p.left()), scan, save); // // case left_t0::value: // default: // typedef default_case default_t; // typedef default_delegate_parse< // Value, IsDefault, default_t::value> // default_parse_t; // // return default_parse_t::parse(cond_value, p.right(), // default_t::get(p), scan, save); // } // } // }; // /////////////////////////////////////////////////////////////////////////////// #define BOOST_SPIRIT_PARSE_SWITCH_TYPEDEFS(z, N, _) \ typedef typename BOOST_PP_CAT(left_t, N)::left_t \ BOOST_PP_CAT(left_t, BOOST_PP_INC(N)); \ /**/ #define BOOST_SPIRIT_PARSE_SWITCH_CASES(z, N, _) \ case (long)(BOOST_PP_CAT(left_t, N)::value): \ return delegate_parse(chain_parser::right(p.left()), \ scan, save); \ /**/ #define BOOST_SPIRIT_PARSE_SWITCHES(z, N, _) \ template \ struct parse_switch { \ \ template \ static typename parser_result::type \ do_(ParserT const &p, ScannerT const &scan, long cond_value, \ typename ScannerT::iterator_t const &save) \ { \ typedef ParserT left_t0; \ BOOST_PP_REPEAT_FROM_TO_ ## z(0, BOOST_PP_INC(N), \ BOOST_SPIRIT_PARSE_SWITCH_TYPEDEFS, _) \ \ switch (cond_value) { \ case (long)(BOOST_PP_CAT(left_t, BOOST_PP_INC(N))::value): \ return delegate_parse( \ chain_parser< \ case_chain::depth, ParserT \ >::left(p), scan, save); \ \ BOOST_PP_REPEAT_FROM_TO_ ## z(1, BOOST_PP_INC(N), \ BOOST_SPIRIT_PARSE_SWITCH_CASES, _) \ \ case (long)(left_t0::value): \ default: \ typedef default_case default_t; \ typedef \ default_delegate_parse \ default_parse_t; \ \ return default_parse_t::parse(cond_value, p.right(), \ default_t::get(p), scan, save); \ } \ } \ }; \ /**/ BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_SPIRIT_SWITCH_CASE_LIMIT), BOOST_SPIRIT_PARSE_SWITCHES, _) #undef BOOST_SPIRIT_PARSE_SWITCH_TYPEDEFS #undef BOOST_SPIRIT_PARSE_SWITCH_CASES #undef BOOST_SPIRIT_PARSE_SWITCHES /////////////////////////////////////////////////////////////////////////////// template template inline typename parser_result< compound_case_parser, ScannerT >::type compound_case_parser:: parse(ScannerT const& scan, CondT const &cond) const { scan.at_end(); // allow skipper to take effect return parse_switch::depth, is_default>:: do_(*this, scan, cond(scan), scan.first); } /////////////////////////////////////////////////////////////////////////////// // The switch condition is to be evaluated from a parser result value. template struct cond_functor { typedef cond_functor self_t; cond_functor(ParserT const &p_) : p(p_) {} template struct result { typedef typename parser_result::type::attr_t type; }; template typename condition_result::type operator()(ScannerT const &scan) const { typedef typename parser_result::type result_t; typedef typename result_t::attr_t attr_t; result_t result(p.parse(scan)); return !result ? attr_t() : result.value(); } typename ParserT::embed_t p; }; template struct make_cond_functor { typedef as_parser as_parser_t; static cond_functor do_(ParserT const &cond) { return cond_functor( as_parser_t::convert(cond)); } }; /////////////////////////////////////////////////////////////////////////////// // The switch condition is to be evaluated from a phoenix actor template struct cond_actor { typedef cond_actor self_t; cond_actor(ActorT const &actor_) : actor(actor_) {} template struct result { typedef typename phoenix::actor_result >::type type; }; template typename condition_result::type operator()(ScannerT const& /*scan*/) const { return actor(); } ActorT const &actor; }; template struct make_cond_functor > { static cond_actor > do_(phoenix::actor const &actor) { return cond_actor >(actor); } }; /////////////////////////////////////////////////////////////////////////////// // The switch condition is to be taken directly from the input stream struct get_next_token_cond { typedef get_next_token_cond self_t; template struct result { typedef typename ScannerT::value_t type; }; template typename condition_result::type operator()(ScannerT const &scan) const { typename ScannerT::value_t val(*scan); ++scan.first; return val; } }; template <> struct make_cond_functor { static get_next_token_cond do_(get_next_token_cond const &cond) { return cond; } }; /////////////////////////////////////////////////////////////////////////////// } // namespace impl }} // namespace boost::spirit #endif // BOOST_SPIRIT_SWITCH_IPP