package game.command; import java.util.List; import com.google.common.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 args = Lists.newArrayList(); List cmds = Lists.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 abstract Class getTypeClass(); public final String getName() { return this.name; } public final String toString() { return this.name; } }