summaryrefslogtreecommitdiff
path: root/boost/spirit/home/qi/numeric/bool.hpp
blob: 74e134391834c20a60a2157ec425dd43c3b199fa (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
/*=============================================================================
    Copyright (c) 2001-2011 Hartmut Kaiser
    Copyright (c) 2011      Bryce Lelbach

    Distributed under 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(SPIRIT_QI_BOOL_SEP_29_2009_0709AM)
#define SPIRIT_QI_BOOL_SEP_29_2009_0709AM

#if defined(_MSC_VER)
#pragma once
#endif

#include <boost/spirit/home/qi/skip_over.hpp>
#include <boost/spirit/home/qi/detail/enable_lit.hpp>
#include <boost/spirit/home/qi/meta_compiler.hpp>
#include <boost/spirit/home/qi/parser.hpp>
#include <boost/spirit/home/qi/numeric/bool_policies.hpp>
#include <boost/spirit/home/support/common_terminals.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/type_traits/is_same.hpp>

namespace boost { namespace spirit
{
    namespace qi
    {
        ///////////////////////////////////////////////////////////////////////
        // forward declaration only
        template <typename T>
        struct bool_policies;

        ///////////////////////////////////////////////////////////////////////
        // This is the class that the user can instantiate directly in
        // order to create a customized bool parser
        template <typename T, typename BoolPolicies = bool_policies<T> >
        struct bool_parser
          : spirit::terminal<tag::stateful_tag<BoolPolicies, tag::bool_, T> >
        {
            typedef tag::stateful_tag<BoolPolicies, tag::bool_, T> tag_type;

            bool_parser() {}
            bool_parser(BoolPolicies const& data)
              : spirit::terminal<tag_type>(data) {}
        };
    }

    ///////////////////////////////////////////////////////////////////////////
    // Enablers
    ///////////////////////////////////////////////////////////////////////////
    template <> // enables bool_
    struct use_terminal<qi::domain, tag::bool_>
      : mpl::true_ {};

    template <> // enables true_
    struct use_terminal<qi::domain, tag::true_>
      : mpl::true_ {};

    template <> // enables false_
    struct use_terminal<qi::domain, tag::false_>
      : mpl::true_ {};

    ///////////////////////////////////////////////////////////////////////////
    template <typename A0> // enables lit(...)
    struct use_terminal<qi::domain
        , terminal_ex<tag::lit, fusion::vector1<A0> >
        , typename enable_if<is_same<A0, bool> >::type>
      : mpl::true_ {};

    ///////////////////////////////////////////////////////////////////////////
    template <typename A0> // enables bool_(...)
    struct use_terminal<qi::domain
      , terminal_ex<tag::bool_, fusion::vector1<A0> >
    > : mpl::true_ {};

    template <> // enables *lazy* bool_(...)
    struct use_lazy_terminal<qi::domain, tag::bool_, 1>
      : mpl::true_ {};

    ///////////////////////////////////////////////////////////////////////////
    // enables any custom bool_parser
    template <typename T, typename BoolPolicies>
    struct use_terminal<qi::domain
        , tag::stateful_tag<BoolPolicies, tag::bool_, T> >
      : mpl::true_ {};

    // enables any custom bool_parser(...)
    template <typename T, typename BoolPolicies, typename A0>
    struct use_terminal<qi::domain
        , terminal_ex<tag::stateful_tag<BoolPolicies, tag::bool_, T>
        , fusion::vector1<A0> > >
      : mpl::true_ {};

    // enables *lazy* custom bool_parser(...)
    template <typename T, typename BoolPolicies>
    struct use_lazy_terminal<
        qi::domain
      , tag::stateful_tag<BoolPolicies, tag::bool_, T>
      , 1 // arity
    > : mpl::true_ {};
}}

namespace boost { namespace spirit { namespace qi
{
#ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
    using spirit::bool_;
    using spirit::true_;
    using spirit::false_;
    using spirit::lit;    // lit(true) is equivalent to true
#endif
    using spirit::bool_type;
    using spirit::true_type;
    using spirit::false_type;
    using spirit::lit_type;

    namespace detail
    {
        template <typename T, typename BoolPolicies>
        struct bool_impl
        {
            template <typename Iterator, typename Attribute>
            static bool parse(Iterator& first, Iterator const& last
              , Attribute& attr, BoolPolicies const& p, bool allow_true = true
              , bool disallow_false = false)
            {
                if (first == last)
                    return false;

#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
                p; // suppresses warning: C4100: 'p' : unreferenced formal parameter
#endif
                return (allow_true && p.parse_true(first, last, attr)) ||
                       (!disallow_false && p.parse_false(first, last, attr));
            }
        };
    }

    ///////////////////////////////////////////////////////////////////////////
    // This actual boolean parser
    ///////////////////////////////////////////////////////////////////////////
    template <typename T, typename BoolPolicies = bool_policies<T> >
    struct any_bool_parser
      : primitive_parser<any_bool_parser<T, BoolPolicies> >
    {
        template <typename Context, typename Iterator>
        struct attribute
        {
            typedef T type;
        };

        template <typename Iterator, typename Context
          , typename Skipper, typename Attribute>
        bool parse(Iterator& first, Iterator const& last
          , Context& /*context*/, Skipper const& skipper
          , Attribute& attr_) const
        {
            typedef detail::bool_impl<T, BoolPolicies> extract;
            qi::skip_over(first, last, skipper);
            return extract::parse(first, last, attr_, BoolPolicies());
        }

        template <typename Context>
        info what(Context& /*context*/) const
        {
            return info("boolean");
        }
    };

    template <typename T, typename BoolPolicies = bool_policies<T>
            , bool no_attribute = true>
    struct literal_bool_parser
      : primitive_parser<literal_bool_parser<T, BoolPolicies, no_attribute> >
    {
        template <typename Value>
        literal_bool_parser(Value const& n) : n_(n) {}

        template <typename Context, typename Iterator>
        struct attribute
          : mpl::if_c<no_attribute, unused_type, T>
        {};

        template <typename Iterator, typename Context
          , typename Skipper, typename Attribute>
        bool parse(Iterator& first, Iterator const& last
          , Context& /*context*/, Skipper const& skipper
          , Attribute& attr_) const
        {
            typedef detail::bool_impl<T, BoolPolicies> extract;
            qi::skip_over(first, last, skipper);
            return extract::parse(first, last, attr_, BoolPolicies(), n_, n_);
        }

        template <typename Context>
        info what(Context& /*context*/) const
        {
            return info("boolean");
        }

        T n_;
    };

    ///////////////////////////////////////////////////////////////////////////
    // Parser generators: make_xxx function (objects)
    ///////////////////////////////////////////////////////////////////////////
    template <typename T, typename Modifiers
            , typename Policies = bool_policies<T> >
    struct make_bool
    {
        typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> >
            no_case;

        typedef typename mpl::if_<
            mpl::and_<
                no_case
              , is_same<bool_policies<T>, Policies>
            >
          , any_bool_parser<T, no_case_bool_policies<T> >
          , any_bool_parser<T, Policies> >::type
        result_type;

        result_type operator()(unused_type, unused_type) const
        {
            return result_type();
        }
    };

    template <typename T, typename Modifiers
            , typename Policies = bool_policies<T> >
    struct make_direct_bool
    {
        typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> >
            no_case;

        typedef typename mpl::if_<
            mpl::and_<
                no_case
              , is_same<bool_policies<T>, Policies>
            >
          , literal_bool_parser<T, no_case_bool_policies<T>, false>
          , literal_bool_parser<T, Policies, false> >::type
        result_type;

        template <typename Terminal>
        result_type operator()(Terminal const& term, unused_type) const
        {
            return result_type(fusion::at_c<0>(term.args));
        }
    };

    template <typename T, typename Modifiers, bool b
            , typename Policies = bool_policies<T> >
    struct make_predefined_direct_bool
    {
        typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> >
            no_case;

        typedef typename mpl::if_<
            mpl::and_<
                no_case
              , is_same<bool_policies<T>, Policies>
            >
          , literal_bool_parser<T, no_case_bool_policies<T>, false>
          , literal_bool_parser<T, Policies, false> >::type
        result_type;

        result_type operator()(unused_type, unused_type) const
        {
            return result_type(b);
        }
    };

    template <typename T, typename Modifiers
            , typename Policies = bool_policies<T> >
    struct make_literal_bool
    {
        typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> >
            no_case;

        typedef typename mpl::if_<
            mpl::and_<
                no_case
              , is_same<bool_policies<T>, Policies>
            >
          , literal_bool_parser<T, no_case_bool_policies<T> >
          , literal_bool_parser<T, Policies> >::type
        result_type;

        template <typename Terminal>
        result_type operator()(Terminal const& term, unused_type) const
        {
            return result_type(fusion::at_c<0>(term.args));
        }
    };

    ///////////////////////////////////////////////////////////////////////////
    template <typename Modifiers, typename A0>
    struct make_primitive<
          terminal_ex<tag::lit, fusion::vector1<A0> >
        , Modifiers, typename enable_if<is_same<A0, bool> >::type>
      : make_literal_bool<bool, Modifiers> {};

    ///////////////////////////////////////////////////////////////////////////
    template <typename Modifiers>
    struct make_primitive<tag::false_, Modifiers>
      : make_predefined_direct_bool<bool, Modifiers, false>
    {};

    template <typename Modifiers>
    struct make_primitive<tag::true_, Modifiers>
      : make_predefined_direct_bool<bool, Modifiers, true>
    {};

    ///////////////////////////////////////////////////////////////////////////
    template <typename T, typename Policies, typename Modifiers>
    struct make_primitive<
        tag::stateful_tag<Policies, tag::bool_, T>, Modifiers>
      : make_bool<T, Modifiers, Policies> {};

    template <typename T, typename Policies, typename A0, typename Modifiers>
    struct make_primitive<
        terminal_ex<tag::stateful_tag<Policies, tag::bool_, T>
      , fusion::vector1<A0> >, Modifiers>
      : make_direct_bool<T, Modifiers, Policies> {};

    ///////////////////////////////////////////////////////////////////////////
    template <typename Modifiers>
    struct make_primitive<tag::bool_, Modifiers>
      : make_bool<bool, Modifiers> {};

    template <typename Modifiers, typename A0>
    struct make_primitive<
        terminal_ex<tag::bool_
      , fusion::vector1<A0> >, Modifiers>
      : make_direct_bool<bool, Modifiers> {};
}}}

#endif