package net.minecraft.util.parsing.packrat; import java.util.Optional; public interface Rule { Optional parse(ParseState parseState); static Rule fromTerm(Term child, Rule.RuleAction action) { return new Rule.WrappedTerm<>(action, child); } static Rule fromTerm(Term child, Rule.SimpleRuleAction action) { return new Rule.WrappedTerm<>((parseState, scope) -> Optional.of(action.run(scope)), child); } @FunctionalInterface public interface RuleAction { Optional run(ParseState parseState, Scope scope); } @FunctionalInterface public interface SimpleRuleAction { T run(Scope scope); } public record WrappedTerm(Rule.RuleAction action, Term child) implements Rule { @Override public Optional parse(ParseState parseState) { Scope scope = new Scope(); return this.child.parse(parseState, scope, Control.UNBOUND) ? this.action.run(parseState, scope) : Optional.empty(); } } }