47 lines
1.1 KiB
Java
47 lines
1.1 KiB
Java
package net.minecraft.util.parsing.packrat;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
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 final List<ErrorEntry<S>> entries = new ArrayList();
|
|
private int lastCursor = -1;
|
|
|
|
private void discardErrorsFromShorterParse(int cursor) {
|
|
if (cursor > this.lastCursor) {
|
|
this.lastCursor = cursor;
|
|
this.entries.clear();
|
|
}
|
|
}
|
|
|
|
@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.entries.add(new ErrorEntry<>(cursor, suggestions, reason));
|
|
}
|
|
}
|
|
|
|
public List<ErrorEntry<S>> entries() {
|
|
return this.entries;
|
|
}
|
|
|
|
public int cursor() {
|
|
return this.lastCursor;
|
|
}
|
|
}
|
|
}
|