1 /*=============================================================================
2 Copyright (c) 1998-2003 Joel de Guzman
3 Copyright (c) 2001-2003 Hartmut Kaiser
4 http://spirit.sourceforge.net/
6 Use, modification and distribution is subject to the Boost Software
7 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #ifndef BOOST_SPIRIT_NUMERICS_IPP
11 #define BOOST_SPIRIT_NUMERICS_IPP
13 #include <boost/config/no_tr1/cmath.hpp>
16 namespace boost { namespace spirit {
18 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
20 struct sign_parser; // forward declaration only
24 ///////////////////////////////////////////////////////////////////////
26 // Extract the prefix sign (- or +)
28 ///////////////////////////////////////////////////////////////////////
29 template <typename ScannerT>
31 extract_sign(ScannerT const& scan, std::size_t& count)
35 bool neg = *scan == '-';
36 if (neg || (*scan == '+'))
46 ///////////////////////////////////////////////////////////////////////
48 // Traits class for radix specific number conversion
50 // Convert a digit from character representation, ch, to binary
51 // representation, returned in val.
52 // Returns whether the conversion was successful.
54 // template<typename CharT> static bool digit(CharT ch, T& val);
56 ///////////////////////////////////////////////////////////////////////
57 template<const int Radix>
60 ////////////////////////////////// Binary
62 struct radix_traits<2>
64 template<typename CharT, typename T>
65 static bool digit(CharT ch, T& val)
68 return ('0' == ch || '1' == ch);
72 ////////////////////////////////// Octal
74 struct radix_traits<8>
76 template<typename CharT, typename T>
77 static bool digit(CharT ch, T& val)
80 return ('0' <= ch && ch <= '7');
84 ////////////////////////////////// Decimal
86 struct radix_traits<10>
88 template<typename CharT, typename T>
89 static bool digit(CharT ch, T& val)
92 return impl::isdigit_(ch);
96 ////////////////////////////////// Hexadecimal
98 struct radix_traits<16>
100 template<typename CharT, typename T>
101 static bool digit(CharT ch, T& val)
103 if (radix_traits<10>::digit(ch, val))
106 CharT lc = impl::tolower_(ch);
107 if ('a' <= lc && lc <= 'f')
116 ///////////////////////////////////////////////////////////////////////
118 // Helper templates for encapsulation of radix specific
119 // conversion of an input string to an integral value.
123 // extract_int<Radix, MinDigits, MaxDigits, Accumulate>
124 // ::f(first, last, n, count);
126 // The template parameter Radix represents the radix of the
127 // number contained in the parsed string. The template
128 // parameter MinDigits specifies the minimum digits to
129 // accept. The template parameter MaxDigits specifies the
130 // maximum digits to parse. A -1 value for MaxDigits will
131 // make it parse an arbitrarilly large number as long as the
132 // numeric type can hold it. Accumulate is either
133 // positive_accumulate<Radix> (default) for parsing positive
134 // numbers or negative_accumulate<Radix> otherwise.
135 // Checking is only performed when std::numeric_limits<T>::
136 // is_specialized is true. Otherwise, there's no way to
139 // scan.first and scan.last are iterators as usual (i.e.
140 // first is mutable and is moved forward when a match is
141 // found), n is a variable that holds the number (passed by
142 // reference). The number of parsed characters is added to
143 // count (also passed by reference)
146 // Returns a non-match, if the number to parse
147 // overflows (or underflows) the used type.
150 // the parameters 'n' and 'count' should be properly
151 // initialized before calling this function.
153 ///////////////////////////////////////////////////////////////////////
154 #if defined(BOOST_MSVC)
155 #pragma warning(push)
156 #pragma warning(disable:4127) //conditional expression is constant
159 template <typename T, int Radix>
160 struct positive_accumulate
162 // Use this accumulator if number is positive
163 static bool add(T& n, T digit)
165 if (std::numeric_limits<T>::is_specialized)
167 static T const max = (std::numeric_limits<T>::max)();
168 static T const max_div_radix = max/Radix;
170 if (n > max_div_radix)
189 template <typename T, int Radix>
190 struct negative_accumulate
192 // Use this accumulator if number is negative
193 static bool add(T& n, T digit)
195 if (std::numeric_limits<T>::is_specialized)
197 typedef std::numeric_limits<T> num_limits;
199 (!num_limits::is_integer && num_limits::is_signed && num_limits::has_denorm) ?
200 -(num_limits::max)() : (num_limits::min)();
201 static T const min_div_radix = min/Radix;
203 if (n < min_div_radix)
222 template <int MaxDigits>
223 inline bool allow_more_digits(std::size_t i)
225 return i < MaxDigits;
229 inline bool allow_more_digits<-1>(std::size_t)
234 //////////////////////////////////
236 int Radix, unsigned MinDigits, int MaxDigits,
241 template <typename ScannerT, typename T>
243 f(ScannerT& scan, T& n, std::size_t& count)
247 while( allow_more_digits<MaxDigits>(i) && !scan.at_end() &&
248 radix_traits<Radix>::digit(*scan, digit) )
250 if (!Accumulate::add(n, digit))
251 return false; // Overflow
252 ++i, ++scan, ++count;
254 return i >= MinDigits;
258 ///////////////////////////////////////////////////////////////////////
260 // uint_parser_impl class
262 ///////////////////////////////////////////////////////////////////////
264 typename T = unsigned,
266 unsigned MinDigits = 1,
269 struct uint_parser_impl
270 : parser<uint_parser_impl<T, Radix, MinDigits, MaxDigits> >
272 typedef uint_parser_impl<T, Radix, MinDigits, MaxDigits> self_t;
274 template <typename ScannerT>
277 typedef typename match_result<ScannerT, T>::type type;
280 template <typename ScannerT>
281 typename parser_result<self_t, ScannerT>::type
282 parse(ScannerT const& scan) const
287 std::size_t count = 0;
288 typename ScannerT::iterator_t save = scan.first;
289 if (extract_int<Radix, MinDigits, MaxDigits,
290 positive_accumulate<T, Radix> >::f(scan, n, count))
292 return scan.create_match(count, n, save, scan.first);
294 // return no-match if number overflows
296 return scan.no_match();
300 ///////////////////////////////////////////////////////////////////////
302 // int_parser_impl class
304 ///////////////////////////////////////////////////////////////////////
306 typename T = unsigned,
308 unsigned MinDigits = 1,
311 struct int_parser_impl
312 : parser<int_parser_impl<T, Radix, MinDigits, MaxDigits> >
314 typedef int_parser_impl<T, Radix, MinDigits, MaxDigits> self_t;
316 template <typename ScannerT>
319 typedef typename match_result<ScannerT, T>::type type;
322 template <typename ScannerT>
323 typename parser_result<self_t, ScannerT>::type
324 parse(ScannerT const& scan) const
326 typedef extract_int<Radix, MinDigits, MaxDigits,
327 negative_accumulate<T, Radix> > extract_int_neg_t;
328 typedef extract_int<Radix, MinDigits, MaxDigits,
329 positive_accumulate<T, Radix> > extract_int_pos_t;
334 std::size_t count = 0;
335 typename ScannerT::iterator_t save = scan.first;
337 bool hit = impl::extract_sign(scan, count);
340 hit = extract_int_neg_t::f(scan, n, count);
342 hit = extract_int_pos_t::f(scan, n, count);
345 return scan.create_match(count, n, save, scan.first);
348 // return no-match if number overflows or underflows
350 return scan.no_match();
354 ///////////////////////////////////////////////////////////////////////
356 // real_parser_impl class
358 ///////////////////////////////////////////////////////////////////////
359 template <typename RT, typename T, typename RealPoliciesT>
360 struct real_parser_impl
362 typedef real_parser_impl<RT, T, RealPoliciesT> self_t;
364 template <typename ScannerT>
365 RT parse_main(ScannerT const& scan) const
368 return scan.no_match();
369 typename ScannerT::iterator_t save = scan.first;
371 typedef typename parser_result<sign_parser, ScannerT>::type
373 typedef typename parser_result<chlit<>, ScannerT>::type
376 sign_match_t sign_match = RealPoliciesT::parse_sign(scan);
377 std::size_t count = sign_match ? sign_match.length() : 0;
378 bool neg = sign_match.has_valid_attribute() ?
379 sign_match.value() : false;
381 RT n_match = RealPoliciesT::parse_n(scan);
382 T n = n_match.has_valid_attribute() ?
383 n_match.value() : T(0);
384 bool got_a_number = n_match;
387 if (!got_a_number && !RealPoliciesT::allow_leading_dot)
388 return scan.no_match();
390 count += n_match.length();
395 if (RealPoliciesT::parse_dot(scan))
397 // We got the decimal point. Now we will try to parse
398 // the fraction if it is there. If not, it defaults
399 // to zero (0) only if we already got a number.
401 if (RT hit = RealPoliciesT::parse_frac_n(scan))
403 #if !defined(BOOST_NO_STDC_NAMESPACE)
404 using namespace std; // allow for ADL to find pow()
406 hit.value(hit.value()
407 * pow(T(10), T(-hit.length())));
412 count += hit.length() + 1;
416 else if (!got_a_number ||
417 !RealPoliciesT::allow_trailing_dot)
418 return scan.no_match();
420 e_hit = RealPoliciesT::parse_exp(scan);
424 // We have reached a point where we
425 // still haven't seen a number at all.
426 // We return early with a no-match.
428 return scan.no_match();
430 // If we must expect a dot and we didn't see
431 // an exponent, return early with a no-match.
432 e_hit = RealPoliciesT::parse_exp(scan);
433 if (RealPoliciesT::expect_dot && !e_hit)
434 return scan.no_match();
439 // We got the exponent prefix. Now we will try to parse the
440 // actual exponent. It is an error if it is not there.
441 if (RT e_n_hit = RealPoliciesT::parse_exp_n(scan))
443 #if !defined(BOOST_NO_STDC_NAMESPACE)
444 using namespace std; // allow for ADL to find pow()
446 n *= pow(T(10), T(e_n_hit.value()));
447 count += e_n_hit.length() + e_hit.length();
451 // Oops, no exponent, return a no-match
452 return scan.no_match();
456 return scan.create_match(count, n, save, scan.first);
459 template <typename ScannerT>
460 static RT parse(ScannerT const& scan)
463 return impl::implicit_lexeme_parse<RT>(this_, scan, scan);
467 #if defined(BOOST_MSVC)
473 ///////////////////////////////////////////////////////////////////////////////
474 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
476 }} // namespace boost::spirit