updated Makefile.common
[org.ibex.core.git] / src / org / ibex / util / Preprocessor.java
index 54c2b1c..4698fdc 100644 (file)
@@ -7,6 +7,7 @@
 
 package org.ibex.util;
 
+import gnu.regexp.*;
 import java.util.*;
 import java.io.*;
 
@@ -29,6 +30,16 @@ import java.io.*;
  */
 public class Preprocessor {
 
+    public static String replaceAll(String source, String regexp, String replaceWith) {
+        try {
+            RE re = new RE(regexp, 0, RESyntax.RE_SYNTAX_PERL5);
+            return (String)re.substituteAll(source, replaceWith);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
     public static void main(String[] args) throws Exception {
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
@@ -50,7 +61,7 @@ public class Preprocessor {
     private PrintWriter out;
 
     private Hashtable replace = new Hashtable();
-    private Hashtable repeatreplace = null;
+    private Hashtable[] repeatreplaces = null;
     private Vector sinceLastRepeat = null;
     private Vector err = new Vector();
 
@@ -133,12 +144,18 @@ PROCESS:
                     out.print("\n");  // preserve line numbers
                 }
                 StringTokenizer st = new StringTokenizer(trimmed, " ");
-                repeatreplace = (Hashtable)replace.clone();
+                repeatreplaces = null;
                 while (st.hasMoreTokens()) {
                     String tok = st.nextToken().trim();
                     String key = tok.substring(0, tok.indexOf('/'));
-                    String val = tok.substring(tok.indexOf('/') + 1);
-                    repeatreplace.put(key, val);
+                    String vals = tok.substring(tok.indexOf('/') + 1);
+                    StringTokenizer st2 = new StringTokenizer(vals,"/");
+                    if(repeatreplaces == null) {
+                        repeatreplaces = new Hashtable[st2.countTokens()];
+                        for(int i=0;i<repeatreplaces.length;i++) repeatreplaces[i] = (Hashtable) replace.clone();
+                    }
+                    for(int i=0;st2.hasMoreTokens() && i<repeatreplaces.length;i++)
+                        repeatreplaces[i].put(key, st2.nextToken());
                 }
                 sinceLastRepeat = new Vector();
                 out.print("\n"); // preserve line numbers
@@ -146,13 +163,16 @@ PROCESS:
             } else if (trimmed.startsWith("//#end")) {
                 if (sinceLastRepeat == null) { err.add(new Warning("#end orphaned")); continue PROCESS; }
                 Hashtable save = replace;
-                replace = repeatreplace;
                 out.print("\n");
-                for(int i=0; i<sinceLastRepeat.size() - 1; i++) out.print(processLine((String)sinceLastRepeat.elementAt(i), true));
+                for(int i=0;i<repeatreplaces.length;i++) {
+                    replace = repeatreplaces[i];
+                    for(int j=0; j<sinceLastRepeat.size() - 1; j++) out.print(processLine((String)sinceLastRepeat.elementAt(j), true));
+                }
                 sinceLastRepeat = null;
                 replace = save;
 
-            } else if (trimmed.startsWith("//#switch")) {
+            } else if (trimmed.startsWith("//#switch") || trimmed.startsWith("//#jswitch")) {
+                boolean jswitch =  trimmed.startsWith("//#jswitch");
                 int expStart = trimmed.indexOf('(') +1;
                 if (expStart < 1) { err.add(new Error("expected ( in #switch")); continue PROCESS; }
                 int expEnd = trimmed.lastIndexOf(')');
@@ -160,7 +180,13 @@ PROCESS:
                 if (expEnd - expStart <= 1) { err.add(new Error("badly formed #switch statement")); continue PROCESS; }
                 String expr = trimmed.substring(expStart, expEnd);
 
-                out.print("final String ccSwitch"+enumSwitch+" = (String)("+expr+");  ");
+                if(jswitch) {
+                    out.print("final org.ibex.js.JS ccSwitchExpr"+enumSwitch+" = " + expr + ";");
+                    out.print("if(org.ibex.js.JS.isString(ccSwitchExpr"+enumSwitch+")) {");
+                    out.print("final String ccSwitch"+enumSwitch+" = org.ibex.js.JS.toString(ccSwitchExpr"+enumSwitch+");");
+                } else {
+                    out.print("final String ccSwitch"+enumSwitch+" = "+expr+";  ");
+                }
                 out.print("SUCCESS:do { switch(ccSwitch"+enumSwitch+".length()) {\n");
 
                 Hashtable[] byLength = new Hashtable[255];
@@ -191,7 +217,7 @@ PROCESS:
 
                     if (key != null) {
                         Hashtable hash = byLength[key.length()];
-                        hash.put(key, (String)hash.get(key) + processLine(trimmed, false).replaceAll("//[^\"]*$", ""));
+                        hash.put(key, (String)hash.get(key) + replaceAll(processLine(trimmed, false), "//[^\"]*$", "").trim() + "\n");
                     } else {
                         out.print(processLine(trimmed, false));
                     }
@@ -199,13 +225,14 @@ PROCESS:
 
                 for(int i=0; i<255; i++) {
                     if (byLength[i] == null) continue;
-                    out.print("case " + i + ": { switch(ccSwitch"+enumSwitch+".charAt(0)) {\n");
+                    out.print("case " + i + ": { switch(ccSwitch"+enumSwitch+".charAt(0)) { ");
                     buildTrie("", byLength[i]);
-                    out.print("}; break; }\n");
+                    out.print("}; break; }  ");
                 }
-                out.print("} /* switch */ ");
-                if (Default != null) out.print("\n" + Default);
-                out.print(" } while(false); //OUTER\n");
+                out.print("} "); /* switch */
+                if (Default != null) out.print(" " + Default);
+                out.print(" } while(false);\n"); /* OUTER */
+                if(jswitch) out.print("}");
                 enumSwitch++;
 
             } else {
@@ -237,7 +264,7 @@ PROCESS:
             } else {
                 out.print("case \'" + prefixPlusOne.charAt(prefixPlusOne.length() - 1) + "\': ");
                 String code = (String)cases.get(keys.elementAt(i));
-                code = code.substring(0, code.length() - 1);
+                code = code.substring(0, code.length());
                 String key = (String)keys.elementAt(i);
                 out.print("if (\""+key+"\".equals(ccSwitch"+enumSwitch+")) { if (true) do { " + code + " } while(false); break SUCCESS; } break;  ");
             }
@@ -264,7 +291,7 @@ PROCESS:
                 ret += tok;
                 i = j - 1;
             } else if (val instanceof JSFunctionMacro) {
-                if (s.charAt(j) != '(') { err.add(new Error("open paren must follow macro binding for macro " + tok)); continue; }
+                if (s.charAt(j) != '(') { ret += tok; i = j - 1; continue; }
                 ret += ((JSFunctionMacro)val).process(s.substring(j+1, s.indexOf(')', j)));
                 i = s.indexOf(')', j);
             } else {
@@ -319,11 +346,11 @@ PROCESS:
             String bound2 = null;
             if (unbound2 == null) {
                 bound1 = args;
-                return expression.replaceAll(unbound1, bound1);
+                return replaceAll(expression, unbound1, bound1);
             } else {
                 bound1 = args.substring(0, args.indexOf(','));
                 bound2 = args.substring(args.indexOf(',') + 1);
-                return (expression.replaceAll(unbound1, bound1).replaceAll(unbound2, bound2));
+                return replaceAll(replaceAll(expression, unbound1, bound1), unbound2, bound2);
             }
         }
     }