Grammar.java

net.minecraft.util.parsing.packrat.commands.Grammar

信息

  • 全限定名:net.minecraft.util.parsing.packrat.commands.Grammar
  • 类型:public record
  • 包:net.minecraft.util.parsing.packrat.commands
  • 源码路径:src/main/java/net/minecraft/util/parsing/packrat/commands/Grammar.java
  • 起始行号:L19
  • 实现:CommandArgumentParser
  • 职责:

    TODO

字段/常量

内部类/嵌套类型

构造器

public Grammar(Dictionary<StringReader> rules, NamedRule<StringReader,T> top) @ L20

  • 构造器名:Grammar
  • 源码定位:L20
  • 修饰符:public

参数:

  • rules: Dictionary
  • top: NamedRule<StringReader,T>

说明:

TODO

方法

下面的方法块按源码顺序生成。

public Optional<T> parse(ParseState<StringReader> state) @ L26

  • 方法名:parse
  • 源码定位:L26
  • 返回类型:Optional
  • 修饰符:public

参数:

  • state: ParseState

说明:

TODO

public T parseForCommands(StringReader reader) @ L30

  • 方法名:parseForCommands
  • 源码定位:L30
  • 返回类型:T
  • 修饰符:public

参数:

  • reader: StringReader

说明:

TODO

public CompletableFuture<Suggestions> parseForSuggestions(SuggestionsBuilder suggestionsBuilder) @ L61

  • 方法名:parseForSuggestions
  • 源码定位:L61
  • 返回类型:CompletableFuture
  • 修饰符:public

参数:

  • suggestionsBuilder: SuggestionsBuilder

说明:

TODO

代码

public record Grammar<T>(Dictionary<StringReader> rules, NamedRule<StringReader, T> top) implements CommandArgumentParser<T> {
    public Grammar(Dictionary<StringReader> rules, NamedRule<StringReader, T> top) {
        rules.checkAllBound();
        this.rules = rules;
        this.top = top;
    }
 
    public Optional<T> parse(ParseState<StringReader> state) {
        return state.parseTopRule(this.top);
    }
 
    @Override
    public T parseForCommands(StringReader reader) throws CommandSyntaxException {
        ErrorCollector.LongestOnly<StringReader> errorCollector = new ErrorCollector.LongestOnly<>();
        StringReaderParserState state = new StringReaderParserState(errorCollector, reader);
        Optional<T> result = this.parse(state);
        if (result.isPresent()) {
            return result.get();
        } else {
            List<ErrorEntry<StringReader>> errorEntries = errorCollector.entries();
            List<Exception> exceptions = errorEntries.stream().<Exception>mapMulti((entry, output) -> {
                if (entry.reason() instanceof DelayedException<?> delayedException) {
                    output.accept(delayedException.create(reader.getString(), entry.cursor()));
                } else if (entry.reason() instanceof Exception exceptionx) {
                    output.accept(exceptionx);
                }
            }).toList();
 
            for (Exception exception : exceptions) {
                if (exception instanceof CommandSyntaxException cse) {
                    throw cse;
                }
            }
 
            if (exceptions.size() == 1 && exceptions.get(0) instanceof RuntimeException re) {
                throw re;
            } else {
                throw new IllegalStateException("Failed to parse: " + errorEntries.stream().map(ErrorEntry::toString).collect(Collectors.joining(", ")));
            }
        }
    }
 
    @Override
    public CompletableFuture<Suggestions> parseForSuggestions(SuggestionsBuilder suggestionsBuilder) {
        StringReader reader = new StringReader(suggestionsBuilder.getInput());
        reader.setCursor(suggestionsBuilder.getStart());
        ErrorCollector.LongestOnly<StringReader> errorCollector = new ErrorCollector.LongestOnly<>();
        StringReaderParserState state = new StringReaderParserState(errorCollector, reader);
        this.parse(state);
        List<ErrorEntry<StringReader>> errorEntries = errorCollector.entries();
        if (errorEntries.isEmpty()) {
            return suggestionsBuilder.buildFuture();
        } else {
            SuggestionsBuilder offsetBuilder = suggestionsBuilder.createOffset(errorCollector.cursor());
 
            for (ErrorEntry<StringReader> entry : errorEntries) {
                if (entry.suggestions() instanceof ResourceSuggestion resourceSuggestionTerm) {
                    SharedSuggestionProvider.suggestResource(resourceSuggestionTerm.possibleResources(), offsetBuilder);
                } else {
                    SharedSuggestionProvider.suggest(entry.suggestions().possibleValues(state), offsetBuilder);
                }
            }
 
            return offsetBuilder.buildFuture();
        }
    }
}

引用的其他类