* //#define FOO bar baz -- replace all instances of token FOO with "bar baz"
* //#replace foo/bar baz/bop -- DUPLICATE everything between here and //#end,
* replacing foo with bar and baz with bop in the *second* copy
+ * //#switch(EXPR) -- switch on strings
+ * {
+ * case "case1":
+ * }
*
* Replacements are done on a token basis. Tokens are defined as a
* sequence of characters which all belong to a single class. The
sinceLastRepeat = null;
replace = save;
+ } else if (trimmed.startsWith("//#switch")) {
+ String expr = trimmed.substring(trimmed.indexOf('(') + 1, trimmed.lastIndexOf(')'));
+ System.out.println("final String neverUseThis = (String)("+expr+"); switch(neverUseThis.length()) {");
+ Hashtable[] byLength = new Hashtable[255];
+ String key = null;
+ for(trimmed = br.readLine().trim(); !trimmed.startsWith("//#end"); trimmed = br.readLine().trim()) {
+ // FIXME: default
+ if (trimmed.startsWith("case ")) {
+ trimmed = trimmed.substring(trimmed.indexOf('\"') + 1);
+ key = trimmed.substring(0, trimmed.indexOf('\"'));
+ Hashtable thisCase = (Hashtable)byLength[key.length()];
+ if (thisCase == null) byLength[key.length()] = thisCase = new Hashtable();
+ thisCase.put(key, "");
+ trimmed = trimmed.substring(trimmed.indexOf('\"') + 1);
+ trimmed = trimmed.substring(trimmed.indexOf(':') + 1);
+ }
+ if (key != null) {
+ Hashtable hash = byLength[key.length()];
+ hash.put(key, (String)hash.get(key) + trimmed + "\n");
+ }
+ else System.out.println(trimmed);
+ }
+
+ for(int i=0; i<255; i++) {
+ if (byLength[i] == null) continue;
+ System.out.println("case " + i + ": {");
+ buildTrie("", byLength[i]);
+ System.out.println("break; }");
+ }
+ System.out.println("} //switch");
+
} else {
processLine(s, false);
}
}
+ static void buildTrie(String prefix, Hashtable cases) {
+ Enumeration caseKeys = cases.keys();
+ Vec keys = new Vec();
+ while(caseKeys.hasMoreElements()) keys.addElement(caseKeys.nextElement());
+ keys.sort(new Vec.CompareFunc() { public int compare(Object a, Object b) {
+ return ((String)a).compareTo((String)b);
+ } } );
+
+ for(int i=0; i<keys.size(); i++) {
+ String prefixPlusOne = ((String)keys.elementAt(i)).substring(0, prefix.length() + 1);
+ if (i<keys.size()-1 && prefixPlusOne.equals((((String)keys.elementAt(i + 1)).substring(0, prefix.length() + 1)))) {
+ System.out.println("case \'" + prefixPlusOne.charAt(prefixPlusOne.length() - 1) + "\': {");
+ System.out.println("switch(neverUseThis.charAt(" + prefix.length() + ")) {");
+ buildTrie(prefixPlusOne, cases);
+ System.out.println("} break; }");
+ while(i<keys.size()-1 &&
+ prefixPlusOne.equals(((String)keys.elementAt(i + 1)).substring(0, prefix.length() + 1))) {
+ i++;
+ prefixPlusOne = ((String)keys.elementAt(i)).substring(0, prefix.length() + 1);
+ }
+ i--;
+ } else {
+ String code = (String)cases.get(keys.elementAt(i));
+ code = code.substring(0, code.length() - 1);
+ String key = (String)keys.elementAt(i);
+ System.out.println("if (\""+key+"\".equals(neverUseThis)) { " + code + " } ");
+ }
+ }
+ }
+
static void processLine(String s, boolean deleteLineEndings) throws IOException {
if (deleteLineEndings && s.indexOf("//") != -1) s = s.substring(0, s.indexOf("//"));
for(int i=0; i<s.length(); i++) {