initial commit
This commit is contained in:
parent
3c9ee26b06
commit
22186c33b9
1458 changed files with 282792 additions and 0 deletions
97
java/src/game/command/ArgumentParser.java
Normal file
97
java/src/game/command/ArgumentParser.java
Normal file
|
@ -0,0 +1,97 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import game.collect.Lists;
|
||||
|
||||
public abstract class ArgumentParser {
|
||||
public static String[][] splitString(String str) {
|
||||
// if(str.isEmpty()) {
|
||||
// return new String[0];
|
||||
// }
|
||||
int pos;
|
||||
int last = 0;
|
||||
boolean space = true;
|
||||
boolean quote = false;
|
||||
boolean escape = false;
|
||||
char c;
|
||||
String arg = "";
|
||||
List<String> args = Lists.<String>newArrayList();
|
||||
List<String[]> cmds = Lists.<String[]>newArrayList();
|
||||
for(pos = 0; pos < str.length(); pos++) {
|
||||
c = str.charAt(pos);
|
||||
if(escape) {
|
||||
escape = false;
|
||||
switch(c) {
|
||||
case '\\':
|
||||
case ' ':
|
||||
case '"':
|
||||
case ';':
|
||||
arg += c;
|
||||
break;
|
||||
default:
|
||||
throw new ScriptException("Unbekannte Sequenz bei Zeichen %d: '\\%c'", pos + 1, c);
|
||||
}
|
||||
}
|
||||
else if(c == '\\') {
|
||||
space = false;
|
||||
escape = true;
|
||||
}
|
||||
else if(c == '"') {
|
||||
space = false;
|
||||
quote = !quote;
|
||||
last = pos;
|
||||
}
|
||||
else if(c == ' ' && !quote) {
|
||||
if(!space) {
|
||||
args.add(arg);
|
||||
arg = "";
|
||||
}
|
||||
space = true;
|
||||
}
|
||||
else if(c == ';' && !quote) {
|
||||
if(!space) {
|
||||
args.add(arg);
|
||||
arg = "";
|
||||
}
|
||||
if(!args.isEmpty()) {
|
||||
cmds.add(args.toArray(new String[args.size()]));
|
||||
args.clear();
|
||||
}
|
||||
}
|
||||
else {
|
||||
space = false;
|
||||
arg += c;
|
||||
}
|
||||
}
|
||||
if(escape)
|
||||
throw new ScriptException("Unvollständige Sequenz bei Zeichen %d", pos + 1);
|
||||
if(quote)
|
||||
throw new ScriptException("Nicht geschlossenes Anführungszeichen bei %d", last + 1);
|
||||
if(!space)
|
||||
args.add(arg);
|
||||
if(!args.isEmpty()) {
|
||||
cmds.add(args.toArray(new String[args.size()]));
|
||||
args.clear();
|
||||
}
|
||||
return cmds.toArray(new String[cmds.size()][]);
|
||||
}
|
||||
|
||||
private final String name;
|
||||
|
||||
public ArgumentParser(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public abstract Object parse(ScriptEnvironment env, String input);
|
||||
public abstract Object getDefault(ScriptEnvironment env);
|
||||
public abstract String[] getCompletions(ScriptEnvironment env);
|
||||
|
||||
public final String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public final String toString() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue