mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-21 07:59:19 +00:00
重命名成人类可读的变量名
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -26,46 +26,44 @@ public class SnbtOperations {
|
||||
public static final Map<BuiltinKey, BuiltinOperation> BUILTIN_OPERATIONS = Map.of(
|
||||
new BuiltinKey("bool", 1), new BuiltinOperation() {
|
||||
@Override
|
||||
public <T> T run(DynamicOps<T> ops, List<T> args, ParseState<StringReader> parseState) {
|
||||
Boolean bool = convert(ops, args.getFirst());
|
||||
if (bool == null) {
|
||||
parseState.errorCollector().store(parseState.mark(), SnbtOperations.ERROR_EXPECTED_NUMBER_OR_BOOLEAN);
|
||||
public <T> T run(DynamicOps<T> ops, List<T> arguments, ParseState<StringReader> state) {
|
||||
Boolean result = convert(ops, arguments.getFirst());
|
||||
if (result == null) {
|
||||
state.errorCollector().store(state.mark(), SnbtOperations.ERROR_EXPECTED_NUMBER_OR_BOOLEAN);
|
||||
return null;
|
||||
} else {
|
||||
return ops.createBoolean(bool);
|
||||
}
|
||||
return ops.createBoolean(result);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static <T> Boolean convert(DynamicOps<T> ops, T value) {
|
||||
Optional<Boolean> optional = ops.getBooleanValue(value).result();
|
||||
if (optional.isPresent()) {
|
||||
return optional.get();
|
||||
private static <T> Boolean convert(DynamicOps<T> ops, T arg) {
|
||||
Optional<Boolean> asBoolean = ops.getBooleanValue(arg).result();
|
||||
if (asBoolean.isPresent()) {
|
||||
return asBoolean.get();
|
||||
} else {
|
||||
Optional<Number> optional1 = ops.getNumberValue(value).result();
|
||||
return optional1.isPresent() ? optional1.get().doubleValue() != 0.0 : null;
|
||||
Optional<Number> asNumber = ops.getNumberValue(arg).result();
|
||||
return asNumber.isPresent() ? asNumber.get().doubleValue() != 0.0 : null;
|
||||
}
|
||||
}
|
||||
}, new BuiltinKey("uuid", 1), new BuiltinOperation() {
|
||||
@Override
|
||||
public <T> T run(DynamicOps<T> ops, List<T> args, ParseState<StringReader> parseState) {
|
||||
Optional<String> optional = ops.getStringValue(args.getFirst()).result();
|
||||
if (optional.isEmpty()) {
|
||||
parseState.errorCollector().store(parseState.mark(), SnbtOperations.ERROR_EXPECTED_STRING_UUID);
|
||||
public <T> T run(DynamicOps<T> ops, List<T> arguments, ParseState<StringReader> state) {
|
||||
Optional<String> arg = ops.getStringValue(arguments.getFirst()).result();
|
||||
if (arg.isEmpty()) {
|
||||
state.errorCollector().store(state.mark(), SnbtOperations.ERROR_EXPECTED_STRING_UUID);
|
||||
return null;
|
||||
} else {
|
||||
}
|
||||
UUID uuid;
|
||||
try {
|
||||
uuid = UUID.fromString(optional.get());
|
||||
uuid = UUID.fromString(arg.get());
|
||||
} catch (IllegalArgumentException var7) {
|
||||
parseState.errorCollector().store(parseState.mark(), SnbtOperations.ERROR_EXPECTED_STRING_UUID);
|
||||
state.errorCollector().store(state.mark(), SnbtOperations.ERROR_EXPECTED_STRING_UUID);
|
||||
return null;
|
||||
}
|
||||
|
||||
return ops.createIntList(IntStream.of(UUIDUtil.uuidToIntArray(uuid)));
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
public static final SuggestionSupplier<StringReader> BUILTIN_IDS = new SuggestionSupplier<>() {
|
||||
private final Set<String> keys = Stream.concat(
|
||||
@@ -74,7 +72,7 @@ public class SnbtOperations {
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
@Override
|
||||
public Stream<String> possibleValues(ParseState<StringReader> parseState) {
|
||||
public Stream<String> possibleValues(ParseState<StringReader> state) {
|
||||
return this.keys.stream();
|
||||
}
|
||||
};
|
||||
@@ -88,6 +86,6 @@ public class SnbtOperations {
|
||||
|
||||
public interface BuiltinOperation {
|
||||
@Nullable
|
||||
<T> T run(DynamicOps<T> ops, List<T> args, ParseState<StringReader> parseState);
|
||||
<T> T run(DynamicOps<T> ops, List<T> arguments, ParseState<StringReader> state);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,34 +42,34 @@ public class TagParser<T> {
|
||||
return new TagParser<>(ops, SnbtGrammar.createParser(ops));
|
||||
}
|
||||
|
||||
private static CompoundTag castToCompoundOrThrow(StringReader reader, Tag tag) throws CommandSyntaxException {
|
||||
if (tag instanceof CompoundTag compoundTag) {
|
||||
private static CompoundTag castToCompoundOrThrow(StringReader reader, Tag result) throws CommandSyntaxException {
|
||||
if (result instanceof CompoundTag compoundTag) {
|
||||
return compoundTag;
|
||||
}
|
||||
throw ERROR_EXPECTED_COMPOUND.createWithContext(reader);
|
||||
}
|
||||
|
||||
public static CompoundTag parseCompoundFully(String data) throws CommandSyntaxException {
|
||||
StringReader stringReader = new StringReader(data);
|
||||
return parseCompoundAsArgument(stringReader);
|
||||
public static CompoundTag parseCompoundFully(String input) throws CommandSyntaxException {
|
||||
StringReader reader = new StringReader(input);
|
||||
return parseCompoundAsArgument(reader);
|
||||
}
|
||||
|
||||
public static Object parseObjectFully(String data) throws CommandSyntaxException {
|
||||
StringReader stringReader = new StringReader(data);
|
||||
return parseObjectAsArgument(stringReader);
|
||||
public static Object parseObjectFully(String input) throws CommandSyntaxException {
|
||||
StringReader reader = new StringReader(input);
|
||||
return parseObjectAsArgument(reader);
|
||||
}
|
||||
|
||||
public T parseFully(String text) throws CommandSyntaxException {
|
||||
return this.parseFully(new StringReader(text));
|
||||
public T parseFully(String input) throws CommandSyntaxException {
|
||||
return this.parseFully(new StringReader(input));
|
||||
}
|
||||
|
||||
public T parseFully(StringReader reader) throws CommandSyntaxException {
|
||||
T object = this.grammar.parse(reader);
|
||||
T result = this.grammar.parse(reader);
|
||||
reader.skipWhitespace();
|
||||
if (reader.canRead()) {
|
||||
throw ERROR_TRAILING_DATA.createWithContext(reader);
|
||||
}
|
||||
return object;
|
||||
return result;
|
||||
}
|
||||
|
||||
public T parseAsArgument(StringReader reader) throws CommandSyntaxException {
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package net.momirealms.craftengine.core.util.snbt.parse;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@@ -14,7 +12,6 @@ public abstract class CachedParseState<S> implements ParseState<S> {
|
||||
private SimpleControl[] controlCache = new SimpleControl[16];
|
||||
private int nextControlToReturn;
|
||||
private final Silent silent = new Silent();
|
||||
private final IntList markedNull = new IntArrayList();
|
||||
public static final Object JAVA_NULL_VALUE_MARKER = new Object() {
|
||||
@Override
|
||||
public String toString() {
|
||||
@@ -39,11 +36,11 @@ public abstract class CachedParseState<S> implements ParseState<S> {
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T parse(NamedRule<S, T> rule) {
|
||||
int i = this.mark();
|
||||
PositionCache cacheForPosition = this.getCacheForPosition(i);
|
||||
int i1 = cacheForPosition.findKeyIndex(rule.name());
|
||||
if (i1 != -1) {
|
||||
CacheEntry<T> value = cacheForPosition.getValue(i1);
|
||||
int markBeforeParse = this.mark();
|
||||
PositionCache positionCache = this.getCacheForPosition(markBeforeParse);
|
||||
int entryIndex = positionCache.findKeyIndex(rule.name());
|
||||
if (entryIndex != -1) {
|
||||
CacheEntry<T> value = positionCache.getValue(entryIndex);
|
||||
if (value != null) {
|
||||
if (value == CachedParseState.CacheEntry.NEGATIVE) {
|
||||
return null;
|
||||
@@ -52,59 +49,60 @@ public abstract class CachedParseState<S> implements ParseState<S> {
|
||||
return value.value;
|
||||
}
|
||||
} else {
|
||||
i1 = cacheForPosition.allocateNewEntry(rule.name());
|
||||
entryIndex = positionCache.allocateNewEntry(rule.name());
|
||||
}
|
||||
|
||||
T object = rule.value().parse(this);
|
||||
CacheEntry<T> cacheEntry;
|
||||
if (object == null) {
|
||||
cacheEntry = (CacheEntry<T>) CacheEntry.NEGATIVE;
|
||||
T result = rule.value().parse(this);
|
||||
CacheEntry<T> entry;
|
||||
if (result == null) {
|
||||
entry = CacheEntry.negativeEntry();
|
||||
} else {
|
||||
cacheEntry = new CacheEntry<>(object, this.mark());
|
||||
int markAfterParse = this.mark();
|
||||
entry = new CacheEntry<>(result, markAfterParse);
|
||||
}
|
||||
|
||||
cacheForPosition.setValue(i1, cacheEntry);
|
||||
return object;
|
||||
positionCache.setValue(entryIndex, entry);
|
||||
return result;
|
||||
}
|
||||
|
||||
private PositionCache getCacheForPosition(int position) {
|
||||
int i = this.positionCache.length;
|
||||
if (position >= i) {
|
||||
int i1 = MiscUtils.growByHalf(i, position + 1);
|
||||
PositionCache[] positionCaches = new PositionCache[i1];
|
||||
System.arraycopy(this.positionCache, 0, positionCaches, 0, i);
|
||||
this.positionCache = positionCaches;
|
||||
private PositionCache getCacheForPosition(int index) {
|
||||
int currentSize = this.positionCache.length;
|
||||
if (index >= currentSize) {
|
||||
int newSize = MiscUtils.growByHalf(currentSize, index + 1);
|
||||
PositionCache[] newCache = new PositionCache[newSize];
|
||||
System.arraycopy(this.positionCache, 0, newCache, 0, currentSize);
|
||||
this.positionCache = newCache;
|
||||
}
|
||||
|
||||
PositionCache positionCache = this.positionCache[position];
|
||||
if (positionCache == null) {
|
||||
positionCache = new PositionCache();
|
||||
this.positionCache[position] = positionCache;
|
||||
PositionCache result = this.positionCache[index];
|
||||
if (result == null) {
|
||||
result = new PositionCache();
|
||||
this.positionCache[index] = result;
|
||||
}
|
||||
|
||||
return positionCache;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Control acquireControl() {
|
||||
int i = this.controlCache.length;
|
||||
if (this.nextControlToReturn >= i) {
|
||||
int i1 = MiscUtils.growByHalf(i, this.nextControlToReturn + 1);
|
||||
SimpleControl[] simpleControls = new SimpleControl[i1];
|
||||
System.arraycopy(this.controlCache, 0, simpleControls, 0, i);
|
||||
this.controlCache = simpleControls;
|
||||
int currentSize = this.controlCache.length;
|
||||
if (this.nextControlToReturn >= currentSize) {
|
||||
int newSize = MiscUtils.growByHalf(currentSize, this.nextControlToReturn + 1);
|
||||
SimpleControl[] newControlCache = new SimpleControl[newSize];
|
||||
System.arraycopy(this.controlCache, 0, newControlCache, 0, currentSize);
|
||||
this.controlCache = newControlCache;
|
||||
}
|
||||
|
||||
int i1 = this.nextControlToReturn++;
|
||||
SimpleControl simpleControl = this.controlCache[i1];
|
||||
if (simpleControl == null) {
|
||||
simpleControl = new SimpleControl();
|
||||
this.controlCache[i1] = simpleControl;
|
||||
int controlIndex = this.nextControlToReturn++;
|
||||
SimpleControl entry = this.controlCache[controlIndex];
|
||||
if (entry == null) {
|
||||
entry = new SimpleControl();
|
||||
this.controlCache[controlIndex] = entry;
|
||||
} else {
|
||||
simpleControl.reset();
|
||||
entry.reset();
|
||||
}
|
||||
|
||||
return simpleControl;
|
||||
return entry;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -117,18 +115,12 @@ public abstract class CachedParseState<S> implements ParseState<S> {
|
||||
return this.silent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markNull(int mark) {
|
||||
this.markedNull.add(mark);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNull(int mark) {
|
||||
return this.markedNull.contains(mark);
|
||||
}
|
||||
|
||||
record CacheEntry<T>(@Nullable T value, int markAfterParse) {
|
||||
public static final CacheEntry<?> NEGATIVE = new CacheEntry<>(null, -1);
|
||||
|
||||
public static <T> CacheEntry<T> negativeEntry() {
|
||||
return (CacheEntry<T>) NEGATIVE;
|
||||
}
|
||||
}
|
||||
|
||||
static class PositionCache {
|
||||
@@ -137,9 +129,9 @@ public abstract class CachedParseState<S> implements ParseState<S> {
|
||||
private Object[] atomCache = new Object[16];
|
||||
private int nextKey;
|
||||
|
||||
public int findKeyIndex(Atom<?> atom) {
|
||||
public int findKeyIndex(Atom<?> key) {
|
||||
for (int i = 0; i < this.nextKey; i += ENTRY_STRIDE) {
|
||||
if (this.atomCache[i] == atom) {
|
||||
if (this.atomCache[i] == key) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -147,29 +139,29 @@ public abstract class CachedParseState<S> implements ParseState<S> {
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
public int allocateNewEntry(Atom<?> entry) {
|
||||
int i = this.nextKey;
|
||||
this.nextKey += 2;
|
||||
int i1 = i + 1;
|
||||
int i2 = this.atomCache.length;
|
||||
if (i1 >= i2) {
|
||||
int i3 = MiscUtils.growByHalf(i2, i1 + 1);
|
||||
Object[] objects = new Object[i3];
|
||||
System.arraycopy(this.atomCache, 0, objects, 0, i2);
|
||||
this.atomCache = objects;
|
||||
public int allocateNewEntry(Atom<?> key) {
|
||||
int newKeyIndex = this.nextKey;
|
||||
this.nextKey += ENTRY_STRIDE;
|
||||
int newValueIndex = newKeyIndex + 1;
|
||||
int currentSize = this.atomCache.length;
|
||||
if (newValueIndex >= currentSize) {
|
||||
int newSize = MiscUtils.growByHalf(currentSize, newValueIndex + 1);
|
||||
Object[] newCache = new Object[newSize];
|
||||
System.arraycopy(this.atomCache, 0, newCache, 0, currentSize);
|
||||
this.atomCache = newCache;
|
||||
}
|
||||
|
||||
this.atomCache[i] = entry;
|
||||
return i;
|
||||
this.atomCache[newKeyIndex] = key;
|
||||
return newKeyIndex;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> CacheEntry<T> getValue(int index) {
|
||||
return (CacheEntry<T>)this.atomCache[index + 1];
|
||||
public <T> CacheEntry<T> getValue(int keyIndex) {
|
||||
return (CacheEntry<T>) this.atomCache[keyIndex + 1];
|
||||
}
|
||||
|
||||
public void setValue(int index, CacheEntry<?> value) {
|
||||
this.atomCache[index + 1] = value;
|
||||
public void setValue(int keyIndex, CacheEntry<?> entry) {
|
||||
this.atomCache[keyIndex + 1] = entry;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,8 +195,8 @@ public abstract class CachedParseState<S> implements ParseState<S> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore(int cursor) {
|
||||
CachedParseState.this.restore(cursor);
|
||||
public void restore(int mark) {
|
||||
CachedParseState.this.restore(mark);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -221,16 +213,6 @@ public abstract class CachedParseState<S> implements ParseState<S> {
|
||||
public ParseState<S> silent() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markNull(int mark) {
|
||||
CachedParseState.this.markNull(mark);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNull(int mark) {
|
||||
return CachedParseState.this.isNull(mark);
|
||||
}
|
||||
}
|
||||
|
||||
static class SimpleControl implements Control {
|
||||
|
||||
@@ -7,13 +7,13 @@ import net.momirealms.craftengine.core.util.snbt.parse.grammar.StringReaderTerms
|
||||
|
||||
@FunctionalInterface
|
||||
public interface DelayedException<T extends Exception> {
|
||||
T create(String message, int cursor);
|
||||
T create(String contents, int position);
|
||||
|
||||
static DelayedException<CommandSyntaxException> create(SimpleCommandExceptionType exception) {
|
||||
return (message, cursor) -> exception.createWithContext(StringReaderTerms.createReader(message, cursor));
|
||||
static DelayedException<CommandSyntaxException> create(SimpleCommandExceptionType type) {
|
||||
return (contents, position) -> type.createWithContext(StringReaderTerms.createReader(contents, position));
|
||||
}
|
||||
|
||||
static DelayedException<CommandSyntaxException> create(DynamicCommandExceptionType exception, String argument) {
|
||||
return (message, cursor) -> exception.createWithContext(StringReaderTerms.createReader(message, cursor), argument);
|
||||
static DelayedException<CommandSyntaxException> create(DynamicCommandExceptionType type, String argument) {
|
||||
return (contents, position) -> type.createWithContext(StringReaderTerms.createReader(contents, position), argument);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,28 +11,30 @@ import java.util.function.Supplier;
|
||||
public class Dictionary<S> {
|
||||
private final Map<Atom<?>, Entry<S, ?>> terms = new IdentityHashMap<>();
|
||||
|
||||
public <T> NamedRule<S, T> put(Atom<T> name, Rule<S, T> rule) {
|
||||
Entry<S, T> entry = (Entry<S, T>)this.terms.computeIfAbsent(name, Entry::new);
|
||||
if (entry.value != null) {
|
||||
public <T> NamedRule<S, T> put(Atom<T> name, Rule<S, T> entry) {
|
||||
Entry<S, T> holder = (Entry<S, T>)this.terms.computeIfAbsent(name, Entry::new);
|
||||
if (holder.value != null) {
|
||||
throw new IllegalArgumentException("Trying to override rule: " + name);
|
||||
} else {
|
||||
entry.value = rule;
|
||||
return entry;
|
||||
}
|
||||
holder.value = entry;
|
||||
return holder;
|
||||
}
|
||||
|
||||
public <T> NamedRule<S, T> putComplex(Atom<T> name, Term<S> term, Rule.RuleAction<S, T> ruleAction) {
|
||||
return this.put(name, Rule.fromTerm(term, ruleAction));
|
||||
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> ruleAction) {
|
||||
return this.put(name, Rule.fromTerm(term, ruleAction));
|
||||
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<?>> list = this.terms.entrySet().stream().filter(entry -> entry.getValue() == null).map(Map.Entry::getKey).toList();
|
||||
if (!list.isEmpty()) {
|
||||
throw new IllegalStateException("Unbound names: " + list);
|
||||
List<? extends Atom<?>> unboundNames = this.terms.entrySet().stream()
|
||||
.filter(entry -> entry.getValue() == null)
|
||||
.map(Map.Entry::getKey)
|
||||
.toList();
|
||||
if (!unboundNames.isEmpty()) {
|
||||
throw new IllegalStateException("Unbound names: " + unboundNames);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,8 +50,8 @@ public class Dictionary<S> {
|
||||
return new Reference<>(this.getOrCreateEntry(name), name);
|
||||
}
|
||||
|
||||
public <T> Term<S> namedWithAlias(Atom<T> name, Atom<T> alias) {
|
||||
return new Reference<>(this.getOrCreateEntry(name), alias);
|
||||
public <T> Term<S> namedWithAlias(Atom<T> nameToParse, Atom<T> nameToStore) {
|
||||
return new Reference<>(this.getOrCreateEntry(nameToParse), nameToStore);
|
||||
}
|
||||
|
||||
static class Entry<S, T> implements NamedRule<S, T>, Supplier<String> {
|
||||
@@ -79,14 +81,13 @@ public class Dictionary<S> {
|
||||
|
||||
record Reference<S, T>(Entry<S, T> ruleToParse, Atom<T> nameToStore) implements Term<S> {
|
||||
@Override
|
||||
public boolean parse(ParseState<S> parseState, Scope scope, Control control) {
|
||||
T object = parseState.parse(this.ruleToParse);
|
||||
if (object == null) {
|
||||
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, object);
|
||||
}
|
||||
scope.put(this.nameToStore, result);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public interface ErrorCollector<S> {
|
||||
this.store(cursor, SuggestionSupplier.empty(), reason);
|
||||
}
|
||||
|
||||
void finish(int cursor);
|
||||
void finish(int finalCursor);
|
||||
|
||||
class LongestOnly<S> implements ErrorCollector<S> {
|
||||
private MutableErrorEntry<S>[] entries = new MutableErrorEntry[16];
|
||||
@@ -28,8 +28,8 @@ public interface ErrorCollector<S> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish(int cursor) {
|
||||
this.discardErrorsFromShorterParse(cursor);
|
||||
public void finish(int finalCursor) {
|
||||
this.discardErrorsFromShorterParse(finalCursor);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,39 +41,38 @@ public interface ErrorCollector<S> {
|
||||
}
|
||||
|
||||
private void addErrorEntry(SuggestionSupplier<S> suggestions, Object reason) {
|
||||
int i = this.entries.length;
|
||||
if (this.nextErrorEntry >= i) {
|
||||
int i1 = MiscUtils.growByHalf(i, this.nextErrorEntry + 1);
|
||||
MutableErrorEntry<S>[] mutableErrorEntrys = new MutableErrorEntry[i1];
|
||||
System.arraycopy(this.entries, 0, mutableErrorEntrys, 0, i);
|
||||
this.entries = mutableErrorEntrys;
|
||||
int currentSize = this.entries.length;
|
||||
if (this.nextErrorEntry >= currentSize) {
|
||||
int newSize = MiscUtils.growByHalf(currentSize, this.nextErrorEntry + 1);
|
||||
MutableErrorEntry<S>[] newEntries = new MutableErrorEntry[newSize];
|
||||
System.arraycopy(this.entries, 0, newEntries, 0, currentSize);
|
||||
this.entries = newEntries;
|
||||
}
|
||||
|
||||
int i1 = this.nextErrorEntry++;
|
||||
MutableErrorEntry<S> mutableErrorEntry = this.entries[i1];
|
||||
if (mutableErrorEntry == null) {
|
||||
mutableErrorEntry = new MutableErrorEntry<>();
|
||||
this.entries[i1] = mutableErrorEntry;
|
||||
int entryIndex = this.nextErrorEntry++;
|
||||
MutableErrorEntry<S> entry = this.entries[entryIndex];
|
||||
if (entry == null) {
|
||||
entry = new MutableErrorEntry<>();
|
||||
this.entries[entryIndex] = entry;
|
||||
}
|
||||
|
||||
mutableErrorEntry.suggestions = suggestions;
|
||||
mutableErrorEntry.reason = reason;
|
||||
entry.suggestions = suggestions;
|
||||
entry.reason = reason;
|
||||
}
|
||||
|
||||
public List<ErrorEntry<S>> entries() {
|
||||
int i = this.nextErrorEntry;
|
||||
if (i == 0) {
|
||||
int errorCount = this.nextErrorEntry;
|
||||
if (errorCount == 0) {
|
||||
return List.of();
|
||||
} else {
|
||||
List<ErrorEntry<S>> list = new ArrayList<>(i);
|
||||
}
|
||||
List<ErrorEntry<S>> result = new ArrayList<>(errorCount);
|
||||
|
||||
for (int i1 = 0; i1 < i; i1++) {
|
||||
MutableErrorEntry<S> mutableErrorEntry = this.entries[i1];
|
||||
list.add(new ErrorEntry<>(this.lastCursor, mutableErrorEntry.suggestions, mutableErrorEntry.reason));
|
||||
for (int i = 0; i < errorCount; i++) {
|
||||
MutableErrorEntry<S> mutableErrorEntry = this.entries[i];
|
||||
result.add(new ErrorEntry<>(this.lastCursor, mutableErrorEntry.suggestions, mutableErrorEntry.reason));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public int cursor() {
|
||||
@@ -92,7 +91,7 @@ public interface ErrorCollector<S> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish(int cursor) {
|
||||
public void finish(int finalCursor) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,15 @@ public interface ParseState<S> {
|
||||
ErrorCollector<S> errorCollector();
|
||||
|
||||
default <T> Optional<T> parseTopRule(NamedRule<S, T> rule) {
|
||||
T object = this.parse(rule);
|
||||
if (object != null) {
|
||||
T result = this.parse(rule);
|
||||
if (result != null) {
|
||||
this.errorCollector().finish(this.mark());
|
||||
}
|
||||
|
||||
if (!this.scope().hasOnlySingleFrame()) {
|
||||
throw new IllegalStateException("Malformed scope: " + this.scope());
|
||||
} else {
|
||||
return Optional.ofNullable(object);
|
||||
}
|
||||
return Optional.ofNullable(result);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -28,15 +27,11 @@ public interface ParseState<S> {
|
||||
|
||||
int mark();
|
||||
|
||||
void restore(int cursor);
|
||||
void restore(int mark);
|
||||
|
||||
Control acquireControl();
|
||||
|
||||
void releaseControl();
|
||||
|
||||
ParseState<S> silent();
|
||||
|
||||
void markNull(int mark);
|
||||
|
||||
boolean isNull(int mark);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import javax.annotation.Nullable;
|
||||
|
||||
public interface Rule<S, T> {
|
||||
@Nullable
|
||||
T parse(ParseState<S> parseState);
|
||||
T parse(ParseState<S> state);
|
||||
|
||||
static <S, T> Rule<S, T> fromTerm(Term<S> child, RuleAction<S, T> action) {
|
||||
return new WrappedTerm<>(action, child);
|
||||
@@ -17,38 +17,35 @@ public interface Rule<S, T> {
|
||||
@FunctionalInterface
|
||||
interface RuleAction<S, T> {
|
||||
@Nullable
|
||||
T run(ParseState<S> parseState);
|
||||
T run(ParseState<S> state);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface SimpleRuleAction<S, T> extends RuleAction<S, T> {
|
||||
T run(Scope scope);
|
||||
T run(Scope ruleScope);
|
||||
|
||||
@Override
|
||||
default T run(ParseState<S> parseState) {
|
||||
return this.run(parseState.scope());
|
||||
default T run(ParseState<S> state) {
|
||||
return this.run(state.scope());
|
||||
}
|
||||
}
|
||||
|
||||
record WrappedTerm<S, T>(RuleAction<S, T> action, Term<S> child) implements Rule<S, T> {
|
||||
@Nullable
|
||||
@Override
|
||||
public T parse(ParseState<S> parseState) {
|
||||
Scope scope = parseState.scope();
|
||||
public T parse(ParseState<S> state) {
|
||||
Scope scope = state.scope();
|
||||
scope.pushFrame();
|
||||
|
||||
T var3;
|
||||
try {
|
||||
if (!this.child.parse(parseState, scope, Control.UNBOUND)) {
|
||||
if (!this.child.parse(state, scope, Control.UNBOUND)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var3 = this.action.run(parseState);
|
||||
return this.action.run(state);
|
||||
} finally {
|
||||
scope.popFrame();
|
||||
}
|
||||
|
||||
return var3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,13 +28,13 @@ public final class Scope {
|
||||
this.stack[1] = null;
|
||||
}
|
||||
|
||||
private int valueIndex(Atom<?> name) {
|
||||
private int valueIndex(Atom<?> atom) {
|
||||
for (int i = this.topEntryKeyIndex; i > this.topMarkerKeyIndex; i -= ENTRY_STRIDE) {
|
||||
Object object = this.stack[i];
|
||||
Object key = this.stack[i];
|
||||
|
||||
assert object instanceof Atom;
|
||||
assert key instanceof Atom;
|
||||
|
||||
if (object == name) {
|
||||
if (key == atom) {
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
@@ -42,14 +42,14 @@ public final class Scope {
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
public int valueIndexForAny(Atom<?>... names) {
|
||||
public int valueIndexForAny(Atom<?>... atoms) {
|
||||
for (int i = this.topEntryKeyIndex; i > this.topMarkerKeyIndex; i -= ENTRY_STRIDE) {
|
||||
Object object = this.stack[i];
|
||||
Object key = this.stack[i];
|
||||
|
||||
assert object instanceof Atom;
|
||||
assert key instanceof Atom;
|
||||
|
||||
for (Atom<?> atom : names) {
|
||||
if (atom == object) {
|
||||
for (Atom<?> atom : atoms) {
|
||||
if (atom == key) {
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
@@ -58,15 +58,15 @@ public final class Scope {
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
private void ensureCapacity(int requiredCapacitty) {
|
||||
int i = this.stack.length;
|
||||
int i1 = this.topEntryKeyIndex + 1;
|
||||
int i2 = i1 + requiredCapacitty * 2;
|
||||
if (i2 >= i) {
|
||||
int i3 = MiscUtils.growByHalf(i, i2 + 1);
|
||||
Object[] objects = new Object[i3];
|
||||
System.arraycopy(this.stack, 0, objects, 0, i);
|
||||
this.stack = objects;
|
||||
private void ensureCapacity(int additionalEntryCount) {
|
||||
int currentSize = this.stack.length;
|
||||
int currentLastValueIndex = this.topEntryKeyIndex + 1;
|
||||
int newLastValueIndex = currentLastValueIndex + additionalEntryCount * 2;
|
||||
if (newLastValueIndex >= currentSize) {
|
||||
int newSize = MiscUtils.growByHalf(currentSize, newLastValueIndex + 1);
|
||||
Object[] newStack = new Object[newSize];
|
||||
System.arraycopy(this.stack, 0, newStack, 0, currentSize);
|
||||
this.stack = newStack;
|
||||
}
|
||||
|
||||
assert this.validateStructure();
|
||||
@@ -86,8 +86,8 @@ public final class Scope {
|
||||
assert this.validateStructure();
|
||||
}
|
||||
|
||||
private int getPreviousMarkerIndex(int markerIndex) {
|
||||
return (Integer)this.stack[markerIndex + 1];
|
||||
private int getPreviousMarkerIndex(int markerKeyIndex) {
|
||||
return (Integer) this.stack[markerKeyIndex + 1];
|
||||
}
|
||||
|
||||
public void popFrame() {
|
||||
@@ -100,25 +100,25 @@ public final class Scope {
|
||||
}
|
||||
|
||||
public void splitFrame() {
|
||||
int i = this.topMarkerKeyIndex;
|
||||
int i1 = (this.topEntryKeyIndex - this.topMarkerKeyIndex) / ENTRY_STRIDE;
|
||||
this.ensureCapacity(i1 + 1);
|
||||
int currentFrameMarkerIndex = this.topMarkerKeyIndex;
|
||||
int nonMarkerEntriesInFrame = (this.topEntryKeyIndex - this.topMarkerKeyIndex) / ENTRY_STRIDE;
|
||||
this.ensureCapacity(nonMarkerEntriesInFrame + 1);
|
||||
this.setupNewFrame();
|
||||
int i2 = i + ENTRY_STRIDE;
|
||||
int i3 = this.topEntryKeyIndex;
|
||||
int sourceCursor = currentFrameMarkerIndex + ENTRY_STRIDE;
|
||||
int targetCursor = this.topEntryKeyIndex;
|
||||
|
||||
for (int i4 = 0; i4 < i1; i4++) {
|
||||
i3 += ENTRY_STRIDE;
|
||||
Object object = this.stack[i2];
|
||||
for (int i = 0; i < nonMarkerEntriesInFrame; i++) {
|
||||
targetCursor += ENTRY_STRIDE;
|
||||
Object key = this.stack[sourceCursor];
|
||||
|
||||
assert object != null;
|
||||
assert key != null;
|
||||
|
||||
this.stack[i3] = object;
|
||||
this.stack[i3 + 1] = null;
|
||||
i2 += ENTRY_STRIDE;
|
||||
this.stack[targetCursor] = key;
|
||||
this.stack[targetCursor + 1] = null;
|
||||
sourceCursor += ENTRY_STRIDE;
|
||||
}
|
||||
|
||||
this.topEntryKeyIndex = i3;
|
||||
this.topEntryKeyIndex = targetCursor;
|
||||
|
||||
assert this.validateStructure();
|
||||
}
|
||||
@@ -135,40 +135,40 @@ public final class Scope {
|
||||
|
||||
public void mergeFrame() {
|
||||
int previousMarkerIndex = this.getPreviousMarkerIndex(this.topMarkerKeyIndex);
|
||||
int i = previousMarkerIndex;
|
||||
int i1 = this.topMarkerKeyIndex;
|
||||
int previousFrameCursor = previousMarkerIndex;
|
||||
int currentFrameCursor = this.topMarkerKeyIndex;
|
||||
|
||||
while (i1 < this.topEntryKeyIndex) {
|
||||
i += ENTRY_STRIDE;
|
||||
i1 += ENTRY_STRIDE;
|
||||
Object object = this.stack[i1];
|
||||
while (currentFrameCursor < this.topEntryKeyIndex) {
|
||||
previousFrameCursor += ENTRY_STRIDE;
|
||||
currentFrameCursor += ENTRY_STRIDE;
|
||||
Object newKey = this.stack[currentFrameCursor];
|
||||
|
||||
assert object instanceof Atom;
|
||||
assert newKey instanceof Atom;
|
||||
|
||||
Object object1 = this.stack[i1 + 1];
|
||||
Object object2 = this.stack[i];
|
||||
if (object2 != object) {
|
||||
this.stack[i] = object;
|
||||
this.stack[i + 1] = object1;
|
||||
} else if (object1 != null) {
|
||||
this.stack[i + 1] = object1;
|
||||
Object newValue = this.stack[currentFrameCursor + 1];
|
||||
Object oldKey = this.stack[previousFrameCursor];
|
||||
if (oldKey != newKey) {
|
||||
this.stack[previousFrameCursor] = newKey;
|
||||
this.stack[previousFrameCursor + 1] = newValue;
|
||||
} else if (newValue != null) {
|
||||
this.stack[previousFrameCursor + 1] = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
this.topEntryKeyIndex = i;
|
||||
this.topEntryKeyIndex = previousFrameCursor;
|
||||
this.topMarkerKeyIndex = previousMarkerIndex;
|
||||
|
||||
assert this.validateStructure();
|
||||
}
|
||||
|
||||
public <T> void put(Atom<T> atom, @Nullable T value) {
|
||||
int i = this.valueIndex(atom);
|
||||
if (i != NOT_FOUND) {
|
||||
this.stack[i] = value;
|
||||
public <T> void put(Atom<T> name, @Nullable T value) {
|
||||
int valueIndex = this.valueIndex(name);
|
||||
if (valueIndex != NOT_FOUND) {
|
||||
this.stack[valueIndex] = value;
|
||||
} else {
|
||||
this.ensureCapacity(1);
|
||||
this.topEntryKeyIndex += ENTRY_STRIDE;
|
||||
this.stack[this.topEntryKeyIndex] = atom;
|
||||
this.stack[this.topEntryKeyIndex] = name;
|
||||
this.stack[this.topEntryKeyIndex + 1] = value;
|
||||
}
|
||||
|
||||
@@ -176,77 +176,75 @@ public final class Scope {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T get(Atom<T> atom) {
|
||||
int i = this.valueIndex(atom);
|
||||
return (T)(i != NOT_FOUND ? this.stack[i] : null);
|
||||
public <T> T get(Atom<T> name) {
|
||||
int valueIndex = this.valueIndex(name);
|
||||
return (T) (valueIndex != NOT_FOUND ? this.stack[valueIndex] : null);
|
||||
}
|
||||
|
||||
public <T> T getOrThrow(Atom<T> atom) {
|
||||
int i = this.valueIndex(atom);
|
||||
if (i == NOT_FOUND) {
|
||||
throw new IllegalArgumentException("No value for atom " + atom);
|
||||
} else {
|
||||
return (T)this.stack[i];
|
||||
public <T> T getOrThrow(Atom<T> name) {
|
||||
int valueIndex = this.valueIndex(name);
|
||||
if (valueIndex == NOT_FOUND) {
|
||||
throw new IllegalArgumentException("No value for atom " + name);
|
||||
}
|
||||
return (T) this.stack[valueIndex];
|
||||
}
|
||||
|
||||
public <T> T getOrDefault(Atom<T> atom, T defaultValue) {
|
||||
int i = this.valueIndex(atom);
|
||||
return (T)(i != NOT_FOUND ? this.stack[i] : defaultValue);
|
||||
public <T> T getOrDefault(Atom<T> name, T fallback) {
|
||||
int valueIndex = this.valueIndex(name);
|
||||
return (T) (valueIndex != NOT_FOUND ? this.stack[valueIndex] : fallback);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@SafeVarargs
|
||||
public final <T> T getAny(Atom<? extends T>... atoms) {
|
||||
int i = this.valueIndexForAny(atoms);
|
||||
return (T)(i != NOT_FOUND ? this.stack[i] : null);
|
||||
public final <T> T getAny(Atom<? extends T>... names) {
|
||||
int valueIndex = this.valueIndexForAny(names);
|
||||
return (T) (valueIndex != NOT_FOUND ? this.stack[valueIndex] : null);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public final <T> T getAnyOrThrow(Atom<? extends T>... atoms) {
|
||||
int i = this.valueIndexForAny(atoms);
|
||||
if (i == NOT_FOUND) {
|
||||
throw new IllegalArgumentException("No value for atoms " + Arrays.toString(atoms));
|
||||
} else {
|
||||
return (T)this.stack[i];
|
||||
public final <T> T getAnyOrThrow(Atom<? extends T>... names) {
|
||||
int valueIndex = this.valueIndexForAny(names);
|
||||
if (valueIndex == NOT_FOUND) {
|
||||
throw new IllegalArgumentException("No value for atoms " + Arrays.toString(names));
|
||||
}
|
||||
return (T) this.stack[valueIndex];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
boolean flag = true;
|
||||
StringBuilder result = new StringBuilder();
|
||||
boolean afterFrame = true;
|
||||
|
||||
for (int i = 0; i <= this.topEntryKeyIndex; i += ENTRY_STRIDE) {
|
||||
Object object = this.stack[i];
|
||||
Object object1 = this.stack[i + 1];
|
||||
if (object == FRAME_START_MARKER) {
|
||||
stringBuilder.append('|');
|
||||
flag = true;
|
||||
Object key = this.stack[i];
|
||||
Object value = this.stack[i + 1];
|
||||
if (key == FRAME_START_MARKER) {
|
||||
result.append('|');
|
||||
afterFrame = true;
|
||||
} else {
|
||||
if (!flag) {
|
||||
stringBuilder.append(',');
|
||||
if (!afterFrame) {
|
||||
result.append(',');
|
||||
}
|
||||
|
||||
flag = false;
|
||||
stringBuilder.append(object).append(':').append(object1);
|
||||
afterFrame = false;
|
||||
result.append(key).append(':').append(value);
|
||||
}
|
||||
}
|
||||
|
||||
return stringBuilder.toString();
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public Map<Atom<?>, ?> lastFrame() {
|
||||
HashMap<Atom<?>, Object> map = new HashMap<>();
|
||||
HashMap<Atom<?>, Object> result = new HashMap<>();
|
||||
|
||||
for (int i = this.topEntryKeyIndex; i > this.topMarkerKeyIndex; i -= ENTRY_STRIDE) {
|
||||
Object object = this.stack[i];
|
||||
Object object1 = this.stack[i + 1];
|
||||
map.put((Atom<?>)object, object1);
|
||||
Object key = this.stack[i];
|
||||
Object value = this.stack[i + 1];
|
||||
result.put((Atom<?>) key, value);
|
||||
}
|
||||
|
||||
return map;
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean hasOnlySingleFrame() {
|
||||
@@ -258,9 +256,8 @@ public final class Scope {
|
||||
|
||||
if (this.stack[0] != FRAME_START_MARKER) {
|
||||
throw new IllegalStateException("Corrupted stack");
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean validateStructure() {
|
||||
@@ -285,14 +282,15 @@ public final class Scope {
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked","rawtypes"})
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static <S> Term<S> increaseDepth() {
|
||||
class IncreasingDepthTerm<W> implements Term<W> {
|
||||
public static final IncreasingDepthTerm INSTANCE = new IncreasingDepthTerm();
|
||||
|
||||
@Override
|
||||
public boolean parse(final ParseState<W> parseState, final Scope scope, final Control control) {
|
||||
public boolean parse(final ParseState<W> state, final Scope scope, final Control control) {
|
||||
if (++scope.depth > 512) {
|
||||
parseState.errorCollector().store(parseState.mark(), new IllegalStateException("Too deep"));
|
||||
state.errorCollector().store(state.mark(), new IllegalStateException("Too deep"));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -301,12 +299,13 @@ public final class Scope {
|
||||
return (Term<S>) IncreasingDepthTerm.INSTANCE;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked","rawtypes"})
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static <S> Term<S> decreaseDepth() {
|
||||
class DecreasingDepthTerm<W> implements Term<W> {
|
||||
public static final DecreasingDepthTerm INSTANCE = new DecreasingDepthTerm();
|
||||
|
||||
@Override
|
||||
public boolean parse(final ParseState<W> parseState, final Scope scope, final Control control) {
|
||||
public boolean parse(final ParseState<W> state, final Scope scope, final Control control) {
|
||||
scope.depth--;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ package net.momirealms.craftengine.core.util.snbt.parse;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface SuggestionSupplier<S> {
|
||||
Stream<String> possibleValues(ParseState<S> parseState);
|
||||
Stream<String> possibleValues(ParseState<S> state);
|
||||
|
||||
static <S> SuggestionSupplier<S> empty() {
|
||||
return parseState -> Stream.empty();
|
||||
return state -> Stream.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,20 +4,20 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public interface Term<S> {
|
||||
boolean parse(ParseState<S> parseState, Scope scope, Control control);
|
||||
boolean parse(ParseState<S> state, Scope scope, Control control);
|
||||
|
||||
static <S, T> Term<S> marker(Atom<T> name, T value) {
|
||||
return new Marker<>(name, value);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
static <S> Term<S> sequence(Term<S>... elements) {
|
||||
return new Sequence<>(elements);
|
||||
static <S> Term<S> sequence(Term<S>... terms) {
|
||||
return new Sequence<>(terms);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
static <S> Term<S> alternative(Term<S>... elements) {
|
||||
return new Alternative<>(elements);
|
||||
static <S> Term<S> alternative(Term<S>... terms) {
|
||||
return new Alternative<>(terms);
|
||||
}
|
||||
|
||||
static <S> Term<S> optional(Term<S> term) {
|
||||
@@ -32,8 +32,8 @@ public interface Term<S> {
|
||||
return new Repeated<>(element, listName, minRepetitions);
|
||||
}
|
||||
|
||||
static <S, T> Term<S> repeatedWithTrailingSeparator(NamedRule<S, T> element, Atom<List<T>> listName, Term<S> seperator) {
|
||||
return repeatedWithTrailingSeparator(element, listName, seperator, 0);
|
||||
static <S, T> Term<S> repeatedWithTrailingSeparator(NamedRule<S, T> element, Atom<List<T>> listName, Term<S> separator) {
|
||||
return repeatedWithTrailingSeparator(element, listName, separator, 0);
|
||||
}
|
||||
|
||||
static <S, T> Term<S> repeatedWithTrailingSeparator(NamedRule<S, T> element, Atom<List<T>> listName, Term<S> seperator, int minRepetitions) {
|
||||
@@ -47,7 +47,7 @@ public interface Term<S> {
|
||||
static <S> Term<S> cut() {
|
||||
return new Term<>() {
|
||||
@Override
|
||||
public boolean parse(ParseState<S> parseState, Scope scope, Control control) {
|
||||
public boolean parse(ParseState<S> state, Scope scope, Control control) {
|
||||
control.cut();
|
||||
return true;
|
||||
}
|
||||
@@ -62,7 +62,7 @@ public interface Term<S> {
|
||||
static <S> Term<S> empty() {
|
||||
return new Term<>() {
|
||||
@Override
|
||||
public boolean parse(ParseState<S> parseState, Scope scope, Control control) {
|
||||
public boolean parse(ParseState<S> state, Scope scope, Control control) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -73,11 +73,11 @@ public interface Term<S> {
|
||||
};
|
||||
}
|
||||
|
||||
static <S> Term<S> fail(final Object reason) {
|
||||
static <S> Term<S> fail(final Object message) {
|
||||
return new Term<>() {
|
||||
@Override
|
||||
public boolean parse(ParseState<S> parseState, Scope scope, Control control) {
|
||||
parseState.errorCollector().store(parseState.mark(), reason);
|
||||
public boolean parse(ParseState<S> state, Scope scope, Control control) {
|
||||
state.errorCollector().store(state.mark(), message);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -90,22 +90,22 @@ public interface Term<S> {
|
||||
|
||||
record Alternative<S>(Term<S>[] elements) implements Term<S> {
|
||||
@Override
|
||||
public boolean parse(ParseState<S> parseState, Scope scope, Control control) {
|
||||
Control control1 = parseState.acquireControl();
|
||||
public boolean parse(ParseState<S> state, Scope scope, Control control) {
|
||||
Control controlForThis = state.acquireControl();
|
||||
|
||||
try {
|
||||
int i = parseState.mark();
|
||||
int mark = state.mark();
|
||||
scope.splitFrame();
|
||||
|
||||
for (Term<S> term : this.elements) {
|
||||
if (term.parse(parseState, scope, control1)) {
|
||||
for (Term<S> element : this.elements) {
|
||||
if (element.parse(state, scope, controlForThis)) {
|
||||
scope.mergeFrame();
|
||||
return true;
|
||||
}
|
||||
|
||||
scope.clearFrameValues();
|
||||
parseState.restore(i);
|
||||
if (control1.hasCut()) {
|
||||
state.restore(mark);
|
||||
if (controlForThis.hasCut()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -113,24 +113,24 @@ public interface Term<S> {
|
||||
scope.popFrame();
|
||||
return false;
|
||||
} finally {
|
||||
parseState.releaseControl();
|
||||
state.releaseControl();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
record LookAhead<S>(Term<S> term, boolean positive) implements Term<S> {
|
||||
@Override
|
||||
public boolean parse(ParseState<S> parseState, Scope scope, Control control) {
|
||||
int i = parseState.mark();
|
||||
boolean flag = this.term.parse(parseState.silent(), scope, control);
|
||||
parseState.restore(i);
|
||||
return this.positive == flag;
|
||||
public boolean parse(ParseState<S> state, Scope scope, Control control) {
|
||||
int mark = state.mark();
|
||||
boolean result = this.term.parse(state.silent(), scope, control);
|
||||
state.restore(mark);
|
||||
return this.positive == result;
|
||||
}
|
||||
}
|
||||
|
||||
record Marker<S, T>(Atom<T> name, T value) implements Term<S> {
|
||||
@Override
|
||||
public boolean parse(ParseState<S> parseState, Scope scope, Control control) {
|
||||
public boolean parse(ParseState<S> state, Scope scope, Control control) {
|
||||
scope.put(this.name, this.value);
|
||||
return true;
|
||||
}
|
||||
@@ -138,10 +138,10 @@ public interface Term<S> {
|
||||
|
||||
record Maybe<S>(Term<S> term) implements Term<S> {
|
||||
@Override
|
||||
public boolean parse(ParseState<S> parseState, Scope scope, Control control) {
|
||||
int i = parseState.mark();
|
||||
if (!this.term.parse(parseState, scope, control)) {
|
||||
parseState.restore(i);
|
||||
public boolean parse(ParseState<S> state, Scope scope, Control control) {
|
||||
int mark = state.mark();
|
||||
if (!this.term.parse(state, scope, control)) {
|
||||
state.restore(mark);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -150,25 +150,24 @@ public interface Term<S> {
|
||||
|
||||
record Repeated<S, T>(NamedRule<S, T> element, Atom<List<T>> listName, int minRepetitions) implements Term<S> {
|
||||
@Override
|
||||
public boolean parse(ParseState<S> parseState, Scope scope, Control control) {
|
||||
int i = parseState.mark();
|
||||
List<T> list = new ArrayList<>(this.minRepetitions);
|
||||
public boolean parse(ParseState<S> state, Scope scope, Control control) {
|
||||
int mark = state.mark();
|
||||
List<T> elements = new ArrayList<>(this.minRepetitions);
|
||||
|
||||
while (true) {
|
||||
int i1 = parseState.mark();
|
||||
T object = parseState.parse(this.element);
|
||||
if (object == null) {
|
||||
parseState.restore(i1);
|
||||
if (list.size() < this.minRepetitions) {
|
||||
parseState.restore(i);
|
||||
int entryMark = state.mark();
|
||||
T parsedElement = state.parse(this.element);
|
||||
if (parsedElement == null) {
|
||||
state.restore(entryMark);
|
||||
if (elements.size() < this.minRepetitions) {
|
||||
state.restore(mark);
|
||||
return false;
|
||||
} else {
|
||||
scope.put(this.listName, list);
|
||||
}
|
||||
scope.put(this.listName, elements);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
list.add(object);
|
||||
elements.add(parsedElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -177,56 +176,55 @@ public interface Term<S> {
|
||||
NamedRule<S, T> element, Atom<List<T>> listName, Term<S> separator, int minRepetitions, boolean allowTrailingSeparator
|
||||
) implements Term<S> {
|
||||
@Override
|
||||
public boolean parse(ParseState<S> parseState, Scope scope, Control control) {
|
||||
int i = parseState.mark();
|
||||
List<T> list = new ArrayList<>(this.minRepetitions);
|
||||
boolean flag = true;
|
||||
public boolean parse(ParseState<S> state, Scope scope, Control control) {
|
||||
int listMark = state.mark();
|
||||
List<T> elements = new ArrayList<>(this.minRepetitions);
|
||||
boolean first = true;
|
||||
|
||||
while (true) {
|
||||
int i1 = parseState.mark();
|
||||
if (!flag && !this.separator.parse(parseState, scope, control)) {
|
||||
parseState.restore(i1);
|
||||
int markBeforeSeparator = state.mark();
|
||||
if (!first && !this.separator.parse(state, scope, control)) {
|
||||
state.restore(markBeforeSeparator);
|
||||
break;
|
||||
}
|
||||
|
||||
int i2 = parseState.mark();
|
||||
T object = parseState.parse(this.element);
|
||||
if (object == null) {
|
||||
if (flag) {
|
||||
parseState.restore(i2);
|
||||
int markAfterSeparator = state.mark();
|
||||
T parsedElement = state.parse(this.element);
|
||||
if (parsedElement == null) {
|
||||
if (first) {
|
||||
state.restore(markAfterSeparator);
|
||||
} else {
|
||||
if (!this.allowTrailingSeparator) {
|
||||
parseState.restore(i);
|
||||
state.restore(listMark);
|
||||
return false;
|
||||
}
|
||||
|
||||
parseState.restore(i2);
|
||||
state.restore(markAfterSeparator);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
list.add(object);
|
||||
flag = false;
|
||||
elements.add(parsedElement);
|
||||
first = false;
|
||||
}
|
||||
|
||||
if (list.size() < this.minRepetitions) {
|
||||
parseState.restore(i);
|
||||
if (elements.size() < this.minRepetitions) {
|
||||
state.restore(listMark);
|
||||
return false;
|
||||
} else {
|
||||
scope.put(this.listName, list);
|
||||
return true;
|
||||
}
|
||||
scope.put(this.listName, elements);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
record Sequence<S>(Term<S>[] elements) implements Term<S> {
|
||||
@Override
|
||||
public boolean parse(ParseState<S> parseState, Scope scope, Control control) {
|
||||
int i = parseState.mark();
|
||||
public boolean parse(ParseState<S> state, Scope scope, Control control) {
|
||||
int mark = state.mark();
|
||||
|
||||
for (Term<S> term : this.elements) {
|
||||
if (!term.parse(parseState, scope, control)) {
|
||||
parseState.restore(i);
|
||||
for (Term<S> element : this.elements) {
|
||||
if (!element.parse(state, scope, control)) {
|
||||
state.restore(mark);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,41 +13,39 @@ public record Grammar<T>(Dictionary<StringReader> rules, NamedRule<StringReader,
|
||||
rules.checkAllBound();
|
||||
}
|
||||
|
||||
public Optional<T> parse(ParseState<StringReader> parseState) {
|
||||
return parseState.parseTopRule(this.top);
|
||||
public Optional<T> parse(ParseState<StringReader> state) {
|
||||
return state.parseTopRule(this.top);
|
||||
}
|
||||
|
||||
public T parse(StringReader reader) throws CommandSyntaxException {
|
||||
ErrorCollector.LongestOnly<StringReader> longestOnly = new ErrorCollector.LongestOnly<>();
|
||||
StringReaderParserState stringReaderParserState = new StringReaderParserState(longestOnly, reader);
|
||||
Optional<T> optional = this.parse(stringReaderParserState);
|
||||
if (optional.isPresent()) {
|
||||
T result = optional.get();
|
||||
ErrorCollector.LongestOnly<StringReader> errorCollector = new ErrorCollector.LongestOnly<>();
|
||||
StringReaderParserState stringReaderParserState = new StringReaderParserState(errorCollector, reader);
|
||||
Optional<T> optionalResult = this.parse(stringReaderParserState);
|
||||
if (optionalResult.isPresent()) {
|
||||
T result = optionalResult.get();
|
||||
if (CachedParseState.JAVA_NULL_VALUE_MARKER.equals(result)) {
|
||||
result = null;
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
List<ErrorEntry<StringReader>> list = longestOnly.entries();
|
||||
List<Exception> list1 = list.stream().<Exception>mapMulti((errorEntry, consumer) -> {
|
||||
if (errorEntry.reason() instanceof DelayedException<?> delayedException) {
|
||||
consumer.accept(delayedException.create(reader.getString(), errorEntry.cursor()));
|
||||
} else if (errorEntry.reason() instanceof Exception exception1) {
|
||||
consumer.accept(exception1);
|
||||
}
|
||||
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 exception1) {
|
||||
output.accept(exception1);
|
||||
}
|
||||
}).toList();
|
||||
|
||||
for (Exception exception : list1) {
|
||||
if (exception instanceof CommandSyntaxException commandSyntaxException) {
|
||||
throw commandSyntaxException;
|
||||
for (Exception exception : exceptions) {
|
||||
if (exception instanceof CommandSyntaxException cse) {
|
||||
throw cse;
|
||||
}
|
||||
}
|
||||
|
||||
if (list1.size() == 1 && list1.getFirst() instanceof RuntimeException runtimeException) {
|
||||
throw runtimeException;
|
||||
} else {
|
||||
throw new IllegalStateException("Failed to parse: " + list.stream().map(ErrorEntry::toString).collect(Collectors.joining(", ")));
|
||||
}
|
||||
if (exceptions.size() == 1 && exceptions.getFirst() instanceof RuntimeException re) {
|
||||
throw re;
|
||||
}
|
||||
throw new IllegalStateException("Failed to parse: " + errorEntries.stream().map(ErrorEntry::toString).collect(Collectors.joining(", ")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,16 +19,15 @@ public final class GreedyPatternParseRule implements Rule<StringReader, String>
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(ParseState<StringReader> parseState) {
|
||||
StringReader stringReader = parseState.input();
|
||||
String string = stringReader.getString();
|
||||
Matcher matcher = this.pattern.matcher(string).region(stringReader.getCursor(), string.length());
|
||||
public String parse(ParseState<StringReader> state) {
|
||||
StringReader input = state.input();
|
||||
String fullString = input.getString();
|
||||
Matcher matcher = this.pattern.matcher(fullString).region(input.getCursor(), fullString.length());
|
||||
if (!matcher.lookingAt()) {
|
||||
parseState.errorCollector().store(parseState.mark(), this.error);
|
||||
state.errorCollector().store(state.mark(), this.error);
|
||||
return null;
|
||||
} else {
|
||||
stringReader.setCursor(matcher.end());
|
||||
}
|
||||
input.setCursor(matcher.end());
|
||||
return matcher.group(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,24 +25,23 @@ public abstract class GreedyPredicateParseRule implements Rule<StringReader, Str
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String parse(ParseState<StringReader> parseState) {
|
||||
StringReader stringReader = parseState.input();
|
||||
String string = stringReader.getString();
|
||||
int cursor = stringReader.getCursor();
|
||||
int i = cursor;
|
||||
public String parse(ParseState<StringReader> state) {
|
||||
StringReader input = state.input();
|
||||
String fullString = input.getString();
|
||||
int start = input.getCursor();
|
||||
int pos = start;
|
||||
|
||||
while (i < string.length() && this.isAccepted(string.charAt(i)) && i - cursor < this.maxSize) {
|
||||
i++;
|
||||
while (pos < fullString.length() && this.isAccepted(fullString.charAt(pos)) && pos - start < this.maxSize) {
|
||||
pos++;
|
||||
}
|
||||
|
||||
int i1 = i - cursor;
|
||||
if (i1 < this.minSize) {
|
||||
parseState.errorCollector().store(parseState.mark(), this.error);
|
||||
int length = pos - start;
|
||||
if (length < this.minSize) {
|
||||
state.errorCollector().store(state.mark(), this.error);
|
||||
return null;
|
||||
} else {
|
||||
stringReader.setCursor(i);
|
||||
return string.substring(cursor, i);
|
||||
}
|
||||
input.setCursor(pos);
|
||||
return fullString.substring(start, pos);
|
||||
}
|
||||
|
||||
protected abstract boolean isAccepted(char c);
|
||||
|
||||
@@ -19,28 +19,27 @@ public abstract class NumberRunParseRule implements Rule<StringReader, String> {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String parse(ParseState<StringReader> parseState) {
|
||||
StringReader stringReader = parseState.input();
|
||||
stringReader.skipWhitespace();
|
||||
String string = stringReader.getString();
|
||||
int cursor = stringReader.getCursor();
|
||||
int i = cursor;
|
||||
public String parse(ParseState<StringReader> state) {
|
||||
StringReader input = state.input();
|
||||
input.skipWhitespace();
|
||||
String fullString = input.getString();
|
||||
int start = input.getCursor();
|
||||
int pos = start;
|
||||
|
||||
while (i < string.length() && this.isAccepted(string.charAt(i))) {
|
||||
i++;
|
||||
while (pos < fullString.length() && this.isAccepted(fullString.charAt(pos))) {
|
||||
pos++;
|
||||
}
|
||||
|
||||
int i1 = i - cursor;
|
||||
if (i1 == 0) {
|
||||
parseState.errorCollector().store(parseState.mark(), this.noValueError);
|
||||
return null;
|
||||
} else if (string.charAt(cursor) != '_' && string.charAt(i - 1) != '_') {
|
||||
stringReader.setCursor(i);
|
||||
return string.substring(cursor, i);
|
||||
} else {
|
||||
parseState.errorCollector().store(parseState.mark(), this.underscoreNotAllowedError);
|
||||
int length = pos - start;
|
||||
if (length == 0) {
|
||||
state.errorCollector().store(state.mark(), this.noValueError);
|
||||
return null;
|
||||
} else if (fullString.charAt(start) != '_' && fullString.charAt(pos - 1) != '_') {
|
||||
input.setCursor(pos);
|
||||
return fullString.substring(start, pos);
|
||||
}
|
||||
state.errorCollector().store(state.mark(), this.underscoreNotAllowedError);
|
||||
return null;
|
||||
}
|
||||
|
||||
protected abstract boolean isAccepted(char c);
|
||||
|
||||
@@ -23,7 +23,7 @@ public class StringReaderParserState extends CachedParseState<StringReader> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore(int cursor) {
|
||||
this.input.setCursor(cursor);
|
||||
public void restore(int mark) {
|
||||
this.input.setCursor(mark);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,50 +16,49 @@ public interface StringReaderTerms {
|
||||
static Term<StringReader> character(final char value) {
|
||||
return new TerminalCharacters(CharList.of(value)) {
|
||||
@Override
|
||||
protected boolean isAccepted(char c) {
|
||||
return value == c;
|
||||
protected boolean isAccepted(char v) {
|
||||
return value == v;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static Term<StringReader> characters(final char value1, final char value2) {
|
||||
return new TerminalCharacters(CharList.of(value1, value2)) {
|
||||
static Term<StringReader> characters(final char v1, final char v2) {
|
||||
return new TerminalCharacters(CharList.of(v1, v2)) {
|
||||
@Override
|
||||
protected boolean isAccepted(char c) {
|
||||
return c == value1 || c == value2;
|
||||
protected boolean isAccepted(char v) {
|
||||
return v == v1 || v == v2;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static StringReader createReader(String input, int cursor) {
|
||||
StringReader stringReader = new StringReader(input);
|
||||
stringReader.setCursor(cursor);
|
||||
return stringReader;
|
||||
static StringReader createReader(String contents, int cursor) {
|
||||
StringReader reader = new StringReader(contents);
|
||||
reader.setCursor(cursor);
|
||||
return reader;
|
||||
}
|
||||
|
||||
abstract class TerminalCharacters implements Term<StringReader> {
|
||||
private final DelayedException<CommandSyntaxException> error;
|
||||
private final SuggestionSupplier<StringReader> suggestions;
|
||||
|
||||
public TerminalCharacters(CharList characters) {
|
||||
String string = characters.intStream().mapToObj(Character::toString).collect(Collectors.joining("|"));
|
||||
this.error = DelayedException.create(LITERAL_INCORRECT, string);
|
||||
this.suggestions = parseState -> characters.intStream().mapToObj(Character::toString);
|
||||
public TerminalCharacters(CharList values) {
|
||||
String joinedValues = values.intStream().mapToObj(Character::toString).collect(Collectors.joining("|"));
|
||||
this.error = DelayedException.create(LITERAL_INCORRECT, joinedValues);
|
||||
this.suggestions = s -> values.intStream().mapToObj(Character::toString);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean parse(ParseState<StringReader> parseState, Scope scope, Control control) {
|
||||
parseState.input().skipWhitespace();
|
||||
int i = parseState.mark();
|
||||
if (parseState.input().canRead() && this.isAccepted(parseState.input().read())) {
|
||||
public boolean parse(ParseState<StringReader> state, Scope scope, Control control) {
|
||||
state.input().skipWhitespace();
|
||||
int cursor = state.mark();
|
||||
if (state.input().canRead() && this.isAccepted(state.input().read())) {
|
||||
return true;
|
||||
} else {
|
||||
parseState.errorCollector().store(i, this.suggestions, this.error);
|
||||
}
|
||||
state.errorCollector().store(cursor, this.suggestions, this.error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract boolean isAccepted(char c);
|
||||
protected abstract boolean isAccepted(char value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,15 +19,14 @@ public class UnquotedStringParseRule implements Rule<StringReader, String> {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String parse(ParseState<StringReader> parseState) {
|
||||
parseState.input().skipWhitespace();
|
||||
int i = parseState.mark();
|
||||
String unquotedString = parseState.input().readUnquotedString();
|
||||
if (unquotedString.length() < this.minSize) {
|
||||
parseState.errorCollector().store(i, this.error);
|
||||
public String parse(ParseState<StringReader> state) {
|
||||
state.input().skipWhitespace();
|
||||
int cursor = state.mark();
|
||||
String value = state.input().readUnquotedString();
|
||||
if (value.length() < this.minSize) {
|
||||
state.errorCollector().store(cursor, this.error);
|
||||
return null;
|
||||
} else {
|
||||
return unquotedString;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user