forked from IntellectualSites/FastAsyncWorldEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RichParser.java
194 lines (178 loc) · 7.21 KB
/
RichParser.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.fastasyncworldedit.core.extension.factory.parser;
import com.fastasyncworldedit.core.configuration.Caption;
import com.google.common.base.Preconditions;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
/**
* A rich parser allows parsing of patterns and masks with extra arguments,
* e.g. #simplex[scale][pattern].
*
* @param <E> the parse result.
*/
public abstract class RichParser<E> extends InputParser<E> implements AliasedParser {
private final String[] prefixes;
/**
* Create a new rich parser with a defined prefix for the result, e.g. {@code #simplex}.
*
* @param worldEdit the worldedit instance.
* @param aliases the prefix of this parser result.
*/
protected RichParser(WorldEdit worldEdit, String... aliases) {
super(worldEdit);
Preconditions.checkArgument(aliases.length >= 1, "Aliases may not be empty");
this.prefixes = aliases;
}
@Nonnull
private static Predicate<String> validPrefix(String other) {
return prefix -> {
if (prefix.length() > other.length()) {
return prefix.startsWith(other);
}
int i = other.indexOf('[');
if (i == -1) {
return other.equals(prefix);
}
return other.substring(0, i).equals(prefix);
};
}
@Nonnull
private Function<String, Stream<? extends String>> extractArguments(String input, ParserContext context) {
return prefix -> {
if (input.length() > prefix.length() && input.startsWith(prefix + "[")) {
// input already contains argument(s) -> extract them
String[] strings = extractArguments(input.substring(prefix.length()), false);
// rebuild the argument string without the last argument
StringBuilder builder = new StringBuilder();
for (int i = 0; i < strings.length - 1; i++) {
builder.append('[').append(strings[i]).append(']');
}
String previous = prefix + builder;
// read the suggestions for the last argument
return getSuggestions(strings[strings.length - 1], strings.length - 1, context)
.map(suggestion -> previous + "[" + suggestion);
} else {
return Stream.of(prefix);
}
};
}
/**
* Gives the default prefix/name of the pattern/mask/transform.
*
* @return default prefix
*/
public String getPrefix() {
return this.prefixes[0];
}
/**
* Return all prefix/name aliases of the pattern/mask/transform
*
* @return all prefix/name aliases
*/
public List<String> getMatchedAliases() {
return Arrays.asList(prefixes);
}
@Override
public Stream<String> getSuggestions(String input) {
return Arrays.stream(this.prefixes)
.filter(validPrefix(input))
.flatMap(extractArguments(input, new ParserContext()));
}
@Override
public E parseFromInput(String input, ParserContext context) throws InputParseException {
int i = input.indexOf('[');
// Rich parser requires arguments, else, it should not be used
if (i == -1) {
return null;
}
String inputPrefix = input.substring(0, i);
for (String prefix : this.prefixes) {
if (!inputPrefix.equals(prefix)) {
continue;
}
String[] arguments = extractArguments(input.substring(prefix.length()), true);
return parseFromInput(arguments, context);
}
return null;
}
/**
* Returns a stream of suggestions for the argument at the given index.
*
* @param argumentInput the already provided input for the argument at the given index.
* @param index the index of the argument to get suggestions for.
* @return a stream of suggestions matching the given input for the argument at the given index.
*/
protected Stream<String> getSuggestions(String argumentInput, int index) {
return Stream.empty();
}
/**
* Returns a stream of suggestions for the argument at the given index.
*
* @param argumentInput the already provided input for the argument at the given index.
* @param index the index of the argument to get suggestions for.
* @param context the context which may optionally be provided by a parser.
* @return a stream of suggestions matching the given input for the argument at the given index.
*/
protected Stream<String> getSuggestions(String argumentInput, int index, ParserContext context) {
return getSuggestions(argumentInput, index);
}
/**
* Parses the already split arguments.
*
* @param arguments the array of arguments that were split (can be empty).
* @param context the context of this parsing process.
* @return the resulting parsed type.
* @throws InputParseException if the input couldn't be parsed correctly.
*/
protected abstract E parseFromInput(@Nonnull String[] arguments, ParserContext context) throws InputParseException;
/**
* Extracts arguments enclosed by {@code []} into an array.
* Example: {@code [Hello][World]} results in a list containing {@code Hello} and {@code World}.
*
* @param input the input to extract arguments from.
* @param requireClosing whether or not the extraction requires valid bracketing.
* @return an array of extracted arguments.
* @throws InputParseException if {@code requireClosing == true} and the count of [ != the count of ]
*/
protected String[] extractArguments(String input, boolean requireClosing) throws InputParseException {
int open = 0; // the "level"
int openIndex = 0;
int i = 0;
List<String> arguments = new ArrayList<>();
for (; i < input.length(); i++) {
if (input.charAt(i) == '[') {
if (open++ == 0) {
openIndex = i;
}
}
if (input.charAt(i) == ']') {
if (--open == 0) {
arguments.add(input.substring(openIndex + 1, i));
}
}
}
if (!requireClosing) {
if (open > 0) {
arguments.add(input.substring(openIndex + 1));
} else {
int last = input.lastIndexOf(']');
if (last != -1) {
arguments.add(input.substring(last));
}
}
}
if (requireClosing && open != 0) {
throw new InputParseException(Caption.of("fawe.error.invalid-bracketing", TextComponent.of("'[' or ']'?")));
}
return arguments.toArray(new String[0]);
}
}