Skip to content

Commit

Permalink
Rearrange parser packages
Browse files Browse the repository at this point in the history
  • Loading branch information
nutyworks committed Mar 30, 2024
1 parent ee1cf0c commit 1c841cb
Show file tree
Hide file tree
Showing 26 changed files with 479 additions and 96 deletions.
12 changes: 12 additions & 0 deletions src/client/java/works/nuty/calcite/parser/BlockPosParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package works.nuty.calcite.parser;

import net.minecraft.predicate.NumberRange;
import works.nuty.calcite.parser.array.IntArrayParser;
import works.nuty.calcite.parser.common.DefaultParser;
import works.nuty.calcite.parser.primitive.IntParser;

public class BlockPosParser extends IntArrayParser {
public BlockPosParser(DefaultParser parent) {
super(parent, new IntParser(parent), NumberRange.IntRange.exactly(3));
}
}
260 changes: 226 additions & 34 deletions src/client/java/works/nuty/calcite/parser/EntityParser.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import works.nuty.calcite.parser.common.DefaultParser;

import java.util.concurrent.CompletableFuture;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.predicate.NumberRange;
import works.nuty.calcite.parser.DefaultParser;
import works.nuty.calcite.parser.common.DefaultParser;

import java.util.concurrent.CompletableFuture;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package works.nuty.calcite.parser.array;

import net.minecraft.predicate.NumberRange;
import works.nuty.calcite.parser.DefaultParser;
import works.nuty.calcite.parser.common.DefaultParser;

public class ByteArrayParser extends ArrayParser {
public ByteArrayParser(DefaultParser parent, DefaultParser elementParser, NumberRange.IntRange sizeRange) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package works.nuty.calcite.parser.array;

import net.minecraft.predicate.NumberRange;
import works.nuty.calcite.parser.DefaultParser;
import works.nuty.calcite.parser.common.DefaultParser;

public class IntArrayParser extends ArrayParser {
public IntArrayParser(DefaultParser parent, DefaultParser elementParser, NumberRange.IntRange sizeRange) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package works.nuty.calcite.parser.array;

import net.minecraft.predicate.NumberRange;
import works.nuty.calcite.parser.DefaultParser;
import works.nuty.calcite.parser.common.DefaultParser;

public class LongArrayParser extends ArrayParser {
public LongArrayParser(DefaultParser parent, DefaultParser elementParser, NumberRange.IntRange sizeRange) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package works.nuty.calcite.parser;
package works.nuty.calcite.parser.common;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package works.nuty.calcite.parser;
package works.nuty.calcite.parser.common;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.text.Text;
import works.nuty.calcite.parser.BlockPosParser;
import works.nuty.calcite.parser.primitive.*;

import java.util.*;
import java.util.concurrent.CompletableFuture;
Expand All @@ -15,6 +17,10 @@ public class CompoundParser<T> extends DefaultParser {
public static final SimpleCommandExceptionType EXPECTED_VALUE = new SimpleCommandExceptionType(Text.translatable("argument.nbt.expected.value"));
private final Set<String> keys = new HashSet<>();
private final Map<String, List<Option>> options = new HashMap<>();
private char compoundOpen = '{';
private char compoundClose = '}';
private char keyValueSeparator = ':';
private char elementSeparator = ',';

public CompoundParser(DefaultParser parent) {
super(parent);
Expand All @@ -32,10 +38,10 @@ public void registerOptions() {
@Override
public void parse() throws CommandSyntaxException {
suggest(this::suggestOpenCompound);
reader().expect('{');
reader().expect(compoundOpen);
suggest(this::suggestOptionKey);
reader().skipWhitespace();
while (reader().canRead() && reader().peek() != '}') {
while (reader().canRead() && reader().peek() != compoundClose) {
int cursor = reader().getCursor();
String key = reader().readString();
ValueHandler handler = getValueHandler(key, reader().getCursor());
Expand All @@ -48,9 +54,9 @@ public void parse() throws CommandSyntaxException {
reader().skipWhitespace();
cursor = reader().getCursor();

if (!reader().canRead() || reader().peek() != ':') {
if (!reader().canRead() || reader().peek() != keyValueSeparator) {
reader().setCursor(cursor);
suggest(this::suggestColon);
suggest(this::suggestKeyValueSeparator);
throw EXPECTED_VALUE.createWithContext(reader());
}
reader().skip();
Expand All @@ -63,28 +69,28 @@ public void parse() throws CommandSyntaxException {

suggest(this::suggestOptionsNextOrClose);
if (!reader().canRead()) continue;
if (reader().peek() == ',') {
if (reader().peek() == elementSeparator) {
reader().skip();
reader().skipWhitespace();
suggest(this::suggestOptionKey);
continue;
}
if (reader().peek() == '}') break;
if (reader().peek() == compoundClose) break;
reader().setCursor(cursor);
throw EXPECTED_KEY.createWithContext(reader());
}
reader().expect('}');
reader().expect(compoundClose);
suggestNothing();
}

private CompletableFuture<Suggestions> suggestOpenCompound(SuggestionsBuilder builder) {
builder.suggest("{");
builder.suggest(Character.toString(compoundOpen));
return builder.buildFuture();
}

private CompletableFuture<Suggestions> suggestOptionsNextOrClose(SuggestionsBuilder builder) {
builder.suggest(",");
builder.suggest("}");
builder.suggest(Character.toString(elementSeparator));
builder.suggest(Character.toString(compoundClose));
return builder.buildFuture();
}

Expand All @@ -94,7 +100,7 @@ private CompletableFuture<Suggestions> suggestOptionKey(SuggestionsBuilder build
for (var option : optionList.getValue()) {
if (this.hasKey(optionList.getKey()) || !option.shouldSuggest() || !optionList.getKey().toLowerCase(Locale.ROOT).startsWith(key))
continue;
builder.suggest(optionList.getKey() + ":");
builder.suggest(optionList.getKey() + keyValueSeparator);
}
}
return builder.buildFuture();
Expand All @@ -115,8 +121,8 @@ private boolean hasPotentialOptionKey(String key) {
return value;
}

private CompletableFuture<Suggestions> suggestColon(SuggestionsBuilder builder) {
builder.suggest(":");
private CompletableFuture<Suggestions> suggestKeyValueSeparator(SuggestionsBuilder builder) {
builder.suggest(Character.toString(keyValueSeparator));
return builder.buildFuture();
}

Expand Down Expand Up @@ -159,12 +165,43 @@ public ValueHandler getValueHandler(String key, int cursor) {
return parser -> new AnyParser(parser).parse();
}

public void setSeparators(char compoundOpen, char compoundClose, char keyValueSeparator, char elementSeparator) {
this.compoundOpen = compoundOpen;
this.compoundClose = compoundClose;
this.keyValueSeparator = keyValueSeparator;
this.elementSeparator = elementSeparator;
}

public interface ValueHandler {
ValueHandler BOOLEAN = parser -> new BooleanParser(parser).parse();
ValueHandler BYTE = parser -> new ByteParser(parser).parse();
ValueHandler SHORT = parser -> new ShortParser(parser).parse();
ValueHandler INT = parser -> new IntParser(parser).parse();
ValueHandler LONG = parser -> new LongParser(parser).parse();
ValueHandler FLOAT = parser -> new FloatParser(parser).parse();
ValueHandler DOUBLE = parser -> new DoubleParser(parser).parse();
ValueHandler STRING = parser -> new StringParser(parser).parse();
ValueHandler UUID = parser -> new UUIDParser(parser).parse();
ValueHandler UUID_OR_PLAYER_NAME = parser -> new UUIDOrPlayerNameParser(parser).parse();
ValueHandler COMPOUND = parser -> new CompoundParser<>(parser).parse();
ValueHandler BLOCK_POS = parser -> new BlockPosParser(parser).parse();

void handle(DefaultParser parser) throws CommandSyntaxException;
}

public interface Option {
boolean shouldSuggest();
ValueHandler getValueHandler();
public static class Option {
final ValueHandler valueHandler;

public Option(ValueHandler valueHandler) {
this.valueHandler = valueHandler;
}

public boolean shouldSuggest() {
return true;
}

public ValueHandler getValueHandler() {
return this.valueHandler;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package works.nuty.calcite.parser;
package works.nuty.calcite.parser.common;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.suggestion.Suggestions;
Expand Down
45 changes: 45 additions & 0 deletions src/client/java/works/nuty/calcite/parser/common/EnumParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package works.nuty.calcite.parser.common;

import com.mojang.brigadier.exceptions.CommandSyntaxException;

import java.util.Set;

public class EnumParser extends DefaultParser {

private final Set<String> values;

public EnumParser(DefaultParser parent, Set<String> values) {
super(parent);
this.values = values;
}

@Override
public void parse() throws CommandSyntaxException {
int cursor = reader().getCursor();
boolean quoted = reader().canRead() && (reader().peek() == '\'' || reader().peek() == '"');
boolean closed = false;

try {
reader().readString();
closed = true;
} catch (CommandSyntaxException ignored) {
}

final String id = reader().getString().substring(cursor + (quoted ? 1 : 0), reader().getCursor() - (quoted && closed ? 1 : 0));

suggest(builder -> {
values.stream()
.filter(registryId -> registryId.startsWith(id))
.forEach(i -> builder.suggest("\"" + i + "\""));

return builder.buildFuture();
});

if (values.stream().anyMatch(registryId -> registryId.equals(id))) {
return;
}

reader().setCursor(cursor);
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedStartOfQuote().createWithContext(reader());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package works.nuty.calcite.parser;
package works.nuty.calcite.parser.common;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.predicate.NumberRange;
import works.nuty.calcite.CalciteMod;

import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -40,15 +39,12 @@ private static CompletableFuture<Suggestions> suggestListClose(SuggestionsBuilde
}

public void parse() throws CommandSyntaxException {
CalciteMod.LOGGER.info("Start of list parser: " + this);
suggest(ListParser::suggestListOpen);
reader().expect('[');
CalciteMod.LOGGER.info(" got [");
reader().skipWhitespace();
int i = 0;
do {
++i;
CalciteMod.LOGGER.info(" element " + i);
suggestNothing();
elementParser.parse();
this.reader().skipWhitespace();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package works.nuty.calcite.parser;
package works.nuty.calcite.parser.common;

import com.mojang.brigadier.exceptions.CommandSyntaxException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package works.nuty.calcite.parser;
package works.nuty.calcite.parser.common;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.entity.EntityType;
import net.minecraft.registry.Registries;
import works.nuty.calcite.CalciteModClient;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;
import works.nuty.calcite.parser.common.DefaultParser;

public class EntityTypeParser extends DefaultParser {
import java.util.Objects;
import java.util.function.Predicate;

public EntityTypeParser(DefaultParser parent) {
public class RegistryParser<T> extends DefaultParser {

private final Registry<T> registry;
private Predicate<T> filter = ignored -> true;

public RegistryParser(DefaultParser parent, Registry<T> registry, Predicate<T> filter) {
this(parent, registry);
this.filter = filter;
}

public RegistryParser(DefaultParser parent, Registry<T> registry) {
super(parent);
this.registry = registry;
}

@Override
Expand All @@ -27,27 +39,26 @@ public void parse() throws CommandSyntaxException {
final String prefixedId = (id.contains(":") || (quoted && "minecraft:".startsWith(id))) ? id : "minecraft:" + id;

suggest(builder -> {
Registries.ENTITY_TYPE.stream()
.filter(EntityType::isSummonable)
.map(entityType -> Registries.ENTITY_TYPE.getId(entityType).toString())
registry.stream()
.filter(filter)
.map(registry::getId)
.filter(Objects::nonNull)
.map(Identifier::toString)
.filter(registryId -> registryId.startsWith(prefixedId))
.forEach(i -> builder.suggest("\"" + i + "\""));

return builder.buildFuture();
});

if (Registries.ENTITY_TYPE.stream()
.filter(EntityType::isSummonable)
.map(entityType -> Registries.ENTITY_TYPE.getId(entityType).toString())
if (registry.stream()
.filter(filter)
.map(registry::getId)
.filter(Objects::nonNull)
.map(Identifier::toString)
.anyMatch(registryId -> registryId.equals(prefixedId))) {
if (parent() instanceof EntityParser entityParser) {
entityParser.setEntityType(prefixedId);
}
return;
}

CalciteModClient.LOGGER.info(prefixedId);

reader().setCursor(cursor);
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedStartOfQuote().createWithContext(reader());
}
Expand Down
Loading

0 comments on commit 1c841cb

Please sign in to comment.