Skip to content
Merged
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
1 change: 1 addition & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ configure<JavaPluginExtension> {
}

dependencies {
implementation(libs.crankcase.checkstyle)
implementation(libs.crankcase.java)
implementation(libs.crankcase.javaLibrary)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import org.gradle.api.plugins.quality.Checkstyle
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.testing.jacoco.tasks.JacocoCoverageVerification

plugins {
id("org.enginehub.crankcase.java")
id("org.enginehub.crankcase.checkstyle")
jacoco
}

Expand All @@ -11,6 +13,15 @@ crankcaseJava {
disabledLints.add("module")
}

crankcaseCheckstyle {
suppressionsFile = isolated.rootProject.projectDirectory.file("config/checkstyle/suppressions.xml")
}

tasks.withType<Checkstyle>().configureEach {
// Checkstyle has no grammar for module declarations: checkstyle/checkstyle#8240
exclude("**/module-info.java")
}

jacoco {
toolVersion = "0.8.15"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,18 @@ private static final class CountToTen extends AbstractIterator<String> {
* Ensures that computeNext is only called until it returns end().
*/
private boolean tripwire = false;
private int i = 0;
private int index = 0;

@Override
protected String computeNext() {
if (i >= 10) {
if (index >= 10) {
if (tripwire) {
throw new IllegalStateException("Should not have called this!");
}
tripwire = true;
return end();
}
i++;
index++;
return "A simple test!";
}
}
Expand Down
7 changes: 7 additions & 0 deletions config/checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<suppress checks="AvoidNestedBlocks" files="[\\/]src[\\/]test[\\/]"/>
</suppressions>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public final class Elusion {
/**
* Is {@code c} a character than can be emitted without quotes?
* Checks if {@code c} is a character that can be emitted without quotes.
*
* @param c character to check
* @return {@code true} if {@code c} is a safe character
Expand Down Expand Up @@ -56,6 +56,7 @@ public static CharSequence escapeIfNeeded(String s) {
case '\'' -> singleCharCount++;
case '"' -> doubleCharCount++;
case '\\' -> backslashCount++;
default -> { }
}
}
if (totallySafe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* Implementation of {@link LinStringIO#write(Appendable, LinStreamable)}.
*/
public class LinSnbtWriter {
private sealed interface WriteState {
private sealed interface WriteState permits WriteState.List, WriteState.Compound, WriteState.WritingArray {
record List(int remainingValues) implements WriteState {
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public void write(Appendable output, LinStream tokens) throws IOException {
break;
}
switch (token) {
case LinToken.Name(String name, Optional<LinTagId> id) -> {
case LinToken.Name(String name, Optional<LinTagId> _) -> {
if (!(state instanceof WriteState.Compound)) {
throw new NbtWriteException("Names can only appear inside compounds");
}
Expand All @@ -90,7 +90,7 @@ public void write(Appendable output, LinStream tokens) throws IOException {
}
output.append(Elusion.escapeIfNeeded(name)).append(':');
}
case LinToken.ByteArrayStart byteArrayStart -> output.append("[B;");
case LinToken.ByteArrayStart _ -> output.append("[B;");
case LinToken.ByteArrayContent(ByteBuffer buffer) -> {
if (state instanceof WriteState.WritingArray) {
output.append(',');
Expand All @@ -104,7 +104,7 @@ public void write(Appendable output, LinStream tokens) throws IOException {
}
}
}
case LinToken.ByteArrayEnd byteArrayEnd -> {
case LinToken.ByteArrayEnd _ -> {
if (state instanceof WriteState.WritingArray) {
stateStack.removeLast();
}
Expand All @@ -117,12 +117,12 @@ public void write(Appendable output, LinStream tokens) throws IOException {

handleValueEnd(output);
}
case LinToken.CompoundStart compoundStart -> {
case LinToken.CompoundStart _ -> {
output.append('{');

stateStack.addLast(WriteState.Compound.DEFAULT);
}
case LinToken.CompoundEnd compoundEnd -> {
case LinToken.CompoundEnd _ -> {
output.append('}');

stateStack.removeLast();
Expand All @@ -138,7 +138,7 @@ public void write(Appendable output, LinStream tokens) throws IOException {

handleValueEnd(output);
}
case LinToken.IntArrayStart intArrayStart -> output.append("[I;");
case LinToken.IntArrayStart _ -> output.append("[I;");
case LinToken.IntArrayContent(IntBuffer buffer) -> {
if (state instanceof WriteState.WritingArray) {
output.append(',');
Expand All @@ -152,7 +152,7 @@ public void write(Appendable output, LinStream tokens) throws IOException {
}
}
}
case LinToken.IntArrayEnd intArrayEnd -> {
case LinToken.IntArrayEnd _ -> {
if (state instanceof WriteState.WritingArray) {
stateStack.removeLast();
}
Expand All @@ -165,18 +165,18 @@ public void write(Appendable output, LinStream tokens) throws IOException {

handleValueEnd(output);
}
case LinToken.ListStart(OptionalInt size, Optional<LinTagId> elementId) -> {
case LinToken.ListStart(OptionalInt size, Optional<LinTagId> _) -> {
output.append('[');

stateStack.addLast(new WriteState.List(size.orElseThrow()));
}
case LinToken.ListEnd listEnd -> {
case LinToken.ListEnd _ -> {
output.append(']');

stateStack.removeLast();
handleValueEnd(output);
}
case LinToken.LongArrayStart longArrayStart -> output.append("[L;");
case LinToken.LongArrayStart _ -> output.append("[L;");
case LinToken.LongArrayContent(LongBuffer buffer) -> {
if (state instanceof WriteState.WritingArray) {
output.append(',');
Expand All @@ -190,7 +190,7 @@ public void write(Appendable output, LinStream tokens) throws IOException {
}
}
}
case LinToken.LongArrayEnd longArrayEnd -> {
case LinToken.LongArrayEnd _ -> {
if (state instanceof WriteState.WritingArray) {
stateStack.removeLast();
}
Expand Down Expand Up @@ -229,7 +229,7 @@ private void handleValueEnd(Appendable output) throws IOException {
output.append(',');
}
}
case WriteState.Compound compound -> stateStack.addLast(WriteState.Compound.HAS_PREVIOUS_ENTRY);
case WriteState.Compound _ -> stateStack.addLast(WriteState.Compound.HAS_PREVIOUS_ENTRY);
default -> throw new NbtWriteException("Unexpected state: " + state);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
*/
public class LinSnbtReader implements LinStream {

private sealed interface State {
private sealed interface State permits
State.InCompound, State.CompoundEntryName, State.InList, State.InByteArray, State.InIntArray,
State.InLongArray, State.ReadValue {
/**
* We're inside a compound right now.
*
Expand Down Expand Up @@ -191,24 +193,24 @@ private NbtParseException unexpectedTokenSpecificError(SnbtToken token, String e
private void fillTokenStack(State state) {
switch (state) {
case State.ReadValue readValue -> readValue(readValue);
case State.InCompound inCompound -> advanceCompound();
case State.CompoundEntryName compoundEntryName -> readName();
case State.InList inList -> advanceList();
case State.InByteArray inByteArray -> advanceArray(
case State.InCompound _ -> advanceCompound();
case State.CompoundEntryName _ -> readName();
case State.InList _ -> advanceList();
case State.InByteArray _ -> advanceArray(
LinToken.Byte.class,
ByteBuffer::allocate,
(buffer, t) -> buffer.put(t.value()),
buffer -> new LinToken.ByteArrayContent(buffer.flip().asReadOnlyBuffer()),
LinToken.ByteArrayEnd::new
);
case State.InIntArray inIntArray -> advanceArray(
case State.InIntArray _ -> advanceArray(
LinToken.Int.class,
IntBuffer::allocate,
(buffer, t) -> buffer.put(t.value()),
buffer -> new LinToken.IntArrayContent(buffer.flip().asReadOnlyBuffer()),
LinToken.IntArrayEnd::new
);
case State.InLongArray inLongArray -> advanceArray(
case State.InLongArray _ -> advanceArray(
LinToken.Long.class,
LongBuffer::allocate,
(buffer, t) -> buffer.put(t.value()),
Expand All @@ -234,7 +236,7 @@ private void readValue(State.ReadValue readValue) {
}

switch (token) {
case SnbtToken.ListLikeStart listLikeStart -> prepareListLike();
case SnbtToken.ListLikeStart _ -> prepareListLike();
case SnbtToken.Text(boolean quoted, String content) -> {
var linToken = quoted ? new LinToken.String(content) : getTokenFor(content);
tokenQueue.addLast(linToken);
Expand All @@ -247,15 +249,15 @@ private void advanceCompound() {
var typing = read();
var token = typing.token();
switch (token) {
case SnbtToken.Text text -> {
case SnbtToken.Text _ -> {
readAgainStack.addLast(typing);
stateStack.addLast(State.CompoundEntryName.INSTANCE);
}
case SnbtToken.CompoundEnd compoundEnd -> {
case SnbtToken.CompoundEnd _ -> {
stateStack.removeLast();
tokenQueue.addLast(new LinToken.CompoundEnd());
}
case SnbtToken.Separator separator -> stateStack.addLast(State.CompoundEntryName.INSTANCE);
case SnbtToken.Separator _ -> stateStack.addLast(State.CompoundEntryName.INSTANCE);
default -> throw unexpectedTokenError(token);
}
}
Expand All @@ -278,11 +280,11 @@ private void readName() {
private void advanceList() {
var token = read().token();
switch (token) {
case SnbtToken.ListLikeEnd listLikeEnd -> {
case SnbtToken.ListLikeEnd _ -> {
stateStack.removeLast();
tokenQueue.addLast(new LinToken.ListEnd());
}
case SnbtToken.Separator separator -> stateStack.addLast(State.ReadValue.ANY);
case SnbtToken.Separator _ -> stateStack.addLast(State.ReadValue.ANY);
default -> throw unexpectedTokenError(token);
}
}
Expand Down Expand Up @@ -361,21 +363,21 @@ private LinToken getTokenFor(String valueString) {
case 'B', 'b' -> {
try {
yield new LinToken.Byte(Byte.parseByte(valueString.substring(0, valueString.length() - 1)));
} catch (NumberFormatException e) {
} catch (NumberFormatException _) {
yield new LinToken.String(valueString);
}
}
case 'L', 'l' -> {
try {
yield new LinToken.Long(Long.parseLong(valueString.substring(0, valueString.length() - 1)));
} catch (NumberFormatException e) {
} catch (NumberFormatException _) {
yield new LinToken.String(valueString);
}
}
case 'S', 's' -> {
try {
yield new LinToken.Short(Short.parseShort(valueString.substring(0, valueString.length() - 1)));
} catch (NumberFormatException e) {
} catch (NumberFormatException _) {
yield new LinToken.String(valueString);
}
}
Expand All @@ -384,28 +386,28 @@ private LinToken getTokenFor(String valueString) {
case 'F', 'f' -> {
try {
yield new LinToken.Float(Float.parseFloat(valueString.substring(0, valueString.length() - 1)));
} catch (NumberFormatException e) {
} catch (NumberFormatException _) {
yield new LinToken.String(valueString);
}
}
case 'D', 'd' -> {
try {
yield new LinToken.Double(Double.parseDouble(valueString.substring(0, valueString.length() - 1)));
} catch (NumberFormatException e) {
} catch (NumberFormatException _) {
yield new LinToken.String(valueString);
}
}
default -> {
// Might be an integer.
try {
yield new LinToken.Int(Integer.parseInt(valueString));
} catch (NumberFormatException e) {
} catch (NumberFormatException _) {
// Nope.
}
// Might be a double.
try {
yield new LinToken.Double(Double.parseDouble(valueString));
} catch (NumberFormatException e) {
} catch (NumberFormatException _) {
// Nope.
}
// Might be a boolean.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
/**
* Not to be confused with {@link LinToken}.
*/
public sealed interface SnbtToken {
public sealed interface SnbtToken permits
SnbtToken.CompoundStart, SnbtToken.CompoundEnd, SnbtToken.ListLikeStart, SnbtToken.ListLikeEnd,
SnbtToken.EntrySeparator, SnbtToken.ListTypeSeparator, SnbtToken.Separator, SnbtToken.Text {
/**
* '{'.
*/
Expand Down Expand Up @@ -55,7 +57,7 @@ public String toString() {
}

/**
* '['
* '['.
*/
enum ListLikeStart implements SnbtToken {
/**
Expand All @@ -70,7 +72,7 @@ public String toString() {
}

/**
* ']'
* ']'.
*/
enum ListLikeEnd implements SnbtToken {
/**
Expand All @@ -85,7 +87,7 @@ public String toString() {
}

/**
* ':'
* ':'.
*/
enum EntrySeparator implements SnbtToken {
/**
Expand All @@ -100,7 +102,7 @@ public String toString() {
}

/**
* ';'
* ';'.
*/
enum ListTypeSeparator implements SnbtToken {
/**
Expand All @@ -115,7 +117,7 @@ public String toString() {
}

/**
* ','
* ','.
*/
enum Separator implements SnbtToken {
/**
Expand Down
Loading