literal_string.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. namespace ascii
  94. {
  95. inline literal_string<wchar_t const*, char_encoding::ascii>
  96. string(wchar_t const* s)
  97. {
  98. return { s };
  99. }
  100. inline literal_string<std::basic_string<wchar_t>, char_encoding::ascii>
  101. string(std::basic_string<wchar_t> const& s)
  102. {
  103. return { s };
  104. }
  105. inline literal_string<char const*, char_encoding::ascii, unused_type>
  106. lit(char const* s)
  107. {
  108. return { s };
  109. }
  110. template <typename Char>
  111. literal_string<std::basic_string<Char>, char_encoding::ascii, unused_type>
  112. lit(std::basic_string<Char> const& s)
  113. {
  114. return { s };
  115. }
  116. }
  117. namespace iso8859_1
  118. {
  119. inline literal_string<wchar_t const*, char_encoding::iso8859_1>
  120. string(wchar_t const* s)
  121. {
  122. return { s };
  123. }
  124. inline literal_string<std::basic_string<wchar_t>, char_encoding::iso8859_1>
  125. string(std::basic_string<wchar_t> const& s)
  126. {
  127. return { s };
  128. }
  129. inline literal_string<char const*, char_encoding::iso8859_1, 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::iso8859_1, unused_type>
  136. lit(std::basic_string<Char> const& s)
  137. {
  138. return { s };
  139. }
  140. }
  141. using standard::string;
  142. using standard::lit;
  143. #ifndef BOOST_SPIRIT_NO_STANDARD_WIDE
  144. using standard_wide::string;
  145. using standard_wide::lit;
  146. #endif
  147. namespace extension
  148. {
  149. template <int N>
  150. struct as_parser<char[N]>
  151. {
  152. typedef literal_string<
  153. char const*, char_encoding::standard, unused_type>
  154. type;
  155. typedef type value_type;
  156. static type call(char const* s)
  157. {
  158. return type(s);
  159. }
  160. };
  161. template <int N>
  162. struct as_parser<char const[N]> : as_parser<char[N]> {};
  163. #ifndef BOOST_SPIRIT_NO_STANDARD_WIDE
  164. template <int N>
  165. struct as_parser<wchar_t[N]>
  166. {
  167. typedef literal_string<
  168. wchar_t const*, char_encoding::standard_wide, unused_type>
  169. type;
  170. typedef type value_type;
  171. static type call(wchar_t const* s)
  172. {
  173. return type(s);
  174. }
  175. };
  176. template <int N>
  177. struct as_parser<wchar_t const[N]> : as_parser<wchar_t[N]> {};
  178. #endif
  179. template <>
  180. struct as_parser<char const*>
  181. {
  182. typedef literal_string<
  183. char const*, char_encoding::standard, unused_type>
  184. type;
  185. typedef type value_type;
  186. static type call(char const* s)
  187. {
  188. return type(s);
  189. }
  190. };
  191. template <typename Char>
  192. struct as_parser< std::basic_string<Char> >
  193. {
  194. typedef literal_string<
  195. Char const*, char_encoding::standard, unused_type>
  196. type;
  197. typedef type value_type;
  198. static type call(std::basic_string<Char> const& s)
  199. {
  200. return type(s.c_str());
  201. }
  202. };
  203. }
  204. template <typename String, typename Encoding, typename Attribute>
  205. struct get_info<literal_string<String, Encoding, Attribute>>
  206. {
  207. typedef std::string result_type;
  208. std::string operator()(literal_string<String, Encoding, Attribute> const& p) const
  209. {
  210. return '"' + to_utf8(p.str) + '"';
  211. }
  212. };
  213. }}}
  214. #endif