summaryrefslogtreecommitdiff
path: root/boost/spirit/home/x3/numeric/bool.hpp
blob: 1fd7018b0fddd3ccb44027934b81c70b1c90d314 (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
/*=============================================================================
    Copyright (c) 2009  Hartmut Kaiser
    Copyright (c) 2014  Joel de Guzman

    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_X3_BOOL_SEP_29_2009_0709AM)
#define SPIRIT_X3_BOOL_SEP_29_2009_0709AM

#include <boost/spirit/home/x3/core/parser.hpp>
#include <boost/spirit/home/x3/core/skip_over.hpp>
#include <boost/spirit/home/x3/numeric/bool_policies.hpp>

namespace boost { namespace spirit { namespace x3
{
    template <typename T, typename Encoding, typename BoolPolicies = bool_policies<T>>
    struct bool_parser : parser<bool_parser<T, Encoding, BoolPolicies>>
    {
        typedef Encoding encoding;
        typedef T attribute_type;
        static bool const has_attribute = true;

        bool_parser()
        	: policies() {}

        bool_parser(BoolPolicies const& policies)
        	: policies(policies) {}

        template <typename Iterator, typename Context>
        bool parse(Iterator& first, Iterator const& last
          , Context const& context, unused_type, T& attr) const
        {
            x3::skip_over(first, last, context);
            return policies.parse_true(first, last, attr, get_case_compare<encoding>(context))
                || policies.parse_false(first, last, attr, get_case_compare<encoding>(context));
        }

        template <typename Iterator, typename Context, typename Attribute>
        bool parse(Iterator& first, Iterator const& last
          , Context const& context, unused_type, Attribute& attr_param) const
        {
            // this case is called when Attribute is not T
            T attr_;
            if (parse(first, last, context, unused, attr_))
            {
                traits::move_to(attr_, attr_param);
                return true;
            }
            return false;
        }

        BoolPolicies policies;
    };

    template <typename T, typename Encoding, typename BoolPolicies = bool_policies<T>>
    struct literal_bool_parser : parser<bool_parser<T, Encoding, BoolPolicies>>
    {
        typedef Encoding encoding;
        typedef T attribute_type;
        static bool const has_attribute = true;

        template <typename Value>
        literal_bool_parser(Value const& n)
        	: policies(), n_(n) {}

        template <typename Value>
        literal_bool_parser(Value const& n, BoolPolicies const& policies)
        	: policies(policies), n_(n) {}

        template <typename Iterator, typename Context>
        bool parse_main(Iterator& first, Iterator const& last
          , Context& context, T& attr) const
        {
            x3::skip_over(first, last, context);
            return (n_ && policies.parse_true(first, last, attr, get_case_compare<encoding>(context)))
                || (!n_ && policies.parse_false(first, last, attr, get_case_compare<encoding>(context)));
        }

        template <typename Iterator, typename Context>
        bool parse(Iterator& first, Iterator const& last
          , Context& context, unused_type, T& attr) const
        {
            return parse_main(first, last, context, attr);
        }

        template <typename Iterator, typename Context, typename Attribute>
        bool parse(Iterator& first, Iterator const& last
          , Context const& context, unused_type, Attribute& attr_param) const
        {
            // this case is called when Attribute is not T
            T attr_;
            if (parse_main(first, last, context, attr_))
            {
                traits::move_to(attr_, attr_param);
                return true;
            }
            return false;
        }

        BoolPolicies policies;
        T n_;
    };

    namespace standard
    {
        typedef bool_parser<bool, char_encoding::standard> bool_type;
        bool_type const bool_ = {};

        typedef literal_bool_parser<bool, char_encoding::standard> true_type;
        true_type const true_ = { true };

        typedef literal_bool_parser<bool, char_encoding::standard> false_type;
        false_type const false_ = { false };
    }

    namespace standard_wide
    {
        typedef bool_parser<bool, char_encoding::standard_wide> bool_type;
        bool_type const bool_ = {};

        typedef literal_bool_parser<bool, char_encoding::standard_wide> true_type;
        true_type const true_ = { true };

        typedef literal_bool_parser<bool, char_encoding::standard_wide> false_type;
        false_type const false_ = { false };
    }

    namespace ascii
    {
        typedef bool_parser<bool, char_encoding::ascii> bool_type;
        bool_type const bool_ = {};

        typedef literal_bool_parser<bool, char_encoding::ascii> true_type;
        true_type const true_ = { true };

        typedef literal_bool_parser<bool, char_encoding::ascii> false_type;
        false_type const false_ = { false };
    }

    namespace iso8859_1
    {
        typedef bool_parser<bool, char_encoding::iso8859_1> bool_type;
        bool_type const bool_ = {};

        typedef literal_bool_parser<bool, char_encoding::iso8859_1> true_type;
        true_type const true_ = { true };

        typedef literal_bool_parser<bool, char_encoding::iso8859_1> false_type;
        false_type const false_ = { false };
    }

    using standard::bool_;
    using standard::true_;
    using standard::false_;

    }}}

#endif