CachedParseState.java

net.minecraft.util.parsing.packrat.CachedParseState

信息

  • 全限定名:net.minecraft.util.parsing.packrat.CachedParseState
  • 类型:public abstract class
  • 包:net.minecraft.util.parsing.packrat
  • 源码路径:src/main/java/net/minecraft/util/parsing/packrat/CachedParseState.java
  • 起始行号:L7
  • 实现:ParseState
  • 职责:

    TODO

字段/常量

  • positionCache

    • 类型: CachedParseState.PositionCache[]
    • 修饰符: private
    • 源码定位: L8
    • 说明:

      TODO

  • errorCollector

    • 类型: ErrorCollector<S>
    • 修饰符: private final
    • 源码定位: L9
    • 说明:

      TODO

  • scope

    • 类型: Scope
    • 修饰符: private final
    • 源码定位: L10
    • 说明:

      TODO

  • controlCache

    • 类型: CachedParseState.SimpleControl[]
    • 修饰符: private
    • 源码定位: L11
    • 说明:

      TODO

  • nextControlToReturn

    • 类型: int
    • 修饰符: private
    • 源码定位: L12
    • 说明:

      TODO

  • silent

    • 类型: CachedParseState<S>.Silent
    • 修饰符: private final
    • 源码定位: L13
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.util.parsing.packrat.CachedParseState.CacheEntry

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

      TODO

  • net.minecraft.util.parsing.packrat.CachedParseState.PositionCache

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

      TODO

  • net.minecraft.util.parsing.packrat.CachedParseState.Silent

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

      TODO

  • net.minecraft.util.parsing.packrat.CachedParseState.SimpleControl

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

      TODO

构造器

protected CachedParseState(ErrorCollector<S> errorCollector) @ L15

  • 构造器名:CachedParseState
  • 源码定位:L15
  • 修饰符:protected

参数:

  • errorCollector: ErrorCollector

说明:

TODO

方法

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

public Scope scope() @ L19

  • 方法名:scope
  • 源码定位:L19
  • 返回类型:Scope
  • 修饰符:public

参数:

说明:

TODO

public ErrorCollector<S> errorCollector() @ L24

  • 方法名:errorCollector
  • 源码定位:L24
  • 返回类型:ErrorCollector
  • 修饰符:public

参数:

说明:

TODO

public <T> T parse(NamedRule<S,T> rule) @ L29

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

参数:

  • rule: NamedRule<S,T>

说明:

TODO

private CachedParseState.PositionCache getCacheForPosition(int index) @ L61

  • 方法名:getCacheForPosition
  • 源码定位:L61
  • 返回类型:CachedParseState.PositionCache
  • 修饰符:private

参数:

  • index: int

说明:

TODO

public Control acquireControl() @ L79

  • 方法名:acquireControl
  • 源码定位:L79
  • 返回类型:Control
  • 修饰符:public

参数:

说明:

TODO

public void releaseControl() @ L101

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

参数:

说明:

TODO

public ParseState<S> silent() @ L106

  • 方法名:silent
  • 源码定位:L106
  • 返回类型:ParseState
  • 修饰符:public

参数:

说明:

TODO

代码

public abstract class CachedParseState<S> implements ParseState<S> {
    private CachedParseState.@Nullable PositionCache[] positionCache = new CachedParseState.PositionCache[256];
    private final ErrorCollector<S> errorCollector;
    private final Scope scope = new Scope();
    private CachedParseState.@Nullable SimpleControl[] controlCache = new CachedParseState.SimpleControl[16];
    private int nextControlToReturn;
    private final CachedParseState<S>.Silent silent = new CachedParseState.Silent();
 
    protected CachedParseState(ErrorCollector<S> errorCollector) {
        this.errorCollector = errorCollector;
    }
 
    @Override
    public Scope scope() {
        return this.scope;
    }
 
    @Override
    public ErrorCollector<S> errorCollector() {
        return this.errorCollector;
    }
 
    @Override
    public <T> @Nullable T parse(NamedRule<S, T> rule) {
        int markBeforeParse = this.mark();
        CachedParseState.PositionCache positionCache = this.getCacheForPosition(markBeforeParse);
        int entryIndex = positionCache.findKeyIndex(rule.name());
        if (entryIndex != -1) {
            CachedParseState.CacheEntry<T> value = positionCache.getValue(entryIndex);
            if (value != null) {
                if (value == CachedParseState.CacheEntry.NEGATIVE) {
                    return null;
                }
 
                this.restore(value.markAfterParse);
                return value.value;
            }
        } else {
            entryIndex = positionCache.allocateNewEntry(rule.name());
        }
 
        T result = rule.value().parse(this);
        CachedParseState.CacheEntry<T> entry;
        if (result == null) {
            entry = CachedParseState.CacheEntry.negativeEntry();
        } else {
            int markAfterParse = this.mark();
            entry = new CachedParseState.CacheEntry<>(result, markAfterParse);
        }
 
        positionCache.setValue(entryIndex, entry);
        return result;
    }
 
    private CachedParseState.PositionCache getCacheForPosition(int index) {
        int currentSize = this.positionCache.length;
        if (index >= currentSize) {
            int newSize = Util.growByHalf(currentSize, index + 1);
            CachedParseState.PositionCache[] newCache = new CachedParseState.PositionCache[newSize];
            System.arraycopy(this.positionCache, 0, newCache, 0, currentSize);
            this.positionCache = newCache;
        }
 
        CachedParseState.PositionCache result = this.positionCache[index];
        if (result == null) {
            result = new CachedParseState.PositionCache();
            this.positionCache[index] = result;
        }
 
        return result;
    }
 
    @Override
    public Control acquireControl() {
        int currentSize = this.controlCache.length;
        if (this.nextControlToReturn >= currentSize) {
            int newSize = Util.growByHalf(currentSize, this.nextControlToReturn + 1);
            CachedParseState.SimpleControl[] newControlCache = new CachedParseState.SimpleControl[newSize];
            System.arraycopy(this.controlCache, 0, newControlCache, 0, currentSize);
            this.controlCache = newControlCache;
        }
 
        int controlIndex = this.nextControlToReturn++;
        CachedParseState.SimpleControl entry = this.controlCache[controlIndex];
        if (entry == null) {
            entry = new CachedParseState.SimpleControl();
            this.controlCache[controlIndex] = entry;
        } else {
            entry.reset();
        }
 
        return entry;
    }
 
    @Override
    public void releaseControl() {
        this.nextControlToReturn--;
    }
 
    @Override
    public ParseState<S> silent() {
        return this.silent;
    }
 
    private record CacheEntry<T>(@Nullable T value, int markAfterParse) {
        public static final CachedParseState.CacheEntry<?> NEGATIVE = new CachedParseState.CacheEntry(null, -1);
 
        public static <T> CachedParseState.CacheEntry<T> negativeEntry() {
            return (CachedParseState.CacheEntry<T>)NEGATIVE;
        }
    }
 
    private static class PositionCache {
        public static final int ENTRY_STRIDE = 2;
        private static final int NOT_FOUND = -1;
        private Object[] atomCache = new Object[16];
        private int nextKey;
 
        public int findKeyIndex(Atom<?> key) {
            for (int i = 0; i < this.nextKey; i += 2) {
                if (this.atomCache[i] == key) {
                    return i;
                }
            }
 
            return -1;
        }
 
        public int allocateNewEntry(Atom<?> key) {
            int newKeyIndex = this.nextKey;
            this.nextKey += 2;
            int newValueIndex = newKeyIndex + 1;
            int currentSize = this.atomCache.length;
            if (newValueIndex >= currentSize) {
                int newSize = Util.growByHalf(currentSize, newValueIndex + 1);
                Object[] newCache = new Object[newSize];
                System.arraycopy(this.atomCache, 0, newCache, 0, currentSize);
                this.atomCache = newCache;
            }
 
            this.atomCache[newKeyIndex] = key;
            return newKeyIndex;
        }
 
        public <T> CachedParseState.@Nullable CacheEntry<T> getValue(int keyIndex) {
            return (CachedParseState.CacheEntry<T>)this.atomCache[keyIndex + 1];
        }
 
        public void setValue(int keyIndex, CachedParseState.CacheEntry<?> entry) {
            this.atomCache[keyIndex + 1] = entry;
        }
    }
 
    private class Silent implements ParseState<S> {
        private final ErrorCollector<S> silentCollector;
 
        private Silent() {
            Objects.requireNonNull(CachedParseState.this);
            super();
            this.silentCollector = new ErrorCollector.Nop<>();
        }
 
        @Override
        public ErrorCollector<S> errorCollector() {
            return this.silentCollector;
        }
 
        @Override
        public Scope scope() {
            return CachedParseState.this.scope();
        }
 
        @Override
        public <T> @Nullable T parse(NamedRule<S, T> rule) {
            return CachedParseState.this.parse(rule);
        }
 
        @Override
        public S input() {
            return CachedParseState.this.input();
        }
 
        @Override
        public int mark() {
            return CachedParseState.this.mark();
        }
 
        @Override
        public void restore(int mark) {
            CachedParseState.this.restore(mark);
        }
 
        @Override
        public Control acquireControl() {
            return CachedParseState.this.acquireControl();
        }
 
        @Override
        public void releaseControl() {
            CachedParseState.this.releaseControl();
        }
 
        @Override
        public ParseState<S> silent() {
            return this;
        }
    }
 
    private static class SimpleControl implements Control {
        private boolean hasCut;
 
        @Override
        public void cut() {
            this.hasCut = true;
        }
 
        @Override
        public boolean hasCut() {
            return this.hasCut;
        }
 
        public void reset() {
            this.hasCut = false;
        }
    }
}

引用的其他类

  • LoggedChatMessage

    • 引用位置: 方法调用
    • 关联成员: System.arraycopy()
  • Util

    • 引用位置: 方法调用
    • 关联成员: Util.growByHalf()
  • Control

    • 引用位置: 返回值
  • ErrorCollector

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

    • 引用位置: 参数
  • ParseState

    • 引用位置: 实现/返回值
  • Scope

    • 引用位置: 字段/构造调用/返回值
    • 关联成员: Scope()