checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / MetaGrammar.java
index a83d3ed..33b3b11 100644 (file)
@@ -136,8 +136,10 @@ public class MetaGrammar extends StringWalker {
             carets.put(ret, s);
             dropAll.add(ret);
             return ret;
+            /*
         } else if ("^^".equals(head)) {
             return new Keep(walk(tree,0));
+            */
         } else if ("psx".equals(head)) return ((PreSequence)walk(tree, 0)).buildSequence();
         else if ("nonTerminal".equals(head)) return getNonTerminal(string(tree.child(0)));
         else if ("::=".equals(head)) return nonTerminal(string(tree.child(0)), (Sequence[][])Reflection.lub(walk(tree, 1)), false, false);
@@ -155,10 +157,12 @@ public class MetaGrammar extends StringWalker {
         else if ("ps".equals(head)) return new PreSequence((Object[])walk(tree,0), null);
         else if (":".equals(head)) {
             String s = string(tree.child(0));
-            return new Keep("_".equals(s)?walk(tree,1):new PreSequence(new Object[] { walk(tree, 1) }, convertLabel(s)).buildUnion());
+            return new Keep(s,walk(tree,1));
         } else if ("::".equals(head)) {
             PreSequence p = (PreSequence)walk(tree, 1);
-            p.tag = convertLabel(string(tree.child(0)));
+            String s = string(tree.child(0));
+            if (s.equals("[]")) p.unwrap = true;
+            else p.tag = convertLabel(s);
             return p;
         }
         else if ("=>".equals(head)) {
@@ -176,11 +180,12 @@ public class MetaGrammar extends StringWalker {
     }
 
     static class Keep {
-        final Object o;
-        public Keep(Object o) { this.o = o; }
+        public final String label;
+        public final Object o;
+        public Keep(String label, Object o) { this.label = label; this.o = o; }
     }
 
-    public String convertLabel(String label) { return label; }
+    public Object convertLabel(String label) { return label; }
 
     public Object walk(String tag, Object[] argo) {
         if (argo.length==0) return super.walk(tag, argo);
@@ -199,7 +204,7 @@ public class MetaGrammar extends StringWalker {
         public Element noFollow = null;
         public final HashSet<Sequence> and  = new HashSet<Sequence>();
         public final HashSet<Sequence> not  = new HashSet<Sequence>();
-        public /*final*/ String tag;
+        public /*final*/ Object tag;
         public final Object[] o;
 
         public PreSequence sparse(Object e) {
@@ -234,8 +239,8 @@ public class MetaGrammar extends StringWalker {
         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, tag, null); }
-        public PreSequence(Object[] o, String tag, boolean[] drops) {
+        public PreSequence(Object[] o, Object tag) { this(o, tag, null); }
+        public PreSequence(Object[] o, Object tag, boolean[] drops) {
             this.o = o;
             this.tag = tag;
             this.drops = drops==null ? new boolean[o.length] : drops;
@@ -250,7 +255,7 @@ public class MetaGrammar extends StringWalker {
         public boolean unwrap = false;
         public Sequence buildSequence() { return buildSequence(null, false, false); }
         public Sequence buildSequence(Union u) { return buildSequence(u, false, false); }
-        public Sequence buildSequence(Union u, boolean lame, String tag) {
+        public Sequence buildSequence(Union u, boolean lame, Object tag) {
             this.tag = tag;
             return buildSequence(u, lame, false);
         }
@@ -263,6 +268,7 @@ public class MetaGrammar extends StringWalker {
             }
             HashSet<Sequence> set = new HashSet<Sequence>();
             Element[] o2 = o==null ? new Element[0] : new Element[o.length];
+            Object[] labels = new Object[drops.length];
             int nonDrop = 0;
             boolean keeping = false;
             if (o != null) {
@@ -273,6 +279,7 @@ public class MetaGrammar extends StringWalker {
                         if (!keeping) { for(int k=0; k<i; k++) drops[k]=true; }
                         drops[i] = false;
                         keeping = true;
+                        labels[i] = ((Keep)oi).label;
                         oi = ((Keep)oi).o;
                     } else {
                         if (keeping) drops[i] = true;
@@ -292,7 +299,7 @@ public class MetaGrammar extends StringWalker {
             Sequence ret = null;
             if (dropAll || lame) ret = Sequence.drop(expansion, lame);
             else if (unwrap)    ret = new Sequence.Unwrap(expansion, drops);
-            else if (tag!=null) ret = Sequence.rewritingSequence(tag, expansion, null, drops);
+            else if (keeping || tag!=null) ret = Sequence.rewritingSequence(tag, expansion, labels, drops);
             else {
                 int idx = -1;
                 for(int i=0; i<expansion.length; i++)
@@ -492,6 +499,9 @@ public class MetaGrammar extends StringWalker {
 
 
 
+
+
+
         // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED
 new edu.berkeley.sbp.Tree(null, "grammar", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "=", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "s", new edu.berkeley.sbp.Tree[] { })}),
         new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "psx", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "ps", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "!", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "nonTerminal", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "w", new edu.berkeley.sbp.Tree[] { }),
@@ -1143,3 +1153,6 @@ new edu.berkeley.sbp.Tree(null, "grammar", new edu.berkeley.sbp.Tree[] { new edu
 
 
 
+
+
+