This commit is contained in:
yhirose 2021-01-20 20:48:28 +00:00
parent 1c00fa77fa
commit 9dfbbc5671

View File

@ -22,6 +22,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <set> #include <set>
#include <sstream>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -472,11 +473,18 @@ struct SemanticValues : protected std::vector<std::any> {
} }
template <typename T> T token_to_number() const { template <typename T> T token_to_number() const {
auto sv = token();
T n = 0; T n = 0;
if constexpr (std::is_floating_point<T>::value) {
// TODO: The following code should be removed eventually.
std::istringstream ss(token_to_string());
ss >> n;
return n;
} else {
auto sv = token();
std::from_chars(sv.data(), sv.data() + sv.size(), n); std::from_chars(sv.data(), sv.data() + sv.size(), n);
return n; return n;
} }
}
// Transform the semantic value vector to another vector // Transform the semantic value vector to another vector
template <typename T> template <typename T>
@ -2684,8 +2692,9 @@ inline size_t PrecedenceClimbing::parse_expression(const char *s, size_t n,
return i; return i;
} }
inline size_t Recovery::parse_core(const char *s, size_t n, SemanticValues &/*vs*/, inline size_t Recovery::parse_core(const char *s, size_t n,
Context &c, std::any &/*dt*/) const { SemanticValues & /*vs*/, Context &c,
std::any & /*dt*/) const {
auto save_log = c.log; auto save_log = c.log;
c.log = nullptr; c.log = nullptr;
@ -3683,11 +3692,18 @@ template <typename Annotation> struct AstBase : public Annotation {
} }
template <typename T> T token_to_number() const { template <typename T> T token_to_number() const {
assert(is_token);
T n = 0; T n = 0;
if constexpr (std::is_floating_point<T>::value) {
// TODO: The following code should be removed eventually.
std::istringstream ss(token_to_string());
ss >> n;
return n;
} else {
assert(is_token);
std::from_chars(token.data(), token.data() + token.size(), n); std::from_chars(token.data(), token.data() + token.size(), n);
return n; return n;
} }
}
}; };
template <typename T> template <typename T>