literal_string.hpp 8.0 KB

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