checkpoint
authoradam <adam@megacz.com>
Fri, 21 Jul 2006 07:14:42 +0000 (03:14 -0400)
committeradam <adam@megacz.com>
Fri, 21 Jul 2006 07:14:42 +0000 (03:14 -0400)
darcs-hash:20060721071442-5007d-1fe3ec8197455f052c110433444360d9023b0883.gz

TODO
src/edu/berkeley/sbp/Repeat.java [deleted file]
src/edu/berkeley/sbp/Sequence.java
src/edu/berkeley/sbp/chr/CharAtom.java
src/edu/berkeley/sbp/meta/MetaGrammarBindings.java
src/edu/berkeley/sbp/meta/Repeat.java [new file with mode: 0644]

diff --git a/TODO b/TODO
index b476721..23b23d2 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,15 +1,21 @@
 _____________________________________________________________________________
 Immediately
 
+  - The repeat API's in Sequence are ugly
+  - segregate the sequence static-constructors?
+
   - Sequence shouldn't be an Element -> make Union.add(Element)
   - Should Tree<T> really be type-parameterized?
-
-  - More topology untangling
-  - needs/hates/follow API ugliness
-
   - do Forest/Tree still need a Region?
+
   - reconsider the degree of genericization
-  - GraphViz stuff pollutes the API...
+
+  - grammar highlighting?
+  - comment indentation vs block indentation?
+  - { and } in <pre>
+  - recursive { { foo } }
+
+  - More topology untangling
   - Forest needs a "manual access" API
       - the unwrap bit in Forest makes it really hard to expose an API for forests
 
@@ -18,10 +24,6 @@ Immediately
   - copyright notices
   - documentation
 
-  - grammar highlighting?
-  - comment indentation vs block indentation?
-  - { and } in <pre>
-  - recursive { { foo } }
 
 ______________________________________________________________________________
 v1.1
diff --git a/src/edu/berkeley/sbp/Repeat.java b/src/edu/berkeley/sbp/Repeat.java
deleted file mode 100644 (file)
index 037e67d..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-package edu.berkeley.sbp;
-import edu.berkeley.sbp.util.*;
-import edu.berkeley.sbp.*;
-import edu.berkeley.sbp.*;
-import java.io.*;
-import java.util.*;
-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 */
-class Repeat extends Union {
-
-    public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Object tag) {
-        this(e, zeroOkay, manyOkay, null, false, tag); }
-    public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, Object tag) {
-        this(e, zeroOkay, manyOkay, separator, false, tag); }
-    protected 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 (zeroOkay && !manyOkay) {
-            add(new Sequence.Constant.Empty());
-            add(new Sequence.Singleton(e));
-            return;
-        }
-        if (zeroOkay) {
-            add(new Sequence.RewritingSequence(tag, new Element[] { }, null));
-            //add(new Sequence.Constant.Empty());
-            // FUGLY
-            add(new Sequence.Singleton(Sequence.many1(e, separator, tag)));
-        } else {
-            add(new Sequence.RewritingSequence(tag, new Element[] { e }, null));
-            if (separator==null)
-                add(new Sequence.Unwrap(new Element[] { e,                 Repeat.this }, tag));
-            else
-                add(new Sequence.Unwrap(new Element[] { e, separator,      Repeat.this }, tag, new boolean[] { false, true, false }));
-        }
-    }
-
-    public static class Maximal extends Repeat {
-        public Maximal(final Element e, boolean zeroOkay, boolean manyOkay, final Atom separator, Object tag) {
-            super(e, zeroOkay, manyOkay, separator, true, tag);
-            if (zeroOkay && separator != null)
-                throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
-            for(Sequence s : this)
-                s.follow = (Atom)separator.complement();
-        }
-        public Maximal(final Atom e, boolean zeroOkay, boolean manyOkay, Object tag) {
-            super(e, zeroOkay, manyOkay, null, true, tag);
-            for(Sequence s : this)
-                s.follow = (Atom)e.complement();
-        }
-    }
-
-}
index 975b2b1..8d6709d 100644 (file)
@@ -33,7 +33,8 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     }
 
     /** the empty sequence (matches the empty string) */
-    public static final Sequence empty = new Sequence.Constant.Empty();
+    static final Sequence empty = new Sequence.Constant.Empty();
+    public static Sequence empty() { return empty; }
 
     /** after matching the sequence, do not add anything to the output tree */
     public static Sequence drop(Element[] e) { return new Constant.Drop(e); }
@@ -64,8 +65,8 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     public Sequence not(Sequence s) { Sequence ret = dup(); ret.hates.add(s); s.hated.add(ret); return ret; }
     public Sequence followedBy(Atom a) { Sequence ret = dup(); ret.follow = a; return ret; }
 
-    public Iterable<Sequence> needs() { return needs; }
-    public Iterable<Sequence> hates() { return hates; }
+    Iterable<Sequence> needs() { return needs; }
+    Iterable<Sequence> hates() { return hates; }
 
     Position firstp() { return firstp; }
 
@@ -199,13 +200,13 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         Sequence _clone() { return new Singleton(elements,idx); }
     }
 
-    public static Unwrap unwrap(Element[] e, Object tag, boolean[] drops) { return new Unwrap(e, tag, drops); }
+    public static Sequence 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; }
         public Unwrap(Element[] e, Object tag, boolean[] drops) { super(e); this.drops = drops; this.tag = tag; }
-        Sequence _clone() { return new Unwrap(elements, drops); }
+        Sequence _clone() { return new Unwrap(elements, tag, drops); }
         public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
             for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
             if (drops==null) return Forest.create(loc, (T)tag, args, true);
@@ -264,40 +265,4 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         }
     }
 
-    // Repeat //////////////////////////////////////////////////////////////////////////////
-
-    /** repeat zero or one times */
-    public  static Element maybe(Element e)                             { return new Repeat(e, true, false, null, null); }
-    public  static Element maybe(Element e, Object tag)                 { return new Repeat(e, true, false, null, tag); }
-    /** repeat zero or more times */
-    public  static Element many0(Element e)                             { return new Repeat(e, true, true, null, null); }
-    public  static Element many0(Element e, Object tag)                 { return new Repeat(e, true, true, null, tag); }
-    /** repeat zero or more times, separated by <tt>sep</tt> */
-    public  static Element many0(Element e, Element sep)                { return new Repeat(e, true, true, sep, null); }
-    public  static Element many0(Element e, Element sep, Object tag)    { return new Repeat(e, true, true, sep, tag); }
-    /** repeat one or more times */
-    public  static Element many1(Element e)                             { return new Repeat(e, false, true, null, null); }
-    public  static Element many1(Element e, Object tag)                 { return new Repeat(e, false, true, null, tag); }
-    /** repeat one or more times, separated by <tt>sep</tt> */
-    public  static Element many1(Element e, Element sep)                { return new Repeat(e, false, true, sep, null); }
-    public  static Element many1(Element e, Element sep, Object tag)    { return new Repeat(e, false, true, sep, tag); }
-
-    /** repeat zero or more times, matching a maximal sequence of atoms */
-    public  static Element maximal0(Atom e)                             { return new Repeat.Maximal(e, true, true, null); }
-    public  static Element maximal0(Atom e, Object tag)                 { return new Repeat.Maximal(e, true, true, tag); }
-    /** repeat one or more times, matching a maximal sequence of atoms */
-    public  static Element maximal1(Atom e)                             { return new Repeat.Maximal(e, false, true, null); }
-    public  static Element maximal1(Atom e, Object tag)                 { return new Repeat.Maximal(e, false, true, tag); }
-    /** repeat one or more times, separated by an atom <tt>sep</tt>, matching a maximal sequence */
-    public  static Element maximal1(Element e, Atom sep)                { return new Repeat.Maximal(e, false, true, sep, null); }
-    public  static Element maximal1(Element e, Atom sep, Object tag)    { return new Repeat.Maximal(e, false, true, sep, tag); }
-
-    public  static Element repeatMaximal(Atom e, boolean zero, boolean many, Object tag) {
-        return new Repeat.Maximal(e, zero, many, tag); }
-    public  static Element repeatMaximal(Element e, boolean zero, boolean many, Atom sep, Object tag) {
-        return new Repeat.Maximal(e, zero, many, sep, tag); }
-    public  static Element repeat(Element e, boolean zero, boolean many, Object tag) {
-        return new Repeat(e, zero, many, tag); }
-    public  static Element repeat(Element e, boolean zero, boolean many, Element sep, Object tag) {
-        return new Repeat(e, zero, many, sep, tag); }
 }
index e0126ab..8db19b6 100644 (file)
@@ -50,7 +50,7 @@ public class CharAtom extends Atom<Character> {
     }
 
     private static Union epsilon = new Union("()");
-    static { epsilon.add(Sequence.empty); }
+    static { epsilon.add(Sequence.empty()); }
 
     public Topology<Atom<Character>>       unwrap() { return this; }
     public Topology<Atom<Character>>       empty()  { return new CharAtom(); }
index 41e343b..3c05751 100644 (file)
@@ -128,7 +128,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             HashSet<Sequence> bad2 = new HashSet<Sequence>();
 
             Union urep = new Union(null, false);
-            urep.add(Sequence.empty);
+            urep.add(Sequence.empty());
             if (sep != null)
                 urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
             else
@@ -273,7 +273,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
     public static @bind.as("()")  ElementNode   epsilon()                         { return new Constant(epsilon); }
 
     private static Union epsilon = new Union("()");
-    static { epsilon.add(Sequence.empty); }
+    static { epsilon.add(Sequence.empty()); }
 
     public static class NonTerminalReferenceNode extends ElementNode {
         public String nonTerminal;
@@ -352,10 +352,10 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
         }
         public Element build(Context cx, NonTerminalNode cnt) {
             return (!max)
-                ? Sequence.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
+                ? Repeat.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
                 : sep==null
-                ? Sequence.repeatMaximal(e.toAtom(cx), zero, many, cx.rm.repeatTag())
-                : Sequence.repeatMaximal(e.build(cx, null), zero, many, sep.toAtom(cx), cx.rm.repeatTag());
+                ? Repeat.repeatMaximal(e.toAtom(cx), zero, many, cx.rm.repeatTag())
+                : Repeat.repeatMaximal(e.build(cx, null), zero, many, sep.toAtom(cx), cx.rm.repeatTag());
         }
     }
 
diff --git a/src/edu/berkeley/sbp/meta/Repeat.java b/src/edu/berkeley/sbp/meta/Repeat.java
new file mode 100644 (file)
index 0000000..f21e820
--- /dev/null
@@ -0,0 +1,84 @@
+package edu.berkeley.sbp.meta;
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.*;
+import java.io.*;
+import java.util.*;
+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 {
+
+    public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Object tag) {
+        this(e, zeroOkay, manyOkay, null, false, tag); }
+    public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, Object tag) {
+        this(e, zeroOkay, manyOkay, separator, false, tag); }
+    protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) {
+        this(e, zeroOkay, manyOkay, separator, maximal, tag, null); }
+    protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag, Atom follow) {
+        super(e+(!manyOkay ? "?" : (zeroOkay ? (maximal ? "**" : "*") : (maximal ? "++" : "+")))+(separator==null?"":("/"+separator)), true);
+        if (zeroOkay && !manyOkay) {
+            add(Sequence.empty().followedBy(follow));
+            add(Sequence.singleton(e).followedBy(follow));
+            return;
+        }
+        if (zeroOkay) {
+            add(Sequence.rewritingSequence(tag, new Element[] { }, null).followedBy(follow));
+            //add(new Sequence.Constant.Empty());
+            // FUGLY
+            add(Sequence.singleton(many1(e, separator, tag)).followedBy(follow));
+        } else {
+            add(Sequence.rewritingSequence(tag, new Element[] { e }, null).followedBy(follow));
+            if (separator==null)
+                add(Sequence.unwrap(new Element[] { e,                 Repeat.this }, tag, new boolean[] { false, false }).followedBy(follow));
+            else
+                add(Sequence.unwrap(new Element[] { e, separator,      Repeat.this }, tag, new boolean[] { false, true, false }).followedBy(follow));
+        }
+    }
+
+    public static class Maximal extends Repeat {
+        public Maximal(final Element e, boolean zeroOkay, boolean manyOkay, final Atom separator, Object tag) {
+            super(e, zeroOkay, manyOkay, separator, true, tag, (Atom)separator.complement());
+            if (zeroOkay && separator != null)
+                throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this);
+        }
+        public Maximal(final Atom e, boolean zeroOkay, boolean manyOkay, Object tag) {
+            super(e, zeroOkay, manyOkay, null, true, tag, (Atom)e.complement());
+        }
+    }
+
+    /** repeat zero or one times */
+    public  static Element maybe(Element e)                             { return new Repeat(e, true, false, null, null); }
+    public  static Element maybe(Element e, Object tag)                 { return new Repeat(e, true, false, null, tag); }
+    /** repeat zero or more times */
+    public  static Element many0(Element e)                             { return new Repeat(e, true, true, null, null); }
+    public  static Element many0(Element e, Object tag)                 { return new Repeat(e, true, true, null, tag); }
+    /** repeat zero or more times, separated by <tt>sep</tt> */
+    public  static Element many0(Element e, Element sep)                { return new Repeat(e, true, true, sep, null); }
+    public  static Element many0(Element e, Element sep, Object tag)    { return new Repeat(e, true, true, sep, tag); }
+    /** repeat one or more times */
+    public  static Element many1(Element e)                             { return new Repeat(e, false, true, null, null); }
+    public  static Element many1(Element e, Object tag)                 { return new Repeat(e, false, true, null, tag); }
+    /** repeat one or more times, separated by <tt>sep</tt> */
+    public  static Element many1(Element e, Element sep)                { return new Repeat(e, false, true, sep, null); }
+    public  static Element many1(Element e, Element sep, Object tag)    { return new Repeat(e, false, true, sep, tag); }
+
+    /** repeat zero or more times, matching a maximal sequence of atoms */
+    public  static Element maximal0(Atom e)                             { return new Repeat.Maximal(e, true, true, null); }
+    public  static Element maximal0(Atom e, Object tag)                 { return new Repeat.Maximal(e, true, true, tag); }
+    /** repeat one or more times, matching a maximal sequence of atoms */
+    public  static Element maximal1(Atom e)                             { return new Repeat.Maximal(e, false, true, null); }
+    public  static Element maximal1(Atom e, Object tag)                 { return new Repeat.Maximal(e, false, true, tag); }
+    /** repeat one or more times, separated by an atom <tt>sep</tt>, matching a maximal sequence */
+    public  static Element maximal1(Element e, Atom sep)                { return new Repeat.Maximal(e, false, true, sep, null); }
+    public  static Element maximal1(Element e, Atom sep, Object tag)    { return new Repeat.Maximal(e, false, true, sep, tag); }
+
+    public  static Element repeatMaximal(Atom e, boolean zero, boolean many, Object tag) {
+        return new Repeat.Maximal(e, zero, many, tag); }
+    public  static Element repeatMaximal(Element e, boolean zero, boolean many, Atom sep, Object tag) {
+        return new Repeat.Maximal(e, zero, many, sep, tag); }
+    public  static Element repeat(Element e, boolean zero, boolean many, Object tag) {
+        return new Repeat(e, zero, many, tag); }
+    public  static Element repeat(Element e, boolean zero, boolean many, Element sep, Object tag) {
+        return new Repeat(e, zero, many, sep, tag); }
+}