| 1 | package net.minecraft.util.parsing.packrat;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.util.Util;
|
| 6 | import org.jspecify.annotations.Nullable;
|
| 7 |
|
| 8 | public interface ErrorCollector<S> {
|
| 9 | void store(int cursor, SuggestionSupplier<S> suggestions, Object reason);
|
| 10 |
|
| 11 | default void store(final int cursor, final Object reason) {
|
| 12 | this.store(cursor, SuggestionSupplier.empty(), reason);
|
| 13 | }
|
| 14 |
|
| 15 | void finish(int finalCursor);
|
| 16 |
|
| 17 | class LongestOnly<S> implements ErrorCollector<S> {
|
| 18 | private ErrorCollector.LongestOnly.@Nullable MutableErrorEntry<S>[] entries = new ErrorCollector.LongestOnly.MutableErrorEntry[16];
|
| 19 | private int nextErrorEntry;
|
| 20 | private int lastCursor = -1;
|
| 21 |
|
| 22 | private void discardErrorsFromShorterParse(final int cursor) {
|
| 23 | if (cursor > this.lastCursor) {
|
| 24 | this.lastCursor = cursor;
|
| 25 | this.nextErrorEntry = 0;
|
| 26 | }
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public void finish(final int finalCursor) {
|
| 31 | this.discardErrorsFromShorterParse(finalCursor);
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public void store(final int cursor, final SuggestionSupplier<S> suggestions, final Object reason) {
|
| 36 | this.discardErrorsFromShorterParse(cursor);
|
| 37 | if (cursor == this.lastCursor) {
|
| 38 | this.addErrorEntry(suggestions, reason);
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | private void addErrorEntry(final SuggestionSupplier<S> suggestions, final Object reason) {
|
| 43 | int currentSize = this.entries.length;
|
| 44 | if (this.nextErrorEntry >= currentSize) {
|
| 45 | int newSize = Util.growByHalf(currentSize, this.nextErrorEntry + 1);
|
| 46 | ErrorCollector.LongestOnly.MutableErrorEntry<S>[] newEntries = new ErrorCollector.LongestOnly.MutableErrorEntry[newSize];
|
| 47 | System.arraycopy(this.entries, 0, newEntries, 0, currentSize);
|
| 48 | this.entries = newEntries;
|
| 49 | }
|
| 50 |
|
| 51 | int entryIndex = this.nextErrorEntry++;
|
| 52 | ErrorCollector.LongestOnly.MutableErrorEntry<S> entry = this.entries[entryIndex];
|
| 53 | if (entry == null) {
|
| 54 | entry = new ErrorCollector.LongestOnly.MutableErrorEntry<>();
|
| 55 | this.entries[entryIndex] = entry;
|
| 56 | }
|
| 57 |
|
| 58 | entry.suggestions = suggestions;
|
| 59 | entry.reason = reason;
|
| 60 | }
|
| 61 |
|
| 62 | public List<ErrorEntry<S>> entries() {
|
| 63 | int errorCount = this.nextErrorEntry;
|
| 64 | if (errorCount == 0) {
|
| 65 | return List.of();
|
| 66 | }
|
| 67 |
|
| 68 | List<ErrorEntry<S>> result = new ArrayList<>(errorCount);
|
| 69 |
|
| 70 | for (int i = 0; i < errorCount; i++) {
|
| 71 | ErrorCollector.LongestOnly.MutableErrorEntry<S> entry = this.entries[i];
|
| 72 | result.add(new ErrorEntry<>(this.lastCursor, entry.suggestions, entry.reason));
|
| 73 | }
|
| 74 |
|
| 75 | return result;
|
| 76 | }
|
| 77 |
|
| 78 | public int cursor() {
|
| 79 | return this.lastCursor;
|
| 80 | }
|
| 81 |
|
| 82 | private static class MutableErrorEntry<S> {
|
| 83 | private SuggestionSupplier<S> suggestions = SuggestionSupplier.empty();
|
| 84 | private Object reason = "empty";
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | class Nop<S> implements ErrorCollector<S> {
|
| 89 | @Override
|
| 90 | public void store(final int cursor, final SuggestionSupplier<S> suggestions, final Object reason) {
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public void finish(final int finalCursor) {
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|