backported some stuff
[sbp.git] / src / edu / berkeley / sbp / misc / MetaGrammar.java
index 3501ee6..b2db78e 100644 (file)
@@ -15,7 +15,7 @@ public class MetaGrammar extends StringWalker {
         return ret.toString();
     }
 
-    private static HashSet<Element> dropAll = new HashSet<Element>();
+    public /*private*/ static HashSet<Element> dropAll = new HashSet<Element>();
 
     // Statics //////////////////////////////////////////////////////////////////////////////
 
@@ -44,6 +44,8 @@ public class MetaGrammar extends StringWalker {
         startSymbol = str;
         nt = new HashMap<String,Union>();
         dropAll = new HashSet<Element>();
+        dropAll.add(leftBrace);
+        dropAll.add(rightBrace);
         nt.put(str, g);
         return ret;
     }
@@ -51,6 +53,7 @@ public class MetaGrammar extends StringWalker {
 
     // MetaGrammar //////////////////////////////////////////////////////////////////////////////
 
+    public Union       nonTerminal(String str) { return nonTerminal(str, null, false, false); }
     public Union       nonTerminal(String str, PreSequence[][] s, boolean synthetic, boolean dropAll) {
         Union n = str.equals(startSymbol) ? g : nt.get(str);
         if (n == null) nt.put(str, n = new Union(str, synthetic));
@@ -127,19 +130,7 @@ public class MetaGrammar extends StringWalker {
             PreSequence ret = new PreSequence(o3, s);
             ret.drops[o1.length] = o3.length>1;
             return ret;
-        } else if ("{".equals(head))
-            return nonTerminal("braced"+(anon++),
-                               new PreSequence[][] {
-                                   new PreSequence[] {
-                                       new PreSequence(new Element[] { leftBrace(),
-                                                                       ((PreSequence)walk(tree, 0)).buildUnion(),
-                                                                       rightBrace()
-                                       })
-                                   }
-                               },
-                               false,
-                               false);
-        else if ("[".equals(head) || "[~".equals(head)) {
+        } else if ("[".equals(head) || "[~".equals(head)) {
             boolean positive = "[".equals(head);
             Range[] rr = (Range[])walk(tree, 0);
             Range.Set ret = positive ? new Range.Set() : new Range.Set(new Range(true, true));
@@ -238,11 +229,12 @@ public class MetaGrammar extends StringWalker {
             else if (tag!=null) ret = Sequence.rewritingSequence(tag, expansion, drops, and, not);
             else {
                 int idx = -1;
-                for(int i=0; i<expansion.length; i++) {
-                    if (!drops[i]) idx = i;
-                    else expansion[i] = expansion[i];
-                }
-                ret = idx==-1 ? Sequence.drop(expansion, and, not, false) : Sequence.singleton(expansion, idx, and, not);
+                for(int i=0; i<expansion.length; i++)
+                    if (!drops[i])
+                        if (idx==-1) idx = i;
+                        else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(expansion,null,null,false));
+                if (idx != -1) return Sequence.singleton(expansion, idx, and, not);
+                return Sequence.drop(expansion, and, not, false);
             }
             set.add(ret);
             return ret;