Implement basic SNBT reader and testsuite
This commit is contained in:
parent
396c660113
commit
a0f372693b
17 changed files with 352 additions and 2 deletions
|
@ -408,3 +408,124 @@ class StringBuilder {
|
|||
return "${_buffer}";
|
||||
}
|
||||
}
|
||||
|
||||
class StringReader {
|
||||
final String _buffer;
|
||||
int _position = 0;
|
||||
int _lastPostion = 0;
|
||||
|
||||
StringReader(this._buffer);
|
||||
|
||||
// Check if there's more to read
|
||||
bool get canRead => _position < _buffer.length;
|
||||
|
||||
// Read the next character
|
||||
String next() {
|
||||
if (canRead) {
|
||||
skipWhitespace();
|
||||
return _buffer[_position++];
|
||||
} else {
|
||||
throw Exception("End of buffer reached");
|
||||
}
|
||||
}
|
||||
|
||||
// Peek the next character without advancing the position
|
||||
String peek() {
|
||||
skipWhitespace();
|
||||
if (canRead) {
|
||||
return _buffer[_position];
|
||||
} else {
|
||||
throw Exception("End of buffer reached");
|
||||
}
|
||||
}
|
||||
|
||||
// Skip any whitespace characters
|
||||
void skipWhitespace() {
|
||||
while (canRead && isWhitespace(_buffer[_position])) {
|
||||
_position++;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if a character is a whitespace
|
||||
bool isWhitespace(String char) {
|
||||
return char.trim().isEmpty;
|
||||
}
|
||||
|
||||
// Read until a specific character is found
|
||||
String readUntil(String stopChar) {
|
||||
StringBuffer result = StringBuffer();
|
||||
while (canRead && peek() != stopChar) {
|
||||
result.write(next());
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
// Read a string enclosed in double quotes
|
||||
String readQuotedString() {
|
||||
if (next() != '"') {
|
||||
throw Exception('Expected double quotes at the start of a string');
|
||||
}
|
||||
StringBuffer result = StringBuffer();
|
||||
while (canRead) {
|
||||
String char = next();
|
||||
if (char == '"') {
|
||||
break;
|
||||
}
|
||||
result.write(char);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
// Read a number (int or double)
|
||||
String readNumber() {
|
||||
StringBuffer result = StringBuffer();
|
||||
while (canRead && (isDigit(peek()) || peek() == '.' || peek() == '-')) {
|
||||
result.write(next());
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
// Check if a character is a digit
|
||||
bool isDigit(String char) {
|
||||
return int.tryParse(char) != null;
|
||||
}
|
||||
|
||||
// Read an unquoted string (used for keys in SNBT)
|
||||
String readUnquotedString() {
|
||||
StringBuffer result = StringBuffer();
|
||||
while (canRead &&
|
||||
!isWhitespace(peek()) &&
|
||||
peek() != ':' &&
|
||||
peek() != ',' &&
|
||||
peek() != '{' &&
|
||||
peek() != '}' &&
|
||||
peek() != '[' &&
|
||||
peek() != ']') {
|
||||
result.write(next());
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
String readString() {
|
||||
if (peek() == "\"") {
|
||||
return readQuotedString();
|
||||
} else
|
||||
return readUnquotedString();
|
||||
}
|
||||
|
||||
// Read a specific character and throw an exception if it's not found
|
||||
void expect(String expectedChar) {
|
||||
if (next().toLowerCase() != expectedChar.toLowerCase()) {
|
||||
throw Exception('Expected $expectedChar');
|
||||
}
|
||||
}
|
||||
|
||||
void startSeek() {
|
||||
_lastPostion = _position;
|
||||
}
|
||||
|
||||
void endSeek() {
|
||||
_position = _lastPostion;
|
||||
_lastPostion = 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue