1 //  Copyright (c) 2001-2011 Hartmut Kaiser
2 //
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)
5 
6 #if !defined(SPIRIT_QI_ATTR_CAST_SEP_26_2009_0735PM)
7 #define SPIRIT_QI_ATTR_CAST_SEP_26_2009_0735PM
8 
9 #if defined(_MSC_VER)
10 #pragma once
11 #endif
12 
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>
21 
22 namespace boost { namespace spirit
23 {
24     ///////////////////////////////////////////////////////////////////////////
25     // Enablers
26     ///////////////////////////////////////////////////////////////////////////
27 
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> >
32       : mpl::true_ {};
33 }}
34 
35 namespace boost { namespace spirit { namespace qi
36 {
37     using spirit::attr_cast;
38 
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> >
46     {
47         typedef typename result_of::compile<qi::domain, Subject>::type
48             subject_type;
49 
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;
55 
attr_cast_parserboost::spirit::qi::attr_cast_parser56         attr_cast_parser(Subject const& subject_)
57           : subject(subject_)
58         {
59             // If you got an error_invalid_expression error message here,
60             // then the expression (Subject) is not a valid spirit qi
61             // expression.
62             BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Subject);
63         }
64 
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>
69         struct attribute
70           : mpl::if_<traits::not_is_unused<Exposed>, Exposed
71               , transformed_attribute_type>
72         {};
73 
74         template <typename Iterator, typename Context, typename Skipper
75           , typename Attribute>
parseboost::spirit::qi::attr_cast_parser76         bool parse(Iterator& first, Iterator const& last
77           , Context& context, Skipper const& skipper
78           , Attribute& attr_param) const
79         {
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;
86 
87             // do down-stream transformation, provides attribute for embedded
88             // parser
89             typedef traits::transform_attribute<
90                 exposed_attribute_type, transformed_attribute_type, domain>
91             transform;
92 
93             typename transform::type attr_ = transform::pre(attr_param);
94 
95             if (!compile<qi::domain>(subject).
96                     parse(first, last, context, skipper, attr_))
97             {
98                 transform::fail(attr_param);
99                 return false;
100             }
101 
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_);
105             return true;
106         }
107 
108         template <typename Context>
whatboost::spirit::qi::attr_cast_parser109         info what(Context& context) const
110         {
111             return info("attr_cast"
112               , compile<qi::domain>(subject).what(context));
113         }
114 
115         Subject subject;
116 
117     private:
118         // silence MSVC warning C4512: assignment operator could not be generated
119         attr_cast_parser& operator= (attr_cast_parser const&);
120     };
121 
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>
129     {
130         typedef attr_cast_parser<Exposed, Transformed, Expr> result_type;
131 
132         template <typename Terminal>
operator ()boost::spirit::qi::make_primitive133         result_type operator()(Terminal const& term, unused_type) const
134         {
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));
139         }
140     };
141 
142 
143 }}}
144 
145 #endif
146