-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ArgumentHelper.java
169 lines (155 loc) · 6.4 KB
/
ArgumentHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.denizenscript.denizencore.objects;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.utilities.AsciiMatcher;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import java.util.*;
public class ArgumentHelper {
// <--[language]
// @name Number and Decimal
// @group Common Terminology
// @description
// Many arguments in Denizen require the use of a 'number', or 'decimal'. Sometimes shorthanded to '#' or '#.#',
// this kind of input can generally be filled with any reasonable positive or negative number.
// 'decimal' inputs allow (but don't require) a decimal point in the number.
// 'number' inputs will be rounded, so avoiding a decimal point is better. For example, '3.1' will be interpreted as just '3'.
// -->
/**
* Turns a list of string arguments (separated by buildArgs) into Argument
* Objects for easy matching and ObjectTag creation throughout Denizen.
*
* @param args a list of string arguments
* @return a list of Arguments
*/
public static List<Argument> interpret(ScriptEntry entry, List<String> args) {
List<Argument> arg_list = new ArrayList<>(args.size());
for (String string : args) {
Argument newArg = new Argument(string);
newArg.scriptEntry = entry;
arg_list.add(newArg);
}
return arg_list;
}
/**
* Builds an arguments array, recognizing items in quotes as a single item, but
* otherwise splitting on a space.
*
* @param stringArgs the line of arguments that need split
* @return an array of arguments
*/
public static String[] buildArgs(String stringArgs) {
if (stringArgs == null) {
return null;
}
stringArgs = stringArgs.trim();
stringArgs = stringArgs.replace('\r', ' ').replace('\n', ' ');
ArrayList<String> matchList = new ArrayList<>(stringArgs.length() / 7);
int start = 0;
int len = stringArgs.length();
char currentQuote = 0;
for (int i = 0; i < len; i++) {
char c = stringArgs.charAt(i);
if (c == ' ' && currentQuote == 0) {
if (i > start) {
matchList.add(stringArgs.substring(start, i));
}
start = i + 1;
}
else if (c == '"' || c == '\'') {
if (currentQuote == 0) {
if (i - 1 < 0 || stringArgs.charAt(i - 1) == ' ') {
currentQuote = c;
start = i + 1;
}
}
else if (currentQuote == c) {
if (i + 1 >= len || stringArgs.charAt(i + 1) == ' ') {
currentQuote = 0;
if (i >= start) {
matchList.add(stringArgs.substring(start, i));
}
i++;
start = i + 1;
}
}
}
}
if (start < len) {
matchList.add(stringArgs.substring(start));
}
if (Debug.showScriptBuilder) {
Debug.log("Constructed args: " + Arrays.toString(matchList.toArray()));
}
return matchList.toArray(new String[0]);
}
public static String debugObj(String prefix, Object value) {
return "<G>" + prefix + "='<Y>" + (value != null ? (value instanceof ObjectTag ? ((ObjectTag) value).debuggable() : value.toString()) : "null") + "<G>' ";
}
public static <T extends ObjectTag> String debugList(String prefix, Collection<T> objects) {
if (objects == null) {
return debugObj(prefix, null);
}
StringBuilder sb = new StringBuilder();
for (ObjectTag obj : objects) {
sb.append(obj == null ? "null" : obj.debuggable()).append("<G>,<Y> ");
}
if (sb.length() == 0) {
return debugObj(prefix, sb);
}
else {
return debugObj(prefix, "[" + sb.substring(0, sb.length() - "<G>, ".length()) + "<Y>]");
}
}
public static String DIGITS = "0123456789", PREFIXES = "+-", DOUBLE_CHARS = "eE";
public static AsciiMatcher DIGIT_MATCHER = new AsciiMatcher(DIGITS);
public static AsciiMatcher INTEGER_MATCHER = new AsciiMatcher(DIGITS + PREFIXES);
public static AsciiMatcher DOUBLE_SPECIAL_MATCHER = new AsciiMatcher(DOUBLE_CHARS);
public static AsciiMatcher PREFIX_MATCHER = new AsciiMatcher(PREFIXES);
public static AsciiMatcher HEX_MATCHER = new AsciiMatcher("abcdefABCDEF0123456789");
public static boolean matchesDouble(String arg) {
if (arg.length() == 0) {
return false;
}
if (!INTEGER_MATCHER.isMatch(arg.charAt(0))) {
return false;
}
if (!DIGIT_MATCHER.containsAnyMatch(arg)) {
return false;
}
boolean hadDoubleSyntax = false;
boolean hadDecimal = false;
for (int i = 1; i < arg.length(); i++) {
if (!DIGIT_MATCHER.isMatch(arg.charAt(i))) {
if (hadDoubleSyntax) {
return false;
}
if (arg.charAt(i) == '.' && !hadDecimal) {
hadDecimal = true;
}
else if (i + 1 < arg.length() && DOUBLE_SPECIAL_MATCHER.isMatch(arg.charAt(i))
&& PREFIX_MATCHER.isMatch(arg.charAt(i + 1))) {
hadDoubleSyntax = true;
i++;
}
else {
return false;
}
}
}
return true;
}
private static final int MIN_LONG_LENGTH = Long.toString(Long.MIN_VALUE).length();
private static final int MAX_LONG_LENGTH = Long.toString(Long.MAX_VALUE).length();
public static boolean matchesInteger(String arg) {
if (!INTEGER_MATCHER.isOnlyMatches(arg)) {
return false;
}
if (!matchesDouble(arg)) {
return false;
}
char firstChar = arg.charAt(0);
if (arg.length() > ((firstChar == '-' || firstChar == '+') ? MIN_LONG_LENGTH : MAX_LONG_LENGTH)) {
return false;
}
return true;
}
}