initial checkin
[org.ibex.nanogoat.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         try {
35             RE re = new RE(regexp, 0, RESyntax.RE_SYNTAX_PERL5);
36             return (String)re.substituteAll(source, replaceWith);
37         } catch (Exception e) {
38             e.printStackTrace();
39             return null;
40         }
41     }
42
43     public static void main(String[] args) throws Exception {
44         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
45         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
46
47         // process stdin to stdout
48         Preprocessor cc = new Preprocessor(br, bw);
49         Vector err = cc.process();
50         bw.flush();
51
52         // handle errors
53         boolean errors = false;
54         for (int i=0; i < err.size(); i++) { if (err.get(i) instanceof Error) errors = true; System.err.println(err.get(i)); }
55         if (errors) throw new Exception();
56     }
57
58     private Reader r;
59     private Writer w;
60     private LineNumberReader in;
61     private PrintWriter out;
62
63     private Hashtable replace = new Hashtable();
64     private Hashtable repeatreplace = null;
65     private Vector sinceLastRepeat = null;
66     private Vector err = new Vector();
67
68     private int enumSwitch = 0; // number appended to variable used in switch implementation
69
70    
71     public Preprocessor(Reader reader, Writer writer) {
72         setReader(reader);
73         setWriter(writer);
74     }
75
76     public void setReader(Reader reader) { r = reader; if (r != null) in = new LineNumberReader(r); }
77     public Reader getReader() { return r; }
78
79     public void setWriter(Writer writer) { w = writer; if (w != null) out = new PrintWriter(w); }
80     public Writer getWriter() { return w; }
81
82
83     /** process data from reader, write to writer, return vector of errors */
84     public Vector process() throws IOException {
85         err.clear();
86
87         String s = null;
88 PROCESS:
89         while((s = in.readLine()) != null) {
90             if (sinceLastRepeat != null) sinceLastRepeat.addElement(s);
91             String trimmed = s.trim();
92
93             if (trimmed.startsWith("//#define ")) {
94                 if (trimmed.length() == 9 || trimmed.charAt(9) != ' ') {
95                     err.add(new Error("#define badly formed, ignored")); continue PROCESS;
96                 }
97                 int keyStart = indexOfNotWS(trimmed, 9);
98                 if (keyStart == -1) {
99                     err.add(new Error("#define requires KEY")); continue PROCESS;
100                 }
101                 int keyEnd = indexOfWS(trimmed, keyStart);
102
103                 int macroStart = trimmed.indexOf('(');
104                 int macroEnd = trimmed.indexOf(')');
105                 if (macroStart > keyEnd) {
106                     // no macro is defined, just make sure something dumb like KEYNA)ME hasn't been done
107                     if (macroEnd < keyEnd) { err.add(new Error("#define key contains invalid char: ')'")); continue PROCESS; }
108                     macroStart = macroEnd = -1;
109                 }
110
111                 if (macroStart == 0) {
112                     err.add(new Error("#define macro requires name")); continue PROCESS;
113                 } else if (macroStart > 0) {
114                     if (macroStart > macroEnd) { err.add(new Error("#define macro badly formed")); continue PROCESS; }
115                     if (macroStart+1 == macroEnd) { err.add(new Error("#define macro requires property name")); continue PROCESS; }
116
117                     JSFunctionMacro fm = new JSFunctionMacro();
118                     String key = trimmed.substring(keyStart, macroStart);
119                     String unbound = trimmed.substring(macroStart +1, macroEnd);
120                     int unboundDiv = unbound.indexOf(',');
121                     if (unboundDiv == -1) {
122                         fm.unbound1 = unbound;
123                     } else {
124                         fm.unbound1 = unbound.substring(0, unboundDiv);
125                         fm.unbound2 = unbound.substring(unboundDiv +1);
126                         if (fm.unbound1.length() == 0) { err.add(new Error("#define macro property 1 requires name")); continue PROCESS; }
127                         if (fm.unbound2.length() == 0) { err.add(new Error("#define macro property 1 requires name")); continue PROCESS; }
128                     }
129                     fm.expression = trimmed.substring(keyEnd).trim();
130                     replace.put(key, fm);
131                 } else {
132                     String key = trimmed.substring(keyStart, keyEnd);
133                     String val = trimmed.substring(keyEnd).trim();
134                     replace.put(key, val);
135                 }
136                 out.print("\n"); // preserve line numbers
137                 
138             } else if (trimmed.startsWith("//#repeat ")) {
139                 trimmed = trimmed.substring(9);
140                 while(trimmed.charAt(trimmed.length() - 1) == '\\') {
141                     String s2 = in.readLine().trim();
142                     if (s2.startsWith("//")) s2 = s2.substring(2).trim();
143                     trimmed = trimmed.substring(0, trimmed.length() - 1) + " " + s2;
144                     out.print("\n");  // preserve line numbers
145                 }
146                 StringTokenizer st = new StringTokenizer(trimmed, " ");
147                 repeatreplace = (Hashtable)replace.clone();
148                 while (st.hasMoreTokens()) {
149                     String tok = st.nextToken().trim();
150                     String key = tok.substring(0, tok.indexOf('/'));
151                     String val = tok.substring(tok.indexOf('/') + 1);
152                     repeatreplace.put(key, val);
153                 }
154                 sinceLastRepeat = new Vector();
155                 out.print("\n"); // preserve line numbers
156
157             } else if (trimmed.startsWith("//#end")) {
158                 if (sinceLastRepeat == null) { err.add(new Warning("#end orphaned")); continue PROCESS; }
159                 Hashtable save = replace;
160                 replace = repeatreplace;
161                 out.print("\n");
162                 for(int i=0; i<sinceLastRepeat.size() - 1; i++) out.print(processLine((String)sinceLastRepeat.elementAt(i), true));
163                 sinceLastRepeat = null;
164                 replace = save;
165
166             } else if (trimmed.startsWith("//#switch")) {
167                 int expStart = trimmed.indexOf('(') +1;
168                 if (expStart < 1) { err.add(new Error("expected ( in #switch")); continue PROCESS; }
169                 int expEnd = trimmed.lastIndexOf(')');
170                 if (expEnd == -1) { err.add(new Error("expected ) in #switch")); continue PROCESS; }
171                 if (expEnd - expStart <= 1) { err.add(new Error("badly formed #switch statement")); continue PROCESS; }
172                 String expr = trimmed.substring(expStart, expEnd);
173
174                 out.print("final String ccSwitch"+enumSwitch+" = (String)("+expr+");  ");
175                 out.print("SUCCESS:do { switch(ccSwitch"+enumSwitch+".length()) {\n");
176
177                 Hashtable[] byLength = new Hashtable[255];
178                 String key = null;
179                 String Default = null;
180                 for(trimmed = in.readLine().trim(); !trimmed.startsWith("//#end"); trimmed = in.readLine().trim()) {
181                     if (trimmed.startsWith("default:")) {
182                         Default = processLine(trimmed.substring(8), false);
183                         continue;
184                     }
185                     if (trimmed.startsWith("case ")) {
186                         // find key
187                         int strStart = trimmed.indexOf('\"') +1;
188                         if (strStart < 1) { err.add(new Error("expected opening of String literal")); continue PROCESS; }
189                         int strEnd = trimmed.indexOf('\"', strStart);
190                         if (strEnd == -1) { err.add(new Error("expected closing of String literal")); continue PROCESS; }
191                         key = trimmed.substring(strStart, strEnd);
192
193                         Hashtable thisCase = (Hashtable)byLength[key.length()];
194                         if (thisCase == null) byLength[key.length()] = thisCase = new Hashtable();
195                         thisCase.put(key, "");
196
197                         // find end of case definition
198                         int caseEnd = trimmed.indexOf(':', strEnd) +1;
199                         if (caseEnd < 1) { err.add(new Error("expected :")); continue PROCESS; }
200                         trimmed = trimmed.substring(caseEnd);
201                     }
202
203                     if (key != null) {
204                         Hashtable hash = byLength[key.length()];
205                         hash.put(key, (String)hash.get(key) + replaceAll(processLine(trimmed, false), "//[^\"]*$", "").trim() + "\n");
206                     } else {
207                         out.print(processLine(trimmed, false));
208                     }
209                 }
210
211                 for(int i=0; i<255; i++) {
212                     if (byLength[i] == null) continue;
213                     out.print("case " + i + ": { switch(ccSwitch"+enumSwitch+".charAt(0)) { ");
214                     buildTrie("", byLength[i]);
215                     out.print("}; break; }  ");
216                 }
217                 out.print("} /* switch */ ");
218                 if (Default != null) out.print(" " + Default);
219                 out.print(" } while(false); /* OUTER */\n");
220                 enumSwitch++;
221
222             } else {
223                 out.print(processLine(s, false));
224             }
225         }
226
227         return err;
228     }
229
230     private void buildTrie(String prefix, Hashtable cases) {
231         Enumeration caseKeys = cases.keys();
232         Vec keys = new Vec();
233         while(caseKeys.hasMoreElements()) keys.addElement(caseKeys.nextElement());
234         keys.sort(new Vec.CompareFunc() { public int compare(Object a, Object b) {
235             return ((String)a).compareTo((String)b);
236         } } );
237
238         for(int i=0; i<keys.size(); i++) {
239             if (!((String)keys.elementAt(i)).startsWith(prefix)) continue;
240             String prefixPlusOne = ((String)keys.elementAt(i)).substring(0, prefix.length() + 1);
241             if (i<keys.size()-1 && prefixPlusOne.equals((((String)keys.elementAt(i + 1)).substring(0, prefix.length() + 1)))) {
242                 out.print("case \'" + prefixPlusOne.charAt(prefixPlusOne.length() - 1) + "\': { ");
243                 out.print("switch(ccSwitch"+enumSwitch+".charAt(" + (prefix.length()+1) + ")) { ");
244                 buildTrie(prefixPlusOne, cases);
245                 out.print("} break; } ");
246                 while(i<keys.size() && prefixPlusOne.equals(((String)keys.elementAt(i)).substring(0, prefix.length() + 1))) i++;
247                 if (i<keys.size()) { i--; continue; }
248             } else {
249                 out.print("case \'" + prefixPlusOne.charAt(prefixPlusOne.length() - 1) + "\': ");
250                 String code = (String)cases.get(keys.elementAt(i));
251                 code = code.substring(0, code.length());
252                 String key = (String)keys.elementAt(i);
253                 out.print("if (\""+key+"\".equals(ccSwitch"+enumSwitch+")) { if (true) do { " + code + " } while(false); break SUCCESS; } break;  ");
254             }
255         }
256     }
257
258     private String processLine(String s, boolean deleteLineEndings) throws IOException {
259         if (deleteLineEndings && s.indexOf("//") != -1) s = s.substring(0, s.indexOf("//"));
260         String ret = "";
261         for(int i=0; i<s.length(); i++) {
262             char c = s.charAt(i);
263             if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') {
264                 ret += c;
265                 continue;
266             }
267             int j;
268             for(j = i; j < s.length(); j++) {
269                 c = s.charAt(j);
270                 if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') break;
271             }
272             String tok = s.substring(i, j);
273             Object val = replace.get(tok);
274             if (val == null) {
275                 ret += tok;
276                 i = j - 1;
277             } else if (val instanceof JSFunctionMacro) {
278                 if (s.charAt(j) != '(') { err.add(new Error("open paren must follow macro binding for macro " + tok)); continue; }
279                 ret += ((JSFunctionMacro)val).process(s.substring(j+1, s.indexOf(')', j)));
280                 i = s.indexOf(')', j);
281             } else {
282                 ret += val;
283                 i = j - 1;
284             }
285         }
286         if (!deleteLineEndings) ret += "\n";
287         return ret;
288     }
289
290     private static int indexOfWS(String s) { return indexOfWS(s, 0); }
291     private static int indexOfWS(String s, int beginIndex) {
292         if (s == null || beginIndex >= s.length()) return -1;
293         for (; beginIndex < s.length(); beginIndex++) {
294             if (s.charAt(beginIndex) == ' ') return beginIndex;
295         }
296         return s.length();
297     }
298
299     private static int indexOfNotWS(String s) { return indexOfWS(s, 0); }
300     private static int indexOfNotWS(String s, int beginIndex) {
301         if (s == null || beginIndex >= s.length()) return -1;
302         for (; beginIndex < s.length(); beginIndex++) {
303             if (s.charAt(beginIndex) != ' ') return beginIndex;
304         }
305         return -1;
306     }
307
308     public class Warning {
309         protected String msg;
310         protected int line;
311
312         public Warning() { msg = ""; }
313         public Warning(String m) { msg = m; if (in != null) line = in.getLineNumber(); }
314
315         public String toString() { return "WARNING Line "+line+": "+msg; }
316     }
317
318     public class Error extends Warning {
319         public Error() { super(); }
320         public Error(String m) { super(m); }
321         public String toString() { return "ERROR Line "+line+": "+msg; }
322     }
323
324     public static class JSFunctionMacro {
325         public String unbound1 = null;
326         public String unbound2 = null;
327         public String expression = null;
328         public String process(String args) {
329             String bound1 = null;
330             String bound2 = null;
331             if (unbound2 == null) {
332                 bound1 = args;
333                 return replaceAll(expression, unbound1, bound1);
334             } else {
335                 bound1 = args.substring(0, args.indexOf(','));
336                 bound2 = args.substring(args.indexOf(',') + 1);
337                 return replaceAll(replaceAll(expression, unbound1, bound1), unbound2, bound2);
338             }
339         }
340     }
341 }
342