checkpoint
authoradam <adam@megacz.com>
Thu, 30 Mar 2006 18:57:26 +0000 (13:57 -0500)
committeradam <adam@megacz.com>
Thu, 30 Mar 2006 18:57:26 +0000 (13:57 -0500)
darcs-hash:20060330185726-5007d-4168d28f3be622833ca02f27723fb56f284ebaa6.gz

src/edu/berkeley/sbp/Atom.java
src/edu/berkeley/sbp/Forest.java
src/edu/berkeley/sbp/GSS.java
src/edu/berkeley/sbp/Parser.java
src/edu/berkeley/sbp/Repeat.java
src/edu/berkeley/sbp/Sequence.java
src/edu/berkeley/sbp/misc/MetaGrammar.java
src/edu/berkeley/sbp/tib/Tib.java

index 55be283..32e7f0f 100644 (file)
@@ -30,23 +30,8 @@ public abstract class Atom<T> extends Element implements Topology<T> {
 
     // Subclasses //////////////////////////////////////////////////////////////////////////////
 
-    /** 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(); }
-    }
     
-    /** 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 ((Topology<T>)a.top()).complement(); }
-        public String toString() { return "~"+a; }
-    }
-
-    static Topology toAtom(Element e) {
+    public static Topology toAtom(Element e) {
         if (e instanceof Atom) return (Atom)e;
         if (e instanceof Sequence) return ((Sequence)e).toAtom();
         Topology ret = null;
index bfa4ecc..12c8cf1 100644 (file)
@@ -35,10 +35,10 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ impl
         public InnerAmbiguous(Forest<?> f) { this.f = f; }
     }
 
-    public static interface TreeConsumer<T> {
+    static interface TreeConsumer<T> {
         public void addTree(Tree<T> t);
     }
-    public static class HashSetTreeConsumer<T> extends HashSet<Tree<T>> implements TreeConsumer<T> {
+    static class HashSetTreeConsumer<T> extends HashSet<Tree<T>> implements TreeConsumer<T> {
         public void addTree(Tree<T> t) {
             super.add(t);
         }
index 5690c07..7d3758f 100644 (file)
@@ -245,7 +245,7 @@ class GSS {
         }
 
 
-        public class Waiting {
+        class Waiting {
             Node parent;
             Forest pending;
             State state;
@@ -268,7 +268,7 @@ class GSS {
         // Node /////////////////////////////////////////////////////////////////////////////////
 
         /** a node in the GSS */
-        public final class Node extends FastSet<Node> implements Invokable<Position, Node, Node>, IntegerMappable {
+        final class Node extends FastSet<Node> implements Invokable<Position, Node, Node>, IntegerMappable {
 
             private Forest.Ref holder = null;
             private boolean allqueued = false;
index 1ea89ad..31bd1b3 100644 (file)
@@ -38,7 +38,7 @@ public abstract class Parser<Tok, Result> {
     // Table //////////////////////////////////////////////////////////////////////////////
 
     /** an SLR(1) parse table which may contain conflicts */
-    public static class Table<Tok> extends Walk.Cache {
+    static class Table<Tok> extends Walk.Cache {
 
         public final Walk.Cache cache = this;
 
@@ -117,7 +117,7 @@ public abstract class Parser<Tok, Result> {
 
         /** a single state in the LR table and the transitions possible from it */
 
-        public class State<Tok> implements Comparable<State<Tok>>, IntegerMappable, Iterable<Position> {
+        class State<Tok> implements Comparable<State<Tok>>, IntegerMappable, Iterable<Position> {
         
             public  final     int               idx    = master_state_idx++;
             private final     HashSet<Position> hs;
index c68120e..4868135 100644 (file)
@@ -8,43 +8,17 @@ import java.lang.reflect.*;
 import java.lang.ref.*;
 
 /** currently this class exports only static methods to create repetitions; there are no public instance methods or constructors */
-public class Repeat extends Union {
+class Repeat extends Union {
 
-    /** repeat zero or one times */
-    public  static Repeat maybe(Element e)              { return new Repeat(e, true, false, null, false); }
-    public  static Repeat maybe(Element e, Object tag)              { return new Repeat(e, true, false, null, false, tag); }
-    /** repeat zero or more times */
-    public  static Repeat many0(Element e)              { return new Repeat(e, true, true, null, false); }
-    public  static Repeat many0(Element e, Object tag)              { return new Repeat(e, true, true, null, false, tag); }
-    /** repeat zero or more times, separated by <tt>sep</tt> */
-    public  static Repeat many0(Element e, Element sep) { return new Repeat(e, true, true, sep, false); }
-    public  static Repeat many0(Element e, Element sep, Object tag) { return new Repeat(e, true, true, sep, false, tag); }
-    /** repeat one or more times */
-    public  static Repeat many1(Element e)              { return new Repeat(e, false, true, null, false); }
-    public  static Repeat many1(Element e, Object tag)              { return new Repeat(e, false, true, null, false, tag); }
-    /** repeat one or more times, separated by <tt>sep</tt> */
-    public  static Repeat many1(Element e, Element sep) { return new Repeat(e, false, true, sep, false); }
-    public  static Repeat many1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, false, tag); }
-
-    /** repeat zero or more times, matching a maximal sequence of atoms */
-    public  static Repeat maximal0(Element e)              { return new Repeat(e, true, true, null, true); }
-    public  static Repeat maximal0(Element e, Object tag)              { return new Repeat(e, true, true, null, true, tag); }
-    /** repeat one or more times, matching a maximal sequence of atoms */
-    public  static Repeat maximal1(Element e)              { return new Repeat(e, false, true, null, true); }
-    public  static Repeat maximal1(Element e, Object tag)              { return new Repeat(e, false, true, null, true, tag); }
-    /** repeat one or more times, separated by an atom <tt>sep</tt>, matching a maximal sequence */
-    public  static Repeat maximal1(Element e, Element sep) { return new Repeat(e, false, true, sep, true); }
-    public  static Repeat maximal1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, true, tag); }
-
-    private Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal) {
+    Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal) {
         this(e, zeroOkay, manyOkay, separator, maximal, null); }
-    private Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) {
+    Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) {
         super(e+(!manyOkay ? "?" : (zeroOkay ? (maximal ? "**" : "*") : (maximal ? "++" : "+")))+(separator==null?"":("/"+separator)), true);
         if (maximal && zeroOkay && separator != null)
             throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
         if (zeroOkay) {
             add(new Sequence.Constant.Empty());
-            if (manyOkay) add(new Sequence.Singleton(many1(e, separator)));
+            if (manyOkay) add(new Sequence.Singleton(Sequence.many1(e, separator)));
             else          add(new Sequence.Singleton(e));
         } else {
             add(new Sequence.RewritingSequence(tag, new Element[] { e }, null));
index 7071ecb..2ba9096 100644 (file)
@@ -195,7 +195,8 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         Sequence _clone() { return new Singleton(elements,idx); }
     }
 
-    public static class Unwrap extends Sequence {
+    public static Unwrap unwrap(Element[] e, Object tag, boolean[] drops) { return new Unwrap(e, tag, drops); }
+    static class Unwrap extends Sequence {
         private boolean[] drops;
         private final Object tag;
         public Unwrap(Element[] e, Object tag)                  { super(e); this.drops = null; this.tag = tag; }
@@ -245,4 +246,34 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             return sb;
         }
     }
+
+    // Repeat //////////////////////////////////////////////////////////////////////////////
+
+    /** repeat zero or one times */
+    public  static Repeat maybe(Element e)              { return new Repeat(e, true, false, null, false); }
+    public  static Repeat maybe(Element e, Object tag)              { return new Repeat(e, true, false, null, false, tag); }
+    /** repeat zero or more times */
+    public  static Repeat many0(Element e)              { return new Repeat(e, true, true, null, false); }
+    public  static Repeat many0(Element e, Object tag)              { return new Repeat(e, true, true, null, false, tag); }
+    /** repeat zero or more times, separated by <tt>sep</tt> */
+    public  static Repeat many0(Element e, Element sep) { return new Repeat(e, true, true, sep, false); }
+    public  static Repeat many0(Element e, Element sep, Object tag) { return new Repeat(e, true, true, sep, false, tag); }
+    /** repeat one or more times */
+    public  static Repeat many1(Element e)              { return new Repeat(e, false, true, null, false); }
+    public  static Repeat many1(Element e, Object tag)              { return new Repeat(e, false, true, null, false, tag); }
+    /** repeat one or more times, separated by <tt>sep</tt> */
+    public  static Repeat many1(Element e, Element sep) { return new Repeat(e, false, true, sep, false); }
+    public  static Repeat many1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, false, tag); }
+
+    /** repeat zero or more times, matching a maximal sequence of atoms */
+    public  static Repeat maximal0(Element e)              { return new Repeat(e, true, true, null, true); }
+    public  static Repeat maximal0(Element e, Object tag)              { return new Repeat(e, true, true, null, true, tag); }
+    /** repeat one or more times, matching a maximal sequence of atoms */
+    public  static Repeat maximal1(Element e)              { return new Repeat(e, false, true, null, true); }
+    public  static Repeat maximal1(Element e, Object tag)              { return new Repeat(e, false, true, null, true, tag); }
+    /** repeat one or more times, separated by an atom <tt>sep</tt>, matching a maximal sequence */
+    public  static Repeat maximal1(Element e, Element sep) { return new Repeat(e, false, true, sep, true); }
+    public  static Repeat maximal1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, true, tag); }
+
+
 }
index 378a53c..bdb8096 100644 (file)
@@ -7,6 +7,22 @@ import java.io.*;
 
 public class MetaGrammar extends StringWalker {
 
+    /** an atom which tracks the possible tokenset of some element, provided that element can only match single-token sequences */
+    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(); }
+    }
+
+    /** an atom which tracks the inverse of some other atom */
+    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);
@@ -115,14 +131,14 @@ public class MetaGrammar extends StringWalker {
         if      ("\\n".equals(head)) return new Character('\n');
         else if ("\\r".equals(head)) return new Character('\r');
         else if ("grammar".equals(head)) { for(Tree<String> t : tree.children()) walk(t); return this; }
-        else if ("*".equals(head))   return Repeat.many0((Element)walk(tree.child(0)), repeatTag());
-        else if ("+".equals(head))   return Repeat.many1((Element)walk(tree.child(0)), repeatTag());
-        else if ("+/".equals(head))  return Repeat.many1((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
-        else if ("*/".equals(head))  return Repeat.many0((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
-        else if ("++/".equals(head)) return Repeat.maximal1((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
-        else if ("**".equals(head))  return Repeat.maximal0((Element)walk(tree.child(0)), repeatTag());
-        else if ("++".equals(head))  return Repeat.maximal1((Element)walk(tree.child(0)), repeatTag());
-        else if ("?".equals(head))   return Repeat.maybe((Element)walk(tree.child(0)), repeatTag());
+        else if ("*".equals(head))   return Sequence.many0((Element)walk(tree.child(0)), repeatTag());
+        else if ("+".equals(head))   return Sequence.many1((Element)walk(tree.child(0)), repeatTag());
+        else if ("+/".equals(head))  return Sequence.many1((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
+        else if ("*/".equals(head))  return Sequence.many0((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
+        else if ("++/".equals(head)) return Sequence.maximal1((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag());
+        else if ("**".equals(head))  return Sequence.maximal0((Element)walk(tree.child(0)), repeatTag());
+        else if ("++".equals(head))  return Sequence.maximal1((Element)walk(tree.child(0)), repeatTag());
+        else if ("?".equals(head))   return Sequence.maybe((Element)walk(tree.child(0)), repeatTag());
         else if ("!".equals(head))   { Element e = (Element)walk(tree.child(0)); dropAll.add(e); return e; }
         else if ("&".equals(head))   return ((Sequence)walk(tree,0)).and(sequence(walk(tree, 1), true));
         else if ("and".equals(head)) return ((Sequence)walk(tree,0)).and(sequence(walk(tree, 1), true));
@@ -153,7 +169,7 @@ public class MetaGrammar extends StringWalker {
         else if ("->".equals(head)) { PreSequence p = (PreSequence)walk(tree, 0); p.noFollow = (Element)walk(tree, 1); return p; }
         else if ("/".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1));
         else if (" /".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1));
-        else if ("~".equals(head)) return new Hack(new Atom.Invert(new Atom.Infer((Element)walk(tree, 0))));
+        else if ("~".equals(head)) return new Hack(new Invert(new Infer((Element)walk(tree, 0))));
         else if ("ps".equals(head)) return new PreSequence((Object[])walk(tree,0), null);
         else if (":".equals(head)) {
             String s = string(tree.child(0));
@@ -300,7 +316,7 @@ public class MetaGrammar extends StringWalker {
             Element[] expansion = o2;
             Sequence ret = null;
             if (dropAll || lame) ret = Sequence.drop(expansion, lame);
-            else if (unwrap)    ret = new Sequence.Unwrap(expansion, repeatTag(), drops);
+            else if (unwrap)    ret = Sequence.unwrap(expansion, repeatTag(), drops);
             else if (keeping || tag!=null) ret = Sequence.rewritingSequence(tag, expansion, labels, drops);
             else {
                 int idx = -1;
@@ -314,7 +330,7 @@ public class MetaGrammar extends StringWalker {
             for(Sequence s : and) ret = ret.and(s);
             for(Sequence s : not) ret = ret.not(s);
             set.add(ret);
-            if (this.noFollow != null) ret.noFollow = new Atom.Invert(new Atom.Infer(this.noFollow));
+            if (this.noFollow != null) ret.noFollow = new Invert(new Infer(this.noFollow));
             return ret;
         }
     }
@@ -510,6 +526,7 @@ 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[] { }),
@@ -1170,3 +1187,4 @@ new edu.berkeley.sbp.Tree(null, "grammar", new edu.berkeley.sbp.Tree[] { new edu
 
 
 
+
index f802ab0..a30e269 100644 (file)
@@ -137,7 +137,7 @@ public class Tib implements Input<Character> {
 
     public static class Grammar extends ReflectiveGrammar {
         private int anon = 0;
-        private final Element ws = Repeat.maximal0(getNonTerminal("w"));
+        private final Element ws = Sequence.maximal0(getNonTerminal("w"));
         public Grammar(Class c) { super(c); dropAll.add(ws); }
         public Object walk(Tree<String> tree) {
             String head = tree.head();