package net.minecraft.util.parsing.packrat; import java.util.ArrayList; import java.util.List; import net.minecraft.Util; 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 ErrorCollector.LongestOnly.MutableErrorEntry[] entries = new ErrorCollector.LongestOnly.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); ErrorCollector.LongestOnly.MutableErrorEntry[] mutableErrorEntrys = new ErrorCollector.LongestOnly.MutableErrorEntry[j]; System.arraycopy(this.entries, 0, mutableErrorEntrys, 0, i); this.entries = mutableErrorEntrys; } int j = this.nextErrorEntry++; ErrorCollector.LongestOnly.MutableErrorEntry mutableErrorEntry = this.entries[j]; if (mutableErrorEntry == null) { mutableErrorEntry = new ErrorCollector.LongestOnly.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++) { ErrorCollector.LongestOnly.MutableErrorEntry mutableErrorEntry = this.entries[j]; list.add(new ErrorEntry<>(this.lastCursor, mutableErrorEntry.suggestions, mutableErrorEntry.reason)); } return list; } } public int cursor() { return this.lastCursor; } static class MutableErrorEntry { SuggestionSupplier suggestions = SuggestionSupplier.empty(); Object reason = "empty"; } } public static class Nop implements ErrorCollector { @Override public void store(int cursor, SuggestionSupplier suggestions, Object reason) { } @Override public void finish(int cursor) { } } }