literal_string.hpp 8.0 KB

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