summaryrefslogtreecommitdiff
path: root/boost/spirit/home/qi/numeric/uint.hpp
blob: ea763d356c21b86fc7484f627b74bc317e42deec (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*=============================================================================
    Copyright (c) 2001-2011 Joel de Guzman
    Copyright (c) 2011 Bryce Lelbach
    Copyright (c) 2011 Jan Frederick Eick

    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_UINT_APR_17_2006_0901AM)
#define SPIRIT_UINT_APR_17_2006_0901AM

#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/numeric/numeric_utils.hpp>
#include <boost/spirit/home/qi/meta_compiler.hpp>
#include <boost/spirit/home/qi/parser.hpp>
#include <boost/spirit/home/support/common_terminals.hpp>
#include <boost/spirit/home/support/info.hpp>
#include <boost/spirit/home/support/detail/is_spirit_tag.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/type_traits/is_same.hpp>

namespace boost { namespace spirit
{
    namespace tag
    {
        template <typename T, unsigned Radix, unsigned MinDigits
                , int MaxDigits>
        struct uint_parser
        {
            BOOST_SPIRIT_IS_TAG()
        };
    }

    namespace qi
    {
        ///////////////////////////////////////////////////////////////////////
        // This one is the class that the user can instantiate directly in
        // order to create a customized int parser
        template <typename T = int, unsigned Radix = 10, unsigned MinDigits = 1
                , int MaxDigits = -1>
        struct uint_parser
          : spirit::terminal<tag::uint_parser<T, Radix, MinDigits, MaxDigits> >
        {};
    }

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

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

    template <typename A0> // enables ushort_(n)
    struct use_terminal<qi::domain
        , terminal_ex<tag::ushort_, fusion::vector1<A0> > >
      : is_arithmetic<A0> {};

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

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

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

    template <typename A0> // enables uint_(n)
    struct use_terminal<qi::domain
        , terminal_ex<tag::uint_, fusion::vector1<A0> > >
      : is_arithmetic<A0> {};

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

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

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

    template <typename A0> // enables ulong_(n)
    struct use_terminal<qi::domain
        , terminal_ex<tag::ulong_, fusion::vector1<A0> > >
      : is_arithmetic<A0> {};

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

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

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

    template <typename A0> // enables ulong_long(n)
    struct use_terminal<qi::domain
        , terminal_ex<tag::ulong_long, fusion::vector1<A0> > >
      : is_arithmetic<A0> {};

    template <> // enables *lazy* ulong_long(n)
    struct use_lazy_terminal<qi::domain, tag::ulong_long, 1> : mpl::true_ {};
#endif

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

    template <typename A0> // enables bin(n)
    struct use_terminal<qi::domain
        , terminal_ex<tag::bin, fusion::vector1<A0> > >
      : is_arithmetic<A0> {};

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

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

    template <typename A0> // enables oct(n)
    struct use_terminal<qi::domain
        , terminal_ex<tag::oct, fusion::vector1<A0> > >
      : is_arithmetic<A0> {};

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

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

    template <typename A0> // enables hex(n)
    struct use_terminal<qi::domain
        , terminal_ex<tag::hex, fusion::vector1<A0> > >
      : is_arithmetic<A0> {};

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

    ///////////////////////////////////////////////////////////////////////////
    // enables any custom uint_parser
    template <typename T, unsigned Radix, unsigned MinDigits
            , int MaxDigits>
    struct use_terminal<qi::domain
        , tag::uint_parser<T, Radix, MinDigits, MaxDigits> >
      : mpl::true_ {};

    // enables any custom uint_parser(n)
    template <typename T, unsigned Radix, unsigned MinDigits
            , int MaxDigits, typename A0>
    struct use_terminal<qi::domain
        , terminal_ex<tag::uint_parser<T, Radix, MinDigits, MaxDigits>
                  , fusion::vector1<A0> >
    > : mpl::true_ {};

    // enables *lazy* custom uint_parser(n)
    template <typename T, unsigned Radix, unsigned MinDigits
            , int MaxDigits>
    struct use_lazy_terminal<qi::domain
      , tag::uint_parser<T, Radix, MinDigits, MaxDigits>, 1
    > : mpl::true_ {};
}}

namespace boost { namespace spirit { namespace qi
{
#ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
    using spirit::bin;
    using spirit::oct;
    using spirit::hex;

    using spirit::ushort_;
    using spirit::uint_;
    using spirit::ulong_;
#ifdef BOOST_HAS_LONG_LONG
    using spirit::ulong_long;
#endif
    using spirit::lit; // lit(1) is equivalent to 1
#endif

    using spirit::bin_type;
    using spirit::oct_type;
    using spirit::hex_type;

    using spirit::ushort_type;
    using spirit::uint_type;
    using spirit::ulong_type;
#ifdef BOOST_HAS_LONG_LONG
    using spirit::ulong_long_type;
#endif
    using spirit::lit_type;

    ///////////////////////////////////////////////////////////////////////////
    // This is the actual uint parser
    ///////////////////////////////////////////////////////////////////////////
    template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
            , int MaxDigits = -1>
    struct any_uint_parser
      : primitive_parser<any_uint_parser<T, Radix, MinDigits, MaxDigits> >
    {
        // check template parameter 'Radix' for validity
        BOOST_SPIRIT_ASSERT_MSG(
            Radix >= 2 && Radix <= 36,
            not_supported_radix, ());

        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 extract_uint<T, Radix, MinDigits, MaxDigits> extract;
            qi::skip_over(first, last, skipper);
            return extract::call(first, last, attr);
        }

        template <typename Context>
        info what(Context& /*context*/) const
        {
            return info("unsigned-integer");
        }
    };
    //]

    template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
            , int MaxDigits = -1, bool no_attribute = true>
    struct literal_uint_parser
      : primitive_parser<literal_uint_parser<T, Radix, MinDigits, MaxDigits
        , no_attribute> >
    {
        // check template parameter 'Radix' for validity
        BOOST_SPIRIT_ASSERT_MSG(
            Radix == 2 || Radix == 8 || Radix == 10 || Radix == 16,
            not_supported_radix, ());

        template <typename Value>
        literal_uint_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 extract_uint<T, Radix, MinDigits, MaxDigits> extract;
            qi::skip_over(first, last, skipper);

            Iterator save = first;
            T attr_;

            if (extract::call(first, last, attr_) && (attr_ == n_))
            {
                traits::assign_to(attr_, attr);
                return true;
            }

            first = save;
            return false;
        }

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

        T n_;
    };

    ///////////////////////////////////////////////////////////////////////////
    // Parser generators: make_xxx function (objects)
    ///////////////////////////////////////////////////////////////////////////
    template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
            , int MaxDigits = -1>
    struct make_uint
    {
        typedef any_uint_parser<T, Radix, MinDigits, MaxDigits> result_type;
        result_type operator()(unused_type, unused_type) const
        {
            return result_type();
        }
    };

    template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
            , int MaxDigits = -1>
    struct make_direct_uint
    {
        typedef literal_uint_parser<T, Radix, MinDigits, MaxDigits, false>
            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, unsigned Radix = 10, unsigned MinDigits = 1
            , int MaxDigits = -1>
    struct make_literal_uint
    {
        typedef literal_uint_parser<T, Radix, MinDigits, MaxDigits> 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, unsigned short> >::type>
      : make_literal_uint<unsigned short> {};

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

    template <typename Modifiers, typename A0>
    struct make_primitive<
          terminal_ex<tag::lit, fusion::vector1<A0> >
        , Modifiers, typename enable_if<is_same<A0, unsigned long> >::type>
      : make_literal_uint<unsigned long> {};

#ifdef BOOST_HAS_LONG_LONG
    template <typename Modifiers, typename A0>
    struct make_primitive<
          terminal_ex<tag::lit, fusion::vector1<A0> >
        , Modifiers, typename enable_if<is_same<A0, boost::ulong_long_type> >::type>
      : make_literal_uint<boost::ulong_long_type> {};
#endif

    ///////////////////////////////////////////////////////////////////////////
    template <typename T, unsigned Radix, unsigned MinDigits, int MaxDigits
            , typename Modifiers>
    struct make_primitive<
        tag::uint_parser<T, Radix, MinDigits, MaxDigits>
      , Modifiers>
      : make_uint<T, Radix, MinDigits, MaxDigits> {};

    template <typename T, unsigned Radix, unsigned MinDigits, int MaxDigits
            , typename A0, typename Modifiers>
    struct make_primitive<
        terminal_ex<tag::uint_parser<T, Radix, MinDigits, MaxDigits>
      , fusion::vector1<A0> >, Modifiers>
      : make_direct_uint<T, Radix, MinDigits, MaxDigits> {};

    ///////////////////////////////////////////////////////////////////////////
    template <typename Modifiers>
    struct make_primitive<tag::bin, Modifiers>
      : make_uint<unsigned, 2> {};

    template <typename Modifiers, typename A0>
    struct make_primitive<
        terminal_ex<tag::bin
      , fusion::vector1<A0> > , Modifiers>
      : make_direct_uint<unsigned, 2> {};

    ///////////////////////////////////////////////////////////////////////////
    template <typename Modifiers>
    struct make_primitive<tag::oct, Modifiers>
      : make_uint<unsigned, 8> {};

    template <typename Modifiers, typename A0>
    struct make_primitive<
        terminal_ex<tag::oct
      , fusion::vector1<A0> > , Modifiers>
      : make_direct_uint<unsigned, 8> {};

    ///////////////////////////////////////////////////////////////////////////
    template <typename Modifiers>
    struct make_primitive<tag::hex, Modifiers>
      : make_uint<unsigned, 16> {};

    template <typename Modifiers, typename A0>
    struct make_primitive<
        terminal_ex<tag::hex
      , fusion::vector1<A0> > , Modifiers>
      : make_direct_uint<unsigned, 16> {};

    ///////////////////////////////////////////////////////////////////////////
    template <typename Modifiers>
    struct make_primitive<tag::ushort_, Modifiers>
      : make_uint<unsigned short> {};

    template <typename Modifiers, typename A0>
    struct make_primitive<
        terminal_ex<tag::ushort_
      , fusion::vector1<A0> > , Modifiers>
      : make_direct_uint<unsigned short> {};

    ///////////////////////////////////////////////////////////////////////////
    template <typename Modifiers>
    struct make_primitive<tag::uint_, Modifiers>
      : make_uint<unsigned> {};

    template <typename Modifiers, typename A0>
    struct make_primitive<
        terminal_ex<tag::uint_
      , fusion::vector1<A0> > , Modifiers>
      : make_direct_uint<unsigned> {};

    ///////////////////////////////////////////////////////////////////////////
    template <typename Modifiers>
    struct make_primitive<tag::ulong_, Modifiers>
      : make_uint<unsigned long> {};

    template <typename Modifiers, typename A0>
    struct make_primitive<
        terminal_ex<tag::ulong_
      , fusion::vector1<A0> > , Modifiers>
      : make_direct_uint<unsigned long> {};

    ///////////////////////////////////////////////////////////////////////////
#ifdef BOOST_HAS_LONG_LONG
    template <typename Modifiers>
    struct make_primitive<tag::ulong_long, Modifiers>
      : make_uint<boost::ulong_long_type> {};

    template <typename Modifiers, typename A0>
    struct make_primitive<
        terminal_ex<tag::ulong_long
      , fusion::vector1<A0> > , Modifiers>
      : make_direct_uint<boost::ulong_long_type> {};
#endif
}}}

#endif