lit.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Copyright (c) 2010 Bryce Lelbach
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #if !defined(BOOST_SPIRIT_LIT_APR_18_2006_1125PM)
  9. #define BOOST_SPIRIT_LIT_APR_18_2006_1125PM
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/spirit/home/qi/domain.hpp>
  14. #include <boost/spirit/home/qi/skip_over.hpp>
  15. #include <boost/spirit/home/qi/detail/string_parse.hpp>
  16. #include <boost/spirit/home/qi/parser.hpp>
  17. #include <boost/spirit/home/qi/meta_compiler.hpp>
  18. #include <boost/spirit/home/qi/auxiliary/lazy.hpp>
  19. #include <boost/spirit/home/qi/detail/enable_lit.hpp>
  20. #include <boost/spirit/home/support/info.hpp>
  21. #include <boost/spirit/home/support/char_class.hpp>
  22. #include <boost/spirit/home/support/modify.hpp>
  23. #include <boost/spirit/home/support/unused.hpp>
  24. #include <boost/spirit/home/support/common_terminals.hpp>
  25. #include <boost/spirit/home/support/string_traits.hpp>
  26. #include <boost/spirit/home/support/detail/get_encoding.hpp>
  27. #include <boost/spirit/home/support/handles_container.hpp>
  28. #include <boost/fusion/include/at.hpp>
  29. #include <boost/fusion/include/value_at.hpp>
  30. #include <boost/type_traits/add_reference.hpp>
  31. #include <boost/type_traits/add_const.hpp>
  32. #include <boost/mpl/assert.hpp>
  33. #include <boost/mpl/if.hpp>
  34. #include <boost/detail/workaround.hpp>
  35. #include <boost/utility/enable_if.hpp>
  36. #include <string>
  37. namespace boost { namespace spirit
  38. {
  39. ///////////////////////////////////////////////////////////////////////////
  40. // Enablers
  41. ///////////////////////////////////////////////////////////////////////////
  42. template <typename T>
  43. struct use_terminal<qi::domain, T
  44. , typename enable_if<traits::is_string<T> >::type> // enables strings
  45. : mpl::true_ {};
  46. template <typename CharEncoding, typename A0>
  47. struct use_terminal<qi::domain
  48. , terminal_ex<
  49. tag::char_code<tag::string, CharEncoding> // enables string(str)
  50. , fusion::vector1<A0> >
  51. > : traits::is_string<A0> {};
  52. template <typename CharEncoding> // enables string(f)
  53. struct use_lazy_terminal<
  54. qi::domain
  55. , tag::char_code<tag::string, CharEncoding>
  56. , 1 /*arity*/
  57. > : mpl::true_ {};
  58. // enables lit(...)
  59. template <typename A0>
  60. struct use_terminal<qi::domain
  61. , terminal_ex<tag::lit, fusion::vector1<A0> >
  62. , typename enable_if<traits::is_string<A0> >::type>
  63. : mpl::true_ {};
  64. }}
  65. namespace boost { namespace spirit { namespace qi
  66. {
  67. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  68. using spirit::lit;
  69. #endif
  70. using spirit::lit_type;
  71. ///////////////////////////////////////////////////////////////////////////
  72. // Parse for literal strings
  73. ///////////////////////////////////////////////////////////////////////////
  74. template <typename String, bool no_attribute>
  75. struct literal_string
  76. : primitive_parser<literal_string<String, no_attribute> >
  77. {
  78. typedef typename
  79. remove_const<typename traits::char_type_of<String>::type>::type
  80. char_type;
  81. typedef std::basic_string<char_type> string_type;
  82. literal_string(typename add_reference<String>::type str_)
  83. : str(str_)
  84. {}
  85. template <typename Context, typename Iterator>
  86. struct attribute
  87. {
  88. typedef typename mpl::if_c<
  89. no_attribute, unused_type, string_type>::type
  90. type;
  91. };
  92. template <typename Iterator, typename Context
  93. , typename Skipper, typename Attribute>
  94. bool parse(Iterator& first, Iterator const& last
  95. , Context& /*context*/, Skipper const& skipper, Attribute& attr_) const
  96. {
  97. qi::skip_over(first, last, skipper);
  98. return detail::string_parse(str, first, last, attr_);
  99. }
  100. template <typename Context>
  101. info what(Context& /*context*/) const
  102. {
  103. return info("literal-string", str);
  104. }
  105. String str;
  106. // silence MSVC warning C4512: assignment operator could not be generated
  107. BOOST_DELETED_FUNCTION(literal_string& operator= (literal_string const&))
  108. };
  109. template <typename String, bool no_attribute>
  110. struct no_case_literal_string
  111. : primitive_parser<no_case_literal_string<String, no_attribute> >
  112. {
  113. typedef typename
  114. remove_const<typename traits::char_type_of<String>::type>::type
  115. char_type;
  116. typedef std::basic_string<char_type> string_type;
  117. template <typename CharEncoding>
  118. no_case_literal_string(char_type const* in, CharEncoding encoding)
  119. : str_lo(in)
  120. , str_hi(in)
  121. {
  122. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  123. encoding; // suppresses warning: C4100: 'encoding' : unreferenced formal parameter
  124. #endif
  125. typename string_type::iterator loi = str_lo.begin();
  126. typename string_type::iterator hii = str_hi.begin();
  127. for (; loi != str_lo.end(); ++loi, ++hii, ++in)
  128. {
  129. typedef typename CharEncoding::char_type encoded_char_type;
  130. *loi = static_cast<char_type>(encoding.tolower(encoded_char_type(*loi)));
  131. *hii = static_cast<char_type>(encoding.toupper(encoded_char_type(*hii)));
  132. }
  133. }
  134. template <typename Context, typename Iterator>
  135. struct attribute
  136. {
  137. typedef typename mpl::if_c<
  138. no_attribute, unused_type, string_type>::type
  139. type;
  140. };
  141. template <typename Iterator, typename Context
  142. , typename Skipper, typename Attribute>
  143. bool parse(Iterator& first, Iterator const& last
  144. , Context& /*context*/, Skipper const& skipper, Attribute& attr_) const
  145. {
  146. qi::skip_over(first, last, skipper);
  147. return detail::string_parse(str_lo, str_hi, first, last, attr_);
  148. }
  149. template <typename Context>
  150. info what(Context& /*context*/) const
  151. {
  152. return info("no-case-literal-string", str_lo);
  153. }
  154. string_type str_lo, str_hi;
  155. };
  156. ///////////////////////////////////////////////////////////////////////////
  157. // Parser generators: make_xxx function (objects)
  158. ///////////////////////////////////////////////////////////////////////////
  159. template <typename T, typename Modifiers>
  160. struct make_primitive<T, Modifiers
  161. , typename enable_if<traits::is_string<T> >::type>
  162. {
  163. typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> > no_case;
  164. typedef typename add_const<T>::type const_string;
  165. typedef typename mpl::if_<
  166. no_case
  167. , no_case_literal_string<const_string, true>
  168. , literal_string<const_string, true> >::type
  169. result_type;
  170. result_type operator()(
  171. typename add_reference<const_string>::type str, unused_type) const
  172. {
  173. return op(str, no_case());
  174. }
  175. template <typename String>
  176. result_type op(String const& str, mpl::false_) const
  177. {
  178. return result_type(str);
  179. }
  180. template <typename String>
  181. result_type op(String const& str, mpl::true_) const
  182. {
  183. typename spirit::detail::get_encoding<Modifiers,
  184. spirit::char_encoding::standard>::type encoding;
  185. return result_type(traits::get_c_string(str), encoding);
  186. }
  187. };
  188. // lit("...")
  189. template <typename Modifiers, typename A0>
  190. struct make_primitive<
  191. terminal_ex<tag::lit, fusion::vector1<A0> >
  192. , Modifiers
  193. , typename enable_if<traits::is_string<A0> >::type>
  194. {
  195. typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> > no_case;
  196. typedef typename add_const<A0>::type const_string;
  197. typedef typename mpl::if_<
  198. no_case
  199. , no_case_literal_string<const_string, true>
  200. , literal_string<const_string, true> >::type
  201. result_type;
  202. template <typename Terminal>
  203. result_type operator()(Terminal const& term, unused_type) const
  204. {
  205. return op(fusion::at_c<0>(term.args), no_case());
  206. }
  207. template <typename String>
  208. result_type op(String const& str, mpl::false_) const
  209. {
  210. return result_type(str);
  211. }
  212. template <typename String>
  213. result_type op(String const& str, mpl::true_) const
  214. {
  215. typedef typename traits::char_encoding_from_char<
  216. typename traits::char_type_of<A0>::type>::type encoding_type;
  217. typename spirit::detail::get_encoding<Modifiers,
  218. encoding_type>::type encoding;
  219. return result_type(traits::get_c_string(str), encoding);
  220. }
  221. };
  222. ///////////////////////////////////////////////////////////////////////////
  223. // string("...")
  224. template <typename CharEncoding, typename Modifiers, typename A0>
  225. struct make_primitive<
  226. terminal_ex<
  227. tag::char_code<tag::string, CharEncoding>
  228. , fusion::vector1<A0> >
  229. , Modifiers>
  230. {
  231. typedef CharEncoding encoding;
  232. typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> > no_case;
  233. typedef typename add_const<A0>::type const_string;
  234. typedef typename mpl::if_<
  235. no_case
  236. , no_case_literal_string<const_string, false>
  237. , literal_string<const_string, false> >::type
  238. result_type;
  239. template <typename Terminal>
  240. result_type operator()(Terminal const& term, unused_type) const
  241. {
  242. return op(fusion::at_c<0>(term.args), no_case());
  243. }
  244. template <typename String>
  245. result_type op(String const& str, mpl::false_) const
  246. {
  247. return result_type(str);
  248. }
  249. template <typename String>
  250. result_type op(String const& str, mpl::true_) const
  251. {
  252. return result_type(traits::get_c_string(str), encoding());
  253. }
  254. };
  255. }}}
  256. namespace boost { namespace spirit { namespace traits
  257. {
  258. ///////////////////////////////////////////////////////////////////////////
  259. template <typename String, bool no_attribute, typename Attribute
  260. ,typename Context, typename Iterator>
  261. struct handles_container<qi::literal_string<String, no_attribute>
  262. , Attribute, Context, Iterator>
  263. : mpl::true_ {};
  264. template <typename String, bool no_attribute, typename Attribute
  265. , typename Context, typename Iterator>
  266. struct handles_container<qi::no_case_literal_string<String, no_attribute>
  267. , Attribute, Context, Iterator>
  268. : mpl::true_ {};
  269. }}}
  270. #endif