* //#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":
- * }
+ * //#end
*
* 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 repeatreplace = null;
- static Vector sinceLastRepeat = null;
-
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String s = null;
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
+
+ // process stdin to stdout
+ Preprocessor cc = new Preprocessor(br, bw);
+ Vector err = cc.process();
+ bw.flush();
+
+ // handle errors
+ boolean errors = false;
+ for (int i=0; i < err.size(); i++) { if (err.get(i) instanceof Error) errors = true; System.err.println(err.get(i)); }
+ if (errors) throw new Exception();
+ }
+
+ private Reader r;
+ private Writer w;
+ private LineNumberReader in;
+ private PrintWriter out;
+
+ private Hashtable replace = new Hashtable();
+ private Hashtable repeatreplace = null;
+ private Vector sinceLastRepeat = null;
+ private Vector err = new Vector();
+
+ private int enumSwitch = 0; // number appended to variable used in switch implementation
+
+ public Preprocessor(Reader reader, Writer writer) {
+ setReader(reader);
+ setWriter(writer);
+ }
+
+ public void setReader(Reader reader) { r = reader; if (r != null) in = new LineNumberReader(r); }
+ public Reader getReader() { return r; }
- while((s = br.readLine()) != null) {
+ public void setWriter(Writer writer) { w = writer; if (w != null) out = new PrintWriter(w); }
+ public Writer getWriter() { return w; }
+
+
+ /** process data from reader, write to writer, return vector of errors */
+ public Vector process() throws IOException {
+ err.clear();
+
+ String s = null;
+PROCESS:
+ while((s = in.readLine()) != null) {
if (sinceLastRepeat != null) sinceLastRepeat.addElement(s);
String trimmed = s.trim();
+
if (trimmed.startsWith("//#define ")) {
- trimmed = trimmed.substring(10).trim();
- if (trimmed.indexOf('(') >= 0 && trimmed.indexOf('(') < trimmed.indexOf(' ')) {
+ if (trimmed.length() == 9 || trimmed.charAt(9) != ' ') {
+ err.add(new Error("#define badly formed, ignored")); continue PROCESS;
+ }
+ int keyStart = indexOfNotWS(trimmed, 9);
+ if (keyStart == -1) {
+ err.add(new Error("#define requires KEY")); continue PROCESS;
+ }
+ int keyEnd = indexOfWS(trimmed, keyStart);
+
+ int macroStart = trimmed.indexOf('(');
+ int macroEnd = trimmed.indexOf(')');
+ if (macroStart > keyEnd) {
+ // no macro is defined, just make sure something dumb like KEYNA)ME hasn't been done
+ if (macroEnd < keyEnd) { err.add(new Error("#define key contains invalid char: ')'")); continue PROCESS; }
+ macroStart = macroEnd = -1;
+ }
+
+ if (macroStart == 0) {
+ err.add(new Error("#define macro requires name")); continue PROCESS;
+ } else if (macroStart > 0) {
+ if (macroStart > macroEnd) { err.add(new Error("#define macro badly formed")); continue PROCESS; }
+ if (macroStart+1 == macroEnd) { err.add(new Error("#define macro requires property name")); continue PROCESS; }
+
JSFunctionMacro fm = new JSFunctionMacro();
- String key = trimmed.substring(0, trimmed.indexOf('('));
- String unbound = trimmed.substring(trimmed.indexOf('(') + 1, trimmed.indexOf(')'));
- if (unbound.indexOf(',') == -1) {
+ String key = trimmed.substring(keyStart, macroStart);
+ String unbound = trimmed.substring(macroStart +1, macroEnd);
+ int unboundDiv = unbound.indexOf(',');
+ if (unboundDiv == -1) {
fm.unbound1 = unbound;
} else {
- fm.unbound1 = unbound.substring(0, unbound.indexOf(','));
- fm.unbound2 = unbound.substring(unbound.indexOf(',') + 1);
+ fm.unbound1 = unbound.substring(0, unboundDiv);
+ fm.unbound2 = unbound.substring(unboundDiv +1);
+ if (fm.unbound1.length() == 0) { err.add(new Error("#define macro property 1 requires name")); continue PROCESS; }
+ if (fm.unbound2.length() == 0) { err.add(new Error("#define macro property 1 requires name")); continue PROCESS; }
}
- fm.expression = trimmed.substring(trimmed.indexOf(')')+1).trim();
+ fm.expression = trimmed.substring(keyEnd).trim();
replace.put(key, fm);
} else {
- String key = trimmed.substring(0, trimmed.indexOf(' '));
- String val = trimmed.substring(trimmed.indexOf(' ')).trim();
+ String key = trimmed.substring(keyStart, keyEnd);
+ String val = trimmed.substring(keyEnd).trim();
replace.put(key, val);
}
- System.out.println(); // preserve line numbers
+ out.println(); // preserve line numbers
} else if (trimmed.startsWith("//#repeat ")) {
trimmed = trimmed.substring(9);
while(trimmed.charAt(trimmed.length() - 1) == '\\') {
- String s2 = br.readLine().trim();
+ String s2 = in.readLine().trim();
if (s2.startsWith("//")) s2 = s2.substring(2).trim();
trimmed += s2;
- System.out.println(); // preserve line numbers
+ out.println(); // preserve line numbers
}
StringTokenizer st = new StringTokenizer(trimmed, " ");
repeatreplace = (Hashtable)replace.clone();
repeatreplace.put(key, val);
}
sinceLastRepeat = new Vector();
- System.out.println(); // preserve line numbers
+ out.println(); // preserve line numbers
} else if (trimmed.startsWith("//#end")) {
+ if (sinceLastRepeat == null) { err.add(new Warning("#end orphaned")); continue PROCESS; }
Hashtable save = replace;
replace = repeatreplace;
- System.out.println();
- for(int i=0; i<sinceLastRepeat.size() - 1; i++) System.out.print(processLine((String)sinceLastRepeat.elementAt(i), true));
+ out.println();
+ for(int i=0; i<sinceLastRepeat.size() - 1; i++) out.print(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()) {");
+ int expStart = trimmed.indexOf('(') +1;
+ if (expStart < 1) { err.add(new Error("expected ( in #switch")); continue PROCESS; }
+ int expEnd = trimmed.indexOf(')');
+ if (expEnd == -1) { err.add(new Error("expected ) in #switch")); continue PROCESS; }
+ if (expEnd - expStart <= 1) { err.add(new Error("badly formed #switch statement")); continue PROCESS; }
+ String expr = trimmed.substring(expStart, expEnd);
+
+ out.println("final String ccSwitch"+enumSwitch+" = (String)("+expr+");"+
+ "switch(ccSwitch"+enumSwitch+".length()) {");
+
Hashtable[] byLength = new Hashtable[255];
String key = null;
String Default = null;
- for(trimmed = br.readLine().trim(); !trimmed.startsWith("//#end"); trimmed = br.readLine().trim()) {
+ for(trimmed = in.readLine().trim(); !trimmed.startsWith("//#end"); trimmed = in.readLine().trim()) {
if (trimmed.startsWith("default:")) {
Default = processLine(trimmed.substring(8), false);
continue;
}
if (trimmed.startsWith("case ")) {
- trimmed = trimmed.substring(trimmed.indexOf('\"') + 1);
- key = trimmed.substring(0, trimmed.indexOf('\"'));
+ // find key
+ int strStart = trimmed.indexOf('\"') +1;
+ if (strStart < 1) { err.add(new Error("expected opening of String literal")); continue PROCESS; }
+ int strEnd = trimmed.indexOf('\"', strStart);
+ if (strEnd == -1) { err.add(new Error("expected closing of String literal")); continue PROCESS; }
+ key = trimmed.substring(strStart, strEnd);
+
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);
+
+ // find end of case definition
+ int caseEnd = trimmed.indexOf(':', strEnd) +1;
+ if (caseEnd < 1) { err.add(new Error("expected :")); continue PROCESS; }
+ trimmed = trimmed.substring(caseEnd);
}
+
if (key != null) {
Hashtable hash = byLength[key.length()];
hash.put(key, (String)hash.get(key) + processLine(trimmed, false) + "\n");
} else {
- System.out.print(processLine(trimmed, false));
+ out.print(processLine(trimmed, false));
}
}
for(int i=0; i<255; i++) {
if (byLength[i] == null) continue;
- System.out.println("case " + i + ": { switch(neverUseThis.charAt(0)) {");
+ out.println("case " + i + ": { switch(ccSwitch"+enumSwitch+".charAt(0)) {");
buildTrie("", byLength[i]);
- System.out.println("}; break; }");
+ out.println("}; break; }");
}
- if (Default != null) System.out.println("default: { " + Default + " }");
- System.out.println("} //switch");
+ if (Default != null) out.println("default: { " + Default + " }");
+ out.println("} //switch");
+ enumSwitch++;
} else {
- System.out.print(processLine(s, false));
+ out.print(processLine(s, false));
}
-
}
+ return err;
}
- 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()+1) + ")) {");
- 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; ");
- }
+ private 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)))) {
+ out.println("case \'" + prefixPlusOne.charAt(prefixPlusOne.length() - 1) + "\': {");
+ out.println("switch(ccSwitch"+enumSwitch+".charAt(" + (prefix.length()+1) + ")) {");
+ buildTrie(prefixPlusOne, cases);
+ 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 {
+ 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);
+ out.println("if (\""+key+"\".equals(ccSwitch"+enumSwitch+")) { " + code + " } break; ");
}
}
+ }
- static String processLine(String s, boolean deleteLineEndings) throws IOException {
+ private String processLine(String s, boolean deleteLineEndings) throws IOException {
if (deleteLineEndings && s.indexOf("//") != -1) s = s.substring(0, s.indexOf("//"));
String ret = "";
for(int i=0; i<s.length(); i++) {
ret += tok;
i = j - 1;
} else if (val instanceof JSFunctionMacro) {
- if (s.charAt(j) != '(') System.err.println("open paren must follow macro binding for macro " + tok);
+ if (s.charAt(j) != '(') { err.add(new Error("open paren must follow macro binding for macro " + tok)); continue; }
ret += ((JSFunctionMacro)val).process(s.substring(j+1, s.indexOf(')', j)));
i = s.indexOf(')', j);
} else {
return ret;
}
+ private static int indexOfWS(String s) { return indexOfWS(s, 0); }
+ private static int indexOfWS(String s, int beginIndex) {
+ if (s == null || beginIndex >= s.length()) return -1;
+ for (; beginIndex < s.length(); beginIndex++) {
+ if (s.charAt(beginIndex) == ' ') return beginIndex;
+ }
+ return s.length();
+ }
+
+ private static int indexOfNotWS(String s) { return indexOfWS(s, 0); }
+ private static int indexOfNotWS(String s, int beginIndex) {
+ if (s == null || beginIndex >= s.length()) return -1;
+ for (; beginIndex < s.length(); beginIndex++) {
+ if (s.charAt(beginIndex) != ' ') return beginIndex;
+ }
+ return -1;
+ }
+
+ public class Warning {
+ protected String msg;
+ protected int line;
+
+ public Warning() { msg = ""; }
+ public Warning(String m) { msg = m; if (in != null) line = in.getLineNumber(); }
+
+ public String toString() { return "WARNING Line "+line+": "+msg; }
+ }
+
+ public class Error extends Warning {
+ public Error() { super(); }
+ public Error(String m) { super(m); }
+ public String toString() { return "ERROR Line "+line+": "+msg; }
+ }
+
public static class JSFunctionMacro {
public String unbound1 = null;
public String unbound2 = null;