Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2001-2011 Joel de Guzman
|
Chris@16
|
3
|
Chris@16
|
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 =============================================================================*/
|
Chris@16
|
7 #if !defined(SPIRIT_ACTION_JANUARY_07_2007_1128AM)
|
Chris@16
|
8 #define SPIRIT_ACTION_JANUARY_07_2007_1128AM
|
Chris@16
|
9
|
Chris@16
|
10 #if defined(_MSC_VER)
|
Chris@16
|
11 #pragma once
|
Chris@16
|
12 #endif
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/spirit/home/qi/meta_compiler.hpp>
|
Chris@16
|
15 #include <boost/spirit/home/qi/parser.hpp>
|
Chris@16
|
16 #include <boost/spirit/home/qi/detail/attributes.hpp>
|
Chris@16
|
17 #include <boost/spirit/home/support/argument.hpp>
|
Chris@16
|
18 #include <boost/spirit/home/support/context.hpp>
|
Chris@16
|
19 #include <boost/spirit/home/support/unused.hpp>
|
Chris@16
|
20 #include <boost/spirit/home/support/info.hpp>
|
Chris@16
|
21 #include <boost/spirit/home/support/action_dispatch.hpp>
|
Chris@16
|
22 #include <boost/spirit/home/support/handles_container.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 #include <boost/mpl/bool.hpp>
|
Chris@16
|
25 #include <boost/mpl/if.hpp>
|
Chris@16
|
26 #include <boost/type_traits/remove_const.hpp>
|
Chris@16
|
27 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 namespace boost { namespace spirit { namespace qi
|
Chris@16
|
30 {
|
Chris@16
|
31 BOOST_PP_REPEAT(SPIRIT_ARGUMENTS_LIMIT, SPIRIT_USING_ARGUMENT, _)
|
Chris@16
|
32
|
Chris@16
|
33 template <typename Subject, typename Action>
|
Chris@16
|
34 struct action : unary_parser<action<Subject, Action> >
|
Chris@16
|
35 {
|
Chris@16
|
36 typedef Subject subject_type;
|
Chris@16
|
37 typedef Action action_type;
|
Chris@16
|
38
|
Chris@16
|
39 template <typename Context, typename Iterator>
|
Chris@16
|
40 struct attribute
|
Chris@16
|
41 : traits::attribute_of<Subject, Context, Iterator>
|
Chris@16
|
42 {};
|
Chris@16
|
43
|
Chris@16
|
44 action(Subject const& subject_, Action f_)
|
Chris@16
|
45 : subject(subject_), f(f_) {}
|
Chris@16
|
46
|
Chris@16
|
47 #ifndef BOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT
|
Chris@16
|
48 template <typename Iterator, typename Context
|
Chris@16
|
49 , typename Skipper, typename Attribute>
|
Chris@16
|
50 bool parse(Iterator& first, Iterator const& last
|
Chris@16
|
51 , Context& context, Skipper const& skipper
|
Chris@16
|
52 , Attribute& attr_) const
|
Chris@16
|
53 {
|
Chris@16
|
54 typedef typename attribute<Context, Iterator>::type attr_type;
|
Chris@16
|
55 typedef traits::make_attribute<attr_type, Attribute> make_attribute;
|
Chris@16
|
56
|
Chris@16
|
57 // create an attribute if one is not supplied
|
Chris@16
|
58 typedef traits::transform_attribute<
|
Chris@16
|
59 typename make_attribute::type, attr_type, domain> transform;
|
Chris@16
|
60
|
Chris@16
|
61 typename make_attribute::type made_attr = make_attribute::call(attr_);
|
Chris@16
|
62 typename transform::type attr = transform::pre(made_attr);
|
Chris@16
|
63
|
Chris@16
|
64 Iterator save = first;
|
Chris@16
|
65 if (subject.parse(first, last, context, skipper, attr))
|
Chris@16
|
66 {
|
Chris@16
|
67 // call the function, passing the attribute, the context.
|
Chris@16
|
68 // The client can return false to fail parsing.
|
Chris@16
|
69 if (traits::action_dispatch<Subject>()(f, attr, context))
|
Chris@16
|
70 {
|
Chris@16
|
71 // Do up-stream transformation, this integrates the results
|
Chris@16
|
72 // back into the original attribute value, if appropriate.
|
Chris@16
|
73 traits::post_transform(attr_, attr);
|
Chris@16
|
74 return true;
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 // reset iterators if semantic action failed the match
|
Chris@16
|
78 // retrospectively
|
Chris@16
|
79 first = save;
|
Chris@16
|
80 }
|
Chris@16
|
81 return false;
|
Chris@16
|
82 }
|
Chris@16
|
83 #else
|
Chris@16
|
84 template <typename Iterator, typename Context
|
Chris@16
|
85 , typename Skipper, typename Attribute>
|
Chris@16
|
86 bool parse(Iterator& first, Iterator const& last
|
Chris@16
|
87 , Context& context, Skipper const& skipper
|
Chris@16
|
88 , Attribute& attr) const
|
Chris@16
|
89 {
|
Chris@16
|
90 Iterator save = first;
|
Chris@16
|
91 if (subject.parse(first, last, context, skipper, attr)) // Use the attribute as-is
|
Chris@16
|
92 {
|
Chris@16
|
93 // call the function, passing the attribute, the context.
|
Chris@16
|
94 // The client can return false to fail parsing.
|
Chris@16
|
95 if (traits::action_dispatch<Subject>()(f, attr, context))
|
Chris@16
|
96 return true;
|
Chris@16
|
97
|
Chris@16
|
98 // reset iterators if semantic action failed the match
|
Chris@16
|
99 // retrospectively
|
Chris@16
|
100 first = save;
|
Chris@16
|
101 }
|
Chris@16
|
102 return false;
|
Chris@16
|
103 }
|
Chris@16
|
104
|
Chris@16
|
105 template <typename Iterator, typename Context
|
Chris@16
|
106 , typename Skipper>
|
Chris@16
|
107 bool parse(Iterator& first, Iterator const& last
|
Chris@16
|
108 , Context& context, Skipper const& skipper
|
Chris@16
|
109 , unused_type) const
|
Chris@16
|
110 {
|
Chris@16
|
111 typedef typename attribute<Context, Iterator>::type attr_type;
|
Chris@16
|
112 typedef traits::make_attribute<attr_type, unused_type> make_attribute;
|
Chris@16
|
113
|
Chris@16
|
114 // synthesize the attribute since one is not supplied
|
Chris@16
|
115 typedef traits::transform_attribute<
|
Chris@16
|
116 typename make_attribute::type, attr_type, domain> transform;
|
Chris@16
|
117
|
Chris@16
|
118 typename make_attribute::type made_attr = make_attribute::call(unused_type());
|
Chris@16
|
119 typename transform::type attr = transform::pre(made_attr);
|
Chris@16
|
120
|
Chris@16
|
121 Iterator save = first;
|
Chris@16
|
122 if (subject.parse(first, last, context, skipper, attr))
|
Chris@16
|
123 {
|
Chris@16
|
124 // call the function, passing the attribute, the context.
|
Chris@16
|
125 // The client can return false to fail parsing.
|
Chris@16
|
126 if (traits::action_dispatch<Subject>()(f, attr, context))
|
Chris@16
|
127 return true;
|
Chris@16
|
128
|
Chris@16
|
129 // reset iterators if semantic action failed the match
|
Chris@16
|
130 // retrospectively
|
Chris@16
|
131 first = save;
|
Chris@16
|
132 }
|
Chris@16
|
133 return false;
|
Chris@16
|
134 }
|
Chris@16
|
135 #endif
|
Chris@16
|
136
|
Chris@16
|
137 template <typename Context>
|
Chris@16
|
138 info what(Context& context) const
|
Chris@16
|
139 {
|
Chris@16
|
140 // the action is transparent (does not add any info)
|
Chris@16
|
141 return subject.what(context);
|
Chris@16
|
142 }
|
Chris@16
|
143
|
Chris@16
|
144 Subject subject;
|
Chris@16
|
145 Action f;
|
Chris@16
|
146
|
Chris@16
|
147 private:
|
Chris@16
|
148 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
149 action& operator= (action const&);
|
Chris@16
|
150 };
|
Chris@16
|
151 }}}
|
Chris@16
|
152
|
Chris@16
|
153 namespace boost { namespace spirit
|
Chris@16
|
154 {
|
Chris@16
|
155 // Qi action meta-compiler
|
Chris@16
|
156 template <>
|
Chris@16
|
157 struct make_component<qi::domain, tag::action>
|
Chris@16
|
158 {
|
Chris@16
|
159 template <typename Sig>
|
Chris@16
|
160 struct result;
|
Chris@16
|
161
|
Chris@16
|
162 template <typename This, typename Elements, typename Modifiers>
|
Chris@16
|
163 struct result<This(Elements, Modifiers)>
|
Chris@16
|
164 {
|
Chris@16
|
165 typedef typename
|
Chris@16
|
166 remove_const<typename Elements::car_type>::type
|
Chris@16
|
167 subject_type;
|
Chris@16
|
168
|
Chris@16
|
169 typedef typename
|
Chris@16
|
170 remove_const<typename Elements::cdr_type::car_type>::type
|
Chris@16
|
171 action_type;
|
Chris@16
|
172
|
Chris@16
|
173 typedef qi::action<subject_type, action_type> type;
|
Chris@16
|
174 };
|
Chris@16
|
175
|
Chris@16
|
176 template <typename Elements>
|
Chris@16
|
177 typename result<make_component(Elements, unused_type)>::type
|
Chris@16
|
178 operator()(Elements const& elements, unused_type) const
|
Chris@16
|
179 {
|
Chris@16
|
180 typename result<make_component(Elements, unused_type)>::type
|
Chris@16
|
181 result(elements.car, elements.cdr.car);
|
Chris@16
|
182 return result;
|
Chris@16
|
183 }
|
Chris@16
|
184 };
|
Chris@16
|
185 }}
|
Chris@16
|
186
|
Chris@16
|
187 namespace boost { namespace spirit { namespace traits
|
Chris@16
|
188 {
|
Chris@16
|
189 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
190 template <typename Subject, typename Action>
|
Chris@16
|
191 struct has_semantic_action<qi::action<Subject, Action> >
|
Chris@16
|
192 : mpl::true_ {};
|
Chris@16
|
193
|
Chris@16
|
194 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
195 template <typename Subject, typename Action, typename Attribute
|
Chris@16
|
196 , typename Context, typename Iterator>
|
Chris@16
|
197 struct handles_container<qi::action<Subject, Action>, Attribute
|
Chris@16
|
198 , Context, Iterator>
|
Chris@16
|
199 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
|
Chris@16
|
200 }}}
|
Chris@16
|
201
|
Chris@16
|
202 #endif
|