aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/server/lexer
diff options
context:
space:
mode:
Diffstat (limited to 'server/lexer')
-rw-r--r--server/lexer/details/parse.cpp38
-rw-r--r--server/lexer/details/parse.hpp4
-rw-r--r--server/lexer/error.hpp6
-rw-r--r--server/lexer/input.hpp11
-rw-r--r--server/lexer/lexer.cpp15
-rw-r--r--server/lexer/lexer.hpp12
-rw-r--r--server/lexer/token.cpp27
-rw-r--r--server/lexer/token.hpp6
-rw-r--r--server/lexer/token_type.cpp25
-rw-r--r--server/lexer/token_type.hpp2
10 files changed, 61 insertions, 85 deletions
diff --git a/server/lexer/details/parse.cpp b/server/lexer/details/parse.cpp
index 15a4428..ccabb7e 100644
--- a/server/lexer/details/parse.cpp
+++ b/server/lexer/details/parse.cpp
@@ -9,7 +9,6 @@
#include <boost/regex.hpp>
#include <cstddef>
-
#include <exception>
#include <optional>
#include <regex>
@@ -20,7 +19,7 @@ namespace math::server::lexer::details {
namespace {
// This approach gives GCC on Travis an "internal compiler error":
-//template <template <typename> class MatchResultsT>
+// template <template <typename> class MatchResultsT>
template <typename MatchResultsT>
class RegexMatcher {
@@ -61,7 +60,8 @@ protected:
// This is a hacky attempt to describe a C-like grammar for floating-point
// numbers using a regex (the tests seem to pass though).
// A proper NFA would be better, I guess.
- static constexpr std::string_view NUMBER_REGEX{R"REGEX(^(?:\d+(?:\.\d*)?|\.\d+)(e[+-]?(\d*))?)REGEX"};
+ static constexpr std::string_view NUMBER_REGEX{
+ R"REGEX(^(?:\d+(?:\.\d*)?|\.\d+)(e[+-]?(\d*))?)REGEX"};
private:
bool matched_e() const { return this->m_match[1].matched; }
@@ -81,8 +81,7 @@ public:
private:
static const std::regex& get_regex() {
static constexpr auto flags =
- std::regex_constants::ECMAScript |
- std::regex_constants::icase;
+ std::regex_constants::ECMAScript | std::regex_constants::icase;
static const std::regex regex{NUMBER_REGEX.data(), NUMBER_REGEX.length(), flags};
return regex;
}
@@ -98,19 +97,16 @@ public:
private:
static const boost::regex& get_regex() {
static constexpr boost::regex::flag_type flags =
- boost::regex::ECMAScript |
- boost::regex::icase;
+ boost::regex::ECMAScript | boost::regex::icase;
static const boost::regex regex{NUMBER_REGEX.data(), NUMBER_REGEX.length(), flags};
return regex;
}
};
template <typename MatchResultsT>
-std::optional<double> parse_number(
- const std::string_view& input,
- RegexNumberMatcher<MatchResultsT>&& matcher,
- std::string_view& token) {
-
+std::optional<double> parse_number(const std::string_view& input,
+ RegexNumberMatcher<MatchResultsT>&& matcher,
+ std::string_view& token) {
if (!matcher.match(input)) {
return {};
}
@@ -162,10 +158,8 @@ private:
};
template <typename MatchResultsT>
-std::string_view parse_whitespace(
- const std::string_view& input,
- RegexWhitespaceMatcher<MatchResultsT>&& matcher) {
-
+std::string_view parse_whitespace(const std::string_view& input,
+ RegexWhitespaceMatcher<MatchResultsT>&& matcher) {
if (matcher.match_regex(input)) {
return matcher.to_view();
}
@@ -173,11 +167,10 @@ std::string_view parse_whitespace(
}
bool starts_with(const std::string_view& a, const std::string_view& b) noexcept {
- return a.length() >= b.length()
- && a.compare(0, b.length(), b) == 0;
+ return a.length() >= b.length() && a.compare(0, b.length(), b) == 0;
}
-}
+} // namespace
namespace impl {
@@ -207,7 +200,7 @@ std::string_view boost_parse_whitespace(const std::string_view& input) {
return parse_whitespace(input, BoostWhitespaceMatcher{});
}
-}
+} // namespace impl
std::optional<double> parse_number(const std::string_view& input, std::string_view& token) {
return impl::boost_parse_number(input, token);
@@ -218,7 +211,8 @@ std::optional<double> parse_number(const std::string_view& input) {
return parse_number(input, token);
}
-std::optional<token::Type> parse_const_token(const std::string_view& input, std::string_view& token) {
+std::optional<token::Type> parse_const_token(const std::string_view& input,
+ std::string_view& token) {
for (const auto type : token::const_tokens()) {
const auto str = token::type_to_string(type);
if (starts_with(input, str)) {
@@ -238,4 +232,4 @@ std::string_view parse_whitespace(const std::string_view& input) {
return impl::boost_parse_whitespace(input);
}
-}
+} // namespace math::server::lexer::details
diff --git a/server/lexer/details/parse.hpp b/server/lexer/details/parse.hpp
index 693dd35..32215f7 100644
--- a/server/lexer/details/parse.hpp
+++ b/server/lexer/details/parse.hpp
@@ -23,7 +23,7 @@ std::optional<double> boost_parse_number(const std::string_view&);
std::string_view std_parse_whitespace(const std::string_view&);
std::string_view boost_parse_whitespace(const std::string_view&);
-}
+} // namespace impl
// Exposed for testing:
std::string_view parse_whitespace(const std::string_view&);
@@ -32,4 +32,4 @@ std::optional<double> parse_number(const std::string_view&);
std::optional<token::Type> parse_const_token(const std::string_view&, std::string_view&);
std::optional<token::Type> parse_const_token(const std::string_view&);
-}
+} // namespace math::server::lexer::details
diff --git a/server/lexer/error.hpp b/server/lexer/error.hpp
index ae6828d..72b3caa 100644
--- a/server/lexer/error.hpp
+++ b/server/lexer/error.hpp
@@ -13,9 +13,7 @@ namespace math::server {
class LexerError : public Error {
public:
- explicit LexerError(const std::string &what)
- : Error{"lexer error: " + what}
- { }
+ explicit LexerError(const std::string& what) : Error{"lexer error: " + what} {}
};
-}
+} // namespace math::server
diff --git a/server/lexer/input.hpp b/server/lexer/input.hpp
index 7cf25ec..b5db77e 100644
--- a/server/lexer/input.hpp
+++ b/server/lexer/input.hpp
@@ -8,16 +8,13 @@
#include "error.hpp"
#include <cstddef>
-
#include <string_view>
namespace math::server::lexer {
class Input {
public:
- explicit Input(const std::string_view& input)
- : m_pos{0}, m_input{input}
- { }
+ explicit Input(const std::string_view& input) : m_pos{0}, m_input{input} {}
const std::string_view& get_input() const { return m_input; }
@@ -35,13 +32,11 @@ public:
m_input.remove_prefix(len);
}
- void consume(const std::string_view& sub) {
- consume(sub.length());
- }
+ void consume(const std::string_view& sub) { consume(sub.length()); }
private:
std::size_t m_pos;
std::string_view m_input;
};
-}
+} // namespace math::server::lexer
diff --git a/server/lexer/lexer.cpp b/server/lexer/lexer.cpp
index cf24189..9b4e500 100644
--- a/server/lexer/lexer.cpp
+++ b/server/lexer/lexer.cpp
@@ -3,9 +3,10 @@
// For details, see https://github.com/egor-tensin/math-server.
// Distributed under the MIT License.
+#include "lexer.hpp"
+
#include "details/parse.hpp"
#include "error.hpp"
-#include "lexer.hpp"
#include "token.hpp"
#include "token_type.hpp"
@@ -17,13 +18,9 @@
namespace math::server {
-Lexer::Lexer(const std::string_view& input)
- : Lexer{lexer::Input{input}} {
-}
-
-Lexer::Lexer(const lexer::Input& input)
- : m_input{input} {
+Lexer::Lexer(const std::string_view& input) : Lexer{lexer::Input{input}} {}
+Lexer::Lexer(const lexer::Input& input) : m_input{input} {
consume_token();
}
@@ -38,7 +35,7 @@ bool Lexer::for_each_token(const TokenProcessor& process) {
std::vector<Lexer::ParsedToken> Lexer::get_tokens() {
std::vector<ParsedToken> tokens;
- for_each_token([&tokens] (const ParsedToken& token) {
+ for_each_token([&tokens](const ParsedToken& token) {
tokens.emplace_back(token);
return true;
});
@@ -122,4 +119,4 @@ Lexer::ParsedToken Lexer::parse_token() const {
throw LexerError{"invalid input at: " + std::string{m_input.get_input()}};
}
-}
+} // namespace math::server
diff --git a/server/lexer/lexer.hpp b/server/lexer/lexer.hpp
index 68950cb..8848837 100644
--- a/server/lexer/lexer.hpp
+++ b/server/lexer/lexer.hpp
@@ -24,19 +24,15 @@ public:
using Token = lexer::Token;
using ParsedToken = lexer::ParsedToken;
using Type = Token::Type;
- using TokenProcessor = std::function<bool (const ParsedToken&)>;
+ using TokenProcessor = std::function<bool(const ParsedToken&)>;
bool for_each_token(const TokenProcessor& process);
std::vector<ParsedToken> get_tokens();
- bool has_token() const {
- return peek_token().has_value();
- }
+ bool has_token() const { return peek_token().has_value(); }
- std::optional<ParsedToken> peek_token() const {
- return m_token_buffer;
- }
+ std::optional<ParsedToken> peek_token() const { return m_token_buffer; }
void drop_token();
std::optional<ParsedToken> drop_token_of_type(Type type);
@@ -55,4 +51,4 @@ private:
std::optional<ParsedToken> m_token_buffer;
};
-}
+} // namespace math::server
diff --git a/server/lexer/token.cpp b/server/lexer/token.cpp
index 79c3a63..9bcd9f3 100644
--- a/server/lexer/token.cpp
+++ b/server/lexer/token.cpp
@@ -3,21 +3,25 @@
// For details, see https://github.com/egor-tensin/math-server.
// Distributed under the MIT License.
-#include "error.hpp"
#include "token.hpp"
+
+#include "error.hpp"
#include "token_type.hpp"
#include <cmath>
-
#include <limits>
#include <variant>
namespace math::server::lexer {
namespace {
-static constexpr double nan() { return std::numeric_limits<double>::quiet_NaN(); }
+static constexpr double nan() {
+ return std::numeric_limits<double>::quiet_NaN();
+}
-static bool is_nan(double x) { return std::isnan(x); }
+static bool is_nan(double x) {
+ return std::isnan(x);
+}
static bool numbers_equal(double x, double y) {
if (is_nan(x) && is_nan(y)) {
@@ -26,19 +30,15 @@ static bool numbers_equal(double x, double y) {
return x == y;
}
-}
-
-Token::Token(Type type)
- : m_type{type} {
+} // namespace
+Token::Token(Type type) : m_type{type} {
if (token::token_has_value(type)) {
throw LexerError{"internal: must have a value: " + token::type_to_int_string(type)};
}
}
-Token::Token(double value)
- : m_type{Type::NUMBER}, m_value{value}
-{ }
+Token::Token(double value) : m_type{Type::NUMBER}, m_value{value} {}
bool Token::operator==(const Token& other) const {
if (m_type != other.m_type) {
@@ -50,7 +50,8 @@ bool Token::operator==(const Token& other) const {
if (m_type == Type::NUMBER) {
return numbers_equal(as_number(), other.as_number());
}
- throw LexerError{"internal: can't compare tokens of type: " + token::type_to_int_string(m_type)};
+ throw LexerError{"internal: can't compare tokens of type: " +
+ token::type_to_int_string(m_type)};
}
double Token::as_number() const {
@@ -73,4 +74,4 @@ std::ostream& operator<<(std::ostream& os, const Token& token) {
return os;
}
-}
+} // namespace math::server::lexer
diff --git a/server/lexer/token.hpp b/server/lexer/token.hpp
index f351d02..aca3b50 100644
--- a/server/lexer/token.hpp
+++ b/server/lexer/token.hpp
@@ -8,7 +8,6 @@
#include "token_type.hpp"
#include <cstddef>
-
#include <string_view>
#include <utility>
#include <variant>
@@ -39,8 +38,7 @@ private:
class ParsedToken : public Token {
public:
ParsedToken(Token&& token, std::size_t pos, const std::string_view& view)
- : Token{std::move(token)}, m_pos{pos}, m_view{view} {
- }
+ : Token{std::move(token)}, m_pos{pos}, m_view{view} {}
std::size_t get_pos() const { return m_pos; }
@@ -51,4 +49,4 @@ private:
std::string_view m_view;
};
-}
+} // namespace math::server::lexer
diff --git a/server/lexer/token_type.cpp b/server/lexer/token_type.cpp
index 6a42a3d..037a761 100644
--- a/server/lexer/token_type.cpp
+++ b/server/lexer/token_type.cpp
@@ -3,9 +3,10 @@
// For details, see https://github.com/egor-tensin/math-server.
// Distributed under the MIT License.
-#include "error.hpp"
#include "token_type.hpp"
+#include "error.hpp"
+
#include <functional>
#include <map>
#include <ostream>
@@ -21,9 +22,7 @@ using FromStringMap = std::map<std::string, Type, std::greater<std::string>>;
class ToStringConverter {
public:
- ToStringConverter() : m_map{to_string_map()} {
- validate();
- }
+ ToStringConverter() : m_map{to_string_map()} { validate(); }
const ToStringMap& map() const { return m_map; }
@@ -43,16 +42,15 @@ private:
return map;
}
- void validate() const {
- check_for_duplicates();
- }
+ void validate() const { check_for_duplicates(); }
void check_for_duplicates() const {
std::unordered_set<std::string> strings;
for (const auto& [type, str] : m_map) {
const auto [_, inserted] = strings.emplace(str);
if (!inserted) {
- throw std::logic_error{"multiple tokens have the same string representation: " + str};
+ throw std::logic_error{"multiple tokens have the same string representation: " +
+ str};
}
}
}
@@ -67,9 +65,7 @@ const ToStringMap& to_string_map() {
class FromStringConverter {
public:
- FromStringConverter(const ToStringMap& to_string)
- : m_map{build_map(to_string)} {
- }
+ FromStringConverter(const ToStringMap& to_string) : m_map{build_map(to_string)} {}
const FromStringMap& map() const { return m_map; }
@@ -79,7 +75,8 @@ private:
for (const auto& [type, str] : to_string) {
const auto [_, inserted] = from_string.emplace(str, type);
if (!inserted) {
- throw std::logic_error{"multiple tokens have the same string representation: " + str};
+ throw std::logic_error{"multiple tokens have the same string representation: " +
+ str};
}
}
return from_string;
@@ -110,7 +107,7 @@ private:
TypeSet m_set;
};
-}
+} // namespace
TypeInt type_to_int(Type type) {
return static_cast<TypeInt>(type);
@@ -167,4 +164,4 @@ std::ostream& operator<<(std::ostream& os, const Type& type) {
return os;
}
-}
+} // namespace math::server::lexer::token
diff --git a/server/lexer/token_type.hpp b/server/lexer/token_type.hpp
index 2040d28..438e2dd 100644
--- a/server/lexer/token_type.hpp
+++ b/server/lexer/token_type.hpp
@@ -40,4 +40,4 @@ Type type_from_string(const std::string&);
std::ostream& operator<<(std::ostream&, const Type&);
-}
+} // namespace math::server::lexer::token