1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
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
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/string/detail/no_case_string_parse.hpp>
15 #include <boost/spirit/home/x3/support/utility/utf8.hpp>
16 #include <boost/spirit/home/support/char_encoding/ascii.hpp>
17 #include <boost/spirit/home/support/char_encoding/standard.hpp>
18 #include <boost/spirit/home/support/char_encoding/standard_wide.hpp>
20 #include <boost/type_traits/is_same.hpp>
21 #include <boost/type_traits/add_reference.hpp>
24 namespace boost { namespace spirit { namespace x3
26 template <typename String, typename Encoding,
27 typename Attribute = std::basic_string<typename Encoding::char_type>>
28 struct literal_string : parser<literal_string<String, Encoding, Attribute>>
30 typedef typename Encoding::char_type char_type;
31 typedef Encoding encoding;
32 typedef Attribute attribute_type;
33 static bool const has_attribute =
34 !is_same<unused_type, attribute_type>::value;
35 static bool const handles_container = has_attribute;
37 literal_string(typename add_reference< typename add_const<String>::type >::type str)
41 template <typename Iterator, typename Context, typename Attribute_>
42 bool parse(Iterator& first, Iterator const& last
43 , Context const& context, unused_type, Attribute_& attr) const
45 x3::skip_over(first, last, context);
46 return detail::string_parse(str, first, last, attr, get_case_compare<encoding>(context));
54 inline literal_string<char const*, char_encoding::standard>
60 inline literal_string<std::basic_string<char>, char_encoding::standard>
61 string(std::basic_string<char> const& s)
66 inline literal_string<char const*, char_encoding::standard, unused_type>
72 template <typename Char>
73 literal_string<std::basic_string<Char>, char_encoding::standard, unused_type>
74 lit(std::basic_string<Char> const& s)
80 namespace standard_wide
82 inline literal_string<wchar_t const*, char_encoding::standard_wide>
83 string(wchar_t const* s)
88 inline literal_string<std::basic_string<wchar_t>, char_encoding::standard_wide>
89 string(std::basic_string<wchar_t> const& s)
94 inline literal_string<wchar_t const*, char_encoding::standard_wide, unused_type>
100 inline literal_string<std::basic_string<wchar_t>, char_encoding::standard_wide, unused_type>
101 lit(std::basic_string<wchar_t> const& s)
109 inline literal_string<wchar_t const*, char_encoding::ascii>
110 string(wchar_t const* s)
115 inline literal_string<std::basic_string<wchar_t>, char_encoding::ascii>
116 string(std::basic_string<wchar_t> const& s)
121 inline literal_string<char const*, char_encoding::ascii, unused_type>
127 template <typename Char>
128 literal_string<std::basic_string<Char>, char_encoding::ascii, unused_type>
129 lit(std::basic_string<Char> const& s)
137 inline literal_string<wchar_t const*, char_encoding::iso8859_1>
138 string(wchar_t const* s)
143 inline literal_string<std::basic_string<wchar_t>, char_encoding::iso8859_1>
144 string(std::basic_string<wchar_t> const& s)
149 inline literal_string<char const*, char_encoding::iso8859_1, unused_type>
155 template <typename Char>
156 literal_string<std::basic_string<Char>, char_encoding::iso8859_1, unused_type>
157 lit(std::basic_string<Char> const& s)
163 using standard::string;
165 using standard_wide::string;
166 using standard_wide::lit;
171 struct as_parser<char[N]>
173 typedef literal_string<
174 char const*, char_encoding::standard, unused_type>
177 typedef type value_type;
179 static type call(char const* s)
186 struct as_parser<char const[N]> : as_parser<char[N]> {};
189 struct as_parser<wchar_t[N]>
191 typedef literal_string<
192 wchar_t const*, char_encoding::standard_wide, unused_type>
195 typedef type value_type;
197 static type call(wchar_t const* s)
204 struct as_parser<wchar_t const[N]> : as_parser<wchar_t[N]> {};
207 struct as_parser<char const*>
209 typedef literal_string<
210 char const*, char_encoding::standard, unused_type>
213 typedef type value_type;
215 static type call(char const* s)
221 template <typename Char>
222 struct as_parser< std::basic_string<Char> >
224 typedef literal_string<
225 Char const*, char_encoding::standard, unused_type>
228 typedef type value_type;
230 static type call(std::basic_string<Char> const& s)
232 return type(s.c_str());
237 template <typename String, typename Encoding, typename Attribute>
238 struct get_info<literal_string<String, Encoding, Attribute>>
240 typedef std::string result_type;
241 std::string operator()(literal_string<String, Encoding, Attribute> const& p) const
243 return '"' + to_utf8(p.str) + '"';