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