2004/01/08 05:32:44
[org.ibex.core.git] / src / org / xwt / util / Grammar.java
index 678a63a..48018c7 100644 (file)
@@ -14,25 +14,28 @@ public abstract class Grammar extends JS {
         throw new Error("this should never happen");
     }
 
+    private static Object NULL = new Object();
+    
     public abstract int match(String s, int start, Hash v, JSScope scope) throws JSExn;
     public int matchAndWrite(final String s, final int start, Hash v, JSScope scope, String key) throws JSExn {
         final Hash v2 = new Hash();
         final int ret = match(s, start, v2, scope);
-        if (ret == -1) return -1;
-        Object result = action == null ?
+        Object result = ret == -1 ? NULL : action == null ?
             s.substring(start, ret) :
             action.cloneWithNewParentScope(new JSScope(scope) {
                     public Object get(Object key) throws JSExn {
-                        if (v2.get(key) != null) return v2.get(key);
+                        Object val = v2.get(key);
+                        if (val == NULL) return null;
+                        if (val != null) return val;
                         if (key.equals("whole")) return s.substring(start, ret);
                         return super.get(key);
                     }
                 }).call(null, null, null, null, 0);
         if (key != null) {
             Object old = v.get(key);
-            if (old == null) { }
-            else if (old instanceof JSArray) { ((JSArray)old).addElement(result); result = old; }
-            else { JSArray j = new JSArray(); j.addElement(old); j.addElement(result); result = j; }
+            if (old == null || old == NULL) { }
+            else if (old instanceof JSArray) { if (result != NULL) { ((JSArray)old).addElement(result); result = old; } }
+            else if (result != NULL) { JSArray j = new JSArray(); j.addElement(old); j.addElement(result); result = j; }
             v.put(key, result);
         }
         return ret;