From 9dfbbc5671fd3e0b921542dd548931718951ce7d Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 20 Jan 2021 20:48:28 +0000 Subject: [PATCH] Fix #136 --- peglib.h | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/peglib.h b/peglib.h index dcc1bb1..f3f5224 100644 --- a/peglib.h +++ b/peglib.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -472,10 +473,17 @@ struct SemanticValues : protected std::vector { } template T token_to_number() const { - auto sv = token(); T n = 0; - std::from_chars(sv.data(), sv.data() + sv.size(), n); - return n; + if constexpr (std::is_floating_point::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); + return n; + } } // Transform the semantic value vector to another vector @@ -2684,8 +2692,9 @@ inline size_t PrecedenceClimbing::parse_expression(const char *s, size_t n, return i; } -inline size_t Recovery::parse_core(const char *s, size_t n, SemanticValues &/*vs*/, - Context &c, std::any &/*dt*/) const { +inline size_t Recovery::parse_core(const char *s, size_t n, + SemanticValues & /*vs*/, Context &c, + std::any & /*dt*/) const { auto save_log = c.log; c.log = nullptr; @@ -3683,10 +3692,17 @@ template struct AstBase : public Annotation { } template T token_to_number() const { - assert(is_token); T n = 0; - std::from_chars(token.data(), token.data() + token.size(), n); - return n; + if constexpr (std::is_floating_point::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); + return n; + } } };