checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / MetaGrammar.java
index bc171da..1a79a1d 100644 (file)
@@ -54,7 +54,6 @@ public class MetaGrammar extends ReflectiveWalker {
 
     public PreSequence _amp_(PreSequence p, Object[] o)        { return p.and(new PreSequence(o, null).buildSequence(null, true, false, true)); }
     public PreSequence _amp__tilde_(PreSequence p, Object[] o) { return p.not(new PreSequence(o, null).buildSequence(null, true, false, true)); }
-    public Object      _star_(Element r)                                              { return Repeat.many0(r); }
     public Element     epsilon(Object o, Object b)                                    { return Union.epsilon; }
     public Object      _leftparen__rightparen_(Object o)                              { return Union.epsilon; }
     public Element     _rightparen_(Object e)                                         { return SELF; }
@@ -99,7 +98,6 @@ public class MetaGrammar extends ReflectiveWalker {
                            false);
     }
 
-    public Object   _plus_(final Element r)                    { return Repeat.many1(r); }
     public PreSequence rewrite(Object[] o)                     { return new PreSequence(o, null); }
 
     public PreSequence seqx(PreSequence p, String tag) { return _equals__greater_(p, tag); }
@@ -111,17 +109,20 @@ public class MetaGrammar extends ReflectiveWalker {
     public PreSequence ps(Object[] o) { return new PreSequence(o); }
     public PreSequence _slash_(PreSequence p, Object sep) { return p.sparse(sep); }
 
+    public Object      _star_(Element r)                    { return Repeat.many0(r); }
+    public Object   _plus_(final Element r)                 { return Repeat.many1(r); }
     public Object _plus__slash_(final Element r, Element s) { return Repeat.many1(r, s); }
     public Object _star__slash_(final Element r, Element s) { return Repeat.many0(r, s); }
-    public Object _star__star_(final Element r) { return Repeat.maximal(Repeat.many0(r)); }
-    public Object _plus__plus_(final Element r) { return Repeat.maximal(Repeat.many1(r)); }
-    public Element _question_(final Element r) { return Repeat.maybe(r); }
+    public Object _star__star_(final Element r)             { return Repeat.maximal(Repeat.many0(r)); }
+    public Object _plus__plus_(final Element r)             { return Repeat.maximal(Repeat.many1(r)); }
+    public Element _question_(final Element r)              { return Repeat.maybe(r); }
+
     public MetaGrammar gram(Object o, MetaGrammar g, Object o2) { return g; }
     public MetaGrammar  grammar(Object[] o) { return this; }
     public MetaGrammar grammar(Object o, Union[] u, Object x) { return this; }
     public char    _backslash_n() { return '\n'; }
     public char    _backslash_r() { return '\r'; }
-    public Object    literal(String s) { return new MyDrop(string(s)); }
+    public Element    literal(String s) { Element ret = string(s); dropAll.add(ret); return ret; }
     public Range     _minus_(char a, char b) { return new Range(a, b); }
     public Range     range(char a)         { return new Range(a, a); }
     public Element   _leftbracket_       (Range[] rr) { return ranges(true, rr); }
@@ -139,10 +140,6 @@ public class MetaGrammar extends ReflectiveWalker {
         public final String s;
         public MyLift(String s) { this.s = s; }
     }
-    public class MyDrop {
-        public final Object o;
-        public MyDrop(Object o) { this.o = o; }
-    }
 
     public class PreSequence {
         public final HashSet<Sequence> and  = new HashSet<Sequence>();
@@ -152,16 +149,25 @@ public class MetaGrammar extends ReflectiveWalker {
 
         public PreSequence sparse(Object e) {
             Object[] ret;
-            if (o.length <= 1) ret = o;
-            else {
+            boolean[] drops;
+            if (o.length <= 1) {
+                ret = o;
+                drops = new boolean[this.drops.length];
+                System.arraycopy(this.drops, 0, drops, 0, this.drops.length);
+            } else {
                 ret = new Object[o.length * 2 - 1];
+                drops = new boolean[o.length * 2 - 1];
                 for(int i=0; i<o.length; i++) {
                     Object oi = o[i];
                     ret[i*2]   = oi;
-                    if (i*2+1<ret.length) ret[i*2+1] = new MyDrop(e);
+                    drops[i*2] = this.drops[i];
+                    if (i*2+1<ret.length) {
+                        ret[i*2+1] = e;
+                        drops[i*2+1] = true;
+                    }
                 }
             }
-            PreSequence p = new PreSequence(ret, tag);
+            PreSequence p = new PreSequence(ret, tag, drops);
             p.not.addAll(not);
             p.and.addAll(and);
             return p;
@@ -170,9 +176,15 @@ public class MetaGrammar extends ReflectiveWalker {
         public PreSequence not(Sequence s) { not.add(s); return this; }
         public PreSequence and(Sequence s) { and.add(s); return this; }
 
+        boolean[] drops = null;
         public PreSequence(Object o) { this(new Object[] { o }, null); }
         public PreSequence(Object[] o) { this(o, null); }
-        public PreSequence(Object[] o, String tag) { this.o = o; this.tag = tag; }
+        public PreSequence(Object[] o, String tag) { this(o, tag, null); }
+        public PreSequence(Object[] o, String tag, boolean[] drops) {
+            this.o = o;
+            this.tag = tag;
+            this.drops = drops==null ? new boolean[o.length] : drops;
+        }
 
         public Union    buildUnion() {
             Union u = new Union("???");
@@ -186,14 +198,10 @@ public class MetaGrammar extends ReflectiveWalker {
             HashSet<Sequence> set = new HashSet<Sequence>();
             Element[] o2 = o==null ? new Element[0] : new Element[o.length];
             int nonDrop = 0;
-            boolean[] drops = null;
             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] = string(tag = ((MyLift)oi).s); drops[j] = o.length>1; }
                     else                             o2[j] = (Element)oi;