source: NonGTP/Boost/boost/spirit/meta/impl/refactoring.ipp @ 857

Revision 857, 15.8 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*=============================================================================
2    Copyright (c) 2002-2003 Hartmut Kaiser
3    http://spirit.sourceforge.net/
4
5    Use, modification and distribution is subject to the Boost Software
6    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7    http://www.boost.org/LICENSE_1_0.txt)
8=============================================================================*/
9#ifndef BOOST_SPIRIT_REFACTORING_IPP
10#define BOOST_SPIRIT_REFACTORING_IPP
11
12///////////////////////////////////////////////////////////////////////////////
13namespace boost { namespace spirit {
14
15///////////////////////////////////////////////////////////////////////////////
16//
17//  The struct 'self_nested_refactoring' is used to indicate, that the
18//  refactoring algorithm should be 'self-nested'.
19//
20//  The struct 'non_nested_refactoring' is used to indicate, that no nesting
21//  of refactoring algorithms is reqired.
22//
23///////////////////////////////////////////////////////////////////////////////
24
25struct non_nested_refactoring { typedef non_nested_refactoring embed_t; };
26struct self_nested_refactoring { typedef self_nested_refactoring embed_t; };
27
28///////////////////////////////////////////////////////////////////////////////
29namespace impl {
30
31///////////////////////////////////////////////////////////////////////////////
32//
33//  Helper templates for refactoring parsers
34//
35///////////////////////////////////////////////////////////////////////////////
36
37    ///////////////////////////////////////////////////////////////////////////
38    //
39    //  refactor the left unary operand of a binary parser
40    //
41    //      The refactoring should be done only if the left operand is an
42    //      unary_parser_category parser.
43    //
44    ///////////////////////////////////////////////////////////////////////////
45
46    ///////////////////////////////////////////////////////////////////////////
47    template <typename CategoryT>
48    struct refactor_unary_nested {
49
50        template <
51            typename ParserT, typename NestedT,
52            typename ScannerT, typename BinaryT
53        >
54        static typename parser_result<ParserT, ScannerT>::type
55        parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
56            NestedT const& /*nested_d*/)
57        {
58            return binary.parse(scan);
59        }
60    };
61
62    template <>
63    struct refactor_unary_nested<unary_parser_category> {
64
65        template <
66            typename ParserT, typename ScannerT, typename BinaryT,
67            typename NestedT
68        >
69        static typename parser_result<ParserT, ScannerT>::type
70        parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
71            NestedT const& nested_d)
72        {
73            typedef typename BinaryT::parser_generator_t op_t;
74            typedef
75                typename BinaryT::left_t::parser_generator_t
76                unary_t;
77
78            return
79                unary_t::generate(
80                    nested_d[
81                        op_t::generate(binary.left().subject(), binary.right())
82                    ]
83                ).parse(scan);
84        }
85    };
86
87    ///////////////////////////////////////////////////////////////////////////
88    template <typename CategoryT>
89    struct refactor_unary_non_nested {
90
91        template <typename ParserT, typename ScannerT, typename BinaryT>
92        static typename parser_result<ParserT, ScannerT>::type
93        parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
94        {
95            return binary.parse(scan);
96        }
97    };
98
99    template <>
100    struct refactor_unary_non_nested<unary_parser_category> {
101
102        template <typename ParserT, typename ScannerT, typename BinaryT>
103        static typename parser_result<ParserT, ScannerT>::type
104        parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
105        {
106            typedef typename BinaryT::parser_generator_t op_t;
107            typedef
108                typename BinaryT::left_t::parser_generator_t
109                unary_t;
110
111            return unary_t::generate(
112                op_t::generate(binary.left().subject(), binary.right())
113            ).parse(scan);
114        }
115    };
116
117    ///////////////////////////////////////////////////////////////////////////
118    template <typename NestedT>
119    struct refactor_unary_type {
120
121        template <typename ParserT, typename ScannerT, typename BinaryT>
122        static typename parser_result<ParserT, ScannerT>::type
123        parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
124            NestedT const& nested_d)
125        {
126            typedef
127                typename BinaryT::left_t::parser_category_t
128                parser_category_t;
129
130            return refactor_unary_nested<parser_category_t>::
131                    parse(p, scan, binary, nested_d);
132        }
133    };
134
135    template <>
136    struct refactor_unary_type<non_nested_refactoring> {
137
138        template <typename ParserT, typename ScannerT, typename BinaryT>
139        static typename parser_result<ParserT, ScannerT>::type
140        parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
141            non_nested_refactoring const&)
142        {
143            typedef
144                typename BinaryT::left_t::parser_category_t
145                parser_category_t;
146
147            return refactor_unary_non_nested<parser_category_t>::
148                    parse(p, scan, binary);
149        }
150
151    };
152
153    template <>
154    struct refactor_unary_type<self_nested_refactoring> {
155
156        template <typename ParserT, typename ScannerT, typename BinaryT>
157        static typename parser_result<ParserT, ScannerT>::type
158        parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
159            self_nested_refactoring const &nested_tag)
160        {
161            typedef
162                typename BinaryT::left_t::parser_category_t
163                parser_category_t;
164            typedef typename ParserT::parser_generator_t parser_generator_t;
165
166            parser_generator_t nested_d(nested_tag);
167            return refactor_unary_nested<parser_category_t>::
168                    parse(p, scan, binary, nested_d);
169        }
170
171    };
172
173    ///////////////////////////////////////////////////////////////////////////
174    //
175    //  refactor the action on the left operand of a binary parser
176    //
177    //      The refactoring should be done only if the left operand is an
178    //      action_parser_category parser.
179    //
180    ///////////////////////////////////////////////////////////////////////////
181
182    ///////////////////////////////////////////////////////////////////////////
183    template <typename CategoryT>
184    struct refactor_action_nested {
185
186        template <
187            typename ParserT, typename ScannerT, typename BinaryT,
188            typename NestedT
189        >
190        static typename parser_result<ParserT, ScannerT>::type
191        parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
192            NestedT const& nested_d)
193        {
194            return nested_d[binary].parse(scan);
195        }
196    };
197
198    template <>
199    struct refactor_action_nested<action_parser_category> {
200
201        template <
202            typename ParserT, typename ScannerT, typename BinaryT,
203            typename NestedT
204        >
205        static typename parser_result<ParserT, ScannerT>::type
206        parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
207            NestedT const& nested_d)
208        {
209            typedef typename BinaryT::parser_generator_t binary_gen_t;
210
211            return (
212                nested_d[
213                    binary_gen_t::generate(
214                        binary.left().subject(),
215                        binary.right()
216                    )
217                ][binary.left().predicate()]
218            ).parse(scan);
219        }
220    };
221
222    ///////////////////////////////////////////////////////////////////////////
223    template <typename CategoryT>
224    struct refactor_action_non_nested {
225
226        template <typename ParserT, typename ScannerT, typename BinaryT>
227        static typename parser_result<ParserT, ScannerT>::type
228        parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
229        {
230            return binary.parse(scan);
231        }
232    };
233
234    template <>
235    struct refactor_action_non_nested<action_parser_category> {
236
237        template <typename ParserT, typename ScannerT, typename BinaryT>
238        static typename parser_result<ParserT, ScannerT>::type
239        parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
240        {
241            typedef typename BinaryT::parser_generator_t binary_gen_t;
242
243            return (
244                binary_gen_t::generate(
245                    binary.left().subject(),
246                    binary.right()
247                )[binary.left().predicate()]
248            ).parse(scan);
249        }
250    };
251
252    ///////////////////////////////////////////////////////////////////////////
253    template <typename NestedT>
254    struct refactor_action_type {
255
256        template <typename ParserT, typename ScannerT, typename BinaryT>
257        static typename parser_result<ParserT, ScannerT>::type
258        parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
259            NestedT const& nested_d)
260        {
261            typedef
262                typename BinaryT::left_t::parser_category_t
263                parser_category_t;
264
265            return refactor_action_nested<parser_category_t>::
266                    parse(p, scan, binary, nested_d);
267        }
268    };
269
270    template <>
271    struct refactor_action_type<non_nested_refactoring> {
272
273        template <typename ParserT, typename ScannerT, typename BinaryT>
274        static typename parser_result<ParserT, ScannerT>::type
275        parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
276            non_nested_refactoring const&)
277        {
278            typedef
279                typename BinaryT::left_t::parser_category_t
280                parser_category_t;
281
282            return refactor_action_non_nested<parser_category_t>::
283                parse(p, scan, binary);
284        }
285    };
286
287    template <>
288    struct refactor_action_type<self_nested_refactoring> {
289
290        template <typename ParserT, typename ScannerT, typename BinaryT>
291        static typename parser_result<ParserT, ScannerT>::type
292        parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
293            self_nested_refactoring const &nested_tag)
294        {
295            typedef typename ParserT::parser_generator_t parser_generator_t;
296            typedef
297                typename BinaryT::left_t::parser_category_t
298                parser_category_t;
299
300            parser_generator_t nested_d(nested_tag);
301            return refactor_action_nested<parser_category_t>::
302                    parse(p, scan, binary, nested_d);
303        }
304    };
305
306    ///////////////////////////////////////////////////////////////////////////
307    //
308    //  refactor the action attached to a binary parser
309    //
310    //      The refactoring should be done only if the given parser is an
311    //      binary_parser_category parser.
312    //
313    ///////////////////////////////////////////////////////////////////////////
314
315    ///////////////////////////////////////////////////////////////////////////
316    template <typename CategoryT>
317    struct attach_action_nested {
318
319        template <
320            typename ParserT, typename ScannerT, typename ActionT,
321            typename NestedT
322        >
323        static typename parser_result<ParserT, ScannerT>::type
324        parse(ParserT const &, ScannerT const& scan, ActionT const &action,
325            NestedT const& nested_d)
326        {
327            return action.parse(scan);
328        }
329    };
330
331    template <>
332    struct attach_action_nested<binary_parser_category> {
333
334        template <
335            typename ParserT, typename ScannerT, typename ActionT,
336            typename NestedT
337        >
338        static typename parser_result<ParserT, ScannerT>::type
339        parse(ParserT const &, ScannerT const& scan, ActionT const &action,
340            NestedT const& nested_d)
341        {
342            typedef
343                typename ActionT::subject_t::parser_generator_t
344                binary_gen_t;
345
346            return (
347                binary_gen_t::generate(
348                    nested_d[action.subject().left()[action.predicate()]],
349                    nested_d[action.subject().right()[action.predicate()]]
350                )
351            ).parse(scan);
352        }
353    };
354
355    ///////////////////////////////////////////////////////////////////////////
356    template <typename CategoryT>
357    struct attach_action_non_nested {
358
359        template <typename ParserT, typename ScannerT, typename ActionT>
360        static typename parser_result<ParserT, ScannerT>::type
361        parse(ParserT const &, ScannerT const& scan, ActionT const &action)
362        {
363            return action.parse(scan);
364        }
365    };
366
367    template <>
368    struct attach_action_non_nested<binary_parser_category> {
369
370        template <typename ParserT, typename ScannerT, typename ActionT>
371        static typename parser_result<ParserT, ScannerT>::type
372        parse(ParserT const &, ScannerT const& scan, ActionT const &action)
373        {
374            typedef
375                typename ActionT::subject_t::parser_generator_t
376                binary_gen_t;
377
378            return (
379                binary_gen_t::generate(
380                    action.subject().left()[action.predicate()],
381                    action.subject().right()[action.predicate()]
382                )
383            ).parse(scan);
384        }
385    };
386
387    ///////////////////////////////////////////////////////////////////////////
388    template <typename NestedT>
389    struct attach_action_type {
390
391        template <typename ParserT, typename ScannerT, typename ActionT>
392        static typename parser_result<ParserT, ScannerT>::type
393        parse(ParserT const &p, ScannerT const& scan, ActionT const& action,
394            NestedT const& nested_d)
395        {
396            typedef
397                typename ActionT::subject_t::parser_category_t
398                parser_category_t;
399
400            return attach_action_nested<parser_category_t>::
401                    parse(p, scan, action, nested_d);
402        }
403    };
404
405    template <>
406    struct attach_action_type<non_nested_refactoring> {
407
408        template <typename ParserT, typename ScannerT, typename ActionT>
409        static typename parser_result<ParserT, ScannerT>::type
410        parse(ParserT const &p, ScannerT const& scan, ActionT const &action,
411            non_nested_refactoring const&)
412        {
413            typedef
414                typename ActionT::subject_t::parser_category_t
415                parser_category_t;
416
417            return attach_action_non_nested<parser_category_t>::
418                parse(p, scan, action);
419        }
420    };
421
422    template <>
423    struct attach_action_type<self_nested_refactoring> {
424
425        template <typename ParserT, typename ScannerT, typename ActionT>
426        static typename parser_result<ParserT, ScannerT>::type
427        parse(ParserT const &p, ScannerT const& scan, ActionT const &action,
428            self_nested_refactoring const& nested_tag)
429        {
430            typedef typename ParserT::parser_generator_t parser_generator_t;
431            typedef
432                typename ActionT::subject_t::parser_category_t
433                parser_category_t;
434
435            parser_generator_t nested_d(nested_tag);
436            return attach_action_nested<parser_category_t>::
437                    parse(p, scan, action, nested_d);
438        }
439    };
440
441}   // namespace impl
442
443///////////////////////////////////////////////////////////////////////////////
444}} // namespace boost::spirit
445
446#endif
447
Note: See TracBrowser for help on using the repository browser.