lit.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. };
  107. template <typename String, bool no_attribute>
  108. struct no_case_literal_string
  109. : primitive_parser<no_case_literal_string<String, no_attribute> >
  110. {
  111. typedef typename
  112. remove_const<typename traits::char_type_of<String>::type>::type
  113. char_type;
  114. typedef std::basic_string<char_type> string_type;
  115. template <typename CharEncoding>
  116. no_case_literal_string(char_type const* in, CharEncoding encoding)
  117. : str_lo(in)
  118. , str_hi(in)
  119. {
  120. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  121. (void)encoding; // suppresses warning: C4100: 'encoding' : unreferenced formal parameter
  122. #endif
  123. typename string_type::iterator loi = str_lo.begin();
  124. typename string_type::iterator hii = str_hi.begin();
  125. for (; loi != str_lo.end(); ++loi, ++hii, ++in)
  126. {
  127. typedef typename CharEncoding::char_type encoded_char_type;
  128. *loi = static_cast<char_type>(encoding.tolower(encoded_char_type(*loi)));
  129. *hii = static_cast<char_type>(encoding.toupper(encoded_char_type(*hii)));
  130. }
  131. }
  132. template <typename Context, typename Iterator>
  133. struct attribute
  134. {
  135. typedef typename mpl::if_c<
  136. no_attribute, unused_type, string_type>::type
  137. type;
  138. };
  139. template <typename Iterator, typename Context
  140. , typename Skipper, typename Attribute>
  141. bool parse(Iterator& first, Iterator const& last
  142. , Context& /*context*/, Skipper const& skipper, Attribute& attr_) const
  143. {
  144. qi::skip_over(first, last, skipper);
  145. return detail::string_parse(str_lo, str_hi, first, last, attr_);
  146. }
  147. template <typename Context>
  148. info what(Context& /*context*/) const
  149. {
  150. return info("no-case-literal-string", str_lo);
  151. }
  152. string_type str_lo, str_hi;
  153. };
  154. ///////////////////////////////////////////////////////////////////////////
  155. // Parser generators: make_xxx function (objects)
  156. ///////////////////////////////////////////////////////////////////////////
  157. template <typename T, typename Modifiers>
  158. struct make_primitive<T, Modifiers
  159. , typename enable_if<traits::is_string<T> >::type>
  160. {
  161. typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> > no_case;
  162. typedef typename add_const<T>::type const_string;
  163. typedef typename mpl::if_<
  164. no_case
  165. , no_case_literal_string<const_string, true>
  166. , literal_string<const_string, true> >::type
  167. result_type;
  168. result_type operator()(
  169. typename add_reference<const_string>::type str, unused_type) const
  170. {
  171. return op(str, no_case());
  172. }
  173. template <typename String>
  174. result_type op(String const& str, mpl::false_) const
  175. {
  176. return result_type(str);
  177. }
  178. template <typename String>
  179. result_type op(String const& str, mpl::true_) const
  180. {
  181. typename spirit::detail::get_encoding<Modifiers,
  182. spirit::char_encoding::standard>::type encoding;
  183. return result_type(traits::get_c_string(str), encoding);
  184. }
  185. };
  186. // lit("...")
  187. template <typename Modifiers, typename A0>
  188. struct make_primitive<
  189. terminal_ex<tag::lit, fusion::vector1<A0> >
  190. , Modifiers
  191. , typename enable_if<traits::is_string<A0> >::type>
  192. {
  193. typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> > no_case;
  194. typedef typename add_const<A0>::type const_string;
  195. typedef typename mpl::if_<
  196. no_case
  197. , no_case_literal_string<const_string, true>
  198. , literal_string<const_string, true> >::type
  199. result_type;
  200. template <typename Terminal>
  201. result_type operator()(Terminal const& term, unused_type) const
  202. {
  203. return op(fusion::at_c<0>(term.args), no_case());
  204. }
  205. template <typename String>
  206. result_type op(String const& str, mpl::false_) const
  207. {
  208. return result_type(str);
  209. }
  210. template <typename String>
  211. result_type op(String const& str, mpl::true_) const
  212. {
  213. typedef typename traits::char_encoding_from_char<
  214. typename traits::char_type_of<A0>::type>::type encoding_type;
  215. typename spirit::detail::get_encoding<Modifiers,
  216. encoding_type>::type encoding;
  217. return result_type(traits::get_c_string(str), encoding);
  218. }
  219. };
  220. ///////////////////////////////////////////////////////////////////////////
  221. // string("...")
  222. template <typename CharEncoding, typename Modifiers, typename A0>
  223. struct make_primitive<
  224. terminal_ex<
  225. tag::char_code<tag::string, CharEncoding>
  226. , fusion::vector1<A0> >
  227. , Modifiers>
  228. {
  229. typedef CharEncoding encoding;
  230. typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> > no_case;
  231. typedef typename add_const<A0>::type const_string;
  232. typedef typename mpl::if_<
  233. no_case
  234. , no_case_literal_string<const_string, false>
  235. , literal_string<const_string, false> >::type
  236. result_type;
  237. template <typename Terminal>
  238. result_type operator()(Terminal const& term, unused_type) const
  239. {
  240. return op(fusion::at_c<0>(term.args), no_case());
  241. }
  242. template <typename String>
  243. result_type op(String const& str, mpl::false_) const
  244. {
  245. return result_type(str);
  246. }
  247. template <typename String>
  248. result_type op(String const& str, mpl::true_) const
  249. {
  250. return result_type(traits::get_c_string(str), encoding());
  251. }
  252. };
  253. }}}
  254. namespace boost { namespace spirit { namespace traits
  255. {
  256. ///////////////////////////////////////////////////////////////////////////
  257. template <typename String, bool no_attribute, typename Attribute
  258. ,typename Context, typename Iterator>
  259. struct handles_container<qi::literal_string<String, no_attribute>
  260. , Attribute, Context, Iterator>
  261. : mpl::true_ {};
  262. template <typename String, bool no_attribute, typename Attribute
  263. , typename Context, typename Iterator>
  264. struct handles_container<qi::no_case_literal_string<String, no_attribute>
  265. , Attribute, Context, Iterator>
  266. : mpl::true_ {};
  267. }}}
  268. #endif