| 1 | package net.minecraft.util.parsing.packrat;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.Arrays;
|
| 5 | import java.util.HashMap;
|
| 6 | import java.util.Map;
|
| 7 | import net.minecraft.util.Util;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 |
|
| 10 | public final class Scope {
|
| 11 | private static final int NOT_FOUND = -1;
|
| 12 | private static final Object FRAME_START_MARKER = new Object() {
|
| 13 | @Override
|
| 14 | public String toString() {
|
| 15 | return "frame";
|
| 16 | }
|
| 17 | };
|
| 18 | private static final int ENTRY_STRIDE = 2;
|
| 19 | private @Nullable Object[] stack = new Object[128];
|
| 20 | private int topEntryKeyIndex = 0;
|
| 21 | private int topMarkerKeyIndex = 0;
|
| 22 |
|
| 23 | public Scope() {
|
| 24 | this.stack[0] = FRAME_START_MARKER;
|
| 25 | this.stack[1] = null;
|
| 26 | }
|
| 27 |
|
| 28 | private int valueIndex(final Atom<?> atom) {
|
| 29 | for (int i = this.topEntryKeyIndex; i > this.topMarkerKeyIndex; i -= 2) {
|
| 30 | Object key = this.stack[i];
|
| 31 | assert key instanceof Atom;
|
| 32 | if (key == atom) {
|
| 33 | return i + 1;
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | return -1;
|
| 38 | }
|
| 39 |
|
| 40 | public int valueIndexForAny(final Atom<?>... atoms) {
|
| 41 | for (int i = this.topEntryKeyIndex; i > this.topMarkerKeyIndex; i -= 2) {
|
| 42 | Object key = this.stack[i];
|
| 43 | assert key instanceof Atom;
|
| 44 |
|
| 45 | for (Atom<?> atom : atoms) {
|
| 46 | if (atom == key) {
|
| 47 | return i + 1;
|
| 48 | }
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | return -1;
|
| 53 | }
|
| 54 |
|
| 55 | private void ensureCapacity(final int additionalEntryCount) {
|
| 56 | int currentSize = this.stack.length;
|
| 57 | int currentLastValueIndex = this.topEntryKeyIndex + 1;
|
| 58 | int newLastValueIndex = currentLastValueIndex + additionalEntryCount * 2;
|
| 59 | if (newLastValueIndex >= currentSize) {
|
| 60 | int newSize = Util.growByHalf(currentSize, newLastValueIndex + 1);
|
| 61 | Object[] newStack = new Object[newSize];
|
| 62 | System.arraycopy(this.stack, 0, newStack, 0, currentSize);
|
| 63 | this.stack = newStack;
|
| 64 | }
|
| 65 |
|
| 66 | assert this.validateStructure();
|
| 67 | }
|
| 68 |
|
| 69 | private void setupNewFrame() {
|
| 70 | this.topEntryKeyIndex += 2;
|
| 71 | this.stack[this.topEntryKeyIndex] = FRAME_START_MARKER;
|
| 72 | this.stack[this.topEntryKeyIndex + 1] = this.topMarkerKeyIndex;
|
| 73 | this.topMarkerKeyIndex = this.topEntryKeyIndex;
|
| 74 | }
|
| 75 |
|
| 76 | public void pushFrame() {
|
| 77 | this.ensureCapacity(1);
|
| 78 | this.setupNewFrame();
|
| 79 | assert this.validateStructure();
|
| 80 | }
|
| 81 |
|
| 82 | private int getPreviousMarkerIndex(final int markerKeyIndex) {
|
| 83 | return (Integer)this.stack[markerKeyIndex + 1];
|
| 84 | }
|
| 85 |
|
| 86 | public void popFrame() {
|
| 87 | assert this.topMarkerKeyIndex != 0;
|
| 88 | this.topEntryKeyIndex = this.topMarkerKeyIndex - 2;
|
| 89 | this.topMarkerKeyIndex = this.getPreviousMarkerIndex(this.topMarkerKeyIndex);
|
| 90 | assert this.validateStructure();
|
| 91 | }
|
| 92 |
|
| 93 | public void splitFrame() {
|
| 94 | int currentFrameMarkerIndex = this.topMarkerKeyIndex;
|
| 95 | int nonMarkerEntriesInFrame = (this.topEntryKeyIndex - this.topMarkerKeyIndex) / 2;
|
| 96 | this.ensureCapacity(nonMarkerEntriesInFrame + 1);
|
| 97 | this.setupNewFrame();
|
| 98 | int sourceCursor = currentFrameMarkerIndex + 2;
|
| 99 | int targetCursor = this.topEntryKeyIndex;
|
| 100 |
|
| 101 | for (int i = 0; i < nonMarkerEntriesInFrame; i++) {
|
| 102 | targetCursor += 2;
|
| 103 | Object key = this.stack[sourceCursor];
|
| 104 | assert key != null;
|
| 105 | this.stack[targetCursor] = key;
|
| 106 | this.stack[targetCursor + 1] = null;
|
| 107 | sourceCursor += 2;
|
| 108 | }
|
| 109 |
|
| 110 | this.topEntryKeyIndex = targetCursor;
|
| 111 | assert this.validateStructure();
|
| 112 | }
|
| 113 |
|
| 114 | public void clearFrameValues() {
|
| 115 | for (int i = this.topEntryKeyIndex; i > this.topMarkerKeyIndex; i -= 2) {
|
| 116 | assert this.stack[i] instanceof Atom;
|
| 117 | this.stack[i + 1] = null;
|
| 118 | }
|
| 119 |
|
| 120 | assert this.validateStructure();
|
| 121 | }
|
| 122 |
|
| 123 | public void mergeFrame() {
|
| 124 | int previousMarkerIndex = this.getPreviousMarkerIndex(this.topMarkerKeyIndex);
|
| 125 | int previousFrameCursor = previousMarkerIndex;
|
| 126 | int currentFrameCursor = this.topMarkerKeyIndex;
|
| 127 |
|
| 128 | while (currentFrameCursor < this.topEntryKeyIndex) {
|
| 129 | previousFrameCursor += 2;
|
| 130 | currentFrameCursor += 2;
|
| 131 | Object newKey = this.stack[currentFrameCursor];
|
| 132 | assert newKey instanceof Atom;
|
| 133 | Object newValue = this.stack[currentFrameCursor + 1];
|
| 134 | Object oldKey = this.stack[previousFrameCursor];
|
| 135 | if (oldKey != newKey) {
|
| 136 | this.stack[previousFrameCursor] = newKey;
|
| 137 | this.stack[previousFrameCursor + 1] = newValue;
|
| 138 | } else if (newValue != null) {
|
| 139 | this.stack[previousFrameCursor + 1] = newValue;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | this.topEntryKeyIndex = previousFrameCursor;
|
| 144 | this.topMarkerKeyIndex = previousMarkerIndex;
|
| 145 | assert this.validateStructure();
|
| 146 | }
|
| 147 |
|
| 148 | public <T> void put(final Atom<T> name, final @Nullable T value) {
|
| 149 | int valueIndex = this.valueIndex(name);
|
| 150 | if (valueIndex != -1) {
|
| 151 | this.stack[valueIndex] = value;
|
| 152 | } else {
|
| 153 | this.ensureCapacity(1);
|
| 154 | this.topEntryKeyIndex += 2;
|
| 155 | this.stack[this.topEntryKeyIndex] = name;
|
| 156 | this.stack[this.topEntryKeyIndex + 1] = value;
|
| 157 | }
|
| 158 |
|
| 159 | assert this.validateStructure();
|
| 160 | }
|
| 161 |
|
| 162 | public <T> @Nullable T get(final Atom<T> name) {
|
| 163 | int valueIndex = this.valueIndex(name);
|
| 164 | return (T)(valueIndex != -1 ? this.stack[valueIndex] : null);
|
| 165 | }
|
| 166 |
|
| 167 | public <T> T getOrThrow(final Atom<T> name) {
|
| 168 | int valueIndex = this.valueIndex(name);
|
| 169 | if (valueIndex == -1) {
|
| 170 | throw new IllegalArgumentException("No value for atom " + name);
|
| 171 | } else {
|
| 172 | return (T)this.stack[valueIndex];
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | public <T> T getOrDefault(final Atom<T> name, final T fallback) {
|
| 177 | int valueIndex = this.valueIndex(name);
|
| 178 | return (T)(valueIndex != -1 ? this.stack[valueIndex] : fallback);
|
| 179 | }
|
| 180 |
|
| 181 | @SafeVarargs
|
| 182 | public final <T> @Nullable T getAny(final Atom<? extends T>... names) {
|
| 183 | int valueIndex = this.valueIndexForAny(names);
|
| 184 | return (T)(valueIndex != -1 ? this.stack[valueIndex] : null);
|
| 185 | }
|
| 186 |
|
| 187 | @SafeVarargs
|
| 188 | public final <T> T getAnyOrThrow(final Atom<? extends T>... names) {
|
| 189 | int valueIndex = this.valueIndexForAny(names);
|
| 190 | if (valueIndex == -1) {
|
| 191 | throw new IllegalArgumentException("No value for atoms " + Arrays.toString(names));
|
| 192 | } else {
|
| 193 | return (T)this.stack[valueIndex];
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | public String toString() {
|
| 199 | StringBuilder result = new StringBuilder();
|
| 200 | boolean afterFrame = true;
|
| 201 |
|
| 202 | for (int i = 0; i <= this.topEntryKeyIndex; i += 2) {
|
| 203 | Object key = this.stack[i];
|
| 204 | Object value = this.stack[i + 1];
|
| 205 | if (key == FRAME_START_MARKER) {
|
| 206 | result.append('|');
|
| 207 | afterFrame = true;
|
| 208 | } else {
|
| 209 | if (!afterFrame) {
|
| 210 | result.append(',');
|
| 211 | }
|
| 212 |
|
| 213 | afterFrame = false;
|
| 214 | result.append(key).append(':').append(value);
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | return result.toString();
|
| 219 | }
|
| 220 |
|
| 221 | @VisibleForTesting
|
| 222 | public Map<Atom<?>, ?> lastFrame() {
|
| 223 | HashMap<Atom<?>, Object> result = new HashMap<>();
|
| 224 |
|
| 225 | for (int i = this.topEntryKeyIndex; i > this.topMarkerKeyIndex; i -= 2) {
|
| 226 | Object key = this.stack[i];
|
| 227 | Object value = this.stack[i + 1];
|
| 228 | result.put((Atom<?>)key, value);
|
| 229 | }
|
| 230 |
|
| 231 | return result;
|
| 232 | }
|
| 233 |
|
| 234 | public boolean hasOnlySingleFrame() {
|
| 235 | for (int i = this.topEntryKeyIndex; i > 0; i--) {
|
| 236 | if (this.stack[i] == FRAME_START_MARKER) {
|
| 237 | return false;
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | if (this.stack[0] != FRAME_START_MARKER) {
|
| 242 | throw new IllegalStateException("Corrupted stack");
|
| 243 | } else {
|
| 244 | return true;
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | private boolean validateStructure() {
|
| 249 | assert this.topMarkerKeyIndex >= 0;
|
| 250 | assert this.topEntryKeyIndex >= this.topMarkerKeyIndex;
|
| 251 |
|
| 252 | for (int i = 0; i <= this.topEntryKeyIndex; i += 2) {
|
| 253 | Object key = this.stack[i];
|
| 254 | if (key != FRAME_START_MARKER && !(key instanceof Atom)) {
|
| 255 | return false;
|
| 256 | }
|
| 257 | }
|
| 258 |
|
| 259 | for (int marker = this.topMarkerKeyIndex; marker != 0; marker = this.getPreviousMarkerIndex(marker)) {
|
| 260 | Object key = this.stack[marker];
|
| 261 | if (key != FRAME_START_MARKER) {
|
| 262 | return false;
|
| 263 | }
|
| 264 | }
|
| 265 |
|
| 266 | return true;
|
| 267 | }
|
| 268 | }
|
| 269 |
|