-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandParser.java
More file actions
57 lines (54 loc) · 1.72 KB
/
CommandParser.java
File metadata and controls
57 lines (54 loc) · 1.72 KB
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
package com.github.pinmacaroon.dchook.bot;
import java.util.ArrayList;
import java.util.List;
/**
* im hella proud of this so it isn't going anywhere >:3
*/
public class CommandParser {
public static List<Object> parseString(String source) {
List<String> parsed = new ArrayList<>();
StringBuilder holder = new StringBuilder();
boolean capture = false;
for (char i : source.toCharArray()) {
if (!(i == '\'' || i == '"' || i == ' ')) {
holder.append(i);
} else if (i == '\'' || i == '"') {
if (capture) {
capture = false;
parsed.add(holder.toString());
holder.setLength(0);
} else {
parsed.add(holder.toString());
holder.setLength(0);
capture = true;
}
} else {
if (capture) {
holder.append(i);
} else {
parsed.add(holder.toString());
holder.setLength(0);
}
}
}
List<Object> scanned = new ArrayList<>();
parsed.add(holder.toString());
for (String i : parsed) {
if (i.isEmpty()) {
continue;
}
try {
scanned.add(Float.valueOf(i));
} catch (NumberFormatException e) {
if (i.equals("true")) {
scanned.add(true);
} else if (i.equals("false")) {
scanned.add(false);
} else {
scanned.add(i.strip());
}
}
}
return scanned;
}
}