int.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2011 Bryce Lelbach
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #if !defined(BOOST_SPIRIT_INT_APR_17_2006_0830AM)
  8. #define BOOST_SPIRIT_INT_APR_17_2006_0830AM
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/spirit/home/qi/skip_over.hpp>
  13. #include <boost/spirit/home/qi/detail/enable_lit.hpp>
  14. #include <boost/spirit/home/qi/numeric/numeric_utils.hpp>
  15. #include <boost/spirit/home/qi/meta_compiler.hpp>
  16. #include <boost/spirit/home/qi/parser.hpp>
  17. #include <boost/spirit/home/support/common_terminals.hpp>
  18. #include <boost/spirit/home/support/info.hpp>
  19. #include <boost/spirit/home/support/detail/is_spirit_tag.hpp>
  20. #include <boost/mpl/assert.hpp>
  21. #include <boost/type_traits/is_same.hpp>
  22. namespace boost { namespace spirit
  23. {
  24. namespace tag
  25. {
  26. template <typename T, unsigned Radix, unsigned MinDigits
  27. , int MaxDigits>
  28. struct int_parser
  29. {
  30. BOOST_SPIRIT_IS_TAG()
  31. };
  32. }
  33. namespace qi
  34. {
  35. ///////////////////////////////////////////////////////////////////////
  36. // This one is the class that the user can instantiate directly in
  37. // order to create a customized int parser
  38. template <typename T = int, unsigned Radix = 10, unsigned MinDigits = 1
  39. , int MaxDigits = -1>
  40. struct int_parser
  41. : spirit::terminal<tag::int_parser<T, Radix, MinDigits, MaxDigits> >
  42. {};
  43. }
  44. ///////////////////////////////////////////////////////////////////////////
  45. // Enablers
  46. ///////////////////////////////////////////////////////////////////////////
  47. //[primitive_parsers_enable_short
  48. template <> // enables short_
  49. struct use_terminal<qi::domain, tag::short_> : mpl::true_ {};
  50. //]
  51. template <typename A0> // enables lit(n)
  52. struct use_terminal<qi::domain
  53. , terminal_ex<tag::lit, fusion::vector1<A0> >
  54. , typename enable_if<is_same<A0, signed short> >::type>
  55. : mpl::true_ {};
  56. template <typename A0> // enables short_(n)
  57. struct use_terminal<qi::domain
  58. , terminal_ex<tag::short_, fusion::vector1<A0> > >
  59. : is_arithmetic<A0> {};
  60. template <> // enables *lazy* short_(n)
  61. struct use_lazy_terminal<qi::domain, tag::short_, 1> : mpl::true_ {};
  62. ///////////////////////////////////////////////////////////////////////////
  63. //[primitive_parsers_enable_int
  64. template <> // enables int_
  65. struct use_terminal<qi::domain, tag::int_> : mpl::true_ {};
  66. //]
  67. template <typename A0> // enables lit(n)
  68. struct use_terminal<qi::domain
  69. , terminal_ex<tag::lit, fusion::vector1<A0> >
  70. , typename enable_if<is_same<A0, signed> >::type>
  71. : mpl::true_ {};
  72. template <typename A0> // enables int_(n)
  73. struct use_terminal<qi::domain
  74. , terminal_ex<tag::int_, fusion::vector1<A0> > >
  75. : is_arithmetic<A0> {};
  76. template <> // enables *lazy* int_(n)
  77. struct use_lazy_terminal<qi::domain, tag::int_, 1> : mpl::true_ {};
  78. ///////////////////////////////////////////////////////////////////////////
  79. //[primitive_parsers_enable_long
  80. template <> // enables long_
  81. struct use_terminal<qi::domain, tag::long_> : mpl::true_ {};
  82. //]
  83. template <typename A0> // enables lit(n)
  84. struct use_terminal<qi::domain
  85. , terminal_ex<tag::lit, fusion::vector1<A0> >
  86. , typename enable_if<is_same<A0, signed long> >::type>
  87. : mpl::true_ {};
  88. template <typename A0> // enables long_(n)
  89. struct use_terminal<qi::domain
  90. , terminal_ex<tag::long_, fusion::vector1<A0> > >
  91. : is_arithmetic<A0> {};
  92. template <> // enables *lazy* long_(n)
  93. struct use_lazy_terminal<qi::domain, tag::long_, 1> : mpl::true_ {};
  94. ///////////////////////////////////////////////////////////////////////////
  95. #ifdef BOOST_HAS_LONG_LONG
  96. //[primitive_parsers_enable_long_long
  97. template <> // enables long_long
  98. struct use_terminal<qi::domain, tag::long_long> : mpl::true_ {};
  99. //]
  100. template <typename A0> // enables lit(n)
  101. struct use_terminal<qi::domain
  102. , terminal_ex<tag::lit, fusion::vector1<A0> >
  103. , typename enable_if<is_same<A0, boost::long_long_type> >::type>
  104. : mpl::true_ {};
  105. template <typename A0> // enables long_long(n)
  106. struct use_terminal<qi::domain
  107. , terminal_ex<tag::long_long, fusion::vector1<A0> > >
  108. : is_arithmetic<A0> {};
  109. template <> // enables *lazy* long_long(n)
  110. struct use_lazy_terminal<qi::domain, tag::long_long, 1> : mpl::true_ {};
  111. #endif
  112. ///////////////////////////////////////////////////////////////////////////
  113. // enables any custom int_parser
  114. template <typename T, unsigned Radix, unsigned MinDigits
  115. , int MaxDigits>
  116. struct use_terminal<qi::domain
  117. , tag::int_parser<T, Radix, MinDigits, MaxDigits> >
  118. : mpl::true_ {};
  119. // enables any custom int_parser(n)
  120. template <typename T, unsigned Radix, unsigned MinDigits
  121. , int MaxDigits, typename A0>
  122. struct use_terminal<qi::domain
  123. , terminal_ex<tag::int_parser<T, Radix, MinDigits, MaxDigits>
  124. , fusion::vector1<A0> >
  125. > : mpl::true_ {};
  126. // enables *lazy* custom int_parser(n)
  127. template <typename T, unsigned Radix, unsigned MinDigits
  128. , int MaxDigits>
  129. struct use_lazy_terminal<qi::domain
  130. , tag::int_parser<T, Radix, MinDigits, MaxDigits>, 1
  131. > : mpl::true_ {};
  132. }}
  133. namespace boost { namespace spirit { namespace qi
  134. {
  135. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  136. using spirit::short_;
  137. using spirit::int_;
  138. using spirit::long_;
  139. #ifdef BOOST_HAS_LONG_LONG
  140. using spirit::long_long;
  141. #endif
  142. using spirit::lit; // lit(1) is equivalent to 1
  143. #endif
  144. using spirit::short_type;
  145. using spirit::int_type;
  146. using spirit::long_type;
  147. using spirit::lit_type;
  148. #ifdef BOOST_HAS_LONG_LONG
  149. using spirit::long_long_type;
  150. #endif
  151. using spirit::lit_type;
  152. ///////////////////////////////////////////////////////////////////////////
  153. // This is the actual int parser
  154. ///////////////////////////////////////////////////////////////////////////
  155. //[primitive_parsers_int_parser
  156. template <
  157. typename T
  158. , unsigned Radix = 10
  159. , unsigned MinDigits = 1
  160. , int MaxDigits = -1>
  161. struct any_int_parser
  162. : primitive_parser<any_int_parser<T, Radix, MinDigits, MaxDigits> >
  163. {
  164. // check template parameter 'Radix' for validity
  165. BOOST_SPIRIT_ASSERT_MSG(
  166. Radix == 2 || Radix == 8 || Radix == 10 || Radix == 16,
  167. not_supported_radix, ());
  168. template <typename Context, typename Iterator>
  169. struct attribute
  170. {
  171. typedef T type;
  172. };
  173. template <typename Iterator, typename Context
  174. , typename Skipper, typename Attribute>
  175. bool parse(Iterator& first, Iterator const& last
  176. , Context& /*context*/, Skipper const& skipper
  177. , Attribute& attr_) const
  178. {
  179. typedef extract_int<T, Radix, MinDigits, MaxDigits> extract;
  180. qi::skip_over(first, last, skipper);
  181. return extract::call(first, last, attr_);
  182. }
  183. template <typename Context>
  184. info what(Context& /*context*/) const
  185. {
  186. return info("integer");
  187. }
  188. };
  189. //]
  190. template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
  191. , int MaxDigits = -1, bool no_attribute = true>
  192. struct literal_int_parser
  193. : primitive_parser<literal_int_parser<T, Radix, MinDigits, MaxDigits
  194. , no_attribute> >
  195. {
  196. // check template parameter 'Radix' for validity
  197. BOOST_SPIRIT_ASSERT_MSG(
  198. Radix == 2 || Radix == 8 || Radix == 10 || Radix == 16,
  199. not_supported_radix, ());
  200. template <typename Value>
  201. literal_int_parser(Value const& n) : n_(n) {}
  202. template <typename Context, typename Iterator>
  203. struct attribute
  204. : mpl::if_c<no_attribute, unused_type, T>
  205. {};
  206. template <typename Iterator, typename Context
  207. , typename Skipper, typename Attribute>
  208. bool parse(Iterator& first, Iterator const& last
  209. , Context& /*context*/, Skipper const& skipper
  210. , Attribute& attr_param) const
  211. {
  212. typedef extract_int<T, Radix, MinDigits, MaxDigits> extract;
  213. qi::skip_over(first, last, skipper);
  214. Iterator save = first;
  215. T attr_;
  216. if (extract::call(first, last, attr_) && (attr_ == n_))
  217. {
  218. traits::assign_to(attr_, attr_param);
  219. return true;
  220. }
  221. first = save;
  222. return false;
  223. }
  224. template <typename Context>
  225. info what(Context& /*context*/) const
  226. {
  227. return info("integer");
  228. }
  229. T n_;
  230. };
  231. ///////////////////////////////////////////////////////////////////////////
  232. // Parser generators: make_xxx function (objects)
  233. ///////////////////////////////////////////////////////////////////////////
  234. //[primitive_parsers_make_int
  235. template <
  236. typename T
  237. , unsigned Radix = 10
  238. , unsigned MinDigits = 1
  239. , int MaxDigits = -1>
  240. struct make_int
  241. {
  242. typedef any_int_parser<T, Radix, MinDigits, MaxDigits> result_type;
  243. result_type operator()(unused_type, unused_type) const
  244. {
  245. return result_type();
  246. }
  247. };
  248. //]
  249. template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
  250. , int MaxDigits = -1>
  251. struct make_direct_int
  252. {
  253. typedef literal_int_parser<T, Radix, MinDigits, MaxDigits, false>
  254. result_type;
  255. template <typename Terminal>
  256. result_type operator()(Terminal const& term, unused_type) const
  257. {
  258. return result_type(fusion::at_c<0>(term.args));
  259. }
  260. };
  261. template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
  262. , int MaxDigits = -1>
  263. struct make_literal_int
  264. {
  265. typedef literal_int_parser<T, Radix, MinDigits, MaxDigits> result_type;
  266. template <typename Terminal>
  267. result_type operator()(Terminal const& term, unused_type) const
  268. {
  269. return result_type(fusion::at_c<0>(term.args));
  270. }
  271. };
  272. ///////////////////////////////////////////////////////////////////////////
  273. template <typename Modifiers, typename A0>
  274. struct make_primitive<
  275. terminal_ex<tag::lit, fusion::vector1<A0> >
  276. , Modifiers, typename enable_if<is_same<A0, signed short> >::type>
  277. : make_literal_int<signed short> {};
  278. template <typename Modifiers, typename A0>
  279. struct make_primitive<
  280. terminal_ex<tag::lit, fusion::vector1<A0> >
  281. , Modifiers, typename enable_if<is_same<A0, signed> >::type>
  282. : make_literal_int<signed> {};
  283. template <typename Modifiers, typename A0>
  284. struct make_primitive<
  285. terminal_ex<tag::lit, fusion::vector1<A0> >
  286. , Modifiers, typename enable_if<is_same<A0, signed long> >::type>
  287. : make_literal_int<signed long> {};
  288. #ifdef BOOST_HAS_LONG_LONG
  289. template <typename Modifiers, typename A0>
  290. struct make_primitive<
  291. terminal_ex<tag::lit, fusion::vector1<A0> >
  292. , Modifiers, typename enable_if<is_same<A0, boost::long_long_type> >::type>
  293. : make_literal_int<boost::long_long_type> {};
  294. #endif
  295. ///////////////////////////////////////////////////////////////////////////
  296. template <typename T, unsigned Radix, unsigned MinDigits, int MaxDigits
  297. , typename Modifiers>
  298. struct make_primitive<
  299. tag::int_parser<T, Radix, MinDigits, MaxDigits>
  300. , Modifiers>
  301. : make_int<T, Radix, MinDigits, MaxDigits> {};
  302. template <typename T, unsigned Radix, unsigned MinDigits, int MaxDigits
  303. , typename A0, typename Modifiers>
  304. struct make_primitive<
  305. terminal_ex<tag::int_parser<T, Radix, MinDigits, MaxDigits>
  306. , fusion::vector1<A0> >, Modifiers>
  307. : make_direct_int<T, Radix, MinDigits, MaxDigits> {};
  308. ///////////////////////////////////////////////////////////////////////////
  309. //[primitive_parsers_short_primitive
  310. template <typename Modifiers>
  311. struct make_primitive<tag::short_, Modifiers>
  312. : make_int<short> {};
  313. //]
  314. template <typename Modifiers, typename A0>
  315. struct make_primitive<
  316. terminal_ex<tag::short_
  317. , fusion::vector1<A0> > , Modifiers>
  318. : make_direct_int<short> {};
  319. ///////////////////////////////////////////////////////////////////////////
  320. //[primitive_parsers_int_primitive
  321. template <typename Modifiers>
  322. struct make_primitive<tag::int_, Modifiers>
  323. : make_int<int> {};
  324. //]
  325. template <typename Modifiers, typename A0>
  326. struct make_primitive<
  327. terminal_ex<tag::int_
  328. , fusion::vector1<A0> > , Modifiers>
  329. : make_direct_int<int> {};
  330. ///////////////////////////////////////////////////////////////////////////
  331. //[primitive_parsers_long_primitive
  332. template <typename Modifiers>
  333. struct make_primitive<tag::long_, Modifiers>
  334. : make_int<long> {};
  335. //]
  336. template <typename Modifiers, typename A0>
  337. struct make_primitive<
  338. terminal_ex<tag::long_
  339. , fusion::vector1<A0> > , Modifiers>
  340. : make_direct_int<long> {};
  341. ///////////////////////////////////////////////////////////////////////////
  342. #ifdef BOOST_HAS_LONG_LONG
  343. //[primitive_parsers_long_long_primitive
  344. template <typename Modifiers>
  345. struct make_primitive<tag::long_long, Modifiers>
  346. : make_int<boost::long_long_type> {};
  347. //]
  348. template <typename Modifiers, typename A0>
  349. struct make_primitive<
  350. terminal_ex<tag::long_long
  351. , fusion::vector1<A0> > , Modifiers>
  352. : make_direct_int<boost::long_long_type> {};
  353. #endif
  354. }}}
  355. #endif