/* * Decompiled with CFR 0.152. */ package net.minecraft.util.parsing.packrat; import java.util.ArrayList; import java.util.List; import net.minecraft.util.parsing.packrat.Atom; import net.minecraft.util.parsing.packrat.Control; import net.minecraft.util.parsing.packrat.NamedRule; import net.minecraft.util.parsing.packrat.ParseState; import net.minecraft.util.parsing.packrat.Scope; public interface Term { public boolean parse(ParseState var1, Scope var2, Control var3); public static Term marker(Atom name, T value) { return new Marker(name, value); } @SafeVarargs public static Term sequence(Term ... terms) { return new Sequence(terms); } @SafeVarargs public static Term alternative(Term ... terms) { return new Alternative(terms); } public static Term optional(Term term) { return new Maybe(term); } public static Term repeated(NamedRule element, Atom> listName) { return Term.repeated(element, listName, 0); } public static Term repeated(NamedRule element, Atom> listName, int minRepetitions) { return new Repeated(element, listName, minRepetitions); } public static Term repeatedWithTrailingSeparator(NamedRule element, Atom> listName, Term separator) { return Term.repeatedWithTrailingSeparator(element, listName, separator, 0); } public static Term repeatedWithTrailingSeparator(NamedRule element, Atom> listName, Term separator, int minRepetitions) { return new RepeatedWithSeparator(element, listName, separator, minRepetitions, true); } public static Term repeatedWithoutTrailingSeparator(NamedRule element, Atom> listName, Term separator) { return Term.repeatedWithoutTrailingSeparator(element, listName, separator, 0); } public static Term repeatedWithoutTrailingSeparator(NamedRule element, Atom> listName, Term separator, int minRepetitions) { return new RepeatedWithSeparator(element, listName, separator, minRepetitions, false); } public static Term positiveLookahead(Term term) { return new LookAhead(term, true); } public static Term negativeLookahead(Term term) { return new LookAhead(term, false); } public static Term cut() { return new Term(){ @Override public boolean parse(ParseState state, Scope scope, Control control) { control.cut(); return true; } public String toString() { return "\u2191"; } }; } public static Term empty() { return new Term(){ @Override public boolean parse(ParseState state, Scope scope, Control control) { return true; } public String toString() { return "\u03b5"; } }; } public static Term fail(final Object message) { return new Term(){ @Override public boolean parse(ParseState state, Scope scope, Control control) { state.errorCollector().store(state.mark(), message); return false; } public String toString() { return "fail"; } }; } public record Marker(Atom name, T value) implements Term { @Override public boolean parse(ParseState state, Scope scope, Control control) { scope.put(this.name, this.value); return true; } } public record Sequence(Term[] elements) implements Term { @Override public boolean parse(ParseState state, Scope scope, Control control) { int mark = state.mark(); for (Term element : this.elements) { if (element.parse(state, scope, control)) continue; state.restore(mark); return false; } return true; } } public record Alternative(Term[] elements) implements Term { /* * WARNING - Removed try catching itself - possible behaviour change. */ @Override public boolean parse(ParseState state, Scope scope, Control control) { Control controlForThis = state.acquireControl(); try { int mark = state.mark(); scope.splitFrame(); for (Term element : this.elements) { if (element.parse(state, scope, controlForThis)) { scope.mergeFrame(); boolean bl = true; return bl; } scope.clearFrameValues(); state.restore(mark); if (controlForThis.hasCut()) break; } scope.popFrame(); boolean bl = false; return bl; } finally { state.releaseControl(); } } } public record Maybe(Term term) implements Term { @Override public boolean parse(ParseState state, Scope scope, Control control) { int mark = state.mark(); if (!this.term.parse(state, scope, control)) { state.restore(mark); } return true; } } public record Repeated(NamedRule element, Atom> listName, int minRepetitions) implements Term { @Override public boolean parse(ParseState state, Scope scope, Control control) { int entryMark; int mark = state.mark(); ArrayList elements = new ArrayList(this.minRepetitions); while (true) { entryMark = state.mark(); T parsedElement = state.parse(this.element); if (parsedElement == null) break; elements.add(parsedElement); } state.restore(entryMark); if (elements.size() < this.minRepetitions) { state.restore(mark); return false; } scope.put(this.listName, elements); return true; } } public record RepeatedWithSeparator(NamedRule element, Atom> listName, Term separator, int minRepetitions, boolean allowTrailingSeparator) implements Term { @Override public boolean parse(ParseState state, Scope scope, Control control) { int listMark = state.mark(); ArrayList elements = new ArrayList(this.minRepetitions); boolean first = true; while (true) { int markBeforeSeparator = state.mark(); if (!first && !this.separator.parse(state, scope, control)) { state.restore(markBeforeSeparator); break; } int markAfterSeparator = state.mark(); T parsedElement = state.parse(this.element); if (parsedElement == null) { if (first) { state.restore(markAfterSeparator); break; } if (this.allowTrailingSeparator) { state.restore(markAfterSeparator); break; } state.restore(listMark); return false; } elements.add(parsedElement); first = false; } if (elements.size() < this.minRepetitions) { state.restore(listMark); return false; } scope.put(this.listName, elements); return true; } } public record LookAhead(Term term, boolean positive) implements Term { @Override public boolean parse(ParseState state, Scope scope, Control control) { int mark = state.mark(); boolean result = this.term.parse(state.silent(), scope, control); state.restore(mark); return this.positive == result; } } }