checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / MetaGrammar.java
index 234673f..5ea2de3 100644 (file)
@@ -27,32 +27,47 @@ public class MetaGrammar extends ReflectiveWalker {
 
     // MetaGrammar //////////////////////////////////////////////////////////////////////////////
 
-    public Object      _star_(Element r)                                              { return new Rep(r, null, false, true); }
+    public Object      _star_(Element r)                                              { return rep(r, null, false, true); }
     public Element     epsilon(Object o, Object b)                                    { return epsilon; }
     public Element     _rightparen_(Object e)                                         { return SELF; }
 
     public PreSequence _amp_(PreSequence p, Object[] o)        { return p.and(new PreSequence(o, null, true).buildSequence(null, true, false)); }
-    public PreSequence _amp__tilde_(PreSequence p, Object[] o) {  return p.not(new PreSequence(o, null, true).buildSequence(null, true, false)); }
+    public PreSequence _amp__tilde_(PreSequence p, Object[] o) { return p.not(new PreSequence(o, null, true).buildSequence(null, true, false)); }
 
     public Element     _bang_(Element r)                       { return r; }
     public Object      care(String s)                          { return new MyLift(s); }
     public Object      _caret_(String s)                       { return new MyLift(s); }
     public Object      _leftparen__rightparen_()               { return epsilon; }
         
-    public Union       nonTerminal(String s) { return nonTerminalX(s, false, false); }
-    public Union nonTerminalX(String s, boolean synthetic, boolean dropAll) {
-        Union n = s.equals(startSymbol) ? g : nt.get(s);
-        if (n == null) nt.put(s, n = new Union(s, synthetic));
+    public Union       nonTerminal(String s)                                          { return nonTerminal(s, null, false, false); }
+    public Union       _colon__colon__equals_(String s, PreSequence[][] p)            { return nonTerminal(s, p, false, false); }
+    public Union       _bang__colon__colon__equals_(String s, PreSequence[][] p)      { return nonTerminal(s, p, false, true); }
+    public Union       _colon__colon__equals_(boolean q, String s, PreSequence[][] p) { return nonTerminal(s, p, false, q); }
+    public Object      _leftparen_(PreSequence[][] p)                                 { return nonTerminal("anon"+(anon++), p, 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));
         if (dropAll) this.dropAll.add(n);
+        if (s==null) return n;
+        HashSet<Sequence> seqs = new HashSet<Sequence>();
+        for(int i=0; i<s.length; i++) {
+            if (s[i]==null) continue;
+            HashSet<Sequence> temp = new HashSet<Sequence>();
+            for(PreSequence pre : s[i]) {
+                pre.not.addAll(seqs);
+                Sequence seq = pre.buildSequence(n, false, dropAll);
+                temp.add(seq);
+                n.add(seq);
+            }
+            seqs.addAll(temp);
+        }
         return n;
     }
-    public Union       _colon__colon__equals_(String s, PreSequence[][] p)            { return nonTerminalX(s, p, false, false); }
-    public Union       _bang__colon__colon__equals_(String s, PreSequence[][] p)      { return nonTerminalX(s, p, false, true); }
-    public Union       _colon__colon__equals_(boolean q, String s, PreSequence[][] p) { return nonTerminalX(s, p, false, q); }
-    public Object      _leftparen_(PreSequence[][] p) { return nonTerminalX("anon"+(anon++), p, false, false); }
+
+
     public Object   _backslash__leftbrace_(String s)      { return SELF; }
     public Object   _leftbrace_(String s)                 { return SELF; }
-    public Object   _plus_(final Element r) { return new Rep(r, null, false, false); }
+    public Object   _plus_(final Element r) { return rep(r, null, false, false); }
     public Object[] _slash_(Object[] o, Object sep) {
         if (o.length <= 1) return o;
         Object[] ret = new Object[o.length * 2 - 1];
@@ -63,10 +78,14 @@ public class MetaGrammar extends ReflectiveWalker {
         }
         return ret;
     }
-    public Object _plus__slash_(final Element r, Object s) { return new Rep(r, (Element)s, false, false); }
-    public Object _star__slash_(final Element r, Object s) { return new Rep(r, (Element)s, false, true); }
-    public Object _star__star_(final Element r) { return new Rep(r, null, true, true); }
-    public Object _plus__plus_(final Element r) { return new Rep(r, null, true, false); }
+    public Element rep(Element e, Element sep, boolean maximal, boolean zero) {
+        Element ret = zero ? Repeat.many0(e, sep) :  Repeat.many1(e, sep);
+        return maximal ? Repeat.maximal(ret) : ret;
+    }
+    public Object _plus__slash_(final Element r, Element s) { return Repeat.many1(r, s); }
+    public Object _star__slash_(final Element r, Element s) { return Repeat.many0(r, s); }
+    public Object _star__star_(final Element r) { return Repeat.maximal(Repeat.many0(r)); }
+    public Object _plus__plus_(final Element r) { return Repeat.maximal(Repeat.many1(r)); }
     public Element _question_(final Element r) { return Repeat.maybe(r); }
     public MetaGrammar gram(Object o, MetaGrammar g, Object o2) { return g; }
     public MetaGrammar  grammar(Object[] o) { return this; }
@@ -110,24 +129,6 @@ public class MetaGrammar extends ReflectiveWalker {
         return ret;
     }
 
-    public Union nonTerminalX(String str, PreSequence[][] s, boolean synthetic, boolean dropAll) {
-        Union n = nonTerminalX(str, synthetic, dropAll);
-        if (s == null || s.length==0) { n.add(Sequence.empty); return n; }
-        HashSet<Sequence> seqs = new HashSet<Sequence>();
-        for(int i=0; i<s.length; i++) {
-            if (s[i]==null) continue;
-            HashSet<Sequence> temp = new HashSet<Sequence>();
-            for(PreSequence pre : s[i]) {
-                pre.not.addAll(seqs);
-                Sequence seq = pre.buildSequence(n, false, dropAll);
-                temp.add(seq);
-                n.add(seq);
-            }
-            seqs.addAll(temp);
-        }
-        return n;
-    }
-
     public String stringify(String s) { return StringUtil.unescapify(s); }
     public char unescape(char x, char c) { return unescape(c); }
     public char unescape(char c) { return StringUtil.unescapify("\\"+c).charAt(0); }
@@ -143,24 +144,6 @@ public class MetaGrammar extends ReflectiveWalker {
         o3[o3.length-1] = o2;
         return rewrite(o3);
     }
-        
-    public static class PreBrace {
-        public final Object[] o;
-        public PreBrace(Object[] o) { this.o = o; }
-    }
-
-    public static class Rep {
-        private final Element e;
-        private final Element s;
-        private final boolean maximal;
-        private final boolean zero;
-        public Rep(Element e, Element s, boolean maximal, boolean zero) { this.e = e; this.s = s; this.zero = zero; this.maximal = maximal;}
-        public Element build() {
-            Element sep = s;
-            Element ret = zero ? Repeat.many0(e, sep) :  Repeat.many1(e, sep);
-            return maximal ? Repeat.maximal(ret) : ret;
-        }
-    }
 
     public static class PreSequence {
         public PreSequence not(Sequence s) { not.add(s); return this; }
@@ -182,11 +165,9 @@ public class MetaGrammar extends ReflectiveWalker {
             for(int i=0; i<o.length; i++) {
                 Object oi = o[i];
                 if (oi instanceof MyDrop)      { oi = ((MyDrop)oi).o; drops[j] = true; }
-                if      (oi instanceof PreSequence) o2[j] = ((PreSequence)oi).buildUnion();
-                else if (oi==SELF)                  o2[j] = u.new Subset("(("+u+"))", set);
-                else if (oi instanceof MyLift)      { o2[j] = CharToken.string(tag = ((MyLift)oi).s); drops[j] = true; }
-                else if (oi instanceof Rep)         o2[j] = ((Rep)oi).build();
-                else                                  o2[j] = (Element)oi;
+                if (oi==SELF)                    o2[j] = u.new Subset("(("+u+"))", set);
+                else if (oi instanceof MyLift) { o2[j] = CharToken.string(tag = ((MyLift)oi).s); drops[j] = true; }
+                else                             o2[j] = (Element)oi;
 
                 if (dropAll.contains(o2[j])) drops[j] = true;