1 // Copyright (c) 2001-2011 Hartmut Kaiser
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #if !defined(SPIRIT_QI_ATTR_CAST_SEP_26_2009_0735PM)
7 #define SPIRIT_QI_ATTR_CAST_SEP_26_2009_0735PM
13 #include <boost/spirit/home/qi/meta_compiler.hpp>
14 #include <boost/spirit/home/qi/parser.hpp>
15 #include <boost/spirit/home/qi/domain.hpp>
16 #include <boost/spirit/home/support/unused.hpp>
17 #include <boost/spirit/home/support/info.hpp>
18 #include <boost/spirit/home/support/common_terminals.hpp>
19 #include <boost/spirit/home/qi/detail/attributes.hpp>
20 #include <boost/spirit/home/support/auxiliary/attr_cast.hpp>
22 namespace boost { namespace spirit
24 ///////////////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////////////
28 // enables attr_cast<>() pseudo parser
29 template <typename Expr, typename Exposed, typename Transformed>
30 struct use_terminal<qi::domain
31 , tag::stateful_tag<Expr, tag::attr_cast, Exposed, Transformed> >
35 namespace boost { namespace spirit { namespace qi
37 using spirit::attr_cast;
39 ///////////////////////////////////////////////////////////////////////////
40 // attr_cast_parser consumes the attribute of subject generator without
41 // generating anything
42 ///////////////////////////////////////////////////////////////////////////
43 template <typename Exposed, typename Transformed, typename Subject>
44 struct attr_cast_parser
45 : unary_parser<attr_cast_parser<Exposed, Transformed, Subject> >
47 typedef typename result_of::compile<qi::domain, Subject>::type
50 typedef typename mpl::eval_if<
51 traits::not_is_unused<Transformed>
52 , mpl::identity<Transformed>
53 , traits::attribute_of<subject_type> >::type
54 transformed_attribute_type;
56 attr_cast_parser(Subject const& subject_)
59 // If you got an error_invalid_expression error message here,
60 // then the expression (Subject) is not a valid spirit qi
62 BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Subject);
65 // If Exposed is given, we use the given type, otherwise all we can do
66 // is to guess, so we expose our inner type as an attribute and
67 // deal with the passed attribute inside the parse function.
68 template <typename Context, typename Iterator>
70 : mpl::if_<traits::not_is_unused<Exposed>, Exposed
71 , transformed_attribute_type>
74 template <typename Iterator, typename Context, typename Skipper
76 bool parse(Iterator& first, Iterator const& last
77 , Context& context, Skipper const& skipper
78 , Attribute& attr_param) const
80 // Find the real exposed attribute. If exposed is given, we use it
81 // otherwise we assume the exposed attribute type to be the actual
82 // attribute type as passed by the user.
83 typedef typename mpl::if_<
84 traits::not_is_unused<Exposed>, Exposed, Attribute>::type
85 exposed_attribute_type;
87 // do down-stream transformation, provides attribute for embedded
89 typedef traits::transform_attribute<
90 exposed_attribute_type, transformed_attribute_type, domain>
93 typename transform::type attr_ = transform::pre(attr_param);
95 if (!compile<qi::domain>(subject).
96 parse(first, last, context, skipper, attr_))
98 transform::fail(attr_param);
102 // do up-stream transformation, this mainly integrates the results
103 // back into the original attribute value, if appropriate
104 traits::post_transform(attr_param, attr_);
108 template <typename Context>
109 info what(Context& context) const
111 return info("attr_cast"
112 , compile<qi::domain>(subject).what(context));
118 // silence MSVC warning C4512: assignment operator could not be generated
119 attr_cast_parser& operator= (attr_cast_parser const&);
122 ///////////////////////////////////////////////////////////////////////////
123 // Parser generator: make_xxx function (objects)
124 ///////////////////////////////////////////////////////////////////////////
125 template <typename Expr, typename Exposed, typename Transformed
126 , typename Modifiers>
127 struct make_primitive<
128 tag::stateful_tag<Expr, tag::attr_cast, Exposed, Transformed>, Modifiers>
130 typedef attr_cast_parser<Exposed, Transformed, Expr> result_type;
132 template <typename Terminal>
133 result_type operator()(Terminal const& term, unused_type) const
135 typedef tag::stateful_tag<
136 Expr, tag::attr_cast, Exposed, Transformed> tag_type;
137 using spirit::detail::get_stateful_data;
138 return result_type(get_stateful_data<tag_type>::call(term));