package org.ibex.util;
+import gnu.regexp.*;
import java.util.*;
import java.io.*;
*/
public class Preprocessor {
+ public static String replaceAll(String source, String regexp, String replaceWith) {
+ try {
+ RE re = new RE(regexp, 0, RESyntax.RE_SYNTAX_PERL5);
+ return (String)re.substituteAll(source, replaceWith);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
private int enumSwitch = 0; // number appended to variable used in switch implementation
+
public Preprocessor(Reader reader, Writer writer) {
setReader(reader);
setWriter(writer);
String val = trimmed.substring(keyEnd).trim();
replace.put(key, val);
}
- out.println(); // preserve line numbers
+ out.print("\n"); // preserve line numbers
} else if (trimmed.startsWith("//#repeat ")) {
trimmed = trimmed.substring(9);
String s2 = in.readLine().trim();
if (s2.startsWith("//")) s2 = s2.substring(2).trim();
trimmed = trimmed.substring(0, trimmed.length() - 1) + " " + s2;
- out.println(); // preserve line numbers
+ out.print("\n"); // preserve line numbers
}
StringTokenizer st = new StringTokenizer(trimmed, " ");
repeatreplace = (Hashtable)replace.clone();
repeatreplace.put(key, val);
}
sinceLastRepeat = new Vector();
- out.println(); // preserve line numbers
+ out.print("\n"); // preserve line numbers
} else if (trimmed.startsWith("//#end")) {
if (sinceLastRepeat == null) { err.add(new Warning("#end orphaned")); continue PROCESS; }
Hashtable save = replace;
replace = repeatreplace;
- out.println();
+ out.print("\n");
for(int i=0; i<sinceLastRepeat.size() - 1; i++) out.print(processLine((String)sinceLastRepeat.elementAt(i), true));
sinceLastRepeat = null;
replace = save;
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+");");
- out.println("SUCCESS:do { switch(ccSwitch"+enumSwitch+".length()) {");
+ out.print("final String ccSwitch"+enumSwitch+" = (String)("+expr+"); ");
+ out.print("SUCCESS:do { switch(ccSwitch"+enumSwitch+".length()) {\n");
Hashtable[] byLength = new Hashtable[255];
String key = null;
if (key != null) {
Hashtable hash = byLength[key.length()];
- hash.put(key, (String)hash.get(key) + processLine(trimmed, false) + "\n");
+ hash.put(key, (String)hash.get(key) + replaceAll(processLine(trimmed, false), "//[^\"]*$", "").trim() + "\n");
} else {
out.print(processLine(trimmed, false));
}
for(int i=0; i<255; i++) {
if (byLength[i] == null) continue;
- out.println("case " + i + ": { switch(ccSwitch"+enumSwitch+".charAt(0)) {");
+ out.print("case " + i + ": { switch(ccSwitch"+enumSwitch+".charAt(0)) { ");
buildTrie("", byLength[i]);
- out.println("}; break; }");
+ out.print("}; break; } ");
}
- out.println("} //switch");
- if (Default != null) out.println(Default);
- out.println("} while(false); //OUTER");
+ out.print("} /* switch */ ");
+ if (Default != null) out.print(" " + Default);
+ out.print(" } while(false); /* OUTER */\n");
enumSwitch++;
} else {
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) + ")) {");
+ out.print("case \'" + prefixPlusOne.charAt(prefixPlusOne.length() - 1) + "\': { ");
+ out.print("switch(ccSwitch"+enumSwitch+".charAt(" + (prefix.length()+1) + ")) { ");
buildTrie(prefixPlusOne, cases);
- out.println("} break; }");
+ out.print("} 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) + "\':");
+ out.print("case \'" + prefixPlusOne.charAt(prefixPlusOne.length() - 1) + "\': ");
String code = (String)cases.get(keys.elementAt(i));
- code = code.substring(0, code.length() - 1);
+ code = code.substring(0, code.length());
String key = (String)keys.elementAt(i);
- out.println("if (\""+key+"\".equals(ccSwitch"+enumSwitch+")) { if (true) do { " + code + " } while(false); break SUCCESS; } break; ");
+ out.print("if (\""+key+"\".equals(ccSwitch"+enumSwitch+")) { if (true) do { " + code + " } while(false); break SUCCESS; } break; ");
}
}
}
ret += tok;
i = j - 1;
} else if (val instanceof JSFunctionMacro) {
- if (s.charAt(j) != '(') { err.add(new Error("open paren must follow macro binding for macro " + tok)); continue; }
+ if (s.charAt(j) != '(') { ret += tok; i = j - 1; continue; }
ret += ((JSFunctionMacro)val).process(s.substring(j+1, s.indexOf(')', j)));
i = s.indexOf(')', j);
} else {
String bound2 = null;
if (unbound2 == null) {
bound1 = args;
- return expression.replaceAll(unbound1, bound1);
+ return replaceAll(expression, unbound1, bound1);
} else {
bound1 = args.substring(0, args.indexOf(','));
bound2 = args.substring(args.indexOf(',') + 1);
- return (expression.replaceAll(unbound1, bound1).replaceAll(unbound2, bound2));
+ return replaceAll(replaceAll(expression, unbound1, bound1), unbound2, bound2);
}
}
}