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(SPIRIT_QI_ATTR_CAST_SEP_26_2009_0735PM) Chris@16: #define SPIRIT_QI_ATTR_CAST_SEP_26_2009_0735PM 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: #include Chris@16: Chris@16: namespace boost { namespace spirit Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Enablers Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: // enables attr_cast<>() pseudo parser Chris@16: template Chris@16: struct use_terminal > Chris@16: : mpl::true_ {}; Chris@16: }} Chris@16: Chris@16: namespace boost { namespace spirit { namespace qi Chris@16: { Chris@16: using spirit::attr_cast; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // attr_cast_parser consumes the attribute of subject generator without Chris@16: // generating anything Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct attr_cast_parser Chris@16: : unary_parser > Chris@16: { Chris@16: typedef typename result_of::compile::type Chris@16: subject_type; Chris@16: Chris@16: typedef typename mpl::eval_if< Chris@16: traits::not_is_unused Chris@16: , mpl::identity Chris@101: , traits::attribute_of >::type Chris@16: transformed_attribute_type; Chris@16: Chris@16: attr_cast_parser(Subject const& subject_) Chris@16: : subject(subject_) Chris@16: { Chris@16: // If you got an error_invalid_expression error message here, Chris@16: // then the expression (Subject) is not a valid spirit qi Chris@16: // expression. Chris@16: BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Subject); Chris@16: } Chris@16: Chris@16: // If Exposed is given, we use the given type, otherwise all we can do Chris@16: // is to guess, so we expose our inner type as an attribute and Chris@16: // deal with the passed attribute inside the parse function. Chris@16: template Chris@16: struct attribute Chris@16: : mpl::if_, Exposed Chris@16: , transformed_attribute_type> Chris@16: {}; Chris@16: Chris@16: template Chris@16: bool parse(Iterator& first, Iterator const& last Chris@16: , Context& context, Skipper const& skipper Chris@16: , Attribute& attr_param) const Chris@16: { Chris@16: // Find the real exposed attribute. If exposed is given, we use it Chris@16: // otherwise we assume the exposed attribute type to be the actual Chris@16: // attribute type as passed by the user. Chris@16: typedef typename mpl::if_< Chris@16: traits::not_is_unused, Exposed, Attribute>::type Chris@16: exposed_attribute_type; Chris@16: Chris@16: // do down-stream transformation, provides attribute for embedded Chris@16: // parser Chris@16: typedef traits::transform_attribute< Chris@101: exposed_attribute_type, transformed_attribute_type, domain> Chris@16: transform; Chris@16: Chris@16: typename transform::type attr_ = transform::pre(attr_param); Chris@16: Chris@16: if (!compile(subject). Chris@16: parse(first, last, context, skipper, attr_)) Chris@16: { Chris@16: transform::fail(attr_param); Chris@16: return false; Chris@16: } Chris@16: Chris@16: // do up-stream transformation, this mainly integrates the results Chris@16: // back into the original attribute value, if appropriate Chris@16: traits::post_transform(attr_param, attr_); Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: info what(Context& context) const Chris@16: { Chris@16: return info("attr_cast" Chris@16: , compile(subject).what(context)); Chris@16: } Chris@16: Chris@16: Subject subject; Chris@16: Chris@16: private: Chris@16: // silence MSVC warning C4512: assignment operator could not be generated Chris@16: attr_cast_parser& operator= (attr_cast_parser const&); Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Parser generator: make_xxx function (objects) Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct make_primitive< Chris@16: tag::stateful_tag, Modifiers> Chris@16: { Chris@16: typedef attr_cast_parser result_type; Chris@16: Chris@16: template Chris@16: result_type operator()(Terminal const& term, unused_type) const Chris@16: { Chris@16: typedef tag::stateful_tag< Chris@16: Expr, tag::attr_cast, Exposed, Transformed> tag_type; Chris@16: using spirit::detail::get_stateful_data; Chris@16: return result_type(get_stateful_data::call(term)); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: }}} Chris@16: Chris@16: #endif