Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty SNBT compounds using LinSnbtReader #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ private void readValue(boolean mustBeCompound) {
var token = read().token();
if (token instanceof SnbtToken.CompoundStart) {
stateStack.addLast(new State.InCompound());
stateStack.addLast(new State.CompoundEntryName());
tokenQueue.addLast(new LinToken.CompoundStart());
return;
}
Expand All @@ -237,8 +236,13 @@ private void readValue(boolean mustBeCompound) {
}

private void advanceCompound() {
var token = read().token();
var typing = read();
var token = typing.token();
switch (token) {
case SnbtToken.Text text -> {
readAgainStack.addLast(typing);
stateStack.addLast(new State.CompoundEntryName());
}
case SnbtToken.CompoundEnd compoundEnd -> {
stateStack.removeLast();
tokenQueue.addLast(new LinToken.CompoundEnd());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ void invalidCharacterAfterSimpleValue() throws IOException {
assertThat(ex).hasMessageThat().isEqualTo(atCharacterIndex(3) + "Unexpected token: ';'");
}

@Test
void emptyRootCompound() {
var list = ImmutableList.copyOf(ezStringRead("{}").asIterator());
assertThat(list).containsExactly(new LinToken.CompoundStart(), new LinToken.CompoundEnd()).inOrder();

list = ImmutableList.copyOf(ezStringRead("{ }").asIterator());
assertThat(list).containsExactly(new LinToken.CompoundStart(), new LinToken.CompoundEnd()).inOrder();
}

@Test
void emptyNestedCompound() {
var list = ImmutableList.copyOf(ezStringRead("{nested:{}}").asIterator());
assertThat(list).containsExactly(
new LinToken.CompoundStart(),
new LinToken.Name("nested"),
new LinToken.CompoundStart(),
new LinToken.CompoundEnd(),
new LinToken.CompoundEnd()
).inOrder();
}

@Test
void simpleValueWithWhitespace() {
var list = ImmutableList.copyOf(ezStringRead("{a:b }").asIterator());
Expand Down Expand Up @@ -168,7 +189,7 @@ void badName() throws IOException {
var reader = ezStringRead("{;");
assertThat(reader.nextOrNull()).isEqualTo(new LinToken.CompoundStart());
var ex = assertThrows(NbtParseException.class, reader::nextOrNull);
assertThat(ex).hasMessageThat().isEqualTo(atCharacterIndex(1) + "Unexpected token: ';', expected Text");
assertThat(ex).hasMessageThat().isEqualTo(atCharacterIndex(1) + "Unexpected token: ';'");
}

@Test
Expand All @@ -186,7 +207,7 @@ void badCompoundEnd() throws IOException {
assertThat(reader.nextOrNull()).isEqualTo(new LinToken.Name("a"));
assertThat(reader.nextOrNull()).isEqualTo(new LinToken.String("@"));
var ex = assertThrows(NbtParseException.class, reader::nextOrNull);
assertThat(ex).hasMessageThat().isEqualTo(atCharacterIndex(6) + "Unexpected token: Text[quoted=false, content=b]");
assertThat(ex).hasMessageThat().isEqualTo(atCharacterIndex(6) + "Unexpected end of input");
}

@Test
Expand Down