Chris@16
|
1 // Copyright (c) 2001-2011 Hartmut Kaiser
|
Chris@16
|
2 //
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #if !defined(SPIRIT_QI_ATTR_CAST_SEP_26_2009_0735PM)
|
Chris@16
|
7 #define SPIRIT_QI_ATTR_CAST_SEP_26_2009_0735PM
|
Chris@16
|
8
|
Chris@16
|
9 #if defined(_MSC_VER)
|
Chris@16
|
10 #pragma once
|
Chris@16
|
11 #endif
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/spirit/home/qi/meta_compiler.hpp>
|
Chris@16
|
14 #include <boost/spirit/home/qi/parser.hpp>
|
Chris@16
|
15 #include <boost/spirit/home/qi/domain.hpp>
|
Chris@16
|
16 #include <boost/spirit/home/support/unused.hpp>
|
Chris@16
|
17 #include <boost/spirit/home/support/info.hpp>
|
Chris@16
|
18 #include <boost/spirit/home/support/common_terminals.hpp>
|
Chris@16
|
19 #include <boost/spirit/home/qi/detail/attributes.hpp>
|
Chris@16
|
20 #include <boost/spirit/home/support/auxiliary/attr_cast.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost { namespace spirit
|
Chris@16
|
23 {
|
Chris@16
|
24 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
25 // Enablers
|
Chris@16
|
26 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
27
|
Chris@16
|
28 // enables attr_cast<>() pseudo parser
|
Chris@16
|
29 template <typename Expr, typename Exposed, typename Transformed>
|
Chris@16
|
30 struct use_terminal<qi::domain
|
Chris@16
|
31 , tag::stateful_tag<Expr, tag::attr_cast, Exposed, Transformed> >
|
Chris@16
|
32 : mpl::true_ {};
|
Chris@16
|
33 }}
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost { namespace spirit { namespace qi
|
Chris@16
|
36 {
|
Chris@16
|
37 using spirit::attr_cast;
|
Chris@16
|
38
|
Chris@16
|
39 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
40 // attr_cast_parser consumes the attribute of subject generator without
|
Chris@16
|
41 // generating anything
|
Chris@16
|
42 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
43 template <typename Exposed, typename Transformed, typename Subject>
|
Chris@16
|
44 struct attr_cast_parser
|
Chris@16
|
45 : unary_parser<attr_cast_parser<Exposed, Transformed, Subject> >
|
Chris@16
|
46 {
|
Chris@16
|
47 typedef typename result_of::compile<qi::domain, Subject>::type
|
Chris@16
|
48 subject_type;
|
Chris@16
|
49
|
Chris@16
|
50 typedef typename mpl::eval_if<
|
Chris@16
|
51 traits::not_is_unused<Transformed>
|
Chris@16
|
52 , mpl::identity<Transformed>
|
Chris@101
|
53 , traits::attribute_of<subject_type> >::type
|
Chris@16
|
54 transformed_attribute_type;
|
Chris@16
|
55
|
Chris@16
|
56 attr_cast_parser(Subject const& subject_)
|
Chris@16
|
57 : subject(subject_)
|
Chris@16
|
58 {
|
Chris@16
|
59 // If you got an error_invalid_expression error message here,
|
Chris@16
|
60 // then the expression (Subject) is not a valid spirit qi
|
Chris@16
|
61 // expression.
|
Chris@16
|
62 BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Subject);
|
Chris@16
|
63 }
|
Chris@16
|
64
|
Chris@16
|
65 // If Exposed is given, we use the given type, otherwise all we can do
|
Chris@16
|
66 // is to guess, so we expose our inner type as an attribute and
|
Chris@16
|
67 // deal with the passed attribute inside the parse function.
|
Chris@16
|
68 template <typename Context, typename Iterator>
|
Chris@16
|
69 struct attribute
|
Chris@16
|
70 : mpl::if_<traits::not_is_unused<Exposed>, Exposed
|
Chris@16
|
71 , transformed_attribute_type>
|
Chris@16
|
72 {};
|
Chris@16
|
73
|
Chris@16
|
74 template <typename Iterator, typename Context, typename Skipper
|
Chris@16
|
75 , typename Attribute>
|
Chris@16
|
76 bool parse(Iterator& first, Iterator const& last
|
Chris@16
|
77 , Context& context, Skipper const& skipper
|
Chris@16
|
78 , Attribute& attr_param) const
|
Chris@16
|
79 {
|
Chris@16
|
80 // Find the real exposed attribute. If exposed is given, we use it
|
Chris@16
|
81 // otherwise we assume the exposed attribute type to be the actual
|
Chris@16
|
82 // attribute type as passed by the user.
|
Chris@16
|
83 typedef typename mpl::if_<
|
Chris@16
|
84 traits::not_is_unused<Exposed>, Exposed, Attribute>::type
|
Chris@16
|
85 exposed_attribute_type;
|
Chris@16
|
86
|
Chris@16
|
87 // do down-stream transformation, provides attribute for embedded
|
Chris@16
|
88 // parser
|
Chris@16
|
89 typedef traits::transform_attribute<
|
Chris@101
|
90 exposed_attribute_type, transformed_attribute_type, domain>
|
Chris@16
|
91 transform;
|
Chris@16
|
92
|
Chris@16
|
93 typename transform::type attr_ = transform::pre(attr_param);
|
Chris@16
|
94
|
Chris@16
|
95 if (!compile<qi::domain>(subject).
|
Chris@16
|
96 parse(first, last, context, skipper, attr_))
|
Chris@16
|
97 {
|
Chris@16
|
98 transform::fail(attr_param);
|
Chris@16
|
99 return false;
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 // do up-stream transformation, this mainly integrates the results
|
Chris@16
|
103 // back into the original attribute value, if appropriate
|
Chris@16
|
104 traits::post_transform(attr_param, attr_);
|
Chris@16
|
105 return true;
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 template <typename Context>
|
Chris@16
|
109 info what(Context& context) const
|
Chris@16
|
110 {
|
Chris@16
|
111 return info("attr_cast"
|
Chris@16
|
112 , compile<qi::domain>(subject).what(context));
|
Chris@16
|
113 }
|
Chris@16
|
114
|
Chris@16
|
115 Subject subject;
|
Chris@16
|
116
|
Chris@16
|
117 private:
|
Chris@16
|
118 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
119 attr_cast_parser& operator= (attr_cast_parser const&);
|
Chris@16
|
120 };
|
Chris@16
|
121
|
Chris@16
|
122 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
123 // Parser generator: make_xxx function (objects)
|
Chris@16
|
124 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
125 template <typename Expr, typename Exposed, typename Transformed
|
Chris@16
|
126 , typename Modifiers>
|
Chris@16
|
127 struct make_primitive<
|
Chris@16
|
128 tag::stateful_tag<Expr, tag::attr_cast, Exposed, Transformed>, Modifiers>
|
Chris@16
|
129 {
|
Chris@16
|
130 typedef attr_cast_parser<Exposed, Transformed, Expr> result_type;
|
Chris@16
|
131
|
Chris@16
|
132 template <typename Terminal>
|
Chris@16
|
133 result_type operator()(Terminal const& term, unused_type) const
|
Chris@16
|
134 {
|
Chris@16
|
135 typedef tag::stateful_tag<
|
Chris@16
|
136 Expr, tag::attr_cast, Exposed, Transformed> tag_type;
|
Chris@16
|
137 using spirit::detail::get_stateful_data;
|
Chris@16
|
138 return result_type(get_stateful_data<tag_type>::call(term));
|
Chris@16
|
139 }
|
Chris@16
|
140 };
|
Chris@16
|
141
|
Chris@16
|
142
|
Chris@16
|
143 }}}
|
Chris@16
|
144
|
Chris@16
|
145 #endif
|