Dictionary .java

net.minecraft.util.parsing.packrat.Dictionary (class): members: namedWithAlias, checkAllBound, putComplex, named, terms, getOrThrow, Reference, forward.

net.minecraft.util.parsing.packratclass94 linescommon2.8 KB

API Surface (12)

<T> NamedRule<S, T> put(final Atom<T> name, final Rule<S, T> entry)
<T> NamedRule<S, T> putComplex(final Atom<T> name, final Term<S> term, final Rule.RuleAction<S, T> action)
<T> NamedRule<S, T> put(final Atom<T> name, final Term<S> term, final Rule.SimpleRuleAction<S, T> action)
void checkAllBound()
<T> NamedRule<S, T> getOrThrow(final Atom<T> name)
<T> NamedRule<S, T> forward(final Atom<T> name)
<T> Term<S> named(final Atom<T> name)
<T> Term<S> namedWithAlias(final Atom<T> nameToParse, final Atom<T> nameToStore)
Atom<T> name()
Rule<S, T> value()
String get()
boolean parse(final ParseState<S> state, final Scope scope, final Control control)

Fields & Constants (1)

terms

String literals (4)

"Trying to override rule: "
"Unbound names: "
"No rule called "
"Unbound rule "

Code Graph: Dependencies (7)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

Dictionary.java94 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.parsing.packrat;
2
3
import java.util.IdentityHashMap;
4
import java.util.List;
5
import java.util.Map;
6
import java.util.Objects;
7
import java.util.function.Supplier;
8
import org.jspecify.annotations.Nullable;
9
10
public class Dictionary<S> {
11
	private final Map<Atom<?>, Dictionary.Entry<S, ?>> terms = new IdentityHashMap<>();
12
13
	public <T> NamedRule<S, T> put(final Atom<T> name, final Rule<S, T> entry) {
14
		Dictionary.Entry<S, T> holder = (Dictionary.Entry<S, T>)this.terms.computeIfAbsent(name, Dictionary.Entry::new);
15
		if (holder.value != null) {
16
			throw new IllegalArgumentException("Trying to override rule: " + name);
17
		}
18
19
		holder.value = entry;
20
		return holder;
21
	}
22
23
	public <T> NamedRule<S, T> putComplex(final Atom<T> name, final Term<S> term, final Rule.RuleAction<S, T> action) {
24
		return this.put(name, Rule.fromTerm(term, action));
25
	}
26
27
	public <T> NamedRule<S, T> put(final Atom<T> name, final Term<S> term, final Rule.SimpleRuleAction<S, T> action) {
28
		return this.put(name, Rule.fromTerm(term, action));
29
	}
30
31
	public void checkAllBound() {
32
		List<? extends Atom<?>> unboundNames = this.terms.entrySet().stream().filter(e -> e.getValue().value == null).map(Map.Entry::getKey).toList();
33
		if (!unboundNames.isEmpty()) {
34
			throw new IllegalStateException("Unbound names: " + unboundNames);
35
		}
36
	}
37
38
	public <T> NamedRule<S, T> getOrThrow(final Atom<T> name) {
39
		return (NamedRule<S, T>)Objects.requireNonNull(this.terms.get(name), () -> "No rule called " + name);
40
	}
41
42
	public <T> NamedRule<S, T> forward(final Atom<T> name) {
43
		return this.getOrCreateEntry(name);
44
	}
45
46
	private <T> Dictionary.Entry<S, T> getOrCreateEntry(final Atom<T> name) {
47
		return (Dictionary.Entry<S, T>)this.terms.computeIfAbsent(name, Dictionary.Entry::new);
48
	}
49
50
	public <T> Term<S> named(final Atom<T> name) {
51
		return new Dictionary.Reference<>(this.getOrCreateEntry(name), name);
52
	}
53
54
	public <T> Term<S> namedWithAlias(final Atom<T> nameToParse, final Atom<T> nameToStore) {
55
		return new Dictionary.Reference<>(this.getOrCreateEntry(nameToParse), nameToStore);
56
	}
57
58
	private static class Entry<S, T> implements NamedRule<S, T>, Supplier<String> {
59
		private final Atom<T> name;
60
		private @Nullable Rule<S, T> value;
61
62
		private Entry(final Atom<T> name) {
63
			this.name = name;
64
		}
65
66
		@Override
67
		public Atom<T> name() {
68
			return this.name;
69
		}
70
71
		@Override
72
		public Rule<S, T> value() {
73
			return Objects.requireNonNull(this.value, this);
74
		}
75
76
		public String get() {
77
			return "Unbound rule " + this.name;
78
		}
79
	}
80
81
	private record Reference<S, T>(Dictionary.Entry<S, T> ruleToParse, Atom<T> nameToStore) implements Term<S> {
82
		@Override
83
		public boolean parse(final ParseState<S> state, final Scope scope, final Control control) {
84
			T result = state.parse(this.ruleToParse);
85
			if (result == null) {
86
				return false;
87
			}
88
89
			scope.put(this.nameToStore, result);
90
			return true;
91
		}
92
	}
93
}
94

Decompiled Minecraft 26.3 source of Dictionary (net/minecraft/util/parsing/packrat/Dictionary.java) with Mojang official mappings, stored verbatim on the site.