1 /*=============================================================================
2 Copyright (c) 2001-2014 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
11 #include <boost/spirit/home/x3/string/detail/string_parse.hpp>
12 #include <boost/spirit/home/x3/support/numeric_utils/extract_int.hpp>
14 namespace boost { namespace spirit { namespace x3
16 ///////////////////////////////////////////////////////////////////////////
17 // Default (unsigned) real number policies
18 ///////////////////////////////////////////////////////////////////////////
22 // trailing dot policy suggested by Gustavo Guerra
23 static bool const allow_leading_dot = true;
24 static bool const allow_trailing_dot = true;
25 static bool const expect_dot = false;
27 template <typename Iterator>
29 parse_sign(Iterator& /*first*/, Iterator const& /*last*/)
34 template <typename Iterator, typename Attribute>
36 parse_n(Iterator& first, Iterator const& last, Attribute& attr_)
38 return extract_uint<T, 10, 1, -1>::call(first, last, attr_);
41 template <typename Iterator>
43 parse_dot(Iterator& first, Iterator const& last)
45 if (first == last || *first != '.')
51 template <typename Iterator, typename Attribute>
53 parse_frac_n(Iterator& first, Iterator const& last, Attribute& attr_)
55 return extract_uint<T, 10, 1, -1, true>::call(first, last, attr_);
58 template <typename Iterator>
60 parse_exp(Iterator& first, Iterator const& last)
62 if (first == last || (*first != 'e' && *first != 'E'))
68 template <typename Iterator>
70 parse_exp_n(Iterator& first, Iterator const& last, int& attr_)
72 return extract_int<int, 10, 1, -1>::call(first, last, attr_);
75 ///////////////////////////////////////////////////////////////////////
76 // The parse_nan() and parse_inf() functions get called whenever:
78 // - a number to parse does not start with a digit (after having
79 // successfully parsed an optional sign)
83 // - after a floating point number of the value 1 (having no
84 // exponential part and a fractional part value of 0) has been
87 // The first call allows to recognize representations of NaN or Inf
88 // starting with a non-digit character (such as NaN, Inf, QNaN etc.).
90 // The second call allows to recognize representation formats starting
91 // with a 1.0 (such as 1.0#NAN or 1.0#INF etc.).
93 // The functions should return true if a Nan or Inf has been found. In
94 // this case the attr should be set to the matched value (NaN or
95 // Inf). The optional sign will be automatically applied afterwards.
97 // The default implementation below recognizes representations of NaN
98 // and Inf as mandated by the C99 Standard and as proposed for
99 // inclusion into the C++0x Standard: nan, nan(...), inf and infinity
100 // (the matching is performed case-insensitively).
101 ///////////////////////////////////////////////////////////////////////
102 template <typename Iterator, typename Attribute>
104 parse_nan(Iterator& first, Iterator const& last, Attribute& attr_)
107 return false; // end of input reached
109 if (*first != 'n' && *first != 'N')
110 return false; // not "nan"
113 if (detail::string_parse("nan", "NAN", first, last, unused))
117 // skip trailing (...) part
120 while (++i != last && *i != ')')
123 return false; // no trailing ')' found, give up
127 attr_ = std::numeric_limits<T>::quiet_NaN();
133 template <typename Iterator, typename Attribute>
135 parse_inf(Iterator& first, Iterator const& last, Attribute& attr_)
138 return false; // end of input reached
140 if (*first != 'i' && *first != 'I')
141 return false; // not "inf"
144 if (detail::string_parse("inf", "INF", first, last, unused))
146 // skip allowed 'inity' part of infinity
147 detail::string_parse("inity", "INITY", first, last, unused);
148 attr_ = std::numeric_limits<T>::infinity();
155 ///////////////////////////////////////////////////////////////////////////
156 // Default (signed) real number policies
157 ///////////////////////////////////////////////////////////////////////////
158 template <typename T>
159 struct real_policies : ureal_policies<T>
161 template <typename Iterator>
163 parse_sign(Iterator& first, Iterator const& last)
165 return extract_sign(first, last);
169 template <typename T>
170 struct strict_ureal_policies : ureal_policies<T>
172 static bool const expect_dot = true;
175 template <typename T>
176 struct strict_real_policies : real_policies<T>
178 static bool const expect_dot = true;