Dictionary.java

net.minecraft.util.parsing.packrat.Dictionary

信息

  • 全限定名:net.minecraft.util.parsing.packrat.Dictionary
  • 类型:public class
  • 包:net.minecraft.util.parsing.packrat
  • 源码路径:src/main/java/net/minecraft/util/parsing/packrat/Dictionary.java
  • 起始行号:L10
  • 职责:

    TODO

字段/常量

  • terms
    • 类型: Map<Atom<?>,Dictionary.Entry<S,?>>
    • 修饰符: private final
    • 源码定位: L11
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.util.parsing.packrat.Dictionary.Entry

    • 类型: class
    • 修饰符: private static
    • 源码定位: L58
    • 说明:

      TODO

  • net.minecraft.util.parsing.packrat.Dictionary.Reference

    • 类型: record
    • 修饰符: private
    • 源码定位: L81
    • 说明:

      TODO

构造器

方法

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

public <T> NamedRule<S,T> put(Atom<T> name, Rule<S,T> entry) @ L13

  • 方法名:put
  • 源码定位:L13
  • 返回类型: NamedRule<S,T>
  • 修饰符:public

参数:

  • name: Atom
  • entry: Rule<S,T>

说明:

TODO

public <T> NamedRule<S,T> putComplex(Atom<T> name, Term<S> term, Rule.RuleAction<S,T> action) @ L23

  • 方法名:putComplex
  • 源码定位:L23
  • 返回类型: NamedRule<S,T>
  • 修饰符:public

参数:

  • name: Atom
  • term: Term
  • action: Rule.RuleAction<S,T>

说明:

TODO

public <T> NamedRule<S,T> put(Atom<T> name, Term<S> term, Rule.SimpleRuleAction<S,T> action) @ L27

  • 方法名:put
  • 源码定位:L27
  • 返回类型: NamedRule<S,T>
  • 修饰符:public

参数:

  • name: Atom
  • term: Term
  • action: Rule.SimpleRuleAction<S,T>

说明:

TODO

public void checkAllBound() @ L31

  • 方法名:checkAllBound
  • 源码定位:L31
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public <T> NamedRule<S,T> getOrThrow(Atom<T> name) @ L38

  • 方法名:getOrThrow
  • 源码定位:L38
  • 返回类型: NamedRule<S,T>
  • 修饰符:public

参数:

  • name: Atom

说明:

TODO

public <T> NamedRule<S,T> forward(Atom<T> name) @ L42

  • 方法名:forward
  • 源码定位:L42
  • 返回类型: NamedRule<S,T>
  • 修饰符:public

参数:

  • name: Atom

说明:

TODO

private <T> Dictionary.Entry<S,T> getOrCreateEntry(Atom<T> name) @ L46

  • 方法名:getOrCreateEntry
  • 源码定位:L46
  • 返回类型: Dictionary.Entry<S,T>
  • 修饰符:private

参数:

  • name: Atom

说明:

TODO

public <T> Term<S> named(Atom<T> name) @ L50

  • 方法名:named
  • 源码定位:L50
  • 返回类型: Term
  • 修饰符:public

参数:

  • name: Atom

说明:

TODO

public <T> Term<S> namedWithAlias(Atom<T> nameToParse, Atom<T> nameToStore) @ L54

  • 方法名:namedWithAlias
  • 源码定位:L54
  • 返回类型: Term
  • 修饰符:public

参数:

  • nameToParse: Atom
  • nameToStore: Atom

说明:

TODO

代码

public class Dictionary<S> {
    private final Map<Atom<?>, Dictionary.Entry<S, ?>> terms = new IdentityHashMap<>();
 
    public <T> NamedRule<S, T> put(Atom<T> name, Rule<S, T> entry) {
        Dictionary.Entry<S, T> holder = (Dictionary.Entry<S, T>)this.terms.computeIfAbsent(name, Dictionary.Entry::new);
        if (holder.value != null) {
            throw new IllegalArgumentException("Trying to override rule: " + name);
        } else {
            holder.value = entry;
            return holder;
        }
    }
 
    public <T> NamedRule<S, T> putComplex(Atom<T> name, Term<S> term, Rule.RuleAction<S, T> action) {
        return this.put(name, Rule.fromTerm(term, action));
    }
 
    public <T> NamedRule<S, T> put(Atom<T> name, Term<S> term, Rule.SimpleRuleAction<S, T> action) {
        return this.put(name, Rule.fromTerm(term, action));
    }
 
    public void checkAllBound() {
        List<? extends Atom<?>> unboundNames = this.terms.entrySet().stream().filter(e -> e.getValue().value == null).map(Map.Entry::getKey).toList();
        if (!unboundNames.isEmpty()) {
            throw new IllegalStateException("Unbound names: " + unboundNames);
        }
    }
 
    public <T> NamedRule<S, T> getOrThrow(Atom<T> name) {
        return (NamedRule<S, T>)Objects.requireNonNull(this.terms.get(name), () -> "No rule called " + name);
    }
 
    public <T> NamedRule<S, T> forward(Atom<T> name) {
        return this.getOrCreateEntry(name);
    }
 
    private <T> Dictionary.Entry<S, T> getOrCreateEntry(Atom<T> name) {
        return (Dictionary.Entry<S, T>)this.terms.computeIfAbsent(name, Dictionary.Entry::new);
    }
 
    public <T> Term<S> named(Atom<T> name) {
        return new Dictionary.Reference<>(this.getOrCreateEntry(name), name);
    }
 
    public <T> Term<S> namedWithAlias(Atom<T> nameToParse, Atom<T> nameToStore) {
        return new Dictionary.Reference<>(this.getOrCreateEntry(nameToParse), nameToStore);
    }
 
    private static class Entry<S, T> implements NamedRule<S, T>, Supplier<String> {
        private final Atom<T> name;
        private @Nullable Rule<S, T> value;
 
        private Entry(Atom<T> name) {
            this.name = name;
        }
 
        @Override
        public Atom<T> name() {
            return this.name;
        }
 
        @Override
        public Rule<S, T> value() {
            return Objects.requireNonNull(this.value, this);
        }
 
        public String get() {
            return "Unbound rule " + this.name;
        }
    }
 
    private record Reference<S, T>(Dictionary.Entry<S, T> ruleToParse, Atom<T> nameToStore) implements Term<S> {
        @Override
        public boolean parse(ParseState<S> state, Scope scope, Control control) {
            T result = state.parse(this.ruleToParse);
            if (result == null) {
                return false;
            } else {
                scope.put(this.nameToStore, result);
                return true;
            }
        }
    }
}

引用的其他类

  • Atom

    • 引用位置: 参数/字段
  • NamedRule

    • 引用位置: 返回值
  • Rule

    • 引用位置: 参数/方法调用
    • 关联成员: Rule.fromTerm()
  • Term

    • 引用位置: 参数/返回值