diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2020-01-03 14:40:12 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2020-01-03 15:12:18 +0300 |
commit | 52e96899511189263648a1197774390b33e8b379 (patch) | |
tree | 83a83da7ff332be188add1d6ed5dd3c00c2c2975 | |
parent | lexer: refactor details/parse.cpp a bit (diff) | |
download | math-server-52e96899511189263648a1197774390b33e8b379.tar.gz math-server-52e96899511189263648a1197774390b33e8b379.zip |
lexer: refactor details/parse.cpp even more
Diffstat (limited to '')
-rw-r--r-- | server/lexer/details/parse.cpp | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/server/lexer/details/parse.cpp b/server/lexer/details/parse.cpp index 5e9ce9f..9f56573 100644 --- a/server/lexer/details/parse.cpp +++ b/server/lexer/details/parse.cpp @@ -23,7 +23,7 @@ namespace { class RegexNumberMatcher { public: bool match(const std::string_view& input) { - if (!basic_match(input)) { + if (!match_regex(input)) { return false; } // If we have the numeric part of a number followed by 'e' and no @@ -36,7 +36,7 @@ public: return true; } - virtual std::string_view get() const = 0; + virtual std::string_view to_view() const = 0; protected: // This is a hacky attempt to describe a C-like grammar for floating-point @@ -45,7 +45,7 @@ protected: static constexpr std::string_view NUMBER_REGEX{R"REGEX(^(?:\d+(?:\.\d*)?|\.\d+)(e[+-]?(\d*))?)REGEX"}; private: - virtual bool basic_match(const std::string_view& input) = 0; + virtual bool match_regex(const std::string_view& input) = 0; virtual std::string to_str() const = 0; @@ -56,15 +56,14 @@ private: class StdNumberMatcher : public RegexNumberMatcher { public: - std::string_view get() const override { - return {m_match[0].first, static_cast<std::size_t>(m_match[0].length())}; + std::string_view to_view() const override { + return {&*m_match[0].first, static_cast<std::size_t>(m_match[0].length())}; + // ^ I fucking hate C++. } private: - bool basic_match(const std::string_view& input) override { - const auto begin = input.data(); - const auto end = begin + input.length(); - return std::regex_search(begin, end, m_match, get_regex()); + bool match_regex(const std::string_view& input) override { + return std::regex_search(input.cbegin(), input.cend(), m_match, get_regex()); } static const std::regex& get_regex() { @@ -81,20 +80,19 @@ private: bool matched_e_power() const override { return m_match[2].matched && m_match[2].length() != 0; } - std::cmatch m_match; + std::match_results<std::string_view::const_iterator> m_match; }; class BoostNumberMatcher : public RegexNumberMatcher { public: - std::string_view get() const override { - return {m_match[0].first, static_cast<std::size_t>(m_match[0].length())}; + std::string_view to_view() const override { + return {&*m_match[0].first, static_cast<std::size_t>(m_match[0].length())}; + // ^ I fucking hate C++. } private: - bool basic_match(const std::string_view& input) override { - const auto begin = input.data(); - const auto end = begin + input.length(); - return boost::regex_search(begin, end, m_match, get_regex()); + bool match_regex(const std::string_view& input) override { + return boost::regex_search(input.cbegin(), input.cend(), m_match, get_regex()); } static const boost::regex& get_regex() { @@ -111,14 +109,14 @@ private: bool matched_e_power() const override { return m_match[2].matched && m_match[2].length() != 0; } - boost::cmatch m_match; + boost::match_results<std::string_view::const_iterator> m_match; }; std::optional<double> parse_number(const std::string_view& input, RegexNumberMatcher&& matcher, std::string_view& token) { if (!matcher.match(input)) { return {}; } - const auto view = matcher.get(); + const auto view = matcher.to_view(); try { const auto result = std::stod(std::string{view}); token = view; |