1 // Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
3 // You may modify, copy, and redistribute this code under the terms of
4 // the GNU Library Public License version 2.1, with the exception of
5 // the portion of clause 6a after the semicolon (aka the "obnoxious
10 //import gnu.regexp.*;
15 * A VERY crude, inefficient Java preprocessor
17 * //#define FOO bar baz -- replace all instances of token FOO with "bar baz"
18 * //#replace foo/bar baz/bop -- DUPLICATE everything between here and //#end,
19 * replacing foo with bar and baz with bop in the *second* copy
20 * //#switch(EXPR) -- switch on strings
24 * Replacements are done on a token basis. Tokens are defined as a
25 * sequence of characters which all belong to a single class. The
26 * two character classes are:
29 * - all other non-whitespace characters
31 public class Preprocessor {
33 public static String replaceAll(String source, String regexp, String replaceWith) {
34 return source.replaceAll(regexp, replaceWith);
37 RE re = new RE(regexp, 0, RESyntax.RE_SYNTAX_PERL5);
38 return (String)re.substituteAll(source, replaceWith);
39 } catch (Exception e) {
46 public static void main(String[] args) throws Exception {
47 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
48 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
50 // process stdin to stdout
51 Preprocessor cc = new Preprocessor(br, bw);
52 Vector err = cc.process();
56 boolean errors = false;
57 for (int i=0; i < err.size(); i++) { if (err.get(i) instanceof Error) errors = true; System.err.println(err.get(i)); }
58 if (errors) throw new Exception();
63 private LineNumberReader in;
64 private PrintWriter out;
66 private Hashtable replace = new Hashtable();
67 private Hashtable repeatreplace = null;
68 private Vector sinceLastRepeat = null;
69 private Vector err = new Vector();
71 private int enumSwitch = 0; // number appended to variable used in switch implementation
74 public Preprocessor(Reader reader, Writer writer) {
79 public void setReader(Reader reader) { r = reader; if (r != null) in = new LineNumberReader(r); }
80 public Reader getReader() { return r; }
82 public void setWriter(Writer writer) { w = writer; if (w != null) out = new PrintWriter(w); }
83 public Writer getWriter() { return w; }
86 /** process data from reader, write to writer, return vector of errors */
87 public Vector process() throws IOException {
92 while((s = in.readLine()) != null) {
93 if (sinceLastRepeat != null) sinceLastRepeat.addElement(s);
94 String trimmed = s.trim();
96 if (trimmed.startsWith("//#define ")) {
97 if (trimmed.length() == 9 || trimmed.charAt(9) != ' ') {
98 err.add(new Error("#define badly formed, ignored")); continue PROCESS;
100 int keyStart = indexOfNotWS(trimmed, 9);
101 if (keyStart == -1) {
102 err.add(new Error("#define requires KEY")); continue PROCESS;
104 int keyEnd = indexOfWS(trimmed, keyStart);
106 int macroStart = trimmed.indexOf('(');
107 int macroEnd = trimmed.indexOf(')');
108 if (macroStart > keyEnd) {
109 // no macro is defined, just make sure something dumb like KEYNA)ME hasn't been done
110 if (macroEnd < keyEnd) { err.add(new Error("#define key contains invalid char: ')'")); continue PROCESS; }
111 macroStart = macroEnd = -1;
114 if (macroStart == 0) {
115 err.add(new Error("#define macro requires name")); continue PROCESS;
116 } else if (macroStart > 0) {
117 if (macroStart > macroEnd) { err.add(new Error("#define macro badly formed")); continue PROCESS; }
118 if (macroStart+1 == macroEnd) { err.add(new Error("#define macro requires property name")); continue PROCESS; }
120 JSFunctionMacro fm = new JSFunctionMacro();
121 String key = trimmed.substring(keyStart, macroStart);
122 String unbound = trimmed.substring(macroStart +1, macroEnd);
123 int unboundDiv = unbound.indexOf(',');
124 if (unboundDiv == -1) {
125 fm.unbound1 = unbound;
127 fm.unbound1 = unbound.substring(0, unboundDiv);
128 fm.unbound2 = unbound.substring(unboundDiv +1);
129 if (fm.unbound1.length() == 0) { err.add(new Error("#define macro property 1 requires name")); continue PROCESS; }
130 if (fm.unbound2.length() == 0) { err.add(new Error("#define macro property 1 requires name")); continue PROCESS; }
132 fm.expression = trimmed.substring(keyEnd).trim();
133 replace.put(key, fm);
135 String key = trimmed.substring(keyStart, keyEnd);
136 String val = trimmed.substring(keyEnd).trim();
137 replace.put(key, val);
139 out.print("\n"); // preserve line numbers
141 } else if (trimmed.startsWith("//#repeat ")) {
142 trimmed = trimmed.substring(9);
143 while(trimmed.charAt(trimmed.length() - 1) == '\\') {
144 String s2 = in.readLine().trim();
145 if (s2.startsWith("//")) s2 = s2.substring(2).trim();
146 trimmed = trimmed.substring(0, trimmed.length() - 1) + " " + s2;
147 out.print("\n"); // preserve line numbers
149 StringTokenizer st = new StringTokenizer(trimmed, " ");
150 repeatreplace = (Hashtable)replace.clone();
151 while (st.hasMoreTokens()) {
152 String tok = st.nextToken().trim();
153 String key = tok.substring(0, tok.indexOf('/'));
154 String val = tok.substring(tok.indexOf('/') + 1);
155 repeatreplace.put(key, val);
157 sinceLastRepeat = new Vector();
158 out.print("\n"); // preserve line numbers
160 } else if (trimmed.startsWith("//#end")) {
161 if (sinceLastRepeat == null) { err.add(new Warning("#end orphaned")); continue PROCESS; }
162 Hashtable save = replace;
163 replace = repeatreplace;
165 for(int i=0; i<sinceLastRepeat.size() - 1; i++) out.print(processLine((String)sinceLastRepeat.elementAt(i), true));
166 sinceLastRepeat = null;
169 } else if (trimmed.startsWith("//#switch")) {
170 int expStart = trimmed.indexOf('(') +1;
171 if (expStart < 1) { err.add(new Error("expected ( in #switch")); continue PROCESS; }
172 int expEnd = trimmed.lastIndexOf(')');
173 if (expEnd == -1) { err.add(new Error("expected ) in #switch")); continue PROCESS; }
174 if (expEnd - expStart <= 1) { err.add(new Error("badly formed #switch statement")); continue PROCESS; }
175 String expr = trimmed.substring(expStart, expEnd);
177 out.print("final String ccSwitch"+enumSwitch+" = (String)("+expr+"); ");
178 out.print("SUCCESS:do { switch(ccSwitch"+enumSwitch+".length()) {\n");
180 Hashtable[] byLength = new Hashtable[255];
182 String Default = null;
183 for(trimmed = in.readLine().trim(); !trimmed.startsWith("//#end"); trimmed = in.readLine().trim()) {
184 if (trimmed.startsWith("default:")) {
185 Default = processLine(trimmed.substring(8), false);
188 if (trimmed.startsWith("case ")) {
190 int strStart = trimmed.indexOf('\"') +1;
191 if (strStart < 1) { err.add(new Error("expected opening of String literal")); continue PROCESS; }
192 int strEnd = trimmed.indexOf('\"', strStart);
193 if (strEnd == -1) { err.add(new Error("expected closing of String literal")); continue PROCESS; }
194 key = trimmed.substring(strStart, strEnd);
196 Hashtable thisCase = (Hashtable)byLength[key.length()];
197 if (thisCase == null) byLength[key.length()] = thisCase = new Hashtable();
198 thisCase.put(key, "");
200 // find end of case definition
201 int caseEnd = trimmed.indexOf(':', strEnd) +1;
202 if (caseEnd < 1) { err.add(new Error("expected :")); continue PROCESS; }
203 trimmed = trimmed.substring(caseEnd);
207 Hashtable hash = byLength[key.length()];
208 hash.put(key, (String)hash.get(key) + replaceAll(processLine(trimmed, false), "//[^\"]*$", "").trim() + "\n");
210 out.print(processLine(trimmed, false));
214 for(int i=0; i<255; i++) {
215 if (byLength[i] == null) continue;
216 out.print("case " + i + ": { switch(ccSwitch"+enumSwitch+".charAt(0)) { ");
217 buildTrie("", byLength[i]);
218 out.print("}; break; } ");
220 out.print("} /* switch */ ");
221 if (Default != null) out.print(" " + Default);
222 out.print(" } while(false); /* OUTER */\n");
226 out.print(processLine(s, false));
233 private static class MyVec {
234 private Object[] store;
235 private int size = 0;
237 public MyVec() { this(10); }
238 public MyVec(int i) { store = new Object[i]; }
239 public MyVec(int i, Object[] store) { size = i; this.store = store; }
241 private void grow() { grow(store.length * 2); }
242 private void grow(int newsize) {
243 Object[] newstore = new Object[newsize];
244 System.arraycopy(store, 0, newstore, 0, size);
248 public void removeAllElements() {
249 for(int i=0; i<size; i++) store[i] = null;
253 public void toArray(Object[] o) {
254 for(int i=0; i<size; i++)
258 public int indexOf(Object o) {
259 for(int i=0; i<size; i++)
260 if (o == null ? store[i] == null : store[i].equals(o)) return i;
265 public void addElement(Object o) {
266 if (size >= store.length - 1) grow();
270 public Object peek() {
271 return lastElement();
274 public Object elementAt(int i) {
278 public Object lastElement() {
279 if (size == 0) return null;
280 return store[size - 1];
283 public void push(Object o) { addElement(o); }
284 public Object pop() {
285 Object ret = lastElement();
286 if (size > 0) store[size--] = null;
290 public int size() { return size; }
292 public void setSize(int newSize) {
293 if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
294 if (newSize > store.length) grow(newSize * 2);
296 for(int i=newSize; i<size; i++)
301 public void copyInto(Object[] out) {
302 for(int i=0; i<size; i++)
306 public void fromArray(Object[] in) {
308 for(int i=0; i<size; i++)
312 public void removeElementAt(int i) {
313 if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
314 for(int j=i; j<size - 1; j++)
315 store[j] = store[j + 1];
319 public void setElementAt(Object o, int i) {
320 if (i >= size) setSize(i);
324 public void removeElement(Object o) {
325 int idx = indexOf(o);
326 if (idx != -1) removeElementAt(idx);
329 public void insertElementAt(Object o, int at) {
330 if (size == store.length) grow();
331 for(int i=size; i>at; i--)
332 store[i] = store[i-1];
337 public interface CompareFunc {
338 public int compare(Object a, Object b);
341 public void sort(CompareFunc c) {
342 sort(this, null, c, 0, size-1);
345 public static void sort(MyVec a, MyVec b, CompareFunc c) {
346 if (b != null && a.size != b.size) throw new IllegalArgumentException("MyVec a and b must be of equal size");
347 sort(a, b, c, 0, a.size-1);
350 private static final void sort(MyVec a, MyVec b, CompareFunc c, int start, int end) {
351 Object tmpa, tmpb = null;
352 if(start >= end) return;
354 for(int i=start+1;i<=end;i++) {
356 if (b != null) tmpb = b.store[i];
358 for(j=i-1;j>=start;j--) {
359 if(c.compare(a.store[j],tmpa) <= 0) break;
360 a.store[j+1] = a.store[j];
361 if (b != null) b.store[j+1] = b.store[j];
364 if (b != null) b.store[j+1] = tmpb;
369 Object pivot = a.store[end];
374 while(c.compare(a.store[++lo],pivot) < 0) { }
375 while((hi > lo) && c.compare(a.store[--hi],pivot) > 0) { }
377 if (b != null) swap(b, lo,hi);
381 if (b != null) swap(b, lo,end);
383 sort(a, b, c, start, lo-1);
384 sort(a, b, c, lo+1, end);
387 private static final void swap(MyVec vec, int a, int b) {
389 Object tmp = vec.store[a];
390 vec.store[a] = vec.store[b];
397 private void buildTrie(String prefix, Hashtable cases) {
398 Enumeration caseKeys = cases.keys();
399 MyVec keys = new MyVec();
400 while(caseKeys.hasMoreElements()) keys.addElement(caseKeys.nextElement());
401 keys.sort(new MyVec.CompareFunc() { public int compare(Object a, Object b) {
402 return ((String)a).compareTo((String)b);
405 for(int i=0; i<keys.size(); i++) {
406 if (!((String)keys.elementAt(i)).startsWith(prefix)) continue;
407 String prefixPlusOne = ((String)keys.elementAt(i)).substring(0, prefix.length() + 1);
408 if (i<keys.size()-1 && prefixPlusOne.equals((((String)keys.elementAt(i + 1)).substring(0, prefix.length() + 1)))) {
409 out.print("case \'" + prefixPlusOne.charAt(prefixPlusOne.length() - 1) + "\': { ");
410 out.print("switch(ccSwitch"+enumSwitch+".charAt(" + (prefix.length()+1) + ")) { ");
411 buildTrie(prefixPlusOne, cases);
412 out.print("} break; } ");
413 while(i<keys.size() && prefixPlusOne.equals(((String)keys.elementAt(i)).substring(0, prefix.length() + 1))) i++;
414 if (i<keys.size()) { i--; continue; }
416 out.print("case \'" + prefixPlusOne.charAt(prefixPlusOne.length() - 1) + "\': ");
417 String code = (String)cases.get(keys.elementAt(i));
418 code = code.substring(0, code.length());
419 String key = (String)keys.elementAt(i);
420 out.print("if (\""+key+"\".equals(ccSwitch"+enumSwitch+")) { if (true) do { " + code + " } while(false); break SUCCESS; } break; ");
425 private String processLine(String s, boolean deleteLineEndings) throws IOException {
426 if (deleteLineEndings && s.indexOf("//") != -1) s = s.substring(0, s.indexOf("//"));
428 for(int i=0; i<s.length(); i++) {
429 char c = s.charAt(i);
430 if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') {
435 for(j = i; j < s.length(); j++) {
437 if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') break;
439 String tok = s.substring(i, j);
440 Object val = replace.get(tok);
444 } else if (val instanceof JSFunctionMacro) {
445 if (s.charAt(j) != '(') { ret += tok; i = j - 1; continue; }
446 ret += ((JSFunctionMacro)val).process(s.substring(j+1, s.indexOf(')', j)));
447 i = s.indexOf(')', j);
453 if (!deleteLineEndings) ret += "\n";
457 private static int indexOfWS(String s) { return indexOfWS(s, 0); }
458 private static int indexOfWS(String s, int beginIndex) {
459 if (s == null || beginIndex >= s.length()) return -1;
460 for (; beginIndex < s.length(); beginIndex++) {
461 if (s.charAt(beginIndex) == ' ') return beginIndex;
466 private static int indexOfNotWS(String s) { return indexOfWS(s, 0); }
467 private static int indexOfNotWS(String s, int beginIndex) {
468 if (s == null || beginIndex >= s.length()) return -1;
469 for (; beginIndex < s.length(); beginIndex++) {
470 if (s.charAt(beginIndex) != ' ') return beginIndex;
475 public class Warning {
476 protected String msg;
479 public Warning() { msg = ""; }
480 public Warning(String m) { msg = m; if (in != null) line = in.getLineNumber(); }
482 public String toString() { return "WARNING Line "+line+": "+msg; }
485 public class Error extends Warning {
486 public Error() { super(); }
487 public Error(String m) { super(m); }
488 public String toString() { return "ERROR Line "+line+": "+msg; }
491 public static class JSFunctionMacro {
492 public String unbound1 = null;
493 public String unbound2 = null;
494 public String expression = null;
495 public String process(String args) {
496 String bound1 = null;
497 String bound2 = null;
498 if (unbound2 == null) {
500 return replaceAll(expression, unbound1, bound1);
502 bound1 = args.substring(0, args.indexOf(','));
503 bound2 = args.substring(args.indexOf(',') + 1);
504 return replaceAll(replaceAll(expression, unbound1, bound1), unbound2, bound2);