/*============================================================================= Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2011 Bryce Lelbach Copyright (c) 2011 Jan Frederick Eick Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) ==============================================================================*/ #if !defined(SPIRIT_UINT_APR_17_2006_0901AM) #define SPIRIT_UINT_APR_17_2006_0901AM #if defined(_MSC_VER) #pragma once #endif #include #include #include #include #include #include #include #include #include #include namespace boost { namespace spirit { namespace tag { template struct uint_parser { BOOST_SPIRIT_IS_TAG() }; } namespace qi { /////////////////////////////////////////////////////////////////////// // This one is the class that the user can instantiate directly in // order to create a customized int parser template struct uint_parser : spirit::terminal > {}; } /////////////////////////////////////////////////////////////////////////// // Enablers /////////////////////////////////////////////////////////////////////////// template <> // enables ushort_ struct use_terminal : mpl::true_ {}; template // enables lit(n) struct use_terminal > , typename enable_if >::type> : mpl::true_ {}; template // enables ushort_(n) struct use_terminal > > : is_arithmetic {}; template <> // enables *lazy* ushort_(n) struct use_lazy_terminal : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// template <> // enables uint_ struct use_terminal : mpl::true_ {}; template // enables lit(n) struct use_terminal > , typename enable_if >::type> : mpl::true_ {}; template // enables uint_(n) struct use_terminal > > : is_arithmetic {}; template <> // enables *lazy* uint_(n) struct use_lazy_terminal : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// template <> // enables ulong_ struct use_terminal : mpl::true_ {}; template // enables lit(n) struct use_terminal > , typename enable_if >::type> : mpl::true_ {}; template // enables ulong_(n) struct use_terminal > > : is_arithmetic {}; template <> // enables *lazy* ulong_(n) struct use_lazy_terminal : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// #ifdef BOOST_HAS_LONG_LONG template <> // enables ulong_long struct use_terminal : mpl::true_ {}; template // enables lit(n) struct use_terminal > , typename enable_if >::type> : mpl::true_ {}; template // enables ulong_long(n) struct use_terminal > > : is_arithmetic {}; template <> // enables *lazy* ulong_long(n) struct use_lazy_terminal : mpl::true_ {}; #endif /////////////////////////////////////////////////////////////////////////// template <> // enables bin struct use_terminal : mpl::true_ {}; template // enables bin(n) struct use_terminal > > : is_arithmetic {}; template <> // enables *lazy* bin(n) struct use_lazy_terminal : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// template <> // enables oct struct use_terminal : mpl::true_ {}; template // enables oct(n) struct use_terminal > > : is_arithmetic {}; template <> // enables *lazy* oct(n) struct use_lazy_terminal : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// template <> // enables hex struct use_terminal : mpl::true_ {}; template // enables hex(n) struct use_terminal > > : is_arithmetic {}; template <> // enables *lazy* hex(n) struct use_lazy_terminal : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// // enables any custom uint_parser template struct use_terminal > : mpl::true_ {}; // enables any custom uint_parser(n) template struct use_terminal , fusion::vector1 > > : mpl::true_ {}; // enables *lazy* custom uint_parser(n) template struct use_lazy_terminal, 1 > : mpl::true_ {}; }} namespace boost { namespace spirit { namespace qi { #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS using spirit::bin; using spirit::oct; using spirit::hex; using spirit::ushort_; using spirit::uint_; using spirit::ulong_; #ifdef BOOST_HAS_LONG_LONG using spirit::ulong_long; #endif using spirit::lit; // lit(1) is equivalent to 1 #endif using spirit::bin_type; using spirit::oct_type; using spirit::hex_type; using spirit::ushort_type; using spirit::uint_type; using spirit::ulong_type; #ifdef BOOST_HAS_LONG_LONG using spirit::ulong_long_type; #endif using spirit::lit_type; /////////////////////////////////////////////////////////////////////////// // This is the actual uint parser /////////////////////////////////////////////////////////////////////////// template struct any_uint_parser : primitive_parser > { // check template parameter 'Radix' for validity BOOST_SPIRIT_ASSERT_MSG( Radix >= 2 && Radix <= 36, not_supported_radix, ()); template struct attribute { typedef T type; }; template bool parse(Iterator& first, Iterator const& last , Context& /*context*/, Skipper const& skipper , Attribute& attr_) const { typedef extract_uint extract; qi::skip_over(first, last, skipper); return extract::call(first, last, attr_); } template info what(Context& /*context*/) const { return info("unsigned-integer"); } }; //] template struct literal_uint_parser : primitive_parser > { // check template parameter 'Radix' for validity BOOST_SPIRIT_ASSERT_MSG( Radix == 2 || Radix == 8 || Radix == 10 || Radix == 16, not_supported_radix, ()); template literal_uint_parser(Value const& n) : n_(n) {} template struct attribute : mpl::if_c {}; template bool parse(Iterator& first, Iterator const& last , Context& /*context*/, Skipper const& skipper , Attribute& attr_param) const { typedef extract_uint extract; qi::skip_over(first, last, skipper); Iterator save = first; T attr_; if (extract::call(first, last, attr_) && (attr_ == n_)) { traits::assign_to(attr_, attr_param); return true; } first = save; return false; } template info what(Context& /*context*/) const { return info("unsigned-integer"); } T n_; }; /////////////////////////////////////////////////////////////////////////// // Parser generators: make_xxx function (objects) /////////////////////////////////////////////////////////////////////////// template struct make_uint { typedef any_uint_parser result_type; result_type operator()(unused_type, unused_type) const { return result_type(); } }; template struct make_direct_uint { typedef literal_uint_parser result_type; template result_type operator()(Terminal const& term, unused_type) const { return result_type(fusion::at_c<0>(term.args)); } }; template struct make_literal_uint { typedef literal_uint_parser result_type; template result_type operator()(Terminal const& term, unused_type) const { return result_type(fusion::at_c<0>(term.args)); } }; /////////////////////////////////////////////////////////////////////////// template struct make_primitive< terminal_ex > , Modifiers, typename enable_if >::type> : make_literal_uint {}; template struct make_primitive< terminal_ex > , Modifiers, typename enable_if >::type> : make_literal_uint {}; template struct make_primitive< terminal_ex > , Modifiers, typename enable_if >::type> : make_literal_uint {}; #ifdef BOOST_HAS_LONG_LONG template struct make_primitive< terminal_ex > , Modifiers, typename enable_if >::type> : make_literal_uint {}; #endif /////////////////////////////////////////////////////////////////////////// template struct make_primitive< tag::uint_parser , Modifiers> : make_uint {}; template struct make_primitive< terminal_ex , fusion::vector1 >, Modifiers> : make_direct_uint {}; /////////////////////////////////////////////////////////////////////////// template struct make_primitive : make_uint {}; template struct make_primitive< terminal_ex > , Modifiers> : make_direct_uint {}; /////////////////////////////////////////////////////////////////////////// template struct make_primitive : make_uint {}; template struct make_primitive< terminal_ex > , Modifiers> : make_direct_uint {}; /////////////////////////////////////////////////////////////////////////// template struct make_primitive : make_uint {}; template struct make_primitive< terminal_ex > , Modifiers> : make_direct_uint {}; /////////////////////////////////////////////////////////////////////////// template struct make_primitive : make_uint {}; template struct make_primitive< terminal_ex > , Modifiers> : make_direct_uint {}; /////////////////////////////////////////////////////////////////////////// template struct make_primitive : make_uint {}; template struct make_primitive< terminal_ex > , Modifiers> : make_direct_uint {}; /////////////////////////////////////////////////////////////////////////// template struct make_primitive : make_uint {}; template struct make_primitive< terminal_ex > , Modifiers> : make_direct_uint {}; /////////////////////////////////////////////////////////////////////////// #ifdef BOOST_HAS_LONG_LONG template struct make_primitive : make_uint {}; template struct make_primitive< terminal_ex > , Modifiers> : make_direct_uint {}; #endif }}} #endif