real_policies.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #if !defined(SPIRIT_REAL_POLICIES_APRIL_17_2006_1158PM)
  8. #define SPIRIT_REAL_POLICIES_APRIL_17_2006_1158PM
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/spirit/home/qi/numeric/numeric_utils.hpp>
  13. #include <boost/spirit/home/qi/detail/string_parse.hpp>
  14. namespace boost { namespace spirit { namespace qi
  15. {
  16. ///////////////////////////////////////////////////////////////////////////
  17. // Default (unsigned) real number policies
  18. ///////////////////////////////////////////////////////////////////////////
  19. template <typename T>
  20. struct ureal_policies
  21. {
  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;
  26. template <typename Iterator>
  27. static bool
  28. parse_sign(Iterator& /*first*/, Iterator const& /*last*/)
  29. {
  30. return false;
  31. }
  32. template <typename Iterator, typename Attribute>
  33. static bool
  34. parse_n(Iterator& first, Iterator const& last, Attribute& attr_)
  35. {
  36. return extract_uint<T, 10, 1, -1>::call(first, last, attr_);
  37. }
  38. template <typename Iterator>
  39. static bool
  40. parse_dot(Iterator& first, Iterator const& last)
  41. {
  42. if (first == last || *first != '.')
  43. return false;
  44. ++first;
  45. return true;
  46. }
  47. template <typename Iterator, typename Attribute>
  48. static bool
  49. parse_frac_n(Iterator& first, Iterator const& last, Attribute& attr_)
  50. {
  51. return extract_uint<T, 10, 1, -1, true>::call(first, last, attr_);
  52. }
  53. template <typename Iterator>
  54. static bool
  55. parse_exp(Iterator& first, Iterator const& last)
  56. {
  57. if (first == last || (*first != 'e' && *first != 'E'))
  58. return false;
  59. ++first;
  60. return true;
  61. }
  62. template <typename Iterator>
  63. static bool
  64. parse_exp_n(Iterator& first, Iterator const& last, int& attr_)
  65. {
  66. return extract_int<int, 10, 1, -1>::call(first, last, attr_);
  67. }
  68. ///////////////////////////////////////////////////////////////////////
  69. // The parse_nan() and parse_inf() functions get called whenever:
  70. //
  71. // - a number to parse does not start with a digit (after having
  72. // successfully parsed an optional sign)
  73. //
  74. // or
  75. //
  76. // - after a floating point number of the value 1 (having no
  77. // exponential part and a fractional part value of 0) has been
  78. // parsed.
  79. //
  80. // The first call allows to recognize representations of NaN or Inf
  81. // starting with a non-digit character (such as NaN, Inf, QNaN etc.).
  82. //
  83. // The second call allows to recognize representation formats starting
  84. // with a 1.0 (such as 1.0#NAN or 1.0#INF etc.).
  85. //
  86. // The functions should return true if a Nan or Inf has been found. In
  87. // this case the attr should be set to the matched value (NaN or
  88. // Inf). The optional sign will be automatically applied afterwards.
  89. //
  90. // The default implementation below recognizes representations of NaN
  91. // and Inf as mandated by the C99 Standard and as proposed for
  92. // inclusion into the C++0x Standard: nan, nan(...), inf and infinity
  93. // (the matching is performed case-insensitively).
  94. ///////////////////////////////////////////////////////////////////////
  95. template <typename Iterator, typename Attribute>
  96. static bool
  97. parse_nan(Iterator& first, Iterator const& last, Attribute& attr_)
  98. {
  99. if (first == last)
  100. return false; // end of input reached
  101. if (*first != 'n' && *first != 'N')
  102. return false; // not "nan"
  103. // nan[(...)] ?
  104. if (detail::string_parse("nan", "NAN", first, last, unused))
  105. {
  106. if (*first == '(')
  107. {
  108. // skip trailing (...) part
  109. Iterator i = first;
  110. while (++i != last && *i != ')')
  111. ;
  112. if (i == last)
  113. return false; // no trailing ')' found, give up
  114. first = ++i;
  115. }
  116. attr_ = std::numeric_limits<T>::quiet_NaN();
  117. return true;
  118. }
  119. return false;
  120. }
  121. template <typename Iterator, typename Attribute>
  122. static bool
  123. parse_inf(Iterator& first, Iterator const& last, Attribute& attr_)
  124. {
  125. if (first == last)
  126. return false; // end of input reached
  127. if (*first != 'i' && *first != 'I')
  128. return false; // not "inf"
  129. // inf or infinity ?
  130. if (detail::string_parse("inf", "INF", first, last, unused))
  131. {
  132. // skip allowed 'inity' part of infinity
  133. detail::string_parse("inity", "INITY", first, last, unused);
  134. attr_ = std::numeric_limits<T>::infinity();
  135. return true;
  136. }
  137. return false;
  138. }
  139. };
  140. ///////////////////////////////////////////////////////////////////////////
  141. // Default (signed) real number policies
  142. ///////////////////////////////////////////////////////////////////////////
  143. template <typename T>
  144. struct real_policies : ureal_policies<T>
  145. {
  146. template <typename Iterator>
  147. static bool
  148. parse_sign(Iterator& first, Iterator const& last)
  149. {
  150. return extract_sign(first, last);
  151. }
  152. };
  153. template <typename T>
  154. struct strict_ureal_policies : ureal_policies<T>
  155. {
  156. static bool const expect_dot = true;
  157. };
  158. template <typename T>
  159. struct strict_real_policies : real_policies<T>
  160. {
  161. static bool const expect_dot = true;
  162. };
  163. }}}
  164. #endif