Skip to content

Commit

Permalink
Fixed failed tests by removing TokenOutOfRangeException.java
Browse files Browse the repository at this point in the history
Also removed the useless TokenParseException.java
  • Loading branch information
Mqzn committed Oct 16, 2024
1 parent 6b0a0e7 commit 667d6ba
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,7 @@ public void registerGlobalPostProcessor(CommandPostProcessor<S> postProcessor) {
source.error("Unknown command input: '" + commandName + "'");
return CommandDispatch.Result.UNKNOWN;
}
//temp debugging
//command.visualize();
command.visualizeTree();
return dispatch(source, command, rawInput);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public final class ParameterBoolean<S extends Source> extends BaseParameterType<
@Override
public @Nullable Boolean resolve(ExecutionContext<S> context, @NotNull CommandInputStream<S> commandInputStream) throws ImperatException {

var raw = commandInputStream.currentRaw();
var raw = commandInputStream.currentRaw().orElse(null);
assert raw != null;

if (raw.equalsIgnoreCase("true") || raw.equalsIgnoreCase("false")) {
return Boolean.parseBoolean(raw);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ public final class ParameterCommand<S extends Source> extends BaseParameterType<

@Override
public @Nullable Command<S> resolve(ExecutionContext<S> context, @NotNull CommandInputStream<S> commandInputStream) throws ImperatException {
var currentParameter = commandInputStream.currentParameter();
if (currentParameter == null)
return null;
return currentParameter.asCommand();
return commandInputStream.currentParameter()
.map(CommandParameter::asCommand).orElse(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;
import java.util.Objects;

@SuppressWarnings({"unchecked", "rawtypes"})
public final class ParameterEnum<S extends Source> extends BaseParameterType<S, Enum<?>> {
Expand All @@ -27,14 +26,15 @@ public ParameterEnum(TypeWrap<Enum<?>> typeWrap) {
@Override
public @NotNull Enum<?> resolve(ExecutionContext<S> context, @NotNull CommandInputStream<S> commandInputStream) throws ImperatException {

Type enumType = TypeUtility.matches(typeWrap.getType(), Enum.class)
? Objects.requireNonNull(commandInputStream.currentParameter()).valueType() : typeWrap.getType();
Type enumType = commandInputStream.currentParameter()
.filter(param -> TypeUtility.matches(typeWrap.getType(), Enum.class))
.map(CommandParameter::valueType)
.orElse(typeWrap.getType());

var raw = commandInputStream.currentRaw();
try {
assert raw != null;

return Enum.valueOf((Class<? extends Enum>) enumType, raw.toUpperCase());
assert raw.isPresent();
return Enum.valueOf((Class<? extends Enum>) enumType, raw.get());
} catch (EnumConstantNotPresentException ex) {
throw new SourceException("Invalid " + enumType.getTypeName() + " '" + raw + "'");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ protected ParameterFlag() {

@Override
public @Nullable CommandFlag resolve(ExecutionContext<S> context, @NotNull CommandInputStream<S> commandInputStream) throws ImperatException {
CommandParameter<S> currentParameter = commandInputStream.currentParameter();
assert currentParameter != null;
var currentParameter = commandInputStream.currentParameter()
.orElse(null);
if (currentParameter == null)
return null;

if (!currentParameter.isFlag()) {
throw new IllegalArgumentException();
}

FlagParameter<S> flagParameter = currentParameter.asFlagParameter();

String rawFlag = commandInputStream.currentRaw();
String rawFlag = commandInputStream.currentRaw().orElse(null);
if (rawFlag == null) {
throw new IllegalArgumentException();
}

String rawInput = null;
Object input = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static <S extends Source, N extends Number> ParameterNumber<S, N> from(Class<N>
@Override
public @Nullable N resolve(ExecutionContext<S> context, @NotNull CommandInputStream<S> commandInputStream) throws ImperatException {

String input = commandInputStream.currentRaw();
String input = commandInputStream.currentRaw().orElse(null);
try {
return parse(input);
} catch (NumberFormatException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.velix.imperat.exception.ImperatException;
import dev.velix.imperat.util.TypeWrap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class ParameterString<S extends Source> extends BaseParameterType<S, String> {

Expand All @@ -18,17 +17,18 @@ public final class ParameterString<S extends Source> extends BaseParameterType<S
}

@Override
public @Nullable String resolve(ExecutionContext<S> context, @NotNull CommandInputStream<S> inputStream) throws ImperatException {
public @NotNull String resolve(ExecutionContext<S> context, @NotNull CommandInputStream<S> inputStream) throws ImperatException {
StringBuilder builder = new StringBuilder();
final CommandParameter<S> parameter = inputStream.currentParameter();
assert parameter != null;
final CommandParameter<S> parameter = inputStream.currentParameter().orElse(null);
if (parameter == null) return builder.toString();

final Character current = inputStream.currentLetter();
if (current == null) return null;
final Character current = inputStream.currentLetter().orElse(null);
if (current == null)
return builder.toString();

if (!isQuoteChar(current)) {

builder.append(inputStream.currentRaw());
builder.append(inputStream.currentRaw().orElse(""));
if (parameter.isGreedy()) {
handleGreedy(builder, inputStream);
}
Expand Down Expand Up @@ -58,11 +58,6 @@ private boolean isQuoteChar(char ch) {
return ch == DOUBLE_QUOTE || ch == SINGLE_QUOTE;
}

@Override
public boolean matchesInput(String input, CommandParameter<S> parameter) {
return true;
}

private void handleGreedy(StringBuilder builder, CommandInputStream<S> inputStream) {
builder.append(" ");
while (inputStream.hasNextRaw()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public ParameterUUID() {
ExecutionContext<S> context,
@NotNull CommandInputStream<S> commandInputStream
) throws ImperatException {
String raw = commandInputStream.currentRaw();
String raw = commandInputStream.currentRaw().orElse(null);
if (raw == null) {
return null;
}

try {
return UUID.fromString(raw);
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

public final class ParameterWord<S extends Source> extends BaseParameterType<S, String> {

Expand All @@ -28,10 +27,9 @@ public final class ParameterWord<S extends Source> extends BaseParameterType<S,
public @Nullable String resolve(ExecutionContext<S> context, @NotNull CommandInputStream<S> commandInputStream) throws ImperatException {
var nextRaw = commandInputStream.currentRaw();
if (restrictions.isEmpty()) {
return nextRaw;
return nextRaw.orElse(null);
}
assert nextRaw != null;
return Optional.of(nextRaw).filter(restrictions::contains)
return nextRaw.filter(restrictions::contains)
.orElseThrow(() -> new SourceException("Word '%s' is not within the given restrictions=%s", nextRaw, restrictions.toString()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@
import dev.velix.imperat.context.ArgumentQueue;
import dev.velix.imperat.context.Source;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public interface CommandInputStream<S extends Source> {

@NotNull Cursor<S> cursor();

@Nullable CommandParameter<S> currentParameter();
@NotNull Optional<CommandParameter<S>> currentParameter();

Optional<CommandParameter<S>> peekParameter();

Optional<CommandParameter<S>> popParameter();

@NotNull Character currentLetter();
@NotNull Optional<Character> currentLetter();

Optional<Character> peekLetter();

Optional<Character> popLetter();

@NotNull String currentRaw();
@NotNull Optional<String> currentRaw();

Optional<String> peekRaw();

Expand Down Expand Up @@ -83,8 +82,10 @@ default int parametersLength() {

default boolean skipTill(char target) {
boolean reached = false;

while (hasNextLetter()) {
if (currentLetter() == target) {
if (currentLetter().map((current) -> current == target)
.orElse(false)) {
reached = true;
break;
}
Expand All @@ -93,13 +94,14 @@ default boolean skipTill(char target) {
//skipping current letter (which equals the target)
skipLetter();
return reached;

}

default String collectBeforeFirst(char c) {
StringBuilder builder = new StringBuilder();
while (hasNextLetter()) {
var current = currentLetter();
if (current == c) {
var current = currentLetter().orElse(null);
if (current == null || current == c) {
break;
}
builder.append(current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.context.ArgumentQueue;
import dev.velix.imperat.context.Source;
import dev.velix.imperat.exception.TokenOutOfRangeException;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.Optional;

final class CommandInputStreamImpl<S extends Source> implements CommandInputStream<S> {
Expand All @@ -33,9 +31,8 @@ final class CommandInputStreamImpl<S extends Source> implements CommandInputStre
}

@Override
public @NotNull CommandParameter<S> currentParameter() {
return Optional.ofNullable(usage.getParameter(cursor.parameter))
.orElseThrow(TokenOutOfRangeException::new);
public @NotNull Optional<CommandParameter<S>> currentParameter() {
return Optional.ofNullable(usage.getParameter(cursor.parameter));
}

@Override
Expand All @@ -48,54 +45,55 @@ public Optional<CommandParameter<S>> peekParameter() {
@Override
public Optional<CommandParameter<S>> popParameter() {
cursor.shift(ShiftTarget.PARAMETER_ONLY, ShiftOperation.RIGHT);
return Optional.of(currentParameter());
return currentParameter();
}

@Override
public @NotNull String currentRaw() {
public @NotNull Optional<String> currentRaw() {
if (cursor.raw >= queue.size())
throw new TokenOutOfRangeException();
return queue.get(cursor.raw);
return Optional.empty();
return Optional.of(queue.get(cursor.raw));
}

@Override
public @NotNull Character currentLetter() {
public @NotNull Optional<Character> currentLetter() {
if (cursor.raw >= queue.size()) {
throw new TokenOutOfRangeException();
return Optional.empty();
}
@NotNull String raw = Objects.requireNonNull(currentRaw());
return raw.charAt(letterPos);
return currentRaw().map((raw) -> raw.charAt(letterPos));
}

@Override
public Optional<Character> peekLetter() {
int nextLetterPos = letterPos + 1;
String currentRaw = Objects.requireNonNull(currentRaw());
if (nextLetterPos == currentRaw.length()) {
return Optional.of(WHITE_SPACE);
} else if (nextLetterPos > currentRaw.length()) {
//next raw
return peekRaw().map((nextRaw) -> nextRaw.charAt(0));
} else {
//nextLetterPos < currentRaw.length()
return Optional.of(currentRaw.charAt(nextLetterPos));
}
return currentRaw().flatMap((currentRaw) -> {
if (nextLetterPos == currentRaw.length()) {
return Optional.of(WHITE_SPACE);
} else if (nextLetterPos > currentRaw.length()) {
//next raw
return peekRaw().map((nextRaw) -> nextRaw.charAt(0));
} else {
//nextLetterPos < currentRaw.length()
return Optional.of(currentRaw.charAt(nextLetterPos));
}
});
}

@Override
public Optional<Character> popLetter() {
letterPos++;
String currentRaw = Objects.requireNonNull(currentRaw());
if (letterPos == currentRaw.length()) {
return Optional.of(WHITE_SPACE);
} else if (letterPos > currentRaw.length()) {
//next raw
letterPos = 0;
return popRaw().map((nextRaw) -> nextRaw.charAt(0));
} else {
//nextLetterPos < currentRaw.length()
return Optional.of(currentRaw.charAt(letterPos));
}
return currentRaw().flatMap((currentRaw) -> {
if (letterPos == currentRaw.length()) {
return Optional.of(WHITE_SPACE);
} else if (letterPos > currentRaw.length()) {
//next raw
letterPos = 0;
return popRaw().map((nextRaw) -> nextRaw.charAt(0));
} else {
//nextLetterPos < currentRaw.length()
return Optional.of(currentRaw.charAt(letterPos));
}
});
}

@Override
Expand All @@ -109,12 +107,12 @@ public Optional<String> peekRaw() {
@Override
public Optional<String> popRaw() {
cursor.shift(ShiftTarget.RAW_ONLY, ShiftOperation.RIGHT);
return Optional.of(currentRaw());
return currentRaw();
}

@Override
public boolean hasNextLetter() {
return Optional.of(currentRaw())
return currentRaw()
.map((raw) -> {
if (letterPos >= raw.length()) {
return peekRaw().isPresent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public void resolve() throws ImperatException {
final int lengthWithoutFlags = usage.getParametersWithoutFlags().size();
while (stream.hasNextParameter()) {

CommandParameter<S> currentParameter = stream.currentParameter();
assert currentParameter != null;
CommandParameter<S> currentParameter = stream.currentParameter().orElse(null);
if (currentParameter == null) break;

String currentRaw = stream.currentRaw();
String currentRaw = stream.currentRaw().orElse(null);
if (currentRaw == null) {
if (currentParameter.isOptional()) {
handleEmptyOptional(currentParameter);
Expand Down

This file was deleted.

Loading

0 comments on commit 667d6ba

Please sign in to comment.