|
| 1 | +/* |
| 2 | + * PermissionsEx |
| 3 | + * Copyright (C) zml and PermissionsEx contributors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package ca.stellardrift.permissionsex.minecraft.command.argument; |
| 18 | + |
| 19 | +import ca.stellardrift.permissionsex.minecraft.command.CommandException; |
| 20 | +import cloud.commandframework.arguments.parser.ArgumentParseResult; |
| 21 | +import cloud.commandframework.arguments.parser.ArgumentParser; |
| 22 | +import cloud.commandframework.context.CommandContext; |
| 23 | +import cloud.commandframework.exceptions.parsing.NoInputProvidedException; |
| 24 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 25 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 26 | + |
| 27 | +import java.util.Queue; |
| 28 | + |
| 29 | +public final class OptionValueParser<C> implements ArgumentParser<C, String> { |
| 30 | + private static final char ESCAPE = '\\'; |
| 31 | + private static final String FLAG_STARTER = "-"; |
| 32 | + |
| 33 | + OptionValueParser() { |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public @NonNull ArgumentParseResult<@NonNull String> parse( |
| 38 | + @NonNull final CommandContext<@NonNull C> commandContext, |
| 39 | + @NonNull final Queue<@NonNull String> inputQueue |
| 40 | + ) { |
| 41 | + final @Nullable String input = inputQueue.peek(); |
| 42 | + if (input == null || input.startsWith(FLAG_STARTER)) { |
| 43 | + return ArgumentParseResult.failure(new NoInputProvidedException( |
| 44 | + PermissionValueParser.class, |
| 45 | + commandContext |
| 46 | + )); |
| 47 | + } |
| 48 | + |
| 49 | + final char startChar = input.charAt(0); |
| 50 | + |
| 51 | + // If quoted string: read until a word ending with a quote |
| 52 | + if (startChar == '\'' || startChar == '\"') { |
| 53 | + final StringBuilder result = new StringBuilder(); |
| 54 | + result.append(inputQueue.remove(), 1, input.length()); |
| 55 | + @Nullable String next; |
| 56 | + while ((next = inputQueue.peek()) != null) { |
| 57 | + result.append(" "); |
| 58 | + if (next.length() > 0 && next.charAt(next.length() - 1) == startChar) { |
| 59 | + // We've found the end of a quoted string, maybe |
| 60 | + // if escaped, append without escape then continue |
| 61 | + if (next.charAt(next.length() - 1) == ESCAPE) { |
| 62 | + result.append(inputQueue.remove(), 0, next.length() - 2) |
| 63 | + .append(startChar); |
| 64 | + continue; |
| 65 | + } else { |
| 66 | + result.append(inputQueue.remove(), 0, next.length() - 1); |
| 67 | + // then return our full quoted string |
| 68 | + return ArgumentParseResult.success(result.toString()); |
| 69 | + } |
| 70 | + } |
| 71 | + result.append(inputQueue.remove()); |
| 72 | + } |
| 73 | + |
| 74 | + // If we made it to the end without finding an end quote, throw an error |
| 75 | + return ArgumentParseResult.failure(new CommandException(Messages.OPTION_VALUE_ERROR_QUOTE_UNTERMINATED.tr())); |
| 76 | + } else { // otherwise, read until end of line, or a word starting with '-' |
| 77 | + final StringBuilder result = new StringBuilder(); |
| 78 | + result.append(inputQueue.remove()); |
| 79 | + @Nullable String next; |
| 80 | + while ((next = inputQueue.peek()) != null) { |
| 81 | + if (next.startsWith(FLAG_STARTER)) { |
| 82 | + break; |
| 83 | + } |
| 84 | + result.append(" ").append(inputQueue.remove()); |
| 85 | + } |
| 86 | + |
| 87 | + return ArgumentParseResult.success(result.toString()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean isContextFree() { |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments