checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / MetaGrammar.java
index d51afed..f252ad5 100644 (file)
@@ -35,11 +35,11 @@ public class MetaGrammar extends ReflectiveWalker {
     public Element     _rightparen_(Object e)                                         { return SELF; }
 
     public PreSequence _amp_(PreSequence p, Object[] o) {
-        p.needs.add(new PreSequence(o, null, true));
+        p.and.add(new PreSequence(o, null, true).buildSequence(null, true, false));
         return p;
     }
     public PreSequence _amp__tilde_(PreSequence p, Object[] o) {
-        p.hates.add(new PreSequence(o, null, true));
+        p.not.add(new PreSequence(o, null, true).buildSequence(null, true, false));
         return p;
     }
 
@@ -47,10 +47,6 @@ public class MetaGrammar extends ReflectiveWalker {
     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 Element     _backtick_(Element r)                   { return new Unwrap(r); }
-    //public Element     _hash_(Element e)                       { return e; }
-    //public Element     _hash_(Element e)                       { return _plus__plus_(e); }
-
     public PreSequence[] alternatives(PreSequence[] s) { return s; }
         
     public Object  _leftparen_(PreSequence[][] p) { return nonTerminalZ(p); }
@@ -87,21 +83,11 @@ public class MetaGrammar extends ReflectiveWalker {
     public MetaGrammar grammar(Object o, Union[] u, Object x) { return this; }
     public char    _backslash_n() { return '\n'; }
     public char    _backslash_r() { return '\r'; }
-    //public String  literal(String s) { return s; }
-
-    public Object literal(String s) {
-        Element e = CharToken.string(s);
-        return new MyDrop(e);
-        //dropAll.add(e);
-        //return e;
-    }
-
-    public Range     range(char a)         { return new Range(a, a); }
+    public Object    literal(String s) { return new MyDrop(CharToken.string(s)); }
     public Range     _minus_(char a, char b) { return new Range(a, b); }
-    public Element   rangesx(Range[] rr) { return ranges("~", rr); }
-    public Element   ranges(Range[] rr) { return ranges(null, rr); }
     public Element   _leftbracket_       (Range[] rr) { return ranges(null, rr); }
     public Element   _leftbracket__tilde_(Range[] rr) { return ranges("~",  rr); }
+    public Range     range(char a)         { return new Range(a, a); }
     public Element   ranges(Object o, Range[] rr) {
         Range.Set ret = !"~".equals(o+"") ? new Range.Set() : new Range.Set(new Range(true, true));
         if (rr != null)
@@ -110,21 +96,6 @@ public class MetaGrammar extends ReflectiveWalker {
                 else                   ret.remove(r);
         return CharToken.set(ret);
     }
-    public Element rangex(Object o, Object[] r) {
-        Range.Set ret = o==null ? new Range.Set() : new Range.Set(new Range(true, true));
-        if (r != null)
-            for(Object aa : r) {
-                Range range =
-                    aa instanceof Range
-                    ? (Range)aa
-                    : aa instanceof Character
-                    ? new Range(((Character)aa).charValue())
-                    : new Range(((String)aa).charAt(0));
-                if (o==null) ret.add(range);
-                else         ret.remove(range);
-            }
-        return CharToken.set(ret);
-    }
 
 
     ////////////////////////////////////////////////////////////////////////////////
@@ -157,10 +128,9 @@ public class MetaGrammar extends ReflectiveWalker {
             if (s[i]==null) continue;
             HashSet<Sequence> temp = new HashSet<Sequence>();
             for(PreSequence pre : s[i]) {
-                pre.hatess.addAll(seqs);
+                pre.not.addAll(seqs);
                 Sequence seq = pre.buildSequence(n, false, dropAll);
                 temp.add(seq);
-                //for(Sequence dom : seqs) seq.hates.add(dom);
                 n.add(seq);
             }
             seqs.addAll(temp);
@@ -173,11 +143,6 @@ public class MetaGrammar extends ReflectiveWalker {
     public char unescape(char c) { return StringUtil.unescapify("\\"+c).charAt(0); }
     public PreSequence sequence(Object[] o) { return new PreSequence(o, null); }
 
-    public static class PreBrace {
-        public final Object[] o;
-        public PreBrace(Object[] o) { this.o = o; }
-    }
-
     public PreSequence _equals__greater_(Object[] o, String s) { return new PreSequence(o, s); }
     public PreSequence wrap(Object[] o) { return new PreSequence(o, ""); }
     public PreSequence mwrap(Object[] o) { return new PreSequence(o, ""); }
@@ -189,6 +154,11 @@ public class MetaGrammar extends ReflectiveWalker {
         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;
@@ -203,9 +173,8 @@ public class MetaGrammar extends ReflectiveWalker {
     }
 
     public static class PreSequence {
-        public final HashSet<PreSequence> needs  = new HashSet<PreSequence>();
-        public final HashSet<PreSequence> hates  = new HashSet<PreSequence>();
-        public final HashSet<Sequence> hatess = new HashSet<Sequence>();
+        public final HashSet<Sequence> and  = new HashSet<Sequence>();
+        public final HashSet<Sequence> not  = new HashSet<Sequence>();
         public /*final*/ String tag;
         public final Object[] o;
         public final boolean keeper;
@@ -241,21 +210,8 @@ public class MetaGrammar extends ReflectiveWalker {
         }
         public Sequence buildSequence(Union u) { return buildSequence(u, false, false); }
         public Sequence buildSequence(Union u, boolean lame, boolean dropAll) {
-
-            HashSet<Sequence> and = new HashSet<Sequence>();
-            HashSet<Sequence> not = new HashSet<Sequence>();
-            for(PreSequence p : needs) {
-                Sequence ps = p.buildSequence(u, true, dropAll);
-                u.add(ps);
-                and.add(ps);
-            }
-            for(Sequence p : hatess) not.add(p);
-            for(PreSequence p : hates) {
-                Sequence ps = p.buildSequence(u, true, dropAll);
-                u.add(ps);
-                not.add(ps);
-            }
-
+            for(Sequence s : and) u.add(s);
+            for(Sequence s : not) u.add(s);
             HashSet<Sequence> set = new HashSet<Sequence>();
             Element[] expansion = expand(u, set);
             boolean keeper = this.keeper;
@@ -322,30 +278,6 @@ public class MetaGrammar extends ReflectiveWalker {
 
     public static final Tree meta =
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
         // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED
 new Tree(null, "gram", new Tree[] { new Tree(null, null, new Tree[] { }),
         new Tree(null, "grammar", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "::=", new Tree[] { new Tree(null, null, new Tree[] { new Tree(null, "s", new Tree[] { })}),