89f4e4e356818909285aa8617c41c59a7b6a7d9a
[org.ibex.util.git] / src / org / ibex / util / Preprocessor.java
1 // Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
2 //
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
6 // relink clause")
7
8 package org.ibex.util;
9
10 //import gnu.regexp.*;
11 import java.util.*;
12 import java.io.*;
13
14 /**
15  *   A VERY crude, inefficient Java preprocessor
16  *
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
21  *       case "case1":
22  *   //#end
23  *
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:
27  *
28  *     - [a-zA-Z0-9_]
29  *     - all other non-whitespace characters
30  */
31 public class Preprocessor {
32
33     public static String replaceAll(String source, String regexp, String replaceWith) {
34         return source.replaceAll(regexp, replaceWith);
35         /*
36         try {
37             RE re = new RE(regexp, 0, RESyntax.RE_SYNTAX_PERL5);
38             return (String)re.substituteAll(source, replaceWith);
39         } catch (Exception e) {
40             e.printStackTrace();
41             return null;
42         }
43         */
44     }
45
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));
49
50         // process stdin to stdout
51         Preprocessor cc = new Preprocessor(br, bw);
52         Vector err = cc.process();
53         bw.flush();
54
55         // handle errors
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();
59     }
60
61     private Reader r;
62     private Writer w;
63     private LineNumberReader in;
64     private PrintWriter out;
65
66     private Hashtable replace = new Hashtable();
67     private Hashtable repeatreplace = null;
68     private Vector sinceLastRepeat = null;
69     private Vector err = new Vector();
70
71     private int enumSwitch = 0; // number appended to variable used in switch implementation
72
73    
74     public Preprocessor(Reader reader, Writer writer) {
75         setReader(reader);
76         setWriter(writer);
77     }
78
79     public void setReader(Reader reader) { r = reader; if (r != null) in = new LineNumberReader(r); }
80     public Reader getReader() { return r; }
81
82     public void setWriter(Writer writer) { w = writer; if (w != null) out = new PrintWriter(w); }
83     public Writer getWriter() { return w; }
84
85
86     /** process data from reader, write to writer, return vector of errors */
87     public Vector process() throws IOException {
88         err.clear();
89
90         String s = null;
91 PROCESS:
92         while((s = in.readLine()) != null) {
93             if (sinceLastRepeat != null) sinceLastRepeat.addElement(s);
94             String trimmed = s.trim();
95
96             if (trimmed.startsWith("//#define ")) {
97                 if (trimmed.length() == 9 || trimmed.charAt(9) != ' ') {
98                     err.add(new Error("#define badly formed, ignored")); continue PROCESS;
99                 }
100                 int keyStart = indexOfNotWS(trimmed, 9);
101                 if (keyStart == -1) {
102                     err.add(new Error("#define requires KEY")); continue PROCESS;
103                 }
104                 int keyEnd = indexOfWS(trimmed, keyStart);
105
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;
112                 }
113
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; }
119
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;
126                     } else {
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; }
131                     }
132                     fm.expression = trimmed.substring(keyEnd).trim();
133                     replace.put(key, fm);
134                 } else {
135                     String key = trimmed.substring(keyStart, keyEnd);
136                     String val = trimmed.substring(keyEnd).trim();
137                     replace.put(key, val);
138                 }
139                 out.print("\n"); // preserve line numbers
140                 
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
148                 }
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);
156                 }
157                 sinceLastRepeat = new Vector();
158                 out.print("\n"); // preserve line numbers
159
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;
164                 out.print("\n");
165                 for(int i=0; i<sinceLastRepeat.size() - 1; i++) out.print(processLine((String)sinceLastRepeat.elementAt(i), true));
166                 sinceLastRepeat = null;
167                 replace = save;
168
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);
176
177                 out.print("final String ccSwitch"+enumSwitch+" = (String)("+expr+");  ");
178                 out.print("SUCCESS:do { switch(ccSwitch"+enumSwitch+".length()) {\n");
179
180                 Hashtable[] byLength = new Hashtable[255];
181                 String key = null;
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);
186                         continue;
187                     }
188                     if (trimmed.startsWith("case ")) {
189                         // find key
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);
195
196                         Hashtable thisCase = (Hashtable)byLength[key.length()];
197                         if (thisCase == null) byLength[key.length()] = thisCase = new Hashtable();
198                         thisCase.put(key, "");
199
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);
204                     }
205
206                     if (key != null) {
207                         Hashtable hash = byLength[key.length()];
208                         hash.put(key, (String)hash.get(key) + replaceAll(processLine(trimmed, false), "//[^\"]*$", "").trim() + "\n");
209                     } else {
210                         out.print(processLine(trimmed, false));
211                     }
212                 }
213
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; }  ");
219                 }
220                 out.print("} /* switch */ ");
221                 if (Default != null) out.print(" " + Default);
222                 out.print(" } while(false); /* OUTER */\n");
223                 enumSwitch++;
224
225             } else {
226                 out.print(processLine(s, false));
227             }
228         }
229
230         return err;
231     }
232
233     private static class MyVec {
234         private Object[] store;
235         private int size = 0;
236     
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; }
240     
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);
245             store = newstore;
246         }
247
248         public void removeAllElements() {
249             for(int i=0; i<size; i++) store[i] = null;
250             size = 0;
251         }
252
253         public void toArray(Object[] o) {
254             for(int i=0; i<size; i++)
255                 o[i] = store[i];
256         }
257     
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;
261         
262             return -1;
263         }
264     
265         public void addElement(Object o) {
266             if (size >= store.length - 1) grow();
267             store[size++] = o;
268         }
269
270         public Object peek() {
271             return lastElement();
272         }
273
274         public Object elementAt(int i) {
275             return store[i];
276         }
277     
278         public Object lastElement() {
279             if (size == 0) return null;
280             return store[size - 1];
281         }
282
283         public void push(Object o) { addElement(o); }
284         public Object pop() {
285             Object ret = lastElement();
286             if (size > 0) store[size--] = null;
287             return ret;
288         }
289
290         public int size() { return size; }
291
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);
295             if (newSize < size)
296                 for(int i=newSize; i<size; i++)
297                     store[i] = null;
298             size = newSize;
299         }
300
301         public void copyInto(Object[] out) {
302             for(int i=0; i<size; i++)
303                 out[i] = store[i];
304         }
305
306         public void fromArray(Object[] in) {
307             setSize(in.length);
308             for(int i=0; i<size; i++)
309                 store[i] = in[i];
310         }
311
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];
316             setSize(size - 1);
317         }
318
319         public void setElementAt(Object o, int i) {
320             if (i >= size) setSize(i);
321             store[i] = o;
322         }
323
324         public void removeElement(Object o) {
325             int idx = indexOf(o);
326             if (idx != -1) removeElementAt(idx);
327         }
328
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];
333             store[at] = o;
334             size++;
335         }
336
337         public interface CompareFunc {
338             public int compare(Object a, Object b);
339         }
340
341         public void sort(CompareFunc c) {
342             sort(this, null, c, 0, size-1);
343         }
344
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);
348         }
349
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;
353             if(end-start <= 6) {
354                 for(int i=start+1;i<=end;i++) {
355                     tmpa = a.store[i];
356                     if (b != null) tmpb = b.store[i];
357                     int j;
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];
362                     }
363                     a.store[j+1] = tmpa;
364                     if (b != null) b.store[j+1] = tmpb;
365                 }
366                 return;
367             }
368
369             Object pivot = a.store[end];
370             int lo = start - 1;
371             int hi = end;
372
373             do {
374                 while(c.compare(a.store[++lo],pivot) < 0) { }
375                 while((hi > lo) && c.compare(a.store[--hi],pivot) > 0) { }
376                 swap(a, lo,hi);
377                 if (b != null) swap(b, lo,hi);
378             } while(lo < hi);
379
380             swap(a, lo,end);
381             if (b != null) swap(b, lo,end);
382
383             sort(a, b, c, start, lo-1);
384             sort(a, b, c, lo+1, end);
385         }
386
387         private static final void swap(MyVec vec, int a, int b) {
388             if(a != b) {
389                 Object tmp = vec.store[a];
390                 vec.store[a] = vec.store[b];
391                 vec.store[b] = tmp;
392             }
393         }
394
395     }
396
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);
403         } } );
404
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; }
415             } else {
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;  ");
421             }
422         }
423     }
424
425     private String processLine(String s, boolean deleteLineEndings) throws IOException {
426         if (deleteLineEndings && s.indexOf("//") != -1) s = s.substring(0, s.indexOf("//"));
427         String ret = "";
428         for(int i=0; i<s.length(); i++) {
429             char c = s.charAt(i);
430             if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') {
431                 ret += c;
432                 continue;
433             }
434             int j;
435             for(j = i; j < s.length(); j++) {
436                 c = s.charAt(j);
437                 if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') break;
438             }
439             String tok = s.substring(i, j);
440             Object val = replace.get(tok);
441             if (val == null) {
442                 ret += tok;
443                 i = j - 1;
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);
448             } else {
449                 ret += val;
450                 i = j - 1;
451             }
452         }
453         if (!deleteLineEndings) ret += "\n";
454         return ret;
455     }
456
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;
462         }
463         return s.length();
464     }
465
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;
471         }
472         return -1;
473     }
474
475     public class Warning {
476         protected String msg;
477         protected int line;
478
479         public Warning() { msg = ""; }
480         public Warning(String m) { msg = m; if (in != null) line = in.getLineNumber(); }
481
482         public String toString() { return "WARNING Line "+line+": "+msg; }
483     }
484
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; }
489     }
490
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) {
499                 bound1 = args;
500                 return replaceAll(expression, unbound1, bound1);
501             } else {
502                 bound1 = args.substring(0, args.indexOf(','));
503                 bound2 = args.substring(args.indexOf(',') + 1);
504                 return replaceAll(replaceAll(expression, unbound1, bound1), unbound2, bound2);
505             }
506         }
507     }
508 }
509