/* * Decompiled with CFR 0.152. * * Could not load the following classes: * org.jspecify.annotations.Nullable */ package net.minecraft.util.parsing.packrat; import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.function.Supplier; 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.Rule; import net.minecraft.util.parsing.packrat.Scope; import net.minecraft.util.parsing.packrat.Term; import org.jspecify.annotations.Nullable; public class Dictionary { private final Map, Entry> terms = new IdentityHashMap(); public NamedRule put(Atom name, Rule entry) { Entry holder = this.terms.computeIfAbsent(name, Entry::new); if (holder.value != null) { throw new IllegalArgumentException("Trying to override rule: " + String.valueOf(name)); } holder.value = entry; return holder; } public NamedRule putComplex(Atom name, Term term, Rule.RuleAction action) { return this.put(name, Rule.fromTerm(term, action)); } public NamedRule put(Atom name, Term term, Rule.SimpleRuleAction action) { return this.put(name, Rule.fromTerm(term, action)); } public void checkAllBound() { List unboundNames = this.terms.entrySet().stream().filter(e -> ((Entry)e.getValue()).value == null).map(Map.Entry::getKey).toList(); if (!unboundNames.isEmpty()) { throw new IllegalStateException("Unbound names: " + String.valueOf(unboundNames)); } } public NamedRule getOrThrow(Atom name) { return Objects.requireNonNull(this.terms.get(name), () -> "No rule called " + String.valueOf(name)); } public NamedRule forward(Atom name) { return this.getOrCreateEntry(name); } private Entry getOrCreateEntry(Atom name) { return this.terms.computeIfAbsent(name, Entry::new); } public Term named(Atom name) { return new Reference(this.getOrCreateEntry(name), name); } public Term namedWithAlias(Atom nameToParse, Atom nameToStore) { return new Reference(this.getOrCreateEntry(nameToParse), nameToStore); } private static class Entry implements NamedRule, Supplier { private final Atom name; private @Nullable Rule value; private Entry(Atom name) { this.name = name; } @Override public Atom name() { return this.name; } @Override public Rule value() { return Objects.requireNonNull(this.value, this); } @Override public String get() { return "Unbound rule " + String.valueOf(this.name); } } private record Reference(Entry ruleToParse, Atom nameToStore) implements Term { @Override public boolean parse(ParseState state, Scope scope, Control control) { T result = state.parse(this.ruleToParse); if (result == null) { return false; } scope.put(this.nameToStore, result); return true; } } }