Ported new methods to StringReader from nbt parsing
This commit is contained in:
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import java.util.Collections;
|
|||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class StringReaderTest {
|
||||
@Test
|
||||
|
@ -55,6 +56,17 @@ public class StringReaderTest {
|
|||
assertThat(reader.getCursor(), is(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void peek_length() throws Exception {
|
||||
final StringReader reader = new StringReader("abc");
|
||||
assertThat(reader.peek(0), is('a'));
|
||||
assertThat(reader.peek(2), is('c'));
|
||||
assertThat(reader.getCursor(), is(0));
|
||||
reader.setCursor(1);
|
||||
assertThat(reader.peek(1), is('c'));
|
||||
assertThat(reader.getCursor(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws Exception {
|
||||
final StringReader reader = new StringReader("abc");
|
||||
|
@ -91,6 +103,27 @@ public class StringReaderTest {
|
|||
assertThat(reader.getRead(), equalTo("Hello!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipWhitespace_none() throws Exception {
|
||||
final StringReader reader = new StringReader("Hello!");
|
||||
reader.skipWhitespace();
|
||||
assertThat(reader.getCursor(), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipWhitespace_mixed() throws Exception {
|
||||
final StringReader reader = new StringReader(" \t \t\nHello!");
|
||||
reader.skipWhitespace();
|
||||
assertThat(reader.getCursor(), is(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipWhitespace_empty() throws Exception {
|
||||
final StringReader reader = new StringReader("");
|
||||
reader.skipWhitespace();
|
||||
assertThat(reader.getCursor(), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readUnquotedString() throws Exception {
|
||||
final StringReader reader = new StringReader("hello world");
|
||||
|
@ -320,4 +353,35 @@ public class StringReaderTest {
|
|||
assertThat(reader.getRead(), equalTo("12.34"));
|
||||
assertThat(reader.getRemaining(), equalTo("foo bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expect_correct() throws Exception {
|
||||
final StringReader reader = new StringReader("abc");
|
||||
reader.expect('a');
|
||||
assertThat(reader.getCursor(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expect_incorrect() throws Exception {
|
||||
final StringReader reader = new StringReader("bcd");
|
||||
try {
|
||||
reader.expect('a');
|
||||
fail();
|
||||
} catch (final CommandException ex) {
|
||||
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_SYMBOL));
|
||||
assertThat(ex.getData(), equalTo(ImmutableMap.of("symbol", "a")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expect_none() throws Exception {
|
||||
final StringReader reader = new StringReader("");
|
||||
try {
|
||||
reader.expect('a');
|
||||
fail();
|
||||
} catch (final CommandException ex) {
|
||||
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_SYMBOL));
|
||||
assertThat(ex.getData(), equalTo(ImmutableMap.of("symbol", "a")));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue