checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / MetaGrammar.java
index 285016b..53c9bed 100644 (file)
@@ -9,32 +9,6 @@ public class MetaGrammar extends StringWalker {
 
     public static Object repeatTag = null;
 
-    /** an atom which tracks the possible tokenset of some element, provided that element can only match single-token sequences */
-    public static class Infer<T extends Input> extends Atom<T> {
-        private final Element e;
-        public Infer(Element e) { this.e = e; }
-        public Topology<T> top() { return (Topology<T>)toAtom(e); }
-        public String toString() { return e.toString(); }
-    }
-
-    public static Atom infer(Element e) { return new CharRange((Topology<Character>)Atom.toAtom(e)); }
-
-    /** an atom which tracks the inverse of some other atom */
-    public static class Invert<T extends Input> extends Atom<T> {
-        private final Atom<T> a;
-        public Invert(Atom<T> a) { this.a = a; }
-        public Topology<T> top() { return a.complement(); }
-        public String toString() { return "~"+a; }
-    }
-
-    public static class Hack<T extends Input> extends Atom<T> {
-        private final Atom<T> a;
-        static final Topology leftright = CharRange.rightBrace.union(CharRange.leftBrace);
-        public Hack(Atom<T> a) { this.a = a; }
-        public Topology<T> top() { return a.minus(leftright); }
-        public String toString() { return a.toString(); }
-    }
-
     public static Union make() throws Exception { return make(MetaGrammarTree.meta, "s"); }
     public static Union make(Tree<String> tree, String nt) throws Exception {
         Meta.MetaGrammarFile mgf = new MetaGrammar.Meta.MetaGrammarFile(tree);
@@ -44,9 +18,10 @@ public class MetaGrammar extends StringWalker {
 
     ////////////////////////////////////////////////////////////////////////////////
 
-    private static boolean strings;
-    private static Element  set(Range.Set r) { if (strings) throw new Error(); return CharRange.set(r); }
-    private static Element  string(String s) { return strings ? StringInput.string(s) : CharRange.string(s); }
+    private static Element  set(Range.Set r) { return CharRange.set(r); }
+    private static Element  string(String s) { return CharRange.string(s); }
+    private static Atom infer(Element e)  { return infer(Atom.toAtom(e)); }
+    private static Atom infer(Topology t) { return new CharRange((Topology<Character>)t); }
 
     private MetaGrammar() { }
 
@@ -227,7 +202,8 @@ public class MetaGrammar extends StringWalker {
                     if (idx != -1) ret = Sequence.singleton(els, idx);
                     else           ret = Sequence.drop(els, false);
                 }
-                if (this.followedBy != null) ret.follow = new Hack(infer(this.followedBy.build(bc)));
+                if (this.followedBy != null)
+                    ret.follow = infer(this.followedBy.build(bc));
                 return ret;
             }
             private MetaConjunct(Tree<String> t) {
@@ -305,10 +281,12 @@ public class MetaGrammar extends StringWalker {
                 public boolean maximal, zero, many;
                 public Element build(BuildContext bc) {
                     return !maximal
-                        ? new Repeat(element.build(bc), zero, many, separator==null?null:separator.build(bc), null)
-                        : separator==null
-                        ? new Repeat.Maximal(infer(element.build(bc)), zero, many, null)
-                        : new Repeat.Maximal(element.build(bc), zero, many, infer(separator.build(bc)), null);
+                        ? (separator==null
+                           ? Sequence.repeat(element.build(bc), zero, many, null, null)
+                           : Sequence.repeat(element.build(bc), zero, many, separator.build(bc), null))
+                        : (separator==null
+                           ? Sequence.repeatMaximal(infer(element.build(bc)), zero, many, null)
+                           : Sequence.repeatMaximal(element.build(bc), zero, many, infer(separator.build(bc)), null));
                 }
                 public MetaRepeat(MetaClause element, boolean maximal, MetaClause separator, boolean zero, boolean many) {
                     this.separator = separator;
@@ -379,7 +357,7 @@ public class MetaGrammar extends StringWalker {
                 public MetaClause element;
                 public MetaInvert(Tree<String> t, MetaConjunct c) { this.element = make(t, c); }
                 public String toString() { return "~"+element; }
-                public Element build(BuildContext bc) { return new Hack(new Invert(infer(element.build(bc)))); }
+                public Element build(BuildContext bc) { return infer(Atom.toAtom(element.build(bc)).complement()); }
             }
         }
 
@@ -431,4 +409,5 @@ public class MetaGrammar extends StringWalker {
         p.flush();
         os.close();
     }
+
 }