1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
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(SPIRIT_REAL_POLICIES_APRIL_17_2006_1158PM)
9 #define SPIRIT_REAL_POLICIES_APRIL_17_2006_1158PM
15 #include <boost/spirit/home/qi/numeric/numeric_utils.hpp>
16 #include <boost/spirit/home/qi/detail/string_parse.hpp>
18 namespace boost { namespace spirit { namespace qi
20 ///////////////////////////////////////////////////////////////////////////
21 // Default (unsigned) real number policies
22 ///////////////////////////////////////////////////////////////////////////
26 // trailing dot policy suggested by Gustavo Guerra
27 static bool const allow_leading_dot = true;
28 static bool const allow_trailing_dot = true;
29 static bool const expect_dot = false;
31 template <typename Iterator>
33 parse_sign(Iterator& /*first*/, Iterator const& /*last*/)
38 template <typename Iterator, typename Attribute>
40 parse_n(Iterator& first, Iterator const& last, Attribute& attr_)
42 return extract_uint<Attribute, 10, 1, -1>::call(first, last, attr_);
45 template <typename Iterator>
47 parse_dot(Iterator& first, Iterator const& last)
49 if (first == last || *first != '.')
55 template <typename Iterator, typename Attribute>
57 parse_frac_n(Iterator& first, Iterator const& last, Attribute& attr_, int& frac_digits)
59 Iterator savef = first;
60 bool r = extract_uint<Attribute, 10, 1, -1, true, true>::call(first, last, attr_);
63 // Optimization note: don't compute frac_digits if T is
64 // an unused_type. This should be optimized away by the compiler.
65 if (!is_same<T, unused_type>::value)
67 static_cast<int>(std::distance(savef, first));
68 // ignore extra (non-significant digits)
69 extract_uint<unused_type, 10, 1, -1>::call(first, last, unused);
74 template <typename Iterator>
76 parse_exp(Iterator& first, Iterator const& last)
78 if (first == last || (*first != 'e' && *first != 'E'))
84 template <typename Iterator>
86 parse_exp_n(Iterator& first, Iterator const& last, int& attr_)
88 return extract_int<int, 10, 1, -1>::call(first, last, attr_);
91 ///////////////////////////////////////////////////////////////////////
92 // The parse_nan() and parse_inf() functions get called whenever:
94 // - a number to parse does not start with a digit (after having
95 // successfully parsed an optional sign)
99 // - after a floating point number of the value 1 (having no
100 // exponential part and a fractional part value of 0) has been
103 // The first call allows to recognize representations of NaN or Inf
104 // starting with a non-digit character (such as NaN, Inf, QNaN etc.).
106 // The second call allows to recognize representation formats starting
107 // with a 1.0 (such as 1.0#NAN or 1.0#INF etc.).
109 // The functions should return true if a Nan or Inf has been found. In
110 // this case the attr should be set to the matched value (NaN or
111 // Inf). The optional sign will be automatically applied afterwards.
113 // The default implementation below recognizes representations of NaN
114 // and Inf as mandated by the C99 Standard and as proposed for
115 // inclusion into the C++0x Standard: nan, nan(...), inf and infinity
116 // (the matching is performed case-insensitively).
117 ///////////////////////////////////////////////////////////////////////
118 template <typename Iterator, typename Attribute>
120 parse_nan(Iterator& first, Iterator const& last, Attribute& attr_)
123 return false; // end of input reached
125 if (*first != 'n' && *first != 'N')
126 return false; // not "nan"
129 if (detail::string_parse("nan", "NAN", first, last, unused))
131 if (first != last && *first == '(')
133 // skip trailing (...) part
136 while (++i != last && *i != ')')
139 return false; // no trailing ')' found, give up
143 attr_ = std::numeric_limits<T>::quiet_NaN();
149 template <typename Iterator, typename Attribute>
151 parse_inf(Iterator& first, Iterator const& last, Attribute& attr_)
154 return false; // end of input reached
156 if (*first != 'i' && *first != 'I')
157 return false; // not "inf"
160 if (detail::string_parse("inf", "INF", first, last, unused))
162 // skip allowed 'inity' part of infinity
163 detail::string_parse("inity", "INITY", first, last, unused);
164 attr_ = std::numeric_limits<T>::infinity();
171 ///////////////////////////////////////////////////////////////////////////
172 // Default (signed) real number policies
173 ///////////////////////////////////////////////////////////////////////////
174 template <typename T>
175 struct real_policies : ureal_policies<T>
177 template <typename Iterator>
179 parse_sign(Iterator& first, Iterator const& last)
181 return extract_sign(first, last);
185 template <typename T>
186 struct strict_ureal_policies : ureal_policies<T>
188 static bool const expect_dot = true;
191 template <typename T>
192 struct strict_real_policies : real_policies<T>
194 static bool const expect_dot = true;