🚚 Switch namespace
This commit is contained in:
parent
0064093dbf
commit
c74cda3a0f
207 changed files with 2689 additions and 611 deletions
|
|
@ -1,77 +0,0 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package com.intellectualsites.commands.annotations;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Parses command syntax into syntax fragments
|
||||
*/
|
||||
final class SyntaxParser implements Function<String, LinkedHashMap<String, SyntaxFragment>> {
|
||||
|
||||
private static final Predicate<String> PATTERN_ARGUMENT_LITERAL = Pattern.compile("([A-Za-z0-9]+)(|([A-Za-z0-9]+))*")
|
||||
.asPredicate();
|
||||
private static final Predicate<String> PATTERN_ARGUMENT_REQUIRED = Pattern.compile("<([A-Za-z0-9]+)>")
|
||||
.asPredicate();
|
||||
private static final Predicate<String> PATTERN_ARGUMENT_OPTIONAL = Pattern.compile("\\[([A-Za-z0-9]+)]")
|
||||
.asPredicate();
|
||||
|
||||
@Override
|
||||
public LinkedHashMap<String, SyntaxFragment> apply(@Nonnull final String syntax) {
|
||||
final StringTokenizer stringTokenizer = new StringTokenizer(syntax, " ");
|
||||
final LinkedHashMap<String, SyntaxFragment> map = new LinkedHashMap<>();
|
||||
while (stringTokenizer.hasMoreTokens()) {
|
||||
final String token = stringTokenizer.nextToken();
|
||||
String major;
|
||||
List<String> minor = new ArrayList<>();
|
||||
ArgumentMode mode;
|
||||
if (PATTERN_ARGUMENT_REQUIRED.test(token)) {
|
||||
major = token.substring(1, token.length() - 1);
|
||||
mode = ArgumentMode.REQUIRED;
|
||||
} else if (PATTERN_ARGUMENT_OPTIONAL.test(token)) {
|
||||
major = token.substring(1, token.length() - 1);
|
||||
mode = ArgumentMode.OPTIONAL;
|
||||
} else if (PATTERN_ARGUMENT_LITERAL.test(token)) {
|
||||
final String[] literals = token.split("\\|");
|
||||
/* Actually use the other literals as well */
|
||||
major = literals[0];
|
||||
minor.addAll(Arrays.asList(literals).subList(1, literals.length));
|
||||
mode = ArgumentMode.LITERAL;
|
||||
} else {
|
||||
throw new IllegalArgumentException(String.format("Unrecognizable syntax token '%s'", syntax));
|
||||
}
|
||||
map.put(major, new SyntaxFragment(major, minor, mode));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue