| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2014 Joel de Guzman |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | ==============================================================================*/ |
| 7 | #if !defined(BOOST_SPIRIT_X3_LITERAL_STRING_APR_18_2006_1125PM) |
| 8 | #define BOOST_SPIRIT_X3_LITERAL_STRING_APR_18_2006_1125PM |
| 9 | |
| 10 | #include <boost/spirit/home/x3/core/parser.hpp> |
| 11 | #include <boost/spirit/home/x3/core/skip_over.hpp> |
| 12 | #include <boost/spirit/home/x3/string/detail/string_parse.hpp> |
| 13 | #include <boost/spirit/home/x3/support/no_case.hpp> |
| 14 | #include <boost/spirit/home/x3/support/utility/utf8.hpp> |
| 15 | #include <boost/spirit/home/support/char_encoding/ascii.hpp> |
| 16 | #include <boost/spirit/home/support/char_encoding/standard.hpp> |
| 17 | #include <boost/spirit/home/support/char_encoding/standard_wide.hpp> |
| 18 | |
| 19 | #include <boost/type_traits/is_same.hpp> |
| 20 | #include <boost/type_traits/add_reference.hpp> |
| 21 | #include <string> |
| 22 | |
| 23 | namespace boost { namespace spirit { namespace x3 |
| 24 | { |
| 25 | template <typename String, typename Encoding, |
| 26 | typename Attribute = std::basic_string<typename Encoding::char_type>> |
| 27 | struct literal_string : parser<literal_string<String, Encoding, Attribute>> |
| 28 | { |
| 29 | typedef typename Encoding::char_type char_type; |
| 30 | typedef Encoding encoding; |
| 31 | typedef Attribute attribute_type; |
| 32 | static bool const has_attribute = |
| 33 | !is_same<unused_type, attribute_type>::value; |
| 34 | static bool const handles_container = has_attribute; |
| 35 | |
| 36 | constexpr literal_string(typename add_reference< typename add_const<String>::type >::type str) |
| 37 | : str(str) |
| 38 | {} |
| 39 | |
| 40 | template <typename Iterator, typename Context, typename Attribute_> |
| 41 | bool parse(Iterator& first, Iterator const& last |
| 42 | , Context const& context, unused_type, Attribute_& attr) const |
| 43 | { |
| 44 | x3::skip_over(first, last, context); |
| 45 | return detail::string_parse(str, first, last, attr, get_case_compare<encoding>(context)); |
| 46 | } |
| 47 | |
| 48 | String str; |
| 49 | }; |
| 50 | |
| 51 | namespace standard |
| 52 | { |
| 53 | constexpr literal_string<char const*, char_encoding::standard> |
| 54 | string(char const* s) |
| 55 | { |
| 56 | return { s }; |
| 57 | } |
| 58 | |
| 59 | inline literal_string<std::basic_string<char>, char_encoding::standard> |
| 60 | string(std::basic_string<char> const& s) |
| 61 | { |
| 62 | return { s }; |
| 63 | } |
| 64 | |
| 65 | inline constexpr literal_string<char const*, char_encoding::standard, unused_type> |
| 66 | lit(char const* s) |
| 67 | { |
| 68 | return { s }; |
| 69 | } |
| 70 | |
| 71 | template <typename Char> |
| 72 | literal_string<std::basic_string<Char>, char_encoding::standard, unused_type> |
| 73 | lit(std::basic_string<Char> const& s) |
| 74 | { |
| 75 | return { s }; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #ifndef BOOST_SPIRIT_NO_STANDARD_WIDE |
| 80 | namespace standard_wide |
| 81 | { |
| 82 | constexpr literal_string<wchar_t const*, char_encoding::standard_wide> |
| 83 | string(wchar_t const* s) |
| 84 | { |
| 85 | return { s }; |
| 86 | } |
| 87 | |
| 88 | inline literal_string<std::basic_string<wchar_t>, char_encoding::standard_wide> |
| 89 | string(std::basic_string<wchar_t> const& s) |
| 90 | { |
| 91 | return { s }; |
| 92 | } |
| 93 | |
| 94 | constexpr literal_string<wchar_t const*, char_encoding::standard_wide, unused_type> |
| 95 | lit(wchar_t const* s) |
| 96 | { |
| 97 | return { s }; |
| 98 | } |
| 99 | |
| 100 | inline literal_string<std::basic_string<wchar_t>, char_encoding::standard_wide, unused_type> |
| 101 | lit(std::basic_string<wchar_t> const& s) |
| 102 | { |
| 103 | return { s }; |
| 104 | } |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | #if defined(BOOST_SPIRIT_X3_UNICODE) |
| 109 | namespace unicode |
| 110 | { |
| 111 | constexpr literal_string<char32_t const*, char_encoding::unicode> |
| 112 | string(char32_t const* s) |
| 113 | { |
| 114 | return { s }; |
| 115 | } |
| 116 | |
| 117 | inline literal_string<std::basic_string<char32_t>, char_encoding::unicode> |
| 118 | string(std::basic_string<char32_t> const& s) |
| 119 | { |
| 120 | return { s }; |
| 121 | } |
| 122 | |
| 123 | constexpr literal_string<char32_t const*, char_encoding::unicode, unused_type> |
| 124 | lit(char32_t const* s) |
| 125 | { |
| 126 | return { s }; |
| 127 | } |
| 128 | |
| 129 | inline literal_string<std::basic_string<char32_t>, char_encoding::unicode, unused_type> |
| 130 | lit(std::basic_string<char32_t> const& s) |
| 131 | { |
| 132 | return { s }; |
| 133 | } |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | namespace ascii |
| 138 | { |
| 139 | constexpr literal_string<wchar_t const*, char_encoding::ascii> |
| 140 | string(wchar_t const* s) |
| 141 | { |
| 142 | return { s }; |
| 143 | } |
| 144 | |
| 145 | inline literal_string<std::basic_string<wchar_t>, char_encoding::ascii> |
| 146 | string(std::basic_string<wchar_t> const& s) |
| 147 | { |
| 148 | return { s }; |
| 149 | } |
| 150 | |
| 151 | constexpr literal_string<char const*, char_encoding::ascii, unused_type> |
| 152 | lit(char const* s) |
| 153 | { |
| 154 | return { s }; |
| 155 | } |
| 156 | |
| 157 | template <typename Char> |
| 158 | literal_string<std::basic_string<Char>, char_encoding::ascii, unused_type> |
| 159 | lit(std::basic_string<Char> const& s) |
| 160 | { |
| 161 | return { s }; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | namespace iso8859_1 |
| 166 | { |
| 167 | constexpr literal_string<wchar_t const*, char_encoding::iso8859_1> |
| 168 | string(wchar_t const* s) |
| 169 | { |
| 170 | return { s }; |
| 171 | } |
| 172 | |
| 173 | inline literal_string<std::basic_string<wchar_t>, char_encoding::iso8859_1> |
| 174 | string(std::basic_string<wchar_t> const& s) |
| 175 | { |
| 176 | return { s }; |
| 177 | } |
| 178 | |
| 179 | constexpr literal_string<char const*, char_encoding::iso8859_1, unused_type> |
| 180 | lit(char const* s) |
| 181 | { |
| 182 | return { s }; |
| 183 | } |
| 184 | |
| 185 | template <typename Char> |
| 186 | literal_string<std::basic_string<Char>, char_encoding::iso8859_1, unused_type> |
| 187 | lit(std::basic_string<Char> const& s) |
| 188 | { |
| 189 | return { s }; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | using standard::string; |
| 194 | using standard::lit; |
| 195 | #ifndef BOOST_SPIRIT_NO_STANDARD_WIDE |
| 196 | using standard_wide::string; |
| 197 | using standard_wide::lit; |
| 198 | #endif |
| 199 | |
| 200 | namespace extension |
| 201 | { |
| 202 | template <int N> |
| 203 | struct as_parser<char[N]> |
| 204 | { |
| 205 | typedef literal_string< |
| 206 | char const*, char_encoding::standard, unused_type> |
| 207 | type; |
| 208 | |
| 209 | typedef type value_type; |
| 210 | |
| 211 | static constexpr type call(char const* s) |
| 212 | { |
| 213 | return type(s); |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | template <int N> |
| 218 | struct as_parser<char const[N]> : as_parser<char[N]> {}; |
| 219 | |
| 220 | #ifndef BOOST_SPIRIT_NO_STANDARD_WIDE |
| 221 | template <int N> |
| 222 | struct as_parser<wchar_t[N]> |
| 223 | { |
| 224 | typedef literal_string< |
| 225 | wchar_t const*, char_encoding::standard_wide, unused_type> |
| 226 | type; |
| 227 | |
| 228 | typedef type value_type; |
| 229 | |
| 230 | static constexpr type call(wchar_t const* s) |
| 231 | { |
| 232 | return type(s); |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | template <int N> |
| 237 | struct as_parser<wchar_t const[N]> : as_parser<wchar_t[N]> {}; |
| 238 | #endif |
| 239 | |
| 240 | template <> |
| 241 | struct as_parser<char const*> |
| 242 | { |
| 243 | typedef literal_string< |
| 244 | char const*, char_encoding::standard, unused_type> |
| 245 | type; |
| 246 | |
| 247 | typedef type value_type; |
| 248 | |
| 249 | static constexpr type call(char const* s) |
| 250 | { |
| 251 | return type(s); |
| 252 | } |
| 253 | }; |
| 254 | |
| 255 | template <typename Char> |
| 256 | struct as_parser< std::basic_string<Char> > |
| 257 | { |
| 258 | typedef literal_string< |
| 259 | Char const*, char_encoding::standard, unused_type> |
| 260 | type; |
| 261 | |
| 262 | typedef type value_type; |
| 263 | |
| 264 | static type call(std::basic_string<Char> const& s) |
| 265 | { |
| 266 | return type(s.c_str()); |
| 267 | } |
| 268 | }; |
| 269 | } |
| 270 | |
| 271 | template <typename String, typename Encoding, typename Attribute> |
| 272 | struct get_info<literal_string<String, Encoding, Attribute>> |
| 273 | { |
| 274 | typedef std::string result_type; |
| 275 | std::string operator()(literal_string<String, Encoding, Attribute> const& p) const |
| 276 | { |
| 277 | return '"' + to_utf8(p.str) + '"'; |
| 278 | } |
| 279 | }; |
| 280 | }}} |
| 281 | |
| 282 | #endif |
| 283 | |