minecraft-src/net/minecraft/util/parsing/packrat/ErrorCollector.java
2025-07-04 03:45:38 +03:00

92 lines
2.5 KiB
Java

package net.minecraft.util.parsing.packrat;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.Util;
import net.minecraft.util.parsing.packrat.ErrorCollector.LongestOnly.MutableErrorEntry;
public interface ErrorCollector<S> {
void store(int cursor, SuggestionSupplier<S> suggestions, Object reason);
default void store(int cursor, Object reason) {
this.store(cursor, SuggestionSupplier.empty(), reason);
}
void finish(int cursor);
public static class LongestOnly<S> implements ErrorCollector<S> {
private MutableErrorEntry<S>[] entries = new MutableErrorEntry[16];
private int nextErrorEntry;
private int lastCursor = -1;
private void discardErrorsFromShorterParse(int cursor) {
if (cursor > this.lastCursor) {
this.lastCursor = cursor;
this.nextErrorEntry = 0;
}
}
@Override
public void finish(int cursor) {
this.discardErrorsFromShorterParse(cursor);
}
@Override
public void store(int cursor, SuggestionSupplier<S> suggestions, Object reason) {
this.discardErrorsFromShorterParse(cursor);
if (cursor == this.lastCursor) {
this.addErrorEntry(suggestions, reason);
}
}
private void addErrorEntry(SuggestionSupplier<S> suggestions, Object reason) {
int i = this.entries.length;
if (this.nextErrorEntry >= i) {
int j = Util.growByHalf(i, this.nextErrorEntry + 1);
MutableErrorEntry<S>[] mutableErrorEntrys = new MutableErrorEntry[j];
System.arraycopy(this.entries, 0, mutableErrorEntrys, 0, i);
this.entries = mutableErrorEntrys;
}
int j = this.nextErrorEntry++;
MutableErrorEntry<S> mutableErrorEntry = this.entries[j];
if (mutableErrorEntry == null) {
mutableErrorEntry = new MutableErrorEntry();
this.entries[j] = mutableErrorEntry;
}
mutableErrorEntry.suggestions = suggestions;
mutableErrorEntry.reason = reason;
}
public List<ErrorEntry<S>> entries() {
int i = this.nextErrorEntry;
if (i == 0) {
return List.of();
} else {
List<ErrorEntry<S>> list = new ArrayList(i);
for (int j = 0; j < i; j++) {
MutableErrorEntry<S> mutableErrorEntry = this.entries[j];
list.add(new ErrorEntry(this.lastCursor, mutableErrorEntry.suggestions, mutableErrorEntry.reason));
}
return list;
}
}
public int cursor() {
return this.lastCursor;
}
}
public static class Nop<S> implements ErrorCollector<S> {
@Override
public void store(int cursor, SuggestionSupplier<S> suggestions, Object reason) {
}
@Override
public void finish(int cursor) {
}
}
}