-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathQuotedStringTokenizer.java
197 lines (172 loc) · 6.68 KB
/
QuotedStringTokenizer.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
195
196
197
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* 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 revxrsal.commands.util.tokenize;
import revxrsal.commands.exception.ArgumentParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Parser for converting a quoted string into a list of arguments.
*
* <p>Grammar is roughly (yeah, this is not really a proper grammar but it gives
* you an idea of what's happening:</p>
*
* <blockquote><pre> WHITESPACE = Character.isWhiteSpace(codePoint)
* CHAR := (all unicode)
* ESCAPE := '\' CHAR
* QUOTE = ' | "
* UNQUOTED_ARG := (CHAR | ESCAPE)+ WHITESPACE
* QUOTED_ARG := QUOTE (CHAR | ESCAPE)+ QUOTE
* ARGS := ((UNQUOTED_ARG | QUOTED_ARG) WHITESPACE+)+</pre></blockquote>
*/
public final class QuotedStringTokenizer {
public static final List<String> EMPTY_TEXT = Collections.singletonList("");
private QuotedStringTokenizer() {}
private static final int CHAR_BACKSLASH = '\\';
private static final int CHAR_SINGLE_QUOTE = '\'';
private static final int CHAR_DOUBLE_QUOTE = '"';
/**
* Returns a list of tokens from parsing the given input,
* respecting quotes and breaks.
*
* @param arguments Argument string to parse
* @return A list of tokens.
*/
public static List<String> tokenize(String arguments) {
if (arguments.length() == 0) {
return Collections.emptyList();
}
final TokenizerState state = new TokenizerState(arguments);
List<String> returnedArgs = new ArrayList<>(arguments.length() / 4);
while (state.hasMore()) {
skipWhiteSpace(state);
String arg = nextArg(state);
returnedArgs.add(arg);
}
return returnedArgs;
}
/**
* Returns a list of tokens from parsing the given input,
* respecting quotes and breaks.
*
* @param arguments Argument string to parse
* @return A list of tokens.
*/
public static List<String> tokenizeUnsafe(String arguments) {
if (arguments.length() == 0) {
return EMPTY_TEXT;
}
return tokenize(arguments);
}
// Parsing methods
private static void skipWhiteSpace(TokenizerState state) throws ArgumentParseException {
if (!state.hasMore()) {
return;
}
while (state.hasMore() && Character.isWhitespace(state.peek())) {
state.next();
}
}
private static String nextArg(TokenizerState state) throws ArgumentParseException {
StringBuilder argBuilder = new StringBuilder();
if (state.hasMore()) {
int codePoint = state.peek();
if (codePoint == CHAR_DOUBLE_QUOTE || codePoint == CHAR_SINGLE_QUOTE) {
// quoted string
parseQuotedString(state, codePoint, argBuilder);
} else {
parseUnquotedString(state, argBuilder);
}
}
return argBuilder.toString();
}
private static void parseQuotedString(TokenizerState state, int startQuotation, StringBuilder builder) throws ArgumentParseException {
// Consume the start quotation character
int nextCodePoint = state.next();
if (nextCodePoint != startQuotation) {
throw state.createException(String.format("Actual next character '%c' did not match expected quotation character '%c'",
nextCodePoint, startQuotation));
}
while (true) {
if (!state.hasMore()) {
return;
}
nextCodePoint = state.peek();
if (nextCodePoint == startQuotation) {
state.next();
return;
} else if (nextCodePoint == CHAR_BACKSLASH) {
parseEscape(state, builder);
} else {
builder.appendCodePoint(state.next());
}
}
}
private static void parseUnquotedString(TokenizerState state, StringBuilder builder) throws ArgumentParseException {
while (state.hasMore()) {
int nextCodePoint = state.peek();
if (Character.isWhitespace(nextCodePoint)) {
return;
} else if (nextCodePoint == CHAR_BACKSLASH) {
parseEscape(state, builder);
} else {
builder.appendCodePoint(state.next());
}
}
}
private static void parseEscape(TokenizerState state, StringBuilder builder) throws ArgumentParseException {
state.next(); // Consume \
builder.appendCodePoint(state.next());
}
private static class TokenizerState {
private final String buffer;
private int index = -1;
TokenizerState(String buffer) {
this.buffer = buffer;
}
// Utility methods
public boolean hasMore() {
return index + 1 < buffer.length();
}
public int peek() throws ArgumentParseException {
if (!hasMore()) {
throw createException("Buffer overrun while parsing args");
}
return buffer.codePointAt(index + 1);
}
public int next() throws ArgumentParseException {
if (!hasMore()) {
throw createException("Buffer overrun while parsing args");
}
return buffer.codePointAt(++index);
}
public ArgumentParseException createException(String message) {
return new ArgumentParseException(message, buffer, index);
}
public int getIndex() {
return index;
}
}
}