Grammar .java

net.minecraft.util.parsing.packrat.commands.Grammar (record): members: rules, top, parseForSuggestions, parseForCommands, parse. imports: ErrorEntry.

net.minecraft.util.parsing.packrat.commandsrecord84 linescommon3.3 KB

Implements

API Surface (3)

Optional<T> parse(final ParseState<StringReader> state)
T parseForCommands(final StringReader reader)
CompletableFuture<Suggestions> parseForSuggestions(final SuggestionsBuilder suggestionsBuilder)

String literals (1)

"Failed to parse: "

Full Source

Grammar.java84 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.parsing.packrat.commands;
2
3
import com.mojang.brigadier.StringReader;
4
import com.mojang.brigadier.exceptions.CommandSyntaxException;
5
import com.mojang.brigadier.suggestion.Suggestions;
6
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
7
import java.util.List;
8
import java.util.Optional;
9
import java.util.concurrent.CompletableFuture;
10
import java.util.stream.Collectors;
11
import net.minecraft.commands.SharedSuggestionProvider;
12
import net.minecraft.util.parsing.packrat.DelayedException;
13
import net.minecraft.util.parsing.packrat.Dictionary;
14
import net.minecraft.util.parsing.packrat.ErrorCollector;
15
import net.minecraft.util.parsing.packrat.ErrorEntry;
16
import net.minecraft.util.parsing.packrat.NamedRule;
17
import net.minecraft.util.parsing.packrat.ParseState;
18
19
public record Grammar<T>(Dictionary<StringReader> rules, NamedRule<StringReader, T> top) implements CommandArgumentParser<T> {
20
	public Grammar {
21
		rules.checkAllBound();
22
	}
23
24
	public Optional<T> parse(final ParseState<StringReader> state) {
25
		return state.parseTopRule(this.top);
26
	}
27
28
	@Override
29
	public T parseForCommands(final StringReader reader) throws CommandSyntaxException {
30
		ErrorCollector.LongestOnly<StringReader> errorCollector = new ErrorCollector.LongestOnly<>();
31
		StringReaderParserState state = new StringReaderParserState(errorCollector, reader);
32
		Optional<T> result = this.parse(state);
33
		if (result.isPresent()) {
34
			return result.get();
35
		}
36
37
		List<ErrorEntry<StringReader>> errorEntries = errorCollector.entries();
38
		List<Exception> exceptions = errorEntries.stream().<Exception>mapMulti((entry, output) -> {
39
			if (entry.reason() instanceof DelayedException<?> delayedException) {
40
				output.accept(delayedException.create(reader.getString(), entry.cursor()));
41
			} else if (entry.reason() instanceof Exception exceptionx) {
42
				output.accept(exceptionx);
43
			}
44
		}).toList();
45
46
		for (Exception exception : exceptions) {
47
			if (exception instanceof CommandSyntaxException cse) {
48
				throw cse;
49
			}
50
		}
51
52
		if (exceptions.size() == 1 && exceptions.get(0) instanceof RuntimeException re) {
53
			throw re;
54
		} else {
55
			throw new IllegalStateException("Failed to parse: " + errorEntries.stream().map(ErrorEntry::toString).collect(Collectors.joining(", ")));
56
		}
57
	}
58
59
	@Override
60
	public CompletableFuture<Suggestions> parseForSuggestions(final SuggestionsBuilder suggestionsBuilder) {
61
		StringReader reader = new StringReader(suggestionsBuilder.getInput());
62
		reader.setCursor(suggestionsBuilder.getStart());
63
		ErrorCollector.LongestOnly<StringReader> errorCollector = new ErrorCollector.LongestOnly<>();
64
		StringReaderParserState state = new StringReaderParserState(errorCollector, reader);
65
		this.parse(state);
66
		List<ErrorEntry<StringReader>> errorEntries = errorCollector.entries();
67
		if (errorEntries.isEmpty()) {
68
			return suggestionsBuilder.buildFuture();
69
		}
70
71
		SuggestionsBuilder offsetBuilder = suggestionsBuilder.createOffset(errorCollector.cursor());
72
73
		for (ErrorEntry<StringReader> entry : errorEntries) {
74
			if (entry.suggestions() instanceof ResourceSuggestion resourceSuggestionTerm) {
75
				SharedSuggestionProvider.suggestResource(resourceSuggestionTerm.possibleResources(), offsetBuilder);
76
			} else {
77
				SharedSuggestionProvider.suggest(entry.suggestions().possibleValues(state), offsetBuilder);
78
			}
79
		}
80
81
		return offsetBuilder.buildFuture();
82
	}
83
}
84

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