Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2002-2003 Hartmut Kaiser
|
Chris@16
|
3 http://spirit.sourceforge.net/
|
Chris@16
|
4
|
Chris@16
|
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 =============================================================================*/
|
Chris@16
|
8 #ifndef BOOST_SPIRIT_REFACTORING_HPP
|
Chris@16
|
9 #define BOOST_SPIRIT_REFACTORING_HPP
|
Chris@16
|
10
|
Chris@16
|
11 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
12 #include <boost/static_assert.hpp>
|
Chris@16
|
13 #include <boost/spirit/home/classic/namespace.hpp>
|
Chris@16
|
14 #include <boost/spirit/home/classic/meta/as_parser.hpp>
|
Chris@16
|
15 #include <boost/spirit/home/classic/core/parser.hpp>
|
Chris@16
|
16 #include <boost/spirit/home/classic/core/composite/composite.hpp>
|
Chris@16
|
17 #include <boost/spirit/home/classic/meta/impl/refactoring.ipp>
|
Chris@16
|
18
|
Chris@16
|
19 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
20 namespace boost { namespace spirit {
|
Chris@16
|
21
|
Chris@16
|
22 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
|
Chris@16
|
23
|
Chris@16
|
24 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
|
Chris@16
|
25 #pragma warning(push)
|
Chris@16
|
26 #pragma warning(disable:4512) //assignment operator could not be generated
|
Chris@16
|
27 #endif
|
Chris@16
|
28
|
Chris@16
|
29 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
30 //
|
Chris@16
|
31 // refactor_unary_parser class
|
Chris@16
|
32 //
|
Chris@16
|
33 // This helper template allows to attach an unary operation to a newly
|
Chris@16
|
34 // constructed parser, which combines the subject of the left operand of
|
Chris@16
|
35 // the original given parser (BinaryT) with the right operand of the
|
Chris@16
|
36 // original binary parser through the original binary operation and
|
Chris@16
|
37 // rewraps the resulting parser with the original unary operator.
|
Chris@16
|
38 //
|
Chris@16
|
39 // For instance given the parser:
|
Chris@16
|
40 // *some_parser - another_parser
|
Chris@16
|
41 //
|
Chris@16
|
42 // will be refactored to:
|
Chris@16
|
43 // *(some_parser - another_parser)
|
Chris@16
|
44 //
|
Chris@16
|
45 // If the parser to refactor is not a unary parser, no refactoring is done
|
Chris@16
|
46 // at all.
|
Chris@16
|
47 //
|
Chris@16
|
48 // The original parser should be a binary_parser_category parser,
|
Chris@16
|
49 // else the compilation will fail
|
Chris@16
|
50 //
|
Chris@16
|
51 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
52
|
Chris@16
|
53 template <typename NestedT = non_nested_refactoring>
|
Chris@16
|
54 class refactor_unary_gen;
|
Chris@16
|
55
|
Chris@16
|
56 template <typename BinaryT, typename NestedT = non_nested_refactoring>
|
Chris@16
|
57 class refactor_unary_parser :
|
Chris@16
|
58 public parser<refactor_unary_parser<BinaryT, NestedT> > {
|
Chris@16
|
59
|
Chris@16
|
60 public:
|
Chris@16
|
61 // the parser to refactor has to be at least a binary_parser_category
|
Chris@16
|
62 // parser
|
Chris@16
|
63 BOOST_STATIC_ASSERT((
|
Chris@16
|
64 boost::is_convertible<typename BinaryT::parser_category_t,
|
Chris@16
|
65 binary_parser_category>::value
|
Chris@16
|
66 ));
|
Chris@16
|
67
|
Chris@16
|
68 refactor_unary_parser(BinaryT const& binary_, NestedT const& nested_)
|
Chris@16
|
69 : binary(binary_), nested(nested_) {}
|
Chris@16
|
70
|
Chris@16
|
71 typedef refactor_unary_parser<BinaryT, NestedT> self_t;
|
Chris@16
|
72 typedef refactor_unary_gen<NestedT> parser_generator_t;
|
Chris@16
|
73 typedef typename BinaryT::left_t::parser_category_t parser_category_t;
|
Chris@16
|
74
|
Chris@16
|
75 template <typename ScannerT>
|
Chris@16
|
76 typename parser_result<self_t, ScannerT>::type
|
Chris@16
|
77 parse(ScannerT const& scan) const
|
Chris@16
|
78 {
|
Chris@16
|
79 return impl::refactor_unary_type<NestedT>::
|
Chris@16
|
80 parse(*this, scan, binary, nested);
|
Chris@16
|
81 }
|
Chris@16
|
82
|
Chris@16
|
83 private:
|
Chris@16
|
84 typename as_parser<BinaryT>::type::embed_t binary;
|
Chris@16
|
85 typename NestedT::embed_t nested;
|
Chris@16
|
86 };
|
Chris@16
|
87
|
Chris@16
|
88 //////////////////////////////////
|
Chris@16
|
89 template <typename NestedT>
|
Chris@16
|
90 class refactor_unary_gen {
|
Chris@16
|
91
|
Chris@16
|
92 public:
|
Chris@16
|
93 typedef refactor_unary_gen<NestedT> embed_t;
|
Chris@16
|
94
|
Chris@16
|
95 refactor_unary_gen(NestedT const& nested_ = non_nested_refactoring())
|
Chris@16
|
96 : nested(nested_) {}
|
Chris@16
|
97
|
Chris@16
|
98 template <typename ParserT>
|
Chris@16
|
99 refactor_unary_parser<ParserT, NestedT>
|
Chris@16
|
100 operator[](parser<ParserT> const& subject) const
|
Chris@16
|
101 {
|
Chris@16
|
102 return refactor_unary_parser<ParserT, NestedT>
|
Chris@16
|
103 (subject.derived(), nested);
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 private:
|
Chris@16
|
107 typename NestedT::embed_t nested;
|
Chris@16
|
108 };
|
Chris@16
|
109
|
Chris@16
|
110 const refactor_unary_gen<> refactor_unary_d = refactor_unary_gen<>();
|
Chris@16
|
111
|
Chris@16
|
112 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
113 //
|
Chris@16
|
114 // refactor_action_parser class
|
Chris@16
|
115 //
|
Chris@16
|
116 // This helper template allows to attach an action taken from the left
|
Chris@16
|
117 // operand of the given binary parser to a newly constructed parser,
|
Chris@16
|
118 // which combines the subject of the left operand of the original binary
|
Chris@16
|
119 // parser with the right operand of the original binary parser by means of
|
Chris@16
|
120 // the original binary operator parser.
|
Chris@16
|
121 //
|
Chris@16
|
122 // For instance the parser:
|
Chris@16
|
123 // some_parser[some_attached_functor] - another_parser
|
Chris@16
|
124 //
|
Chris@16
|
125 // will be refactored to:
|
Chris@16
|
126 // (some_parser - another_parser)[some_attached_functor]
|
Chris@16
|
127 //
|
Chris@16
|
128 // If the left operand to refactor is not an action parser, no refactoring
|
Chris@16
|
129 // is done at all.
|
Chris@16
|
130 //
|
Chris@16
|
131 // The original parser should be a binary_parser_category parser,
|
Chris@16
|
132 // else the compilation will fail
|
Chris@16
|
133 //
|
Chris@16
|
134 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
135
|
Chris@16
|
136 template <typename NestedT = non_nested_refactoring>
|
Chris@16
|
137 class refactor_action_gen;
|
Chris@16
|
138
|
Chris@16
|
139 template <typename BinaryT, typename NestedT = non_nested_refactoring>
|
Chris@16
|
140 class refactor_action_parser :
|
Chris@16
|
141 public parser<refactor_action_parser<BinaryT, NestedT> > {
|
Chris@16
|
142
|
Chris@16
|
143 public:
|
Chris@16
|
144 // the parser to refactor has to be at least a binary_parser_category
|
Chris@16
|
145 // parser
|
Chris@16
|
146 BOOST_STATIC_ASSERT((
|
Chris@16
|
147 boost::is_convertible<typename BinaryT::parser_category_t,
|
Chris@16
|
148 binary_parser_category>::value
|
Chris@16
|
149 ));
|
Chris@16
|
150
|
Chris@16
|
151 refactor_action_parser(BinaryT const& binary_, NestedT const& nested_)
|
Chris@16
|
152 : binary(binary_), nested(nested_) {}
|
Chris@16
|
153
|
Chris@16
|
154 typedef refactor_action_parser<BinaryT, NestedT> self_t;
|
Chris@16
|
155 typedef refactor_action_gen<NestedT> parser_generator_t;
|
Chris@16
|
156 typedef typename BinaryT::left_t::parser_category_t parser_category_t;
|
Chris@16
|
157
|
Chris@16
|
158 template <typename ScannerT>
|
Chris@16
|
159 typename parser_result<self_t, ScannerT>::type
|
Chris@16
|
160 parse(ScannerT const& scan) const
|
Chris@16
|
161 {
|
Chris@16
|
162 return impl::refactor_action_type<NestedT>::
|
Chris@16
|
163 parse(*this, scan, binary, nested);
|
Chris@16
|
164 }
|
Chris@16
|
165
|
Chris@16
|
166 private:
|
Chris@16
|
167 typename as_parser<BinaryT>::type::embed_t binary;
|
Chris@16
|
168 typename NestedT::embed_t nested;
|
Chris@16
|
169 };
|
Chris@16
|
170
|
Chris@16
|
171 //////////////////////////////////
|
Chris@16
|
172 template <typename NestedT>
|
Chris@16
|
173 class refactor_action_gen {
|
Chris@16
|
174
|
Chris@16
|
175 public:
|
Chris@16
|
176 typedef refactor_action_gen<NestedT> embed_t;
|
Chris@16
|
177
|
Chris@16
|
178 refactor_action_gen(NestedT const& nested_ = non_nested_refactoring())
|
Chris@16
|
179 : nested(nested_) {}
|
Chris@16
|
180
|
Chris@16
|
181 template <typename ParserT>
|
Chris@16
|
182 refactor_action_parser<ParserT, NestedT>
|
Chris@16
|
183 operator[](parser<ParserT> const& subject) const
|
Chris@16
|
184 {
|
Chris@16
|
185 return refactor_action_parser<ParserT, NestedT>
|
Chris@16
|
186 (subject.derived(), nested);
|
Chris@16
|
187 }
|
Chris@16
|
188
|
Chris@16
|
189 private:
|
Chris@16
|
190 typename NestedT::embed_t nested;
|
Chris@16
|
191 };
|
Chris@16
|
192
|
Chris@16
|
193 const refactor_action_gen<> refactor_action_d = refactor_action_gen<>();
|
Chris@16
|
194
|
Chris@16
|
195 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
196 //
|
Chris@16
|
197 // attach_action_parser class
|
Chris@16
|
198 //
|
Chris@16
|
199 // This helper template allows to attach an action given separately
|
Chris@16
|
200 // to to all parsers, out of which the given parser is constructed and
|
Chris@16
|
201 // reconstructs a new parser having the same structure.
|
Chris@16
|
202 //
|
Chris@16
|
203 // For instance the parser:
|
Chris@16
|
204 // (some_parser >> another_parser)[some_attached_functor]
|
Chris@16
|
205 //
|
Chris@16
|
206 // will be refactored to:
|
Chris@16
|
207 // some_parser[some_attached_functor]
|
Chris@16
|
208 // >> another_parser[some_attached_functor]
|
Chris@16
|
209 //
|
Chris@16
|
210 // The original parser should be a action_parser_category parser,
|
Chris@16
|
211 // else the compilation will fail
|
Chris@16
|
212 //
|
Chris@16
|
213 // If the parser, to which the action is attached is not an binary parser,
|
Chris@16
|
214 // no refactoring is done at all.
|
Chris@16
|
215 //
|
Chris@16
|
216 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
217
|
Chris@16
|
218 template <typename NestedT = non_nested_refactoring>
|
Chris@16
|
219 class attach_action_gen;
|
Chris@16
|
220
|
Chris@16
|
221 template <typename ActionT, typename NestedT = non_nested_refactoring>
|
Chris@16
|
222 class attach_action_parser :
|
Chris@16
|
223 public parser<attach_action_parser<ActionT, NestedT> > {
|
Chris@16
|
224
|
Chris@16
|
225 public:
|
Chris@16
|
226 // the parser to refactor has to be at least a action_parser_category
|
Chris@16
|
227 // parser
|
Chris@16
|
228 BOOST_STATIC_ASSERT((
|
Chris@16
|
229 boost::is_convertible<typename ActionT::parser_category_t,
|
Chris@16
|
230 action_parser_category>::value
|
Chris@16
|
231 ));
|
Chris@16
|
232
|
Chris@16
|
233 attach_action_parser(ActionT const& actor_, NestedT const& nested_)
|
Chris@16
|
234 : actor(actor_), nested(nested_) {}
|
Chris@16
|
235
|
Chris@16
|
236 typedef attach_action_parser<ActionT, NestedT> self_t;
|
Chris@16
|
237 typedef attach_action_gen<NestedT> parser_generator_t;
|
Chris@16
|
238 typedef typename ActionT::parser_category_t parser_category_t;
|
Chris@16
|
239
|
Chris@16
|
240 template <typename ScannerT>
|
Chris@16
|
241 typename parser_result<self_t, ScannerT>::type
|
Chris@16
|
242 parse(ScannerT const& scan) const
|
Chris@16
|
243 {
|
Chris@16
|
244 return impl::attach_action_type<NestedT>::
|
Chris@16
|
245 parse(*this, scan, actor, nested);
|
Chris@16
|
246 }
|
Chris@16
|
247
|
Chris@16
|
248 private:
|
Chris@16
|
249 typename as_parser<ActionT>::type::embed_t actor;
|
Chris@16
|
250 typename NestedT::embed_t nested;
|
Chris@16
|
251 };
|
Chris@16
|
252
|
Chris@16
|
253 //////////////////////////////////
|
Chris@16
|
254 template <typename NestedT>
|
Chris@16
|
255 class attach_action_gen {
|
Chris@16
|
256
|
Chris@16
|
257 public:
|
Chris@16
|
258 typedef attach_action_gen<NestedT> embed_t;
|
Chris@16
|
259
|
Chris@16
|
260 attach_action_gen(NestedT const& nested_ = non_nested_refactoring())
|
Chris@16
|
261 : nested(nested_) {}
|
Chris@16
|
262
|
Chris@16
|
263 template <typename ParserT, typename ActionT>
|
Chris@16
|
264 attach_action_parser<action<ParserT, ActionT>, NestedT>
|
Chris@16
|
265 operator[](action<ParserT, ActionT> const& actor) const
|
Chris@16
|
266 {
|
Chris@16
|
267 return attach_action_parser<action<ParserT, ActionT>, NestedT>
|
Chris@16
|
268 (actor, nested);
|
Chris@16
|
269 }
|
Chris@16
|
270
|
Chris@16
|
271 private:
|
Chris@16
|
272 typename NestedT::embed_t nested;
|
Chris@16
|
273 };
|
Chris@16
|
274
|
Chris@16
|
275 const attach_action_gen<> attach_action_d = attach_action_gen<>();
|
Chris@16
|
276
|
Chris@16
|
277 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
|
Chris@16
|
278 #pragma warning(pop)
|
Chris@16
|
279 #endif
|
Chris@16
|
280
|
Chris@16
|
281 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
282 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
|
Chris@16
|
283
|
Chris@16
|
284 }} // namespace BOOST_SPIRIT_CLASSIC_NS
|
Chris@16
|
285
|
Chris@16
|
286 #endif // BOOST_SPIRIT_REFACTORING_HPP
|
Chris@16
|
287
|