/*============================================================================= Copyright (c) 1998-2003 Joel de Guzman Copyright (c) 2001-2003 Hartmut Kaiser http://spirit.sourceforge.net/ 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) =============================================================================*/ #ifndef BOOST_SPIRIT_NUMERICS_HPP #define BOOST_SPIRIT_NUMERICS_HPP #include #include #include #include #include #include namespace boost { namespace spirit { BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN /////////////////////////////////////////////////////////////////////////// // // uint_parser class // /////////////////////////////////////////////////////////////////////////// template < typename T, int Radix, unsigned MinDigits, int MaxDigits > struct uint_parser : parser > { typedef uint_parser self_t; template struct result { typedef typename match_result::type type; }; template typename parser_result::type parse(ScannerT const& scan) const { typedef impl::uint_parser_impl impl_t; typedef typename parser_result::type result_t; return impl::contiguous_parser_parse(impl_t(), scan, scan); } }; /////////////////////////////////////////////////////////////////////////// // // int_parser class // /////////////////////////////////////////////////////////////////////////// template < typename T, int Radix, unsigned MinDigits, int MaxDigits > struct int_parser : parser > { typedef int_parser self_t; template struct result { typedef typename match_result::type type; }; template typename parser_result::type parse(ScannerT const& scan) const { typedef impl::int_parser_impl impl_t; typedef typename parser_result::type result_t; return impl::contiguous_parser_parse(impl_t(), scan, scan); } }; /////////////////////////////////////////////////////////////////////////// // // uint_parser/int_parser instantiations // /////////////////////////////////////////////////////////////////////////// int_parser const int_p = int_parser(); uint_parser const uint_p = uint_parser(); uint_parser const bin_p = uint_parser(); uint_parser const oct_p = uint_parser(); uint_parser const hex_p = uint_parser(); /////////////////////////////////////////////////////////////////////////// // // sign_parser class // /////////////////////////////////////////////////////////////////////////// namespace impl { // Utility to extract the prefix sign ('-' | '+') template bool extract_sign(ScannerT const& scan, std::size_t& count); } struct sign_parser : public parser { typedef sign_parser self_t; template struct result { typedef typename match_result::type type; }; sign_parser() {} template typename parser_result::type parse(ScannerT const& scan) const { if (!scan.at_end()) { std::size_t length; typename ScannerT::iterator_t save(scan.first); bool neg = impl::extract_sign(scan, length); if (length) return scan.create_match(1, neg, save, scan.first); } return scan.no_match(); } }; sign_parser const sign_p = sign_parser(); /////////////////////////////////////////////////////////////////////////// // // default real number policies // /////////////////////////////////////////////////////////////////////////// template struct ureal_parser_policies { // trailing dot policy suggested suggested by Gustavo Guerra BOOST_STATIC_CONSTANT(bool, allow_leading_dot = true); BOOST_STATIC_CONSTANT(bool, allow_trailing_dot = true); BOOST_STATIC_CONSTANT(bool, expect_dot = false); typedef uint_parser uint_parser_t; typedef int_parser int_parser_t; template static typename match_result::type parse_sign(ScannerT& scan) { return scan.no_match(); } template static typename parser_result::type parse_n(ScannerT& scan) { return uint_parser_t().parse(scan); } template static typename parser_result, ScannerT>::type parse_dot(ScannerT& scan) { return ch_p('.').parse(scan); } template static typename parser_result::type parse_frac_n(ScannerT& scan) { return uint_parser_t().parse(scan); } template static typename parser_result, ScannerT>::type parse_exp(ScannerT& scan) { return as_lower_d['e'].parse(scan); } template static typename parser_result::type parse_exp_n(ScannerT& scan) { return int_parser_t().parse(scan); } }; template struct real_parser_policies : public ureal_parser_policies { template static typename parser_result::type parse_sign(ScannerT& scan) { return sign_p.parse(scan); } }; /////////////////////////////////////////////////////////////////////////// // // real_parser class // /////////////////////////////////////////////////////////////////////////// template < typename T, typename RealPoliciesT > struct real_parser : public parser > { typedef real_parser self_t; template struct result { typedef typename match_result::type type; }; real_parser() {} template typename parser_result::type parse(ScannerT const& scan) const { typedef typename parser_result::type result_t; return impl::real_parser_impl::parse(scan); } }; /////////////////////////////////////////////////////////////////////////// // // real_parser instantiations // /////////////////////////////////////////////////////////////////////////// real_parser > const ureal_p = real_parser >(); real_parser > const real_p = real_parser >(); /////////////////////////////////////////////////////////////////////////// // // strict reals (do not allow plain integers (no decimal point)) // /////////////////////////////////////////////////////////////////////////// template struct strict_ureal_parser_policies : public ureal_parser_policies { BOOST_STATIC_CONSTANT(bool, expect_dot = true); }; template struct strict_real_parser_policies : public real_parser_policies { BOOST_STATIC_CONSTANT(bool, expect_dot = true); }; real_parser > const strict_ureal_p = real_parser >(); real_parser > const strict_real_p = real_parser >(); BOOST_SPIRIT_CLASSIC_NAMESPACE_END }} // namespace BOOST_SPIRIT_CLASSIC_NS #endif