2003/08/10 06:03:02
[org.ibex.core.git] / src / org / xwt / util / Preprocessor.java
diff --git a/src/org/xwt/util/Preprocessor.java b/src/org/xwt/util/Preprocessor.java
new file mode 100644 (file)
index 0000000..b653c46
--- /dev/null
@@ -0,0 +1,87 @@
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.xwt.util;
+
+import java.util.*;
+import java.io.*;
+
+/**
+ *   A VERY crude, inefficient Java preprocessor
+ *
+ *   //#define FOO bar baz       -- replace all instances of token FOO with "bar baz"
+ *   //#replace foo/bar baz/bop  -- DUPLICATE everything between here and //#end,
+ *                                  replacing foo with bar and baz with bop in the *second* copy
+ *
+ *   Replacements are done on a token basis.  Tokens are defined as a
+ *   sequence of characters which all belong to a single class.  The
+ *   two character classes are:
+ *
+ *     - [a-zA-Z0-9_]
+ *     - all other non-whitespace characters
+ */
+public class Preprocessor {
+
+    static Hashtable replace = new Hashtable();
+    static Hashtable savereplace = replace;
+    static Vector sinceLastRepeat = null;
+
+    public static void main(String[] args) throws IOException {
+        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
+        String s = null;
+
+        while((s = br.readLine()) != null) {
+            if (sinceLastRepeat != null) sinceLastRepeat.addElement(s);
+            String trimmed = s.trim();
+            if (trimmed.startsWith("//#define ")) {
+                trimmed = trimmed.substring(10).trim();
+                String key = trimmed.substring(0, trimmed.indexOf(' '));
+                String val = trimmed.substring(trimmed.indexOf(' ')).trim();
+                replace.put(key, val);
+                
+            } else if (trimmed.startsWith("//#repeat ")) {
+                StringTokenizer st = new StringTokenizer(trimmed.substring(9), " ");
+                savereplace = replace;
+                replace = (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);
+                }
+                sinceLastRepeat = new Vector();
+
+            } else if (trimmed.startsWith("//#end")) {
+                replace = savereplace;
+                System.out.println();
+                for(int i=0; i<sinceLastRepeat.size(); i++) processLine((String)sinceLastRepeat.elementAt(i));
+                sinceLastRepeat = null;
+
+            } else {
+                processLine(s);
+            }
+           
+        }
+
+    }
+
+    static void processLine(String s) throws IOException {
+        for(int i=0; i<s.length(); i++) {
+            char c = s.charAt(i);
+            if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') {
+                System.out.print(c);
+                continue;
+            }
+            int j;
+            for(j = i; j < s.length(); j++) {
+                c = s.charAt(j);
+                if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_') break;
+            }
+            String tok = s.substring(i, j);
+            String val = (String)replace.get(tok);
+            if (val != null) System.out.print(val);
+            else System.out.print(tok);
+            i = j - 1;
+        }
+        System.out.println();
+    }
+}
+