X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Futil%2FPreprocessor.java;fp=src%2Forg%2Fxwt%2Futil%2FPreprocessor.java;h=7e80f7f3e2147dd5e995c7946e50e9f5fb5476b5;hb=4b35fea2f40ade2fe20d763534b0cdf2818c538f;hp=7fc150290a86335a79ce5713022b6b424c240e3c;hpb=793964725ac9b1ad72ff8bfe0748678b9f81629b;p=org.ibex.core.git diff --git a/src/org/xwt/util/Preprocessor.java b/src/org/xwt/util/Preprocessor.java index 7fc1502..7e80f7f 100644 --- a/src/org/xwt/util/Preprocessor.java +++ b/src/org/xwt/util/Preprocessor.java @@ -10,6 +10,10 @@ import java.io.*; * //#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 @@ -58,6 +62,37 @@ public class Preprocessor { 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); } @@ -66,6 +101,36 @@ public class Preprocessor { } + 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