summaryrefslogtreecommitdiffstats
blob: b25b25fdc06015760c84393d3b3ec931c364c505 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*=============================================================================
    Copyright (c) 1998-2003 Joel de Guzman
    Copyright (c) 2001 Daniel Nuffer
    Copyright (c) 2001 Bruce Florman
    Copyright (c) 2002 Raghavendra Satish
    http://spirit.sourceforge.net/

    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#if !defined(BOOST_SPIRIT_DIRECTIVES_IPP)
#define BOOST_SPIRIT_DIRECTIVES_IPP

///////////////////////////////////////////////////////////////////////////////
#include <boost/spirit/home/classic/core/scanner/skipper.hpp>

namespace boost { namespace spirit {

BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN

    template <typename BaseT>
    struct no_skipper_iteration_policy;

    template <typename BaseT>
    struct inhibit_case_iteration_policy;

    template <typename A, typename B>
    struct alternative;

    template <typename A, typename B>
    struct longest_alternative;

    template <typename A, typename B>
    struct shortest_alternative;

    namespace impl
    {
        template <typename RT, typename ST, typename ScannerT, typename BaseT>
        inline RT
        contiguous_parser_parse(
            ST const& s,
            ScannerT const& scan,
            skipper_iteration_policy<BaseT> const&)
        {
            typedef scanner_policies<
                no_skipper_iteration_policy<
                    BOOST_DEDUCED_TYPENAME ScannerT::iteration_policy_t>,
                BOOST_DEDUCED_TYPENAME ScannerT::match_policy_t,
                BOOST_DEDUCED_TYPENAME ScannerT::action_policy_t
            > policies_t;

            scan.skip(scan);
            RT hit = s.parse(scan.change_policies(policies_t(scan)));
            // We will not do a post skip!!!
            return hit;
        }

        template <typename RT, typename ST, typename ScannerT, typename BaseT>
        inline RT
        contiguous_parser_parse(
            ST const& s,
            ScannerT const& scan,
            no_skipper_iteration_policy<BaseT> const&)
        {
            return s.parse(scan);
        }

        template <typename RT, typename ST, typename ScannerT>
        inline RT
        contiguous_parser_parse(
            ST const& s,
            ScannerT const& scan,
            iteration_policy const&)
        {
            return s.parse(scan);
        }

        template <
            typename RT,
            typename ParserT,
            typename ScannerT,
            typename BaseT>
        inline RT
        implicit_lexeme_parse(
            ParserT const& p,
            ScannerT const& scan,
            skipper_iteration_policy<BaseT> const&)
        {
            typedef scanner_policies<
                no_skipper_iteration_policy<
                    BOOST_DEDUCED_TYPENAME ScannerT::iteration_policy_t>,
                BOOST_DEDUCED_TYPENAME ScannerT::match_policy_t,
                BOOST_DEDUCED_TYPENAME ScannerT::action_policy_t
            > policies_t;

            scan.skip(scan);
            RT hit = p.parse_main(scan.change_policies(policies_t(scan)));
            // We will not do a post skip!!!
            return hit;
        }

        template <
            typename RT,
            typename ParserT,
            typename ScannerT,
            typename BaseT>
        inline RT
        implicit_lexeme_parse(
            ParserT const& p,
            ScannerT const& scan,
            no_skipper_iteration_policy<BaseT> const&)
        {
            return p.parse_main(scan);
        }

        template <typename RT, typename ParserT, typename ScannerT>
        inline RT
        implicit_lexeme_parse(
            ParserT const& p,
            ScannerT const& scan,
            iteration_policy const&)
        {
            return p.parse_main(scan);
        }

        template <typename RT, typename ST, typename ScannerT>
        inline RT
        inhibit_case_parser_parse(
            ST const& s,
            ScannerT const& scan,
            iteration_policy const&)
        {
            typedef scanner_policies<
                inhibit_case_iteration_policy<
                    BOOST_DEDUCED_TYPENAME ScannerT::iteration_policy_t>,
                BOOST_DEDUCED_TYPENAME ScannerT::match_policy_t,
                BOOST_DEDUCED_TYPENAME ScannerT::action_policy_t
            > policies_t;

            return s.parse(scan.change_policies(policies_t(scan)));
        }

        template <typename RT, typename ST, typename ScannerT, typename BaseT>
        inline RT
        inhibit_case_parser_parse(
            ST const& s,
            ScannerT const& scan,
            inhibit_case_iteration_policy<BaseT> const&)
        {
            return s.parse(scan);
        }

#if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)

        ///////////////////////////////////////////////////////////////////////
        //
        //  from spirit 1.1 (copyright (c) 2001 Bruce Florman)
        //  various workarounds to support longest and shortest directives
        //
        ///////////////////////////////////////////////////////////////////////
        template <typename T>
        struct is_alternative
        {
        //  Determine at compile time (without partial specialization)
        //  whether a given type is an instance of the alternative<A,B>

            static T t();
            template <typename A, typename B>
            static char test_(alternative<A, B> const&);    // no implementation
            static int  test_(...);                         // no implementation
            enum { r = sizeof(char) == sizeof(test_(t())) };
            typedef mpl::bool_<r> value;
        };

        template <typename T> struct select_to_longest;

        template <typename T>
        struct to_longest_alternative
        {
            typedef typename select_to_longest<T>::result_t result_t;
            typedef typename select_to_longest<T>::plain_t  plain_t;
            typedef typename select_to_longest<T>::choose_t choose_t;
            static result_t convert(T const& a);
        };

        template <typename T>
        struct to_longest_generic
        {
            typedef T const&        result_t;
            typedef T               plain_t;
            typedef mpl::false_    choose_t;
        };

        template <typename T>
        inline T const&
        to_longest_convert(T const& a, mpl::false_)
        { return a; }

        template <typename T>
        struct to_longest_recursive
        {
            typedef typename to_longest_alternative<
                typename T::left_t>::plain_t    a_t;
            typedef typename to_longest_alternative<
                typename T::right_t>::plain_t   b_t;

            typedef longest_alternative<a_t, b_t>   result_t;

            typedef result_t    plain_t;
            typedef mpl::true_ choose_t;
        };

        template <typename A, typename B>
        inline typename to_longest_alternative<alternative<A, B> >::result_t
        to_longest_convert(alternative<A, B> const& alt, mpl::true_)
        {
            typedef typename to_longest_alternative<
                alternative<A, B> >::result_t result_t;
            return result_t(
                to_longest_alternative<A>::convert(alt.left()),
                to_longest_alternative<B>::convert(alt.right()));
        }

        template <typename T>
        inline typename to_longest_alternative<T>::result_t
        to_longest_alternative<T>::convert(T const& a)
        {
            return to_longest_convert(
                a, to_longest_alternative<T>::choose_t());
        }

        template <typename T>
        struct select_to_longest
        {
            typedef typename mpl::if_<
                is_alternative<T>           //  IF
                , to_longest_recursive<T>   //  THEN
                , to_longest_generic<T>     //  ELSE
            >::type type;

            typedef typename select_to_longest::type::result_t result_t;
            typedef typename select_to_longest::type::plain_t  plain_t;
            typedef typename select_to_longest::type::choose_t choose_t;
        };

        template <typename T> struct select_to_shortest;

        template <typename T>
        struct to_shortest_alternative
        {
            typedef typename select_to_shortest<T>::result_t    result_t;
            typedef typename select_to_shortest<T>::plain_t     plain_t;
            typedef typename select_to_shortest<T>::choose_t    choose_t;
            static result_t convert(T const& a);
        };

        template <typename T>
        struct to_shortest_generic
        {
            typedef T const&        result_t;
            typedef T               plain_t;
            typedef mpl::false_    choose_t;
        };

        template <typename T>
        inline T const&
        to_shortest_convert(T const& a, mpl::false_) { return a; }

        template <typename T>
        struct to_shortest_recursive
        {
            typedef typename to_shortest_alternative<
                typename T::left_t>::plain_t    a_t;
            typedef typename to_shortest_alternative<
                typename T::right_t>::plain_t   b_t;

            typedef shortest_alternative<a_t, b_t>  result_t;

            typedef result_t        plain_t;
            typedef mpl::true_     choose_t;
        };

        template <typename A, typename B>
        inline typename to_shortest_alternative<alternative<A, B> >::result_t
        to_shortest_convert(alternative<A, B> const& alt, mpl::true_)
        {
            typedef typename to_shortest_alternative<
                alternative<A, B> >::result_t result_t;
            return result_t(
                to_shortest_alternative<A>::convert(alt.left()),
                to_shortest_alternative<B>::convert(alt.right()));
        }

        template <typename T>
        inline typename to_shortest_alternative<T>::result_t
        to_shortest_alternative<T>::convert(T const& a)
        {
            return to_shortest_convert(
                a, to_shortest_alternative<T>::choose_t());
        }

        template <typename T>
        struct select_to_shortest
        {
            typedef typename mpl::if_<
                is_alternative<T>           //  IF
                , to_shortest_recursive<T>  //  THEN
                , to_shortest_generic<T>    //  ELSE
            >::type type;

            typedef typename select_to_shortest::type::result_t result_t;
            typedef typename select_to_shortest::type::plain_t  plain_t;
            typedef typename select_to_shortest::type::choose_t choose_t;
        };
#else
        template <typename T>
        struct to_longest_alternative
        {
            typedef T result_t;
            static result_t const&
            convert(T const& a)  //  Special (end) case
            { return a; }
        };

        template <typename A, typename B>
        struct to_longest_alternative<alternative<A, B> >
        {
            typedef typename to_longest_alternative<A>::result_t    a_t;
            typedef typename to_longest_alternative<B>::result_t    b_t;
            typedef longest_alternative<a_t, b_t>                   result_t;

            static result_t
            convert(alternative<A, B> const& alt) // Recursive case
            {
                return result_t(
                    to_longest_alternative<A>::convert(alt.left()),
                    to_longest_alternative<B>::convert(alt.right()));
            }
        };

        template <typename T>
        struct to_shortest_alternative
        {
            typedef T result_t;
            static result_t const&
            convert(T const& a) //  Special (end) case
            { return a; }
        };

        template <typename A, typename B>
        struct to_shortest_alternative<alternative<A, B> >
        {
            typedef typename to_shortest_alternative<A>::result_t   a_t;
            typedef typename to_shortest_alternative<B>::result_t   b_t;
            typedef shortest_alternative<a_t, b_t>                  result_t;

            static result_t
            convert(alternative<A, B> const& alt) //  Recursive case
            {
                return result_t(
                    to_shortest_alternative<A>::convert(alt.left()),
                    to_shortest_alternative<B>::convert(alt.right()));
            }
        };
#endif
    }

BOOST_SPIRIT_CLASSIC_NAMESPACE_END

}} // namespace boost::spirit

#endif