beginning of compiler
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
#include "TextReader.hpp"
|
||||
|
||||
#include <spider/compiler/text/utf8.hpp>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace spider {
|
||||
|
||||
// Text Reader //
|
||||
|
||||
int TextReader::nextByte() {
|
||||
int ch = getStream().get();
|
||||
if (ch == std::istream::traits_type::eof()) {
|
||||
return -1;
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
|
||||
bool TextReader::nextChar(u32& ch) {
|
||||
int n = nextByte();
|
||||
if(n == -1) return false;
|
||||
|
||||
isize len = utf8::seqlen(u8(n));
|
||||
if(len == 0) return false;
|
||||
|
||||
isize i = 1;
|
||||
char arr[4];
|
||||
arr[0] = char(n);
|
||||
|
||||
while(i < len) {
|
||||
n = nextByte();
|
||||
if(n == -1) return false;
|
||||
arr[i++] = char(n);
|
||||
}
|
||||
|
||||
ch = utf8::decodeArr(arr, len);
|
||||
advance(ch);
|
||||
return true;
|
||||
}
|
||||
|
||||
void TextReader::advance(u32 ch) {
|
||||
if (ch == u32('\n')) {
|
||||
if (lastWasCR) {
|
||||
lastWasCR = false; // Mixed CRLF handling
|
||||
} else {
|
||||
at.line++;
|
||||
at.col = 1;
|
||||
}
|
||||
} else if (ch == u32('\r')) {
|
||||
at.line++;
|
||||
at.col = 1;
|
||||
lastWasCR = true;
|
||||
} else {
|
||||
at.col++;
|
||||
lastWasCR = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool TextReader::isEOF() {
|
||||
return getStream().peek() == std::istream::traits_type::eof();
|
||||
}
|
||||
|
||||
pos TextReader::getPosition() const {
|
||||
return at;
|
||||
}
|
||||
|
||||
// File Reader //
|
||||
|
||||
FileTextReader::FileTextReader(const std::string& filename)
|
||||
: fileStream(filename, std::ios::binary) {
|
||||
if (!fileStream.is_open()) {
|
||||
throw std::runtime_error("Failed to open file: " + filename);
|
||||
}
|
||||
}
|
||||
|
||||
std::istream& FileTextReader::getStream() {
|
||||
return fileStream;
|
||||
}
|
||||
|
||||
// String Reader //
|
||||
|
||||
StringTextReader::StringTextReader(std::string initialText)
|
||||
: buffer(std::move(initialText)),
|
||||
stringStream(std::make_unique<std::istringstream>(buffer)) {
|
||||
}
|
||||
|
||||
std::istream& StringTextReader::getStream() {
|
||||
return *stringStream;
|
||||
}
|
||||
|
||||
void StringTextReader::set(const std::string& newText) {
|
||||
buffer = newText;
|
||||
stringStream = std::make_unique<std::istringstream>(buffer);
|
||||
lastWasCR = false;
|
||||
}
|
||||
|
||||
void StringTextReader::append(const std::string& extraText) {
|
||||
std::streampos pos = stringStream->tellg();
|
||||
buffer += extraText;
|
||||
stringStream = std::make_unique<std::istringstream>(buffer);
|
||||
stringStream->seekg(pos);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user