Term .java

net.minecraft.util.parsing.packrat.Term (interface, 253 lines): members: repeatedWithoutTrailingSeparator, repeatedWithTrailingSeparator, Alternative, cut.

net.minecraft.util.parsing.packratinterface253 linescommon6.7 KB

API Surface (13)

boolean parse(final ParseState<S> state, final Scope scope, final Control control)
String toString()
boolean parse(final ParseState<S> state, final Scope scope, final Control control)
String toString()
boolean parse(final ParseState<S> state, final Scope scope, final Control control)
String toString()
boolean parse(final ParseState<S> state, final Scope scope, final Control control)
boolean parse(final ParseState<S> state, final Scope scope, final Control control)
boolean parse(final ParseState<S> state, final Scope scope, final Control control)
boolean parse(final ParseState<S> state, final Scope scope, final Control control)
boolean parse(final ParseState<S> state, final Scope scope, final Control control)
boolean parse(final ParseState<S> state, final Scope scope, final Control control)
boolean parse(final ParseState<S> state, final Scope scope, final Control control)

Fields & Constants (1)

separator

String literals (1)

"fail"

Code Graph: Dependencies (5)

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

Full Source

Term.java253 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.parsing.packrat;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
6
public interface Term<S> {
7
	boolean parse(ParseState<S> state, Scope scope, Control control);
8
9
	static <S, T> Term<S> marker(final Atom<T> name, final T value) {
10
		return new Term.Marker<>(name, value);
11
	}
12
13
	@SafeVarargs
14
	static <S> Term<S> sequence(final Term<S>... terms) {
15
		return new Term.Sequence<>(terms);
16
	}
17
18
	@SafeVarargs
19
	static <S> Term<S> alternative(final Term<S>... terms) {
20
		return new Term.Alternative<>(terms);
21
	}
22
23
	static <S> Term<S> optional(final Term<S> term) {
24
		return new Term.Maybe<>(term);
25
	}
26
27
	static <S, T> Term<S> repeated(final NamedRule<S, T> element, final Atom<List<T>> listName) {
28
		return repeated(element, listName, 0);
29
	}
30
31
	static <S, T> Term<S> repeated(final NamedRule<S, T> element, final Atom<List<T>> listName, final int minRepetitions) {
32
		return new Term.Repeated<>(element, listName, minRepetitions);
33
	}
34
35
	static <S, T> Term<S> repeatedWithTrailingSeparator(final NamedRule<S, T> element, final Atom<List<T>> listName, final Term<S> separator) {
36
		return repeatedWithTrailingSeparator(element, listName, separator, 0);
37
	}
38
39
	static <S, T> Term<S> repeatedWithTrailingSeparator(
40
		final NamedRule<S, T> element, final Atom<List<T>> listName, final Term<S> separator, final int minRepetitions
41
	) {
42
		return new Term.RepeatedWithSeparator<>(element, listName, separator, minRepetitions, true);
43
	}
44
45
	static <S, T> Term<S> repeatedWithoutTrailingSeparator(final NamedRule<S, T> element, final Atom<List<T>> listName, final Term<S> separator) {
46
		return repeatedWithoutTrailingSeparator(element, listName, separator, 0);
47
	}
48
49
	static <S, T> Term<S> repeatedWithoutTrailingSeparator(
50
		final NamedRule<S, T> element, final Atom<List<T>> listName, final Term<S> separator, final int minRepetitions
51
	) {
52
		return new Term.RepeatedWithSeparator<>(element, listName, separator, minRepetitions, false);
53
	}
54
55
	static <S> Term<S> positiveLookahead(final Term<S> term) {
56
		return new Term.LookAhead<>(term, true);
57
	}
58
59
	static <S> Term<S> negativeLookahead(final Term<S> term) {
60
		return new Term.LookAhead<>(term, false);
61
	}
62
63
	static <S> Term<S> cut() {
64
		return new Term<S>() {
65
			@Override
66
			public boolean parse(final ParseState<S> state, final Scope scope, final Control control) {
67
				control.cut();
68
				return true;
69
			}
70
71
			@Override
72
			public String toString() {
73
				return "↑";
74
			}
75
		};
76
	}
77
78
	static <S> Term<S> empty() {
79
		return new Term<S>() {
80
			@Override
81
			public boolean parse(final ParseState<S> state, final Scope scope, final Control control) {
82
				return true;
83
			}
84
85
			@Override
86
			public String toString() {
87
				return "ε";
88
			}
89
		};
90
	}
91
92
	static <S> Term<S> fail(final Object message) {
93
		return new Term<S>() {
94
			@Override
95
			public boolean parse(final ParseState<S> state, final Scope scope, final Control control) {
96
				state.errorCollector().store(state.mark(), message);
97
				return false;
98
			}
99
100
			@Override
101
			public String toString() {
102
				return "fail";
103
			}
104
		};
105
	}
106
107
	record Alternative<S>(Term<S>[] elements) implements Term<S> {
108
		@Override
109
		public boolean parse(final ParseState<S> state, final Scope scope, final Control control) {
110
			Control controlForThis = state.acquireControl();
111
112
			try {
113
				int mark = state.mark();
114
				scope.splitFrame();
115
116
				for (Term<S> element : this.elements) {
117
					if (element.parse(state, scope, controlForThis)) {
118
						scope.mergeFrame();
119
						return true;
120
					}
121
122
					scope.clearFrameValues();
123
					state.restore(mark);
124
					if (controlForThis.hasCut()) {
125
						break;
126
					}
127
				}
128
129
				scope.popFrame();
130
				return false;
131
			} finally {
132
				state.releaseControl();
133
			}
134
		}
135
	}
136
137
	record LookAhead<S>(Term<S> term, boolean positive) implements Term<S> {
138
		@Override
139
		public boolean parse(final ParseState<S> state, final Scope scope, final Control control) {
140
			int mark = state.mark();
141
			boolean result = this.term.parse(state.silent(), scope, control);
142
			state.restore(mark);
143
			return this.positive == result;
144
		}
145
	}
146
147
	record Marker<S, T>(Atom<T> name, T value) implements Term<S> {
148
		@Override
149
		public boolean parse(final ParseState<S> state, final Scope scope, final Control control) {
150
			scope.put(this.name, this.value);
151
			return true;
152
		}
153
	}
154
155
	record Maybe<S>(Term<S> term) implements Term<S> {
156
		@Override
157
		public boolean parse(final ParseState<S> state, final Scope scope, final Control control) {
158
			int mark = state.mark();
159
			if (!this.term.parse(state, scope, control)) {
160
				state.restore(mark);
161
			}
162
163
			return true;
164
		}
165
	}
166
167
	record Repeated<S, T>(NamedRule<S, T> element, Atom<List<T>> listName, int minRepetitions) implements Term<S> {
168
		@Override
169
		public boolean parse(final ParseState<S> state, final Scope scope, final Control control) {
170
			int mark = state.mark();
171
			List<T> elements = new ArrayList<>(this.minRepetitions);
172
173
			while (true) {
174
				int entryMark = state.mark();
175
				T parsedElement = state.parse(this.element);
176
				if (parsedElement == null) {
177
					state.restore(entryMark);
178
					if (elements.size() < this.minRepetitions) {
179
						state.restore(mark);
180
						return false;
181
					} else {
182
						scope.put(this.listName, elements);
183
						return true;
184
					}
185
				}
186
187
				elements.add(parsedElement);
188
			}
189
		}
190
	}
191
192
	record RepeatedWithSeparator<S, T>(NamedRule<S, T> element, Atom<List<T>> listName, Term<S> separator, int minRepetitions, boolean allowTrailingSeparator)
193
		implements Term<S> {
194
		@Override
195
		public boolean parse(final ParseState<S> state, final Scope scope, final Control control) {
196
			int listMark = state.mark();
197
			List<T> elements = new ArrayList<>(this.minRepetitions);
198
			boolean first = true;
199
200
			while (true) {
201
				int markBeforeSeparator = state.mark();
202
				if (!first && !this.separator.parse(state, scope, control)) {
203
					state.restore(markBeforeSeparator);
204
					break;
205
				}
206
207
				int markAfterSeparator = state.mark();
208
				T parsedElement = state.parse(this.element);
209
				if (parsedElement == null) {
210
					if (first) {
211
						state.restore(markAfterSeparator);
212
					} else {
213
						if (!this.allowTrailingSeparator) {
214
							state.restore(listMark);
215
							return false;
216
						}
217
218
						state.restore(markAfterSeparator);
219
					}
220
					break;
221
				}
222
223
				elements.add(parsedElement);
224
				first = false;
225
			}
226
227
			if (elements.size() < this.minRepetitions) {
228
				state.restore(listMark);
229
				return false;
230
			} else {
231
				scope.put(this.listName, elements);
232
				return true;
233
			}
234
		}
235
	}
236
237
	record Sequence<S>(Term<S>[] elements) implements Term<S> {
238
		@Override
239
		public boolean parse(final ParseState<S> state, final Scope scope, final Control control) {
240
			int mark = state.mark();
241
242
			for (Term<S> element : this.elements) {
243
				if (!element.parse(state, scope, control)) {
244
					state.restore(mark);
245
					return false;
246
				}
247
			}
248
249
			return true;
250
		}
251
	}
252
}
253

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