Chris@16: // Copyright (c) 2001-2011 Joel de Guzman Chris@16: // Copyright (c) 2001-2011 Hartmut Kaiser Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #if !defined(BOOST_SPIRIT_KARMA_RULE_MAR_05_2007_0455PM) Chris@16: #define BOOST_SPIRIT_KARMA_RULE_MAR_05_2007_0455PM Chris@16: Chris@16: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: # pragma warning(push) Chris@16: # pragma warning(disable: 4355) // 'this' : used in base member initializer list warning Chris@16: #endif Chris@16: Chris@16: namespace boost { namespace spirit { namespace karma Chris@16: { Chris@16: BOOST_PP_REPEAT(SPIRIT_ATTRIBUTES_LIMIT, SPIRIT_USING_ATTRIBUTE, _) Chris@16: Chris@16: using spirit::_pass_type; Chris@16: using spirit::_val_type; Chris@16: using spirit::_a_type; Chris@16: using spirit::_b_type; Chris@16: using spirit::_c_type; Chris@16: using spirit::_d_type; Chris@16: using spirit::_e_type; Chris@16: using spirit::_f_type; Chris@16: using spirit::_g_type; Chris@16: using spirit::_h_type; Chris@16: using spirit::_i_type; Chris@16: using spirit::_j_type; Chris@16: Chris@16: #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS Chris@16: Chris@16: using spirit::_pass; Chris@16: using spirit::_val; Chris@16: using spirit::_a; Chris@16: using spirit::_b; Chris@16: using spirit::_c; Chris@16: using spirit::_d; Chris@16: using spirit::_e; Chris@16: using spirit::_f; Chris@16: using spirit::_g; Chris@16: using spirit::_h; Chris@16: using spirit::_i; Chris@16: using spirit::_j; Chris@16: Chris@16: #endif Chris@16: Chris@16: using spirit::info; Chris@16: using spirit::locals; Chris@16: Chris@16: template < Chris@16: typename OutputIterator, typename T1, typename T2, typename T3 Chris@16: , typename T4> Chris@16: struct rule Chris@16: : proto::extends< Chris@16: typename proto::terminal< Chris@16: reference const> Chris@16: >::type Chris@16: , rule Chris@16: > Chris@16: , generator > Chris@16: { Chris@16: typedef mpl::int_ properties; Chris@16: Chris@16: typedef OutputIterator iterator_type; Chris@16: typedef rule this_type; Chris@16: typedef reference reference_; Chris@16: typedef typename proto::terminal::type terminal; Chris@16: typedef proto::extends base_type; Chris@16: typedef mpl::vector template_params; Chris@16: Chris@16: // the output iterator is always wrapped by karma Chris@16: typedef detail::output_iterator Chris@16: output_iterator; Chris@16: Chris@16: // locals_type is a sequence of types to be used as local variables Chris@16: typedef typename Chris@16: spirit::detail::extract_locals::type Chris@16: locals_type; Chris@16: Chris@16: // The delimiter-generator type Chris@16: typedef typename Chris@16: spirit::detail::extract_component< Chris@16: karma::domain, template_params>::type Chris@16: delimiter_type; Chris@16: Chris@16: // The rule's signature Chris@16: typedef typename Chris@16: spirit::detail::extract_sig::type Chris@16: sig_type; Chris@16: Chris@16: // The rule's encoding type Chris@16: typedef typename Chris@16: spirit::detail::extract_encoding::type Chris@16: encoding_type; Chris@16: Chris@16: // This is the rule's attribute type Chris@16: typedef typename Chris@16: spirit::detail::attr_from_sig::type Chris@16: attr_type; Chris@16: typedef typename add_reference< Chris@16: typename add_const::type>::type Chris@16: attr_reference_type; Chris@16: Chris@16: // parameter_types is a sequence of types passed as parameters to the rule Chris@16: typedef typename Chris@16: spirit::detail::params_from_sig::type Chris@16: parameter_types; Chris@16: Chris@16: static size_t const params_size = Chris@16: fusion::result_of::size::type::value; Chris@16: Chris@16: // the context passed to the right hand side of a rule contains Chris@16: // the attribute and the parameters for this particular rule invocation Chris@16: typedef context< Chris@16: fusion::cons Chris@16: , locals_type> Chris@16: context_type; Chris@16: Chris@16: typedef function< Chris@16: bool(output_iterator&, context_type&, delimiter_type const&)> Chris@16: function_type; Chris@16: Chris@16: typedef typename Chris@16: mpl::if_< Chris@16: is_same Chris@16: , unused_type Chris@16: , tag::char_code Chris@16: >::type Chris@16: encoding_modifier_type; Chris@16: Chris@16: explicit rule(std::string const& name_ = "unnamed-rule") Chris@16: : base_type(terminal::make(reference_(*this))) Chris@16: , name_(name_) Chris@16: { Chris@16: } Chris@16: Chris@16: rule(rule const& rhs) Chris@16: : base_type(terminal::make(reference_(*this))) Chris@16: , name_(rhs.name_) Chris@16: , f(rhs.f) Chris@16: { Chris@16: } Chris@16: Chris@16: template Chris@16: static void define(rule& lhs, Expr const& expr, mpl::false_) Chris@16: { Chris@16: // Report invalid expression error as early as possible. Chris@16: // If you got an error_invalid_expression error message here, Chris@16: // then the expression (expr) is not a valid spirit karma expression. Chris@16: BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr); Chris@16: } Chris@16: Chris@16: template Chris@16: static void define(rule& lhs, Expr const& expr, mpl::true_) Chris@16: { Chris@16: lhs.f = detail::bind_generator( Chris@16: compile(expr, encoding_modifier_type())); Chris@16: } Chris@16: Chris@16: template Chris@16: rule (Expr const& expr, std::string const& name_ = "unnamed-rule") Chris@16: : base_type(terminal::make(reference_(*this))) Chris@16: , name_(name_) Chris@16: { Chris@16: define(*this, expr, traits::matches()); Chris@16: } Chris@16: Chris@16: rule& operator=(rule const& rhs) Chris@16: { Chris@16: // The following assertion fires when you try to initialize a rule Chris@16: // from an uninitialized one. Did you mean to refer to the right Chris@16: // hand side rule instead of assigning from it? In this case you Chris@16: // should write lhs = rhs.alias(); Chris@16: BOOST_ASSERT(rhs.f && "Did you mean rhs.alias() instead of rhs?"); Chris@16: Chris@16: f = rhs.f; Chris@16: name_ = rhs.name_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: std::string const& name() const Chris@16: { Chris@16: return name_; Chris@16: } Chris@16: Chris@16: void name(std::string const& str) Chris@16: { Chris@16: name_ = str; Chris@16: } Chris@16: Chris@16: template Chris@16: rule& operator=(Expr const& expr) Chris@16: { Chris@16: define(*this, expr, traits::matches()); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // VC7.1 has problems to resolve 'rule' without explicit template parameters Chris@16: #if !BOOST_WORKAROUND(BOOST_MSVC, < 1400) Chris@16: // g++ 3.3 barfs if this is a member function :( Chris@16: template Chris@16: friend rule& operator%=(rule& r, Expr const& expr) Chris@16: { Chris@16: define(r, expr, traits::matches()); Chris@16: return r; Chris@16: } Chris@16: Chris@16: #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) Chris@16: // non-const version needed to suppress proto's %= kicking in Chris@16: template Chris@16: friend rule& operator%=(rule& r, Expr& expr) Chris@16: { Chris@16: return r %= static_cast(expr); Chris@16: } Chris@16: #else Chris@16: // for rvalue references Chris@16: template Chris@16: friend rule& operator%=(rule& r, Expr&& expr) Chris@16: { Chris@16: define(r, expr, traits::matches()); Chris@16: return r; Chris@16: } Chris@16: #endif Chris@16: Chris@16: #else Chris@16: // both friend functions have to be defined out of class as VC7.1 Chris@16: // will complain otherwise Chris@16: template Chris@16: friend rule& operator%=( Chris@16: rule&r, Expr const& expr); Chris@16: Chris@16: // non-const version needed to suppress proto's %= kicking in Chris@16: template Chris@16: friend rule& operator%=( Chris@16: rule& r, Expr& expr); Chris@16: #endif Chris@16: Chris@16: template Chris@16: struct attribute Chris@16: { Chris@16: typedef attr_type type; Chris@16: }; Chris@16: Chris@16: template Chris@16: bool generate(output_iterator& sink, Context&, Delimiter const& delim Chris@16: , Attribute const& attr) const Chris@16: { Chris@16: if (f) Chris@16: { Chris@16: // Create an attribute if none is supplied. Chris@16: typedef traits::make_attribute Chris@16: make_attribute; Chris@16: typedef traits::transform_attribute< Chris@16: typename make_attribute::type, attr_type, domain> Chris@16: transform; Chris@16: Chris@16: typename transform::type attr_ = Chris@16: traits::pre_transform( Chris@16: make_attribute::call(attr)); Chris@16: Chris@16: // If you are seeing a compilation error here, you are probably Chris@16: // trying to use a rule or a grammar which has inherited Chris@16: // attributes, without passing values for them. Chris@16: context_type context(attr_); Chris@16: Chris@16: // If you are seeing a compilation error here stating that the Chris@16: // third parameter can't be converted to a karma::reference Chris@16: // then you are probably trying to use a rule or a grammar with Chris@16: // an incompatible delimiter type. Chris@16: if (f(sink, context, delim)) Chris@16: { Chris@16: // do a post-delimit if this is an implied verbatim Chris@16: if (is_same::value) Chris@16: karma::delimit_out(sink, delim); Chris@16: Chris@16: return true; Chris@16: } Chris@16: } Chris@16: return false; Chris@16: } Chris@16: Chris@16: template Chris@16: bool generate(output_iterator& sink, Context& caller_context Chris@16: , Delimiter const& delim, Attribute const& attr Chris@16: , Params const& params) const Chris@16: { Chris@16: if (f) Chris@16: { Chris@16: // Create an attribute if none is supplied. Chris@16: typedef traits::make_attribute Chris@16: make_attribute; Chris@16: typedef traits::transform_attribute< Chris@16: typename make_attribute::type, attr_type, domain> Chris@16: transform; Chris@16: Chris@16: typename transform::type attr_ = Chris@16: traits::pre_transform( Chris@16: make_attribute::call(attr)); Chris@16: Chris@16: // If you are seeing a compilation error here, you are probably Chris@16: // trying to use a rule or a grammar which has inherited Chris@16: // attributes, passing values of incompatible types for them. Chris@16: context_type context(attr_, params, caller_context); Chris@16: Chris@16: // If you are seeing a compilation error here stating that the Chris@16: // third parameter can't be converted to a karma::reference Chris@16: // then you are probably trying to use a rule or a grammar with Chris@16: // an incompatible delimiter type. Chris@16: if (f(sink, context, delim)) Chris@16: { Chris@16: // do a post-delimit if this is an implied verbatim Chris@16: if (is_same::value) Chris@16: karma::delimit_out(sink, delim); Chris@16: Chris@16: return true; Chris@16: } Chris@16: } Chris@16: return false; Chris@16: } Chris@16: Chris@16: template Chris@16: info what(Context& /*context*/) const Chris@16: { Chris@16: return info(name_); Chris@16: } Chris@16: Chris@16: reference_ alias() const Chris@16: { Chris@16: return reference_(*this); Chris@16: } Chris@16: Chris@16: typename proto::terminal::type copy() const Chris@16: { Chris@16: typename proto::terminal::type result = {*this}; Chris@16: return result; Chris@16: } Chris@16: Chris@16: // bring in the operator() overloads Chris@16: rule const& get_parameterized_subject() const { return *this; } Chris@16: typedef rule parameterized_subject_type; Chris@16: #include Chris@16: Chris@16: std::string name_; Chris@16: function_type f; Chris@16: }; Chris@16: Chris@16: #if BOOST_WORKAROUND(BOOST_MSVC, < 1400) Chris@16: template Chris@16: rule& operator%=( Chris@16: rule&r, Expr const& expr) Chris@16: { Chris@16: // Report invalid expression error as early as possible. Chris@16: // If you got an error_invalid_expression error message here, then Chris@16: // the expression (expr) is not a valid spirit karma expression. Chris@16: BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr); Chris@16: Chris@16: typedef typename Chris@16: rule::encoding_modifier_type Chris@16: encoding_modifier_type; Chris@16: Chris@16: r.f = detail::bind_generator( Chris@16: compile(expr, encoding_modifier_type())); Chris@16: return r; Chris@16: } Chris@16: Chris@16: // non-const version needed to suppress proto's %= kicking in Chris@16: template Chris@16: rule& operator%=( Chris@16: rule& r, Expr& expr) Chris@16: { Chris@16: return r %= static_cast(expr); Chris@16: } Chris@16: #endif Chris@16: }}} Chris@16: Chris@16: namespace boost { namespace spirit { namespace traits Chris@16: { Chris@16: namespace detail Chris@16: { Chris@16: template Chris@16: struct nonterminal_handles_container Chris@16: : mpl::and_< Chris@16: traits::is_container Chris@16: , is_convertible > Chris@16: {}; Chris@16: } Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template < Chris@16: typename IteratorA, typename IteratorB, typename Attribute Chris@16: , typename Context, typename T1, typename T2, typename T3, typename T4> Chris@16: struct handles_container< Chris@16: karma::rule, Attribute, Context Chris@16: , IteratorB> Chris@16: : detail::nonterminal_handles_container< Chris@16: typename attribute_of< Chris@16: karma::rule Chris@16: , Context, IteratorB Chris@16: >::type, Attribute> Chris@16: {}; Chris@16: }}} Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: # pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #endif