| 1 | package net.minecraft.util.parsing.packrat;
|
| 2 |
|
| 3 | import net.minecraft.util.Util;
|
| 4 | import org.jspecify.annotations.Nullable;
|
| 5 |
|
| 6 | public abstract class CachedParseState<S> implements ParseState<S> {
|
| 7 | private CachedParseState.@Nullable PositionCache[] positionCache = new CachedParseState.PositionCache[256];
|
| 8 | private final ErrorCollector<S> errorCollector;
|
| 9 | private final Scope scope = new Scope();
|
| 10 | private CachedParseState.@Nullable SimpleControl[] controlCache = new CachedParseState.SimpleControl[16];
|
| 11 | private int nextControlToReturn;
|
| 12 | private final CachedParseState<S>.Silent silent = new CachedParseState.Silent();
|
| 13 |
|
| 14 | protected CachedParseState(final ErrorCollector<S> errorCollector) {
|
| 15 | this.errorCollector = errorCollector;
|
| 16 | }
|
| 17 |
|
| 18 | @Override
|
| 19 | public Scope scope() {
|
| 20 | return this.scope;
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public ErrorCollector<S> errorCollector() {
|
| 25 | return this.errorCollector;
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public <T> @Nullable T parse(final NamedRule<S, T> rule) {
|
| 30 | int markBeforeParse = this.mark();
|
| 31 | CachedParseState.PositionCache positionCache = this.getCacheForPosition(markBeforeParse);
|
| 32 | int entryIndex = positionCache.findKeyIndex(rule.name());
|
| 33 | if (entryIndex != -1) {
|
| 34 | CachedParseState.CacheEntry<T> value = positionCache.getValue(entryIndex);
|
| 35 | if (value != null) {
|
| 36 | if (value == CachedParseState.CacheEntry.NEGATIVE) {
|
| 37 | return null;
|
| 38 | }
|
| 39 |
|
| 40 | this.restore(value.markAfterParse);
|
| 41 | return value.value;
|
| 42 | }
|
| 43 | } else {
|
| 44 | entryIndex = positionCache.allocateNewEntry(rule.name());
|
| 45 | }
|
| 46 |
|
| 47 | T result = rule.value().parse(this);
|
| 48 | CachedParseState.CacheEntry<T> entry;
|
| 49 | if (result == null) {
|
| 50 | entry = CachedParseState.CacheEntry.negativeEntry();
|
| 51 | } else {
|
| 52 | int markAfterParse = this.mark();
|
| 53 | entry = new CachedParseState.CacheEntry<>(result, markAfterParse);
|
| 54 | }
|
| 55 |
|
| 56 | positionCache.setValue(entryIndex, entry);
|
| 57 | return result;
|
| 58 | }
|
| 59 |
|
| 60 | private CachedParseState.PositionCache getCacheForPosition(final int index) {
|
| 61 | int currentSize = this.positionCache.length;
|
| 62 | if (index >= currentSize) {
|
| 63 | int newSize = Util.growByHalf(currentSize, index + 1);
|
| 64 | CachedParseState.PositionCache[] newCache = new CachedParseState.PositionCache[newSize];
|
| 65 | System.arraycopy(this.positionCache, 0, newCache, 0, currentSize);
|
| 66 | this.positionCache = newCache;
|
| 67 | }
|
| 68 |
|
| 69 | CachedParseState.PositionCache result = this.positionCache[index];
|
| 70 | if (result == null) {
|
| 71 | result = new CachedParseState.PositionCache();
|
| 72 | this.positionCache[index] = result;
|
| 73 | }
|
| 74 |
|
| 75 | return result;
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public Control acquireControl() {
|
| 80 | int currentSize = this.controlCache.length;
|
| 81 | if (this.nextControlToReturn >= currentSize) {
|
| 82 | int newSize = Util.growByHalf(currentSize, this.nextControlToReturn + 1);
|
| 83 | CachedParseState.SimpleControl[] newControlCache = new CachedParseState.SimpleControl[newSize];
|
| 84 | System.arraycopy(this.controlCache, 0, newControlCache, 0, currentSize);
|
| 85 | this.controlCache = newControlCache;
|
| 86 | }
|
| 87 |
|
| 88 | int controlIndex = this.nextControlToReturn++;
|
| 89 | CachedParseState.SimpleControl entry = this.controlCache[controlIndex];
|
| 90 | if (entry == null) {
|
| 91 | entry = new CachedParseState.SimpleControl();
|
| 92 | this.controlCache[controlIndex] = entry;
|
| 93 | } else {
|
| 94 | entry.reset();
|
| 95 | }
|
| 96 |
|
| 97 | return entry;
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public void releaseControl() {
|
| 102 | this.nextControlToReturn--;
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public ParseState<S> silent() {
|
| 107 | return this.silent;
|
| 108 | }
|
| 109 |
|
| 110 | private record CacheEntry<T>(@Nullable T value, int markAfterParse) {
|
| 111 | public static final CachedParseState.CacheEntry<?> NEGATIVE = new CachedParseState.CacheEntry(null, -1);
|
| 112 |
|
| 113 | public static <T> CachedParseState.CacheEntry<T> negativeEntry() {
|
| 114 | return (CachedParseState.CacheEntry<T>)NEGATIVE;
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | private static class PositionCache {
|
| 119 | public static final int ENTRY_STRIDE = 2;
|
| 120 | private static final int NOT_FOUND = -1;
|
| 121 | private Object[] atomCache = new Object[16];
|
| 122 | private int nextKey;
|
| 123 |
|
| 124 | public int findKeyIndex(final Atom<?> key) {
|
| 125 | for (int i = 0; i < this.nextKey; i += 2) {
|
| 126 | if (this.atomCache[i] == key) {
|
| 127 | return i;
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | return -1;
|
| 132 | }
|
| 133 |
|
| 134 | public int allocateNewEntry(final Atom<?> key) {
|
| 135 | int newKeyIndex = this.nextKey;
|
| 136 | this.nextKey += 2;
|
| 137 | int newValueIndex = newKeyIndex + 1;
|
| 138 | int currentSize = this.atomCache.length;
|
| 139 | if (newValueIndex >= currentSize) {
|
| 140 | int newSize = Util.growByHalf(currentSize, newValueIndex + 1);
|
| 141 | Object[] newCache = new Object[newSize];
|
| 142 | System.arraycopy(this.atomCache, 0, newCache, 0, currentSize);
|
| 143 | this.atomCache = newCache;
|
| 144 | }
|
| 145 |
|
| 146 | this.atomCache[newKeyIndex] = key;
|
| 147 | return newKeyIndex;
|
| 148 | }
|
| 149 |
|
| 150 | public <T> CachedParseState.@Nullable CacheEntry<T> getValue(final int keyIndex) {
|
| 151 | return (CachedParseState.CacheEntry<T>)this.atomCache[keyIndex + 1];
|
| 152 | }
|
| 153 |
|
| 154 | public void setValue(final int keyIndex, final CachedParseState.CacheEntry<?> entry) {
|
| 155 | this.atomCache[keyIndex + 1] = entry;
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | private class Silent implements ParseState<S> {
|
| 160 | private final ErrorCollector<S> silentCollector = new ErrorCollector.Nop<>();
|
| 161 |
|
| 162 | @Override
|
| 163 | public ErrorCollector<S> errorCollector() {
|
| 164 | return this.silentCollector;
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public Scope scope() {
|
| 169 | return CachedParseState.this.scope();
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public <T> @Nullable T parse(final NamedRule<S, T> rule) {
|
| 174 | return CachedParseState.this.parse(rule);
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public S input() {
|
| 179 | return CachedParseState.this.input();
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | public int mark() {
|
| 184 | return CachedParseState.this.mark();
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | public void restore(final int mark) {
|
| 189 | CachedParseState.this.restore(mark);
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | public Control acquireControl() {
|
| 194 | return CachedParseState.this.acquireControl();
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | public void releaseControl() {
|
| 199 | CachedParseState.this.releaseControl();
|
| 200 | }
|
| 201 |
|
| 202 | @Override
|
| 203 | public ParseState<S> silent() {
|
| 204 | return this;
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | private static class SimpleControl implements Control {
|
| 209 | private boolean hasCut;
|
| 210 |
|
| 211 | @Override
|
| 212 | public void cut() {
|
| 213 | this.hasCut = true;
|
| 214 | }
|
| 215 |
|
| 216 | @Override
|
| 217 | public boolean hasCut() {
|
| 218 | return this.hasCut;
|
| 219 | }
|
| 220 |
|
| 221 | public void reset() {
|
| 222 | this.hasCut = false;
|
| 223 | }
|
| 224 | }
|
| 225 | }
|
| 226 |
|