/*============================================================================= Copyright (c) 2001-2011 Joel de Guzman Copyright (c) 2011 Bryce Lelbach 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(BOOST_SPIRIT_INT_APR_17_2006_0830AM) #define BOOST_SPIRIT_INT_APR_17_2006_0830AM #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 int_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 int_parser : spirit::terminal > {}; } /////////////////////////////////////////////////////////////////////////// // Enablers /////////////////////////////////////////////////////////////////////////// //[primitive_parsers_enable_short template <> // enables short_ struct use_terminal : mpl::true_ {}; //] template // enables lit(n) struct use_terminal > , typename enable_if >::type> : mpl::true_ {}; template // enables short_(n) struct use_terminal > > : is_arithmetic {}; template <> // enables *lazy* short_(n) struct use_lazy_terminal : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// //[primitive_parsers_enable_int template <> // enables int_ struct use_terminal : mpl::true_ {}; //] template // enables lit(n) struct use_terminal > , typename enable_if >::type> : mpl::true_ {}; template // enables int_(n) struct use_terminal > > : is_arithmetic {}; template <> // enables *lazy* int_(n) struct use_lazy_terminal : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// //[primitive_parsers_enable_long template <> // enables long_ struct use_terminal : mpl::true_ {}; //] template // enables lit(n) struct use_terminal > , typename enable_if >::type> : mpl::true_ {}; template // enables long_(n) struct use_terminal > > : is_arithmetic {}; template <> // enables *lazy* long_(n) struct use_lazy_terminal : mpl::true_ {}; /////////////////////////////////////////////////////////////////////////// #ifdef BOOST_HAS_LONG_LONG //[primitive_parsers_enable_long_long template <> // enables long_long struct use_terminal : mpl::true_ {}; //] template // enables lit(n) struct use_terminal > , typename enable_if >::type> : mpl::true_ {}; template // enables long_long(n) struct use_terminal > > : is_arithmetic {}; template <> // enables *lazy* long_long(n) struct use_lazy_terminal : mpl::true_ {}; #endif /////////////////////////////////////////////////////////////////////////// // enables any custom int_parser template struct use_terminal > : mpl::true_ {}; // enables any custom int_parser(n) template struct use_terminal , fusion::vector1 > > : mpl::true_ {}; // enables *lazy* custom int_parser(n) template struct use_lazy_terminal, 1 > : mpl::true_ {}; }} namespace boost { namespace spirit { namespace qi { #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS using spirit::short_; using spirit::int_; using spirit::long_; #ifdef BOOST_HAS_LONG_LONG using spirit::long_long; #endif using spirit::lit; // lit(1) is equivalent to 1 #endif using spirit::short_type; using spirit::int_type; using spirit::long_type; using spirit::lit_type; #ifdef BOOST_HAS_LONG_LONG using spirit::long_long_type; #endif using spirit::lit_type; /////////////////////////////////////////////////////////////////////////// // This is the actual int parser /////////////////////////////////////////////////////////////////////////// //[primitive_parsers_int_parser template < typename T , unsigned Radix = 10 , unsigned MinDigits = 1 , int MaxDigits = -1> struct any_int_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 struct attribute { typedef T type; }; template bool parse(Iterator& first, Iterator const& last , Context& /*context*/, Skipper const& skipper , Attribute& attr_) const { typedef extract_int extract; qi::skip_over(first, last, skipper); return extract::call(first, last, attr_); } template info what(Context& /*context*/) const { return info("integer"); } }; //] template struct literal_int_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_int_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_int 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("integer"); } T n_; }; /////////////////////////////////////////////////////////////////////////// // Parser generators: make_xxx function (objects) /////////////////////////////////////////////////////////////////////////// //[primitive_parsers_make_int template < typename T , unsigned Radix = 10 , unsigned MinDigits = 1 , int MaxDigits = -1> struct make_int { typedef any_int_parser result_type; result_type operator()(unused_type, unused_type) const { return result_type(); } }; //] template struct make_direct_int { typedef literal_int_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_int { typedef literal_int_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_int {}; template struct make_primitive< terminal_ex > , Modifiers, typename enable_if >::type> : make_literal_int {}; template struct make_primitive< terminal_ex > , Modifiers, typename enable_if >::type> : make_literal_int {}; #ifdef BOOST_HAS_LONG_LONG template struct make_primitive< terminal_ex > , Modifiers, typename enable_if >::type> : make_literal_int {}; #endif /////////////////////////////////////////////////////////////////////////// template struct make_primitive< tag::int_parser , Modifiers> : make_int {}; template struct make_primitive< terminal_ex , fusion::vector1 >, Modifiers> : make_direct_int {}; /////////////////////////////////////////////////////////////////////////// //[primitive_parsers_short_primitive template struct make_primitive : make_int {}; //] template struct make_primitive< terminal_ex > , Modifiers> : make_direct_int {}; /////////////////////////////////////////////////////////////////////////// //[primitive_parsers_int_primitive template struct make_primitive : make_int {}; //] template struct make_primitive< terminal_ex > , Modifiers> : make_direct_int {}; /////////////////////////////////////////////////////////////////////////// //[primitive_parsers_long_primitive template struct make_primitive : make_int {}; //] template struct make_primitive< terminal_ex > , Modifiers> : make_direct_int {}; /////////////////////////////////////////////////////////////////////////// #ifdef BOOST_HAS_LONG_LONG //[primitive_parsers_long_long_primitive template struct make_primitive : make_int {}; //] template struct make_primitive< terminal_ex > , Modifiers> : make_direct_int {}; #endif }}} #endif