jlx/libjlx/modules/sourceStream.cppm

60 lines
1 KiB
C++

module;
#include <cstddef>
#include <string>
#include <optional>
export module jlx:source_stream;
namespace jlx {
export template<typename CharT>
class source_stream {
public:
using char_traits = std::char_traits<CharT>;
using str = std::basic_string<CharT, char_traits>;
protected:
str::size_type pos = 0;
std::size_t line = 1;
std::size_t col = 0;
const str input;
public:
source_stream(const str input) : input(std::move(input)){
}
std::optional<CharT> next() {
if (pos >= input.length()) {
return std::nullopt;
}
auto ch = input.at(pos++);
if(char_traits::to_int_type(ch) == 10) {
line += 1;
col = 0;
} else {
col++;
}
return ch;
}
std::optional<CharT> peek() const {
if (pos >= input.length()) {
return std::nullopt;
}
return input.at(pos);
}
bool eof() {
return pos >= input.length();
}
size_t current_line() const {
return line;
}
size_t current_col() const {
return col;
}
};
}