checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammarBindings.java
index 2866107..2372273 100644 (file)
@@ -49,15 +49,51 @@ public class MetaGrammarBindings {
     public static class NonTerminal extends Un {
         public boolean rep;
         public String  name = null;
+        public String sep = null;
         public @bind NonTerminal(@bind.arg String name, @bind.arg Seq[][] sequences) { this(name, sequences, false); }
-        public NonTerminal(String name, Seq[][] sequences, boolean rep) {
+        public NonTerminal(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
+        public NonTerminal(String name, Seq[][] sequences, boolean rep, String sep) {
             this.name = name;
             this.sequences = sequences;
             this.rep = rep;
+            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) {
+        return new NonTerminal(name, sequences, true, sep);
+    }
 
     public static class AnonUn extends Un {
         public @bind.as("(") AnonUn(Seq[][] sequences) {
@@ -105,6 +141,12 @@ public class MetaGrammarBindings {
         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; }
@@ -155,10 +197,9 @@ public class MetaGrammarBindings {
             Sequence ret = null;
             if (dropAll)     ret = Sequence.drop(els, false);
             else if (unwrap) ret = Sequence.unwrap(els, cx.rm.repeatTag(), drops);
-            else if (tag!=null) {
-                ret = cx.rm.resolveTag(tag, cx.cnt, els, labels, drops);
-            } else {
-                ret = cx.rm.tryResolveTag(tag, cx.cnt, els, labels, drops);
+            else {
+                if (tag != null) ret = cx.rm.resolveTag(tag, cx.cnt, els, labels, drops);
+                else ret = cx.rm.tryResolveTag(tag, cx.cnt, els, labels, drops);
                 if (ret == null) {
                     int idx = -1;
                     for(int i=0; i<els.length; i++)
@@ -212,7 +253,17 @@ public class MetaGrammarBindings {
     public static @bind.as("{")           class XTree                 extends El {
         public @bind.arg Seq body;
         public Element build(MetaGrammar.Context cx) {
-            throw new Error();
+            Union u = new Union();
+            Sequence s = body.build(cx, u, false);
+            Union u2 = new Union();
+            u2.add(Sequence.singleton(new Element[] {
+                CharRange.leftBrace,
+                cx.get("ws"),
+                u,
+                cx.get("ws"),
+                CharRange.rightBrace
+            }, 2));
+            return u2;
         }
     }