aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/server/lexer/input.hpp
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2019-12-07 03:36:21 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2019-12-07 03:36:21 +0300
commit00863566ec4601c65c435b74e575d49546a1c707 (patch)
tree479a0a6e96aba8191c7a65ea9bee2f4d5e3a4aba /server/lexer/input.hpp
parentadd stress_test.py (diff)
downloadmath-server-00863566ec4601c65c435b74e575d49546a1c707.tar.gz
math-server-00863566ec4601c65c435b74e575d49546a1c707.zip
split server into multiple components
In a vague attempt to make header files more readable, split server/ into a number of components. Also, refactor the unit tests to use the "Data-driven test cases" of Boost.Test.
Diffstat (limited to 'server/lexer/input.hpp')
-rw-r--r--server/lexer/input.hpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/server/lexer/input.hpp b/server/lexer/input.hpp
new file mode 100644
index 0000000..1104a4b
--- /dev/null
+++ b/server/lexer/input.hpp
@@ -0,0 +1,42 @@
+#pragma once
+
+#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}
+ { }
+
+ const std::string_view& get_input() const { return m_input; }
+
+ std::size_t get_pos() const { return m_pos; }
+
+ std::size_t get_length() const { return m_input.length(); }
+
+ bool empty() const { return m_input.empty(); }
+
+ void consume(std::size_t len) {
+ if (m_input.length() < len) {
+ throw LexerError{"internal: not enough input to consume"};
+ }
+ m_pos += len;
+ m_input.remove_prefix(len);
+ }
+
+ void consume(const std::string_view& sub) {
+ consume(sub.length());
+ }
+
+private:
+ std::size_t m_pos;
+ std::string_view m_input;
+};
+
+}