Ported new methods to StringReader from nbt parsing

This commit is contained in:
Nathan Adams
2017-07-26 14:01:43 +02:00
parent dcda383dc9
commit 9276feddd4
2 changed files with 82 additions and 0 deletions
@@ -16,6 +16,7 @@ public class StringReader {
public static final SimpleCommandExceptionType ERROR_EXPECTED_INT = new SimpleCommandExceptionType("parsing.int.expected", "Expected integer");
public static final ParameterizedCommandExceptionType ERROR_INVALID_DOUBLE = new ParameterizedCommandExceptionType("parsing.double.invalid", "Invalid double '${value}'", "value");
public static final SimpleCommandExceptionType ERROR_EXPECTED_DOUBLE = new SimpleCommandExceptionType("parsing.double.expected", "Expected double");
public static final ParameterizedCommandExceptionType ERROR_EXPECTED_SYMBOL = new ParameterizedCommandExceptionType("parsing.expected", "Expected '${symbol}'", "symbol");
private final String string;
private int cursor;
@@ -64,6 +65,10 @@ public class StringReader {
return string.charAt(cursor);
}
public char peek(final int offset) {
return string.charAt(cursor + offset);
}
public char read() {
return string.charAt(cursor++);
}
@@ -76,6 +81,12 @@ public class StringReader {
return c >= '0' && c <= '9' || c == '.' || c == '-';
}
public void skipWhitespace() {
while (canRead() && Character.isWhitespace(peek())) {
skip();
}
}
public int readInt() throws CommandException {
final int start = cursor;
while (canRead() && isAllowedNumber(peek())) {
@@ -172,4 +183,11 @@ public class StringReader {
throw ERROR_INVALID_BOOL.create(value);
}
}
public void expect(final char c) throws CommandException {
if (!canRead() || peek() != c) {
throw ERROR_EXPECTED_SYMBOL.create(String.valueOf(c));
}
skip();
}
}