* //#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
public class Preprocessor {
static Hashtable replace = new Hashtable();
- static Hashtable savereplace = replace;
+ static Hashtable repeatreplace = null;
static Vector sinceLastRepeat = null;
public static void main(String[] args) throws IOException {
String key = trimmed.substring(0, trimmed.indexOf(' '));
String val = trimmed.substring(trimmed.indexOf(' ')).trim();
replace.put(key, val);
+ System.out.println(); // preserve line numbers
} else if (trimmed.startsWith("//#repeat ")) {
StringTokenizer st = new StringTokenizer(trimmed.substring(9), " ");
- savereplace = replace;
- replace = (Hashtable)replace.clone();
+ repeatreplace = (Hashtable)replace.clone();
while (st.hasMoreTokens()) {
String tok = st.nextToken().trim();
String key = tok.substring(0, tok.indexOf('/'));
String val = tok.substring(tok.indexOf('/') + 1);
- replace.put(key, val);
+ repeatreplace.put(key, val);
}
sinceLastRepeat = new Vector();
+ System.out.println(); // preserve line numbers
} else if (trimmed.startsWith("//#end")) {
- replace = savereplace;
+ Hashtable save = replace;
+ replace = repeatreplace;
System.out.println();
- for(int i=0; i<sinceLastRepeat.size(); i++) processLine((String)sinceLastRepeat.elementAt(i));
+ for(int i=0; i<sinceLastRepeat.size() - 1; i++) processLine((String)sinceLastRepeat.elementAt(i), true);
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 + ": { switch(neverUseThis.charAt(0)) {");
+ buildTrie("", byLength[i]);
+ System.out.println("}; break; }");
+ }
+ System.out.println("} //switch");
} else {
- processLine(s);
+ processLine(s, false);
}
}
}
- static void processLine(String s) throws IOException {
+ 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++) {
+ if (!((String)keys.elementAt(i)).startsWith(prefix)) continue;
+ 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() && prefixPlusOne.equals(((String)keys.elementAt(i)).substring(0, prefix.length() + 1))) i++;
+ if (i<keys.size()) { i--; continue; }
+ } else {
+ System.out.println("case \'" + prefixPlusOne.charAt(prefixPlusOne.length() - 1) + "\':");
+ 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 + " } break; ");
+ }
+ }
+ }
+
+ 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++) {
char c = s.charAt(i);
if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') {
else System.out.print(tok);
i = j - 1;
}
- System.out.println();
+ if (!deleteLineEndings) System.out.println();
}
}