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 {
- processLine(s);
+ processLine(s, false);
}
}
}
- static void processLine(String s) throws IOException {
+ 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();
}
}