checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammarBindings.java
index da0204a..2d06422 100644 (file)
@@ -59,6 +59,36 @@ public class MetaGrammarBindings {
             this.sep = sep;
         }
         public Element build(MetaGrammar.Context cx) { return cx.get(name); }
             this.sep = sep;
         }
         public Element build(MetaGrammar.Context cx) { return cx.get(name); }
+        public void build(MetaGrammar.Context cx, Union u) {
+            if (!rep) { super.build(cx, u); return; }
+            HashSet<Sequence> bad2 = new HashSet<Sequence>();
+
+            Union urep = new Union();
+            urep.add(Sequence.empty);
+            urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
+
+            for(int i=0; i<sequences.length; i++) {
+                Seq[] group = sequences[i];
+                Union u2 = new Union();
+                if (sequences.length==1) u2 = u;
+                for(int j=0; j<group.length; j++) {
+                    Union u3 = new Union();
+                    group[j].build(cx, u3, false);
+                    Sequence s = Sequence.unwrap(new Element[] { u3, urep },
+                                                 cx.rm.repeatTag(),
+                                                 new boolean[] { false, false });
+                    u2.add(s);
+                }
+                if (sequences.length==1) break;
+                Sequence seq = Sequence.singleton(u2);
+                for(Sequence s : bad2) {
+                    s.lame = true;
+                    seq = seq.not(s);
+                }
+                u.add(seq);
+                bad2.add(Sequence.singleton(u2));
+            }
+        }
     }
     public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg Seq[][] sequences) { return new NonTerminal(name, sequences, true); }
     public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
     }
     public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg Seq[][] sequences) { return new NonTerminal(name, sequences, true); }
     public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
@@ -111,6 +141,12 @@ public class MetaGrammarBindings {
         El follow;
         String tag = null;
         boolean lame;
         El follow;
         String tag = null;
         boolean lame;
+        public void append(El e) {
+            El[] elements = new El[this.elements.length+1];
+            System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
+            this.elements = elements;
+            elements[elements.length-1] = e;
+        }
         public Seq(El e) { this(new El[] { e }); }
         public Seq(El[] elements) { this.elements = elements; }
         public Seq tag(String tag) { this.tag = tag; return this; }
         public Seq(El e) { this(new El[] { e }); }
         public Seq(El[] elements) { this.elements = elements; }
         public Seq tag(String tag) { this.tag = tag; return this; }