checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammarBindings.java
index 3790227..9232d5c 100644 (file)
@@ -68,7 +68,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             Atom ret = null;
             for(Seq[] ss : sequences)
                 for(Seq s : ss)
-                    ret = ret==null ? s.toAtom(cx) : infer(ret.union(s.toAtom(cx)));
+                    ret = ret==null ? s.toAtom(cx) : (Atom)ret.union(s.toAtom(cx));
             return ret;
         }
         public void build(Context cx, Union u, NonTerminalNode cnt) {
@@ -81,10 +81,10 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
                     group[j].build(cx, u2, cnt);
                 }
                 if (sequences.length==1) break;
-                Sequence seq = Sequence.singleton(u2);
+                Sequence seq = Sequence.create(u2);
                 for(Sequence s : bad2) seq = seq.not(s);
                 u.add(seq);
-                bad2.add(Sequence.singleton(u2));
+                bad2.add(Sequence.create(u2));
             }
         }
     }
@@ -128,11 +128,11 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             HashSet<Sequence> bad2 = new HashSet<Sequence>();
 
             Union urep = new Union(null, false);
-            urep.add(Sequence.empty);
+            urep.add(Sequence.create());
             if (sep != null)
-                urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
+                urep.add(Sequence.create(new Element[] { cx.get(sep), u }, 1));
             else
-                urep.add(Sequence.singleton(new Element[] { u }, 0));
+                urep.add(Sequence.create(new Element[] { u }, 0));
 
             for(int i=0; i<sequences.length; i++) {
                 Seq[] group = sequences[i];
@@ -141,16 +141,17 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
                 for(int j=0; j<group.length; j++) {
                     Union u3 = new Union(null, false);
                     group[j].build(cx, u3, this);
-                    Sequence s = Sequence.unwrap(new Element[] { u3, urep },
-                                                 cx.rm.repeatTag(),
-                                                 new boolean[] { false, false });
+                    Sequence s = Sequence.create(cx.rm.repeatTag(),
+                                                 new Element[] { u3, urep },
+                                                 new boolean[] { false, false },
+                                                 true);
                     u2.add(s);
                 }
                 if (sequences.length==1) break;
-                Sequence seq = Sequence.singleton(u2);
+                Sequence seq = Sequence.create(u2);
                 for(Sequence s : bad2) seq = seq.not(s);
                 u.add(seq);
-                bad2.add(Sequence.singleton(u2));
+                bad2.add(Sequence.create(u2));
             }
         }
     }
@@ -229,8 +230,6 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             return ret;
         }
         public Sequence build0(Context cx, NonTerminalNode cnt) {
-            boolean dropAll = false;
-            if (tag!=null && tag.endsWith("()")) dropAll = true;
             boolean[] drops = new boolean[elements.length];
             Element[] els = new Element[elements.length];
             for(int i=0; i<elements.length; i++) {
@@ -240,22 +239,19 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
                     tag = elements[i].getOwnerTag();
             }
             Sequence ret = null;
-            if (dropAll)     ret = Sequence.drop(els);
-            else {
-                Production prod = new Production(tag, (cnt==null?null:cnt.name), els, drops);
-                ret = cx.rm.createSequence(prod);
-                if (ret == null) {
-                    int idx = -1;
-                    for(int i=0; i<els.length; i++)
-                        if (!drops[i])
-                            if (idx==-1) idx = i;
-                            else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els));
-                    if (idx != -1) ret = Sequence.singleton(els, idx);
-                    else           ret = Sequence.drop(els);
-                }
+            Production prod = new Production(tag, (cnt==null?null:cnt.name), els, drops);
+            ret = cx.rm.createSequence(prod);
+            if (ret == null) {
+                int idx = -1;
+                for(int i=0; i<els.length; i++)
+                    if (!drops[i])
+                        if (idx==-1) idx = i;
+                        else throw new Error("multiple non-dropped elements in sequence: " + Sequence.create(els, null));
+                if (idx != -1) ret = Sequence.create(els, idx);
+                else           ret = Sequence.create(els, null);
             }
             if (this.follow != null)
-                ret.follow = this.follow.toAtom(cx);
+                ret = ret.followedBy(this.follow.toAtom(cx));
             return ret;
         }
     }
@@ -273,7 +269,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
     public static @bind.as("()")  ElementNode   epsilon()                         { return new Constant(epsilon); }
 
     private static Union epsilon = new Union("()");
-    static { epsilon.add(Sequence.empty); }
+    static { epsilon.add(Sequence.create()); }
 
     public static class NonTerminalReferenceNode extends ElementNode {
         public String nonTerminal;
@@ -330,7 +326,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             Union u = new Union(null, false);
             Sequence s = body.build(cx, u, null);
             Union u2 = new Union(null, false);
-            u2.add(Sequence.singleton(new Element[] {
+            u2.add(Sequence.create(new Element[] {
                 CharAtom.leftBrace,
                 cx.get("ws"),
                 u,
@@ -352,10 +348,10 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
         }
         public Element build(Context cx, NonTerminalNode cnt) {
             return (!max)
-                ? Sequence.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
+                ? Repeat.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
                 : sep==null
-                ? Sequence.repeatMaximal(e.toAtom(cx), zero, many, cx.rm.repeatTag())
-                : Sequence.repeatMaximal(e.build(cx, null), zero, many, sep.toAtom(cx), cx.rm.repeatTag());
+                ? Repeat.repeatMaximal(e.toAtom(cx), zero, many, cx.rm.repeatTag())
+                : Repeat.repeatMaximal(e.build(cx, null), zero, many, sep.toAtom(cx), cx.rm.repeatTag());
         }
     }
 
@@ -406,7 +402,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
     public static @bind.as("\r")          String lf() { return "\r"; }
 
     //static Atom infer(Element e)  { return infer((Topology<Character>)Atom.toAtom(e)); }
-    static Atom infer(Topology<Character> t) { return new CharAtom(new CharTopology(t)); }
+    static Atom infer(Object t) { return (Atom)t; }
 
     public static class Context {
         public HashMap<String,Union> map = new HashMap<String,Union>();
@@ -497,10 +493,12 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
         public String getLabel() { return label; }
     }
 
+    /*
     static class Invert extends Atom {
         private final Atom a;
         public Invert(Atom a) { this.a = a; }
         public Topology top() { return a.complement(); }
         public String toString() { return "~"+a; }
     }
+    */
 }