checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / MetaGrammar.java
index 900ee3f..53aeff0 100644 (file)
@@ -48,19 +48,17 @@ public class MetaGrammar extends ReflectiveWalker {
     }
     // MetaGrammar //////////////////////////////////////////////////////////////////////////////
 
+
+    public PreSequence _amp_(PreSequence p, Object[] o)        { return p.and(new PreSequence(o, null, true).buildSequence(null, true, false)); }
+    public PreSequence _amp__tilde_(PreSequence p, Object[] o) { return p.not(new PreSequence(o, null, true).buildSequence(null, true, false)); }
+    public PreSequence rewrite(Object[] o)                                            { return new PreSequence(o, null); }
     public PreSequence _equals__greater_(Object[] o, String s)                        { return new PreSequence(o, s); }
     public Object      _star_(Element r)                                              { return Repeat.many0(r); }
     public Element     epsilon(Object o, Object b)                                    { return epsilon; }
     public Element     _rightparen_(Object e)                                         { return SELF; }
-
-    public PreSequence _amp_(PreSequence p, Object[] o)        { return p.and(new PreSequence(o, null, true).buildSequence(null, true, false)); }
-    public PreSequence _amp__tilde_(PreSequence p, Object[] o) { return p.not(new PreSequence(o, null, true).buildSequence(null, true, false)); }
-
-    public Element     _bang_(Element r)                       { return r; }
-    public Object      care(String s)                          { return new MyLift(s); }
-    public Object      _caret_(String s)                       { return new MyLift(s); }
-    public Object      _leftparen__rightparen_()               { return epsilon; }
-        
+    public Element     _bang_(Element r)                                              { return r; }
+    public Object      _caret_(String s)                                              { return new MyLift(s); }
+    public Object      _leftparen__rightparen_()                                      { return epsilon; }
     public Union       nonTerminal(String s)                                          { return nonTerminal(s, null, false, false); }
     public Union       _colon__colon__equals_(String s, PreSequence[][] p)            { return nonTerminal(s, p, false, false); }
     public Union       _bang__colon__colon__equals_(String s, PreSequence[][] p)      { return nonTerminal(s, p, false, true); }
@@ -125,9 +123,6 @@ public class MetaGrammar extends ReflectiveWalker {
         return CharToken.set(ret);
     }
 
-    public PreSequence wrap(Object[] o)    { return new PreSequence(o, ""); }
-    public PreSequence rewrite(Object[] o) { return new PreSequence(o, null); }
-
     public static class MyLift {
         public final String s;
         public MyLift(String s) { this.s = s; }
@@ -149,25 +144,6 @@ public class MetaGrammar extends ReflectiveWalker {
         public PreSequence(Object[] o, String tag) { this(o, tag, false); }
         public PreSequence(Object[] o, String tag, boolean keeper) { this.o = o; this.tag = tag; this.keeper = keeper; }
         boolean[] drops = null;
-        public Element[] expand(Union u, HashSet<Sequence> set) {
-            if (o==null) return new Element[0];
-            Element[] o2 = new Element[o.length];
-            drops = new boolean[o.length];
-            int j = 0;
-            for(int i=0; i<o.length; i++) {
-                Object oi = o[i];
-                if (oi instanceof MyDrop)      { oi = ((MyDrop)oi).o; drops[j] = true; }
-                if (oi==SELF)                    o2[j] = u.new Subset("(("+u+"))", set);
-                else if (oi instanceof MyLift) { o2[j] = CharToken.string(tag = ((MyLift)oi).s); drops[j] = true; }
-                else                             o2[j] = (Element)oi;
-
-                if (dropAll.contains(o2[j])) drops[j] = true;
-
-                o2[j] = o2[j];
-                j++;
-            }
-            return o2;
-        }
         public Union    buildUnion() {
             Union u = new Union("???");
             u.add(buildSequence(u));
@@ -178,7 +154,22 @@ public class MetaGrammar extends ReflectiveWalker {
             for(Sequence s : and) u.add(s);
             for(Sequence s : not) u.add(s);
             HashSet<Sequence> set = new HashSet<Sequence>();
-            Element[] expansion = expand(u, set);
+            Element[] o2 = o==null ? new Element[0] : new Element[o.length];
+            if (o != null) {
+                drops = new boolean[o.length];
+                int j = 0;
+                for(int i=0; i<o.length; i++) {
+                    Object oi = o[i];
+                    if (oi instanceof MyDrop)      { oi = ((MyDrop)oi).o; drops[j] = true; }
+                    if (oi==SELF)                    o2[j] = u.new Subset("(("+u+"))", set);
+                    else if (oi instanceof MyLift) { o2[j] = CharToken.string(tag = ((MyLift)oi).s); drops[j] = true; }
+                    else                             o2[j] = (Element)oi;
+                    if (MetaGrammar.dropAll.contains(o2[j])) drops[j] = true;
+                    o2[j] = o2[j];
+                    j++;
+                }
+            }
+            Element[] expansion = o2;
             boolean keeper = this.keeper;
             Sequence ret = dropAll || lame || keeper ? Sequence.drop(expansion, and, not, lame) : null;
             if (ret==null && tag!=null) {