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