beggining to stablish the EBNF of spider assembly

This commit is contained in:
2026-07-19 22:43:23 -06:00
parent b30a59accd
commit 3a44ec594a
7 changed files with 114 additions and 121 deletions
+25 -36
View File
@@ -33,7 +33,10 @@ namespace spider {
// and then do an easy compare!
std::u32string str;
if(!utf8::toUTF32(chars, str)) throw std::runtime_error("Specified invalid UTF-8 string!");
return eat(str);
}
bool TextReader::eat(const std::u32string& str) {
// compare now
isize index;
for(index = 0; index < str.size(); index++) {
@@ -68,46 +71,37 @@ namespace spider {
/**
* Returns the current character.
*/
u32 TextReader::current() {
optional<u32> TextReader::current() {
if (bufferIndex < buffer.size()) {
return buffer[bufferIndex];
}
return 0;
return {};
}
/**
* Reads the next character and advances the position tracker.
*/
u32 TextReader::nextChar(isize n) {
if (err) return 0;
optional<u32> TextReader::nextChar(isize n) {
// Ensure the character we are moving TO exists
if (fillBufferTo(n)) {
// advance n characters
while(n--) {
advance(current());
advance(buffer[bufferIndex]);
bufferIndex++;
}
return current();
}
// If we couldn't fill the buffer, we hit EOF
eof = true;
return 0;
return {};
}
/**
* Keeps the next n-th character (n = 0 is current).
*/
u32 TextReader::peekChar(isize n) {
if (err) return 0;
optional<u32> TextReader::peekChar(isize n) {
if (fillBufferTo(n)) return buffer[bufferIndex + n];
return 0;
return {};
}
/**
* Clears the buffer from previous characters, keeping current and future ones.
*/
void TextReader::commit() {
if (bufferIndex > 0) {
// Erase everything before the current buffer index
@@ -116,21 +110,12 @@ namespace spider {
}
}
/**
* Rolls back any previous characters within the limits of the uncommitted buffer.
*/
void TextReader::rollback(isize n) {
// Prevent rolling back past the start of our committed buffer
if (n > bufferIndex) n = bufferIndex;
// We must track positions backward or recalculate if exact column match is needed.
// Assuming simple rollback of the pointer here per definition.
bufferIndex -= n;
eof = false;
isize TextReader::push() {
return bufferIndex;
}
TextReader::operator bool() const {
return !err;
void TextReader::pop(isize index) {
bufferIndex = std::min(index, bufferIndex);
}
/**
@@ -191,16 +176,20 @@ namespace spider {
return true;
}
/**
* Returns true if the stream is consumed and no elements remain in the read buffer.
*/
bool TextReader::isEOF() {
if (err) return false;
pos TextReader::getPosition() const {
return at;
}
bool TextReader::isEOF() const{
return eof && bufferIndex >= buffer.size();
}
pos TextReader::getPosition() const {
return at;
bool TextReader::hasError() const{
return err;
}
TextReader::operator bool() const {
return !isEOF() && !hasError();
}
std::string TextReader::getError() const {