/*============================================================================= Copyright (c) 2002-2003 Joel de Guzman Copyright (c) 2002-2003 Hartmut Kaiser http://spirit.sourceforge.net/ 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) =============================================================================*/ #if !defined(BOOST_SPIRIT_SUBRULE_HPP) #define BOOST_SPIRIT_SUBRULE_HPP #include #include #include #include #include #include #include namespace boost { namespace spirit { BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN /////////////////////////////////////////////////////////////////////////// // // subrules_scanner class // /////////////////////////////////////////////////////////////////////////// template struct subrules_scanner : public ScannerT { typedef ScannerT scanner_t; typedef ListT list_t; typedef subrules_scanner self_t; subrules_scanner(ScannerT const& scan, ListT const& list_) : ScannerT(scan), list(list_) {} template struct rebind_policies { typedef typename rebind_scanner_policies::type rebind_scanner; typedef subrules_scanner type; }; template subrules_scanner< typename rebind_scanner_policies::type, ListT> change_policies(PoliciesT const& policies) const { typedef subrules_scanner< BOOST_DEDUCED_TYPENAME rebind_scanner_policies::type, ListT> subrules_scanner_t; return subrules_scanner_t( ScannerT::change_policies(policies), list); } template struct rebind_iterator { typedef typename rebind_scanner_iterator::type rebind_scanner; typedef subrules_scanner type; }; template subrules_scanner< typename rebind_scanner_iterator::type, ListT> change_iterator(IteratorT const& first, IteratorT const &last) const { typedef subrules_scanner< BOOST_DEDUCED_TYPENAME rebind_scanner_iterator::type, ListT> subrules_scanner_t; return subrules_scanner_t( ScannerT::change_iterator(first, last), list); } ListT const& list; }; /////////////////////////////////////////////////////////////////////////// // // subrule_scanner type computer class // // This computer ensures that the scanner will not be recursively // instantiated if it's not needed. // /////////////////////////////////////////////////////////////////////////// template struct subrules_scanner_finder { typedef subrules_scanner type; }; template struct subrules_scanner_finder, ListT> { typedef subrules_scanner type; }; /////////////////////////////////////////////////////////////////////////// // // subrule_list class // /////////////////////////////////////////////////////////////////////////// template struct subrule_list : public parser > { typedef subrule_list self_t; typedef FirstT first_t; typedef RestT rest_t; subrule_list(FirstT const& first_, RestT const& rest_) : first(first_), rest(rest_) {} template struct result { typedef typename parser_result::type type; }; template typename parser_result::type parse(ScannerT const& scan) const { typedef typename subrules_scanner_finder::type subrules_scanner_t; subrules_scanner_t g_arg(scan, *this); return first.start.parse(g_arg); } template subrule_list< FirstT, subrule_list< subrule_parser, RestT> > operator,(subrule_parser const& rhs_) { return subrule_list< FirstT, subrule_list< subrule_parser, RestT> >( first, subrule_list< subrule_parser, RestT>(rhs_, rest)); } FirstT first; RestT rest; }; /////////////////////////////////////////////////////////////////////////// // // subrule_parser class // /////////////////////////////////////////////////////////////////////////// template struct subrule_parser : public parser > { typedef subrule_parser self_t; typedef subrule subrule_t; typedef DefT def_t; BOOST_STATIC_CONSTANT(int, id = ID); template struct result { typedef typename impl::get_subrule_parser_result< DefT, ScannerT, typename subrule_t::attr_t>::type type; }; subrule_parser(subrule_t const& start_, DefT const& rhs_) : rhs(rhs_), start(start_) {} template typename parser_result::type parse(ScannerT const& scan) const { // This will only be called when parsing single subrules. typedef subrule_list list_t; typedef subrules_scanner scanner_t; list_t list(*this, nil_t()); scanner_t g_arg(scan, list); return start.parse(g_arg); } template inline subrule_list< self_t, subrule_list< subrule_parser, nil_t> > operator,(subrule_parser const& rhs) const { return subrule_list< self_t, subrule_list< subrule_parser, nil_t> >( *this, subrule_list< subrule_parser, nil_t>( rhs, nil_t())); } typename DefT::embed_t rhs; subrule_t const& start; }; /////////////////////////////////////////////////////////////////////////// // // subrule class // /////////////////////////////////////////////////////////////////////////// template struct subrule : public parser > , public ContextT::base_t , public context_aux > { typedef subrule self_t; typedef subrule const& embed_t; typedef typename ContextT::context_linker_t context_t; typedef typename context_t::attr_t attr_t; BOOST_STATIC_CONSTANT(int, id = ID); template struct result { typedef typename impl::get_subrule_result::type type; }; template typename parser_result::type parse_main(ScannerT const& scan) const { typedef typename parser_result::type result_t; result_t result_; impl::parse_subrule:: do_(result_, scan); return result_; } template typename parser_result::type parse(ScannerT const& scan) const { typedef typename parser_result::type result_t; typedef parser_scanner_linker scanner_t; BOOST_SPIRIT_CONTEXT_PARSE( scan, *this, scanner_t, context_t, result_t); } template subrule_parser operator=(parser const& rhs) const { return subrule_parser(*this, rhs.derived()); } private: // assignment of subrules is not allowed. Use subrules // with identical IDs if you want to have aliases. subrule& operator=(subrule const&); template subrule& operator=(subrule const&); }; BOOST_SPIRIT_CLASSIC_NAMESPACE_END }} // namespace BOOST_SPIRIT_CLASSIC_NS #endif