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 { void store(int cursor, SuggestionSupplier suggestions, Object reason); default void store(int cursor, Object reason) { this.store(cursor, SuggestionSupplier.empty(), reason); } void finish(int cursor); public static class LongestOnly implements ErrorCollector { private MutableErrorEntry[] 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 suggestions, Object reason) { this.discardErrorsFromShorterParse(cursor); if (cursor == this.lastCursor) { this.addErrorEntry(suggestions, reason); } } private void addErrorEntry(SuggestionSupplier suggestions, Object reason) { int i = this.entries.length; if (this.nextErrorEntry >= i) { int j = Util.growByHalf(i, this.nextErrorEntry + 1); MutableErrorEntry[] mutableErrorEntrys = new MutableErrorEntry[j]; System.arraycopy(this.entries, 0, mutableErrorEntrys, 0, i); this.entries = mutableErrorEntrys; } int j = this.nextErrorEntry++; MutableErrorEntry mutableErrorEntry = this.entries[j]; if (mutableErrorEntry == null) { mutableErrorEntry = new MutableErrorEntry(); this.entries[j] = mutableErrorEntry; } mutableErrorEntry.suggestions = suggestions; mutableErrorEntry.reason = reason; } public List> entries() { int i = this.nextErrorEntry; if (i == 0) { return List.of(); } else { List> list = new ArrayList(i); for (int j = 0; j < i; j++) { MutableErrorEntry 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 implements ErrorCollector { @Override public void store(int cursor, SuggestionSupplier suggestions, Object reason) { } @Override public void finish(int cursor) { } } }