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
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) {
35 RE re = new RE(regexp, 0, RESyntax.RE_SYNTAX_PERL5);
36 return (String)re.substituteAll(source, replaceWith);
37 } catch (Exception e) {
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));
47 // process stdin to stdout
48 Preprocessor cc = new Preprocessor(br, bw);
49 Vector err = cc.process();
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();
60 private LineNumberReader in;
61 private PrintWriter out;
63 private Hashtable replace = new Hashtable();
64 private Hashtable repeatreplace = null;
65 private Vector sinceLastRepeat = null;
66 private Vector err = new Vector();
68 private int enumSwitch = 0; // number appended to variable used in switch implementation
71 public Preprocessor(Reader reader, Writer writer) {
76 public void setReader(Reader reader) { r = reader; if (r != null) in = new LineNumberReader(r); }
77 public Reader getReader() { return r; }
79 public void setWriter(Writer writer) { w = writer; if (w != null) out = new PrintWriter(w); }
80 public Writer getWriter() { return w; }
83 /** process data from reader, write to writer, return vector of errors */
84 public Vector process() throws IOException {
89 while((s = in.readLine()) != null) {
90 if (sinceLastRepeat != null) sinceLastRepeat.addElement(s);
91 String trimmed = s.trim();
93 if (trimmed.startsWith("//#define ")) {
94 if (trimmed.length() == 9 || trimmed.charAt(9) != ' ') {
95 err.add(new Error("#define badly formed, ignored")); continue PROCESS;
97 int keyStart = indexOfNotWS(trimmed, 9);
99 err.add(new Error("#define requires KEY")); continue PROCESS;
101 int keyEnd = indexOfWS(trimmed, keyStart);
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;
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; }
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;
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; }
129 fm.expression = trimmed.substring(keyEnd).trim();
130 replace.put(key, fm);
132 String key = trimmed.substring(keyStart, keyEnd);
133 String val = trimmed.substring(keyEnd).trim();
134 replace.put(key, val);
136 out.print("\n"); // preserve line numbers
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
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);
154 sinceLastRepeat = new Vector();
155 out.print("\n"); // preserve line numbers
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;
162 for(int i=0; i<sinceLastRepeat.size() - 1; i++) out.print(processLine((String)sinceLastRepeat.elementAt(i), true));
163 sinceLastRepeat = null;
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);
174 out.print("final String ccSwitch"+enumSwitch+" = (String)("+expr+"); ");
175 out.print("SUCCESS:do { switch(ccSwitch"+enumSwitch+".length()) {\n");
177 Hashtable[] byLength = new Hashtable[255];
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);
185 if (trimmed.startsWith("case ")) {
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);
193 Hashtable thisCase = (Hashtable)byLength[key.length()];
194 if (thisCase == null) byLength[key.length()] = thisCase = new Hashtable();
195 thisCase.put(key, "");
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);
204 Hashtable hash = byLength[key.length()];
205 hash.put(key, (String)hash.get(key) + replaceAll(processLine(trimmed, false), "//[^\"]*$", "").trim() + "\n");
207 out.print(processLine(trimmed, false));
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; } ");
217 out.print("} /* switch */ ");
218 if (Default != null) out.print(" " + Default);
219 out.print(" } while(false); /* OUTER */\n");
223 out.print(processLine(s, false));
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);
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; }
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; ");
258 private String processLine(String s, boolean deleteLineEndings) throws IOException {
259 if (deleteLineEndings && s.indexOf("//") != -1) s = s.substring(0, s.indexOf("//"));
261 for(int i=0; i<s.length(); i++) {
262 char c = s.charAt(i);
263 if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') {
268 for(j = i; j < s.length(); j++) {
270 if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') break;
272 String tok = s.substring(i, j);
273 Object val = replace.get(tok);
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);
286 if (!deleteLineEndings) ret += "\n";
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;
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;
308 public class Warning {
309 protected String msg;
312 public Warning() { msg = ""; }
313 public Warning(String m) { msg = m; if (in != null) line = in.getLineNumber(); }
315 public String toString() { return "WARNING Line "+line+": "+msg; }
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; }
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) {
333 return replaceAll(expression, unbound1, bound1);
335 bound1 = args.substring(0, args.indexOf(','));
336 bound2 = args.substring(args.indexOf(',') + 1);
337 return replaceAll(replaceAll(expression, unbound1, bound1), unbound2, bound2);