Clean up StringParser

This commit is contained in:
Jason Penilla 2021-07-04 20:53:46 -07:00 committed by Jason
parent d6e17a0d99
commit 75e59c9fe6

View file

@ -311,62 +311,72 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
inputQueue.remove(); inputQueue.remove();
return ArgumentParseResult.success(input); return ArgumentParseResult.success(input);
} else if (this.stringMode == StringMode.QUOTED) { } else if (this.stringMode == StringMode.QUOTED) {
final StringJoiner sj = new StringJoiner(" "); return this.parseQuoted(commandContext, inputQueue);
for (final String string : inputQueue) { } else {
sj.add(string); return this.parseGreedy(commandContext, inputQueue);
} }
final String string = sj.toString(); }
final Matcher doubleMatcher = QUOTED_DOUBLE.matcher(string); private @NonNull ArgumentParseResult<String> parseQuoted(
String doubleMatch = null; final @NonNull CommandContext<C> commandContext,
if (doubleMatcher.find()) { final @NonNull Queue<@NonNull String> inputQueue
doubleMatch = doubleMatcher.group("inner"); ) {
} final StringJoiner sj = new StringJoiner(" ");
final Matcher singleMatcher = QUOTED_SINGLE.matcher(string); for (final String string : inputQueue) {
String singleMatch = null; sj.add(string);
if (singleMatcher.find()) { }
singleMatch = singleMatcher.group("inner"); final String string = sj.toString();
}
String inner = null; final Matcher doubleMatcher = QUOTED_DOUBLE.matcher(string);
if (singleMatch != null && doubleMatch != null) { String doubleMatch = null;
final int singleIndex = string.indexOf(singleMatch); if (doubleMatcher.find()) {
final int doubleIndex = string.indexOf(doubleMatch); doubleMatch = doubleMatcher.group("inner");
inner = doubleIndex < singleIndex ? doubleMatch : singleMatch; }
} else if (singleMatch == null && doubleMatch != null) { final Matcher singleMatcher = QUOTED_SINGLE.matcher(string);
inner = doubleMatch; String singleMatch = null;
} else if (singleMatch != null) { if (singleMatcher.find()) {
inner = singleMatch; singleMatch = singleMatcher.group("inner");
}
if (inner != null) {
final int numSpaces = StringUtils.countCharOccurrences(inner, ' ');
for (int i = 0; i <= numSpaces; i++) {
inputQueue.remove();
}
} else {
inner = inputQueue.remove();
if (inner.startsWith("\"") || inner.startsWith("'")) {
return ArgumentParseResult.failure(new StringParseException(sj.toString(),
StringMode.QUOTED, commandContext
));
}
}
inner = inner.replace("\\\"", "\"").replace("\\'", "'");
return ArgumentParseResult.success(inner);
} }
String inner = null;
if (singleMatch != null && doubleMatch != null) {
final int singleIndex = string.indexOf(singleMatch);
final int doubleIndex = string.indexOf(doubleMatch);
inner = doubleIndex < singleIndex ? doubleMatch : singleMatch;
} else if (singleMatch == null && doubleMatch != null) {
inner = doubleMatch;
} else if (singleMatch != null) {
inner = singleMatch;
}
if (inner != null) {
final int numSpaces = StringUtils.countCharOccurrences(inner, ' ');
for (int i = 0; i <= numSpaces; i++) {
inputQueue.remove();
}
} else {
inner = inputQueue.remove();
if (inner.startsWith("\"") || inner.startsWith("'")) {
return ArgumentParseResult.failure(new StringParseException(sj.toString(),
StringMode.QUOTED, commandContext
));
}
}
inner = inner.replace("\\\"", "\"").replace("\\'", "'");
return ArgumentParseResult.success(inner);
}
private @NonNull ArgumentParseResult<String> parseGreedy(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
final StringJoiner sj = new StringJoiner(" "); final StringJoiner sj = new StringJoiner(" ");
final int size = inputQueue.size(); final int size = inputQueue.size();
boolean started = false;
boolean finished = false;
char start = ' ';
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
String string = inputQueue.peek(); final String string = inputQueue.peek();
if (string == null) { if (string == null) {
break; break;