uint.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Copyright (c) 2011 Jan Frederick Eick
  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_X3_UINT_APR_17_2006_0901AM)
  8. #define BOOST_SPIRIT_X3_UINT_APR_17_2006_0901AM
  9. #include <boost/spirit/home/x3/core/parser.hpp>
  10. #include <boost/spirit/home/x3/core/skip_over.hpp>
  11. #include <boost/spirit/home/x3/support/numeric_utils/extract_int.hpp>
  12. #include <cstdint>
  13. namespace boost { namespace spirit { namespace x3
  14. {
  15. ///////////////////////////////////////////////////////////////////////////
  16. template <
  17. typename T
  18. , unsigned Radix = 10
  19. , unsigned MinDigits = 1
  20. , int MaxDigits = -1>
  21. struct uint_parser : parser<uint_parser<T, Radix, MinDigits, MaxDigits>>
  22. {
  23. // check template parameter 'Radix' for validity
  24. static_assert(
  25. (Radix >= 2 && Radix <= 36),
  26. "Error Unsupported Radix");
  27. typedef T attribute_type;
  28. static bool const has_attribute = true;
  29. template <typename Iterator, typename Context, typename Attribute>
  30. bool parse(Iterator& first, Iterator const& last
  31. , Context const& context, unused_type, Attribute& attr) const
  32. {
  33. typedef extract_uint<T, Radix, MinDigits, MaxDigits> extract;
  34. x3::skip_over(first, last, context);
  35. return extract::call(first, last, attr);
  36. }
  37. };
  38. #define BOOST_SPIRIT_X3_UINT_PARSER(uint_type, name) \
  39. typedef uint_parser<uint_type> name##type; \
  40. name##type const name = {}; \
  41. /***/
  42. BOOST_SPIRIT_X3_UINT_PARSER(unsigned long, ulong_)
  43. BOOST_SPIRIT_X3_UINT_PARSER(unsigned short, ushort_)
  44. BOOST_SPIRIT_X3_UINT_PARSER(unsigned int, uint_)
  45. BOOST_SPIRIT_X3_UINT_PARSER(unsigned long long, ulong_long)
  46. BOOST_SPIRIT_X3_UINT_PARSER(uint8_t, uint8)
  47. BOOST_SPIRIT_X3_UINT_PARSER(uint16_t, uint16)
  48. BOOST_SPIRIT_X3_UINT_PARSER(uint32_t, uint32)
  49. BOOST_SPIRIT_X3_UINT_PARSER(uint64_t, uint64)
  50. #undef BOOST_SPIRIT_X3_UINT_PARSER
  51. #define BOOST_SPIRIT_X3_UINT_PARSER(uint_type, radix, name) \
  52. typedef uint_parser<uint_type, radix> name##type; \
  53. name##type const name = name##type(); \
  54. /***/
  55. BOOST_SPIRIT_X3_UINT_PARSER(unsigned, 2, bin)
  56. BOOST_SPIRIT_X3_UINT_PARSER(unsigned, 8, oct)
  57. BOOST_SPIRIT_X3_UINT_PARSER(unsigned, 16, hex)
  58. #undef BOOST_SPIRIT_X3_UINT_PARSER
  59. }}}
  60. #endif