From 23b284d0a3d1184850b3ad08d8374d3617f7bd03 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 28 Jan 2021 19:21:59 -0500 Subject: [PATCH] Code cleanup --- peglib.h | 66 ++++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 40 deletions(-) diff --git a/peglib.h b/peglib.h index ecf31d8..73ab200 100644 --- a/peglib.h +++ b/peglib.h @@ -325,6 +325,24 @@ inline std::string resolve_escape_sequence(const char *s, size_t n) { return r; } +/*----------------------------------------------------------------------------- + * token_to_number_ - This function should be removed eventually + *---------------------------------------------------------------------------*/ + +template T token_to_number_(std::string_view sv) { + T n = 0; +#if __has_include() + if constexpr (!std::is_floating_point::value) { + std::from_chars(sv.data(), sv.data() + sv.size(), n); + return n; + } +#endif + auto s = std::string(sv); + std::istringstream ss(s); + ss >> n; + return n; +} + /*----------------------------------------------------------------------------- * Trie *---------------------------------------------------------------------------*/ @@ -479,23 +497,7 @@ struct SemanticValues : protected std::vector { } template T token_to_number() const { - T n = 0; -#if __has_include() - 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; - } -#else - std::istringstream ss(token_to_string()); - ss >> n; - return n; -#endif + return token_to_number_(token()); } // Transform the semantic value vector to another vector @@ -549,7 +551,7 @@ private: /* * Semantic action */ -template std::any call(F fn, Args &&... args) { +template std::any call(F fn, Args &&...args) { using R = decltype(fn(std::forward(args)...)); if constexpr (std::is_void::value) { fn(std::forward(args)...); @@ -942,7 +944,7 @@ public: class Sequence : public Ope { public: template - Sequence(const Args &... args) + Sequence(const Args &...args) : opes_{static_cast>(args)...} {} Sequence(const std::vector> &opes) : opes_(opes) {} Sequence(std::vector> &&opes) : opes_(opes) {} @@ -985,7 +987,7 @@ public: class PrioritizedChoice : public Ope { public: template - PrioritizedChoice(bool for_label, const Args &... args) + PrioritizedChoice(bool for_label, const Args &...args) : opes_{static_cast>(args)...}, for_label_(for_label) {} PrioritizedChoice(const std::vector> &opes) @@ -1543,16 +1545,16 @@ public: /* * Factories */ -template std::shared_ptr seq(Args &&... args) { +template std::shared_ptr seq(Args &&...args) { return std::make_shared(static_cast>(args)...); } -template std::shared_ptr cho(Args &&... args) { +template std::shared_ptr cho(Args &&...args) { return std::make_shared( false, static_cast>(args)...); } -template std::shared_ptr cho4label_(Args &&... args) { +template std::shared_ptr cho4label_(Args &&...args) { return std::make_shared( true, static_cast>(args)...); } @@ -3777,23 +3779,7 @@ template struct AstBase : public Annotation { } template T token_to_number() const { - T n = 0; -#if __has_include() - 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; - } -#else - std::istringstream ss(token_to_string()); - ss >> n; - return n; -#endif + return token_to_number_(token); } };