summaryrefslogtreecommitdiff
path: root/boost/spirit/home/x3/string/literal_string.hpp
blob: 5066fe18a9ec83f6eece67d5daa55530f500e0e9 (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
/*=============================================================================
    Copyright (c) 2001-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(BOOST_SPIRIT_X3_LITERAL_STRING_APR_18_2006_1125PM)
#define BOOST_SPIRIT_X3_LITERAL_STRING_APR_18_2006_1125PM

#include <boost/spirit/home/x3/core/parser.hpp>
#include <boost/spirit/home/x3/core/skip_over.hpp>
#include <boost/spirit/home/x3/string/detail/string_parse.hpp>
#include <boost/spirit/home/x3/support/no_case.hpp>
#include <boost/spirit/home/x3/string/detail/no_case_string_parse.hpp>
#include <boost/spirit/home/x3/support/utility/utf8.hpp>
#include <boost/spirit/home/support/char_encoding/ascii.hpp>
#include <boost/spirit/home/support/char_encoding/standard.hpp>
#include <boost/spirit/home/support/char_encoding/standard_wide.hpp>

#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <string>

namespace boost { namespace spirit { namespace x3
{
    template <typename String, typename Encoding,
        typename Attribute = std::basic_string<typename Encoding::char_type>>
    struct literal_string : parser<literal_string<String, Encoding, Attribute>>
    {
        typedef typename Encoding::char_type char_type;
        typedef Encoding encoding;
        typedef Attribute attribute_type;
        static bool const has_attribute =
            !is_same<unused_type, attribute_type>::value;
        static bool const handles_container = has_attribute;

        literal_string(typename add_reference< typename add_const<String>::type >::type str)
          : str(str)
        {}

        template <typename Iterator, typename Context, typename Attribute_>
        bool parse(Iterator& first, Iterator const& last
          , Context const& context, unused_type, Attribute_& attr) const
        {
            x3::skip_over(first, last, context);
            return detail::string_parse(str, first, last, attr, get_case_compare<encoding>(context));
        }

        String str;
    };

    namespace standard
    {
        inline literal_string<char const*, char_encoding::standard>
        string(char const* s)
        {
            return { s };
        }

        inline literal_string<std::basic_string<char>, char_encoding::standard>
        string(std::basic_string<char> const& s)
        {
            return { s };
        }

        inline literal_string<char const*, char_encoding::standard, unused_type>
        lit(char const* s)
        {
            return { s };
        }

        template <typename Char>
        literal_string<std::basic_string<Char>, char_encoding::standard, unused_type>
        lit(std::basic_string<Char> const& s)
        {
            return { s };
        }
    }

    namespace standard_wide
    {
        inline literal_string<wchar_t const*, char_encoding::standard_wide>
        string(wchar_t const* s)
        {
            return { s };
        }

        inline literal_string<std::basic_string<wchar_t>, char_encoding::standard_wide>
        string(std::basic_string<wchar_t> const& s)
        {
            return { s };
        }

        inline literal_string<wchar_t const*, char_encoding::standard_wide, unused_type>
        lit(wchar_t const* s)
        {
            return { s };
        }

        inline literal_string<std::basic_string<wchar_t>, char_encoding::standard_wide, unused_type>
        lit(std::basic_string<wchar_t> const& s)
        {
            return { s };
        }
    }

    namespace ascii
    {
        inline literal_string<wchar_t const*, char_encoding::ascii>
        string(wchar_t const* s)
        {
            return { s };
        }

        inline literal_string<std::basic_string<wchar_t>, char_encoding::ascii>
        string(std::basic_string<wchar_t> const& s)
        {
            return { s };
        }

        inline literal_string<char const*, char_encoding::ascii, unused_type>
        lit(char const* s)
        {
            return { s };
        }

        template <typename Char>
        literal_string<std::basic_string<Char>, char_encoding::ascii, unused_type>
        lit(std::basic_string<Char> const& s)
        {
            return { s };
        }
    }

    namespace iso8859_1
    {
        inline literal_string<wchar_t const*, char_encoding::iso8859_1>
        string(wchar_t const* s)
        {
            return { s };
        }

        inline literal_string<std::basic_string<wchar_t>, char_encoding::iso8859_1>
        string(std::basic_string<wchar_t> const& s)
        {
            return { s };
        }

        inline literal_string<char const*, char_encoding::iso8859_1, unused_type>
        lit(char const* s)
        {
            return { s };
        }

        template <typename Char>
        literal_string<std::basic_string<Char>, char_encoding::iso8859_1, unused_type>
        lit(std::basic_string<Char> const& s)
        {
            return { s };
        }
    }

    using standard::string;
    using standard::lit;
    using standard_wide::string;
    using standard_wide::lit;

    namespace extension
    {
        template <int N>
        struct as_parser<char[N]>
        {
            typedef literal_string<
                char const*, char_encoding::standard, unused_type>
            type;

            typedef type value_type;

            static type call(char const* s)
            {
                return type(s);
            }
        };

        template <int N>
        struct as_parser<char const[N]> : as_parser<char[N]> {};

        template <int N>
        struct as_parser<wchar_t[N]>
        {
            typedef literal_string<
                wchar_t const*, char_encoding::standard_wide, unused_type>
            type;

            typedef type value_type;

            static type call(wchar_t const* s)
            {
                return type(s);
            }
        };

        template <int N>
        struct as_parser<wchar_t const[N]> : as_parser<wchar_t[N]> {};

        template <>
        struct as_parser<char const*>
        {
            typedef literal_string<
                char const*, char_encoding::standard, unused_type>
            type;

            typedef type value_type;

            static type call(char const* s)
            {
                return type(s);
            }
        };

        template <typename Char>
        struct as_parser< std::basic_string<Char> >
        {
            typedef literal_string<
                Char const*, char_encoding::standard, unused_type>
            type;

            typedef type value_type;

            static type call(std::basic_string<Char> const& s)
            {
                return type(s.c_str());
            }
        };
    }

    template <typename String, typename Encoding, typename Attribute>
    struct get_info<literal_string<String, Encoding, Attribute>>
    {
        typedef std::string result_type;
        std::string operator()(literal_string<String, Encoding, Attribute> const& p) const
        {
            return '"' + to_utf8(p.str) + '"';
        }
    };
}}}

#endif