int.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_INT_APR_17_2006_0830AM)
  7. #define BOOST_SPIRIT_X3_INT_APR_17_2006_0830AM
  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/support/numeric_utils/extract_int.hpp>
  11. #include <cstdint>
  12. namespace boost { namespace spirit { namespace x3
  13. {
  14. ///////////////////////////////////////////////////////////////////////////
  15. template <
  16. typename T
  17. , unsigned Radix = 10
  18. , unsigned MinDigits = 1
  19. , int MaxDigits = -1>
  20. struct int_parser : parser<int_parser<T, Radix, MinDigits, MaxDigits>>
  21. {
  22. // check template parameter 'Radix' for validity
  23. static_assert(
  24. (Radix == 2 || Radix == 8 || Radix == 10 || Radix == 16),
  25. "Error Unsupported Radix");
  26. typedef T attribute_type;
  27. static bool const has_attribute = true;
  28. template <typename Iterator, typename Context, typename Attribute>
  29. bool parse(Iterator& first, Iterator const& last
  30. , Context const& context, unused_type, Attribute& attr) const
  31. {
  32. typedef extract_int<T, Radix, MinDigits, MaxDigits> extract;
  33. x3::skip_over(first, last, context);
  34. return extract::call(first, last, attr);
  35. }
  36. };
  37. #define BOOST_SPIRIT_X3_INT_PARSER(int_type, name) \
  38. typedef int_parser<int_type> name##type; \
  39. name##type const name = {}; \
  40. /***/
  41. BOOST_SPIRIT_X3_INT_PARSER(long, long_)
  42. BOOST_SPIRIT_X3_INT_PARSER(short, short_)
  43. BOOST_SPIRIT_X3_INT_PARSER(int, int_)
  44. BOOST_SPIRIT_X3_INT_PARSER(long long, long_long)
  45. BOOST_SPIRIT_X3_INT_PARSER(int8_t, int8)
  46. BOOST_SPIRIT_X3_INT_PARSER(int16_t, int16)
  47. BOOST_SPIRIT_X3_INT_PARSER(int32_t, int32)
  48. BOOST_SPIRIT_X3_INT_PARSER(int64_t, int64)
  49. #undef BOOST_SPIRIT_X3_INT_PARSER
  50. }}}
  51. #endif