checkpoint
authoradam <adam@megacz.com>
Fri, 21 Jul 2006 02:32:21 +0000 (22:32 -0400)
committeradam <adam@megacz.com>
Fri, 21 Jul 2006 02:32:21 +0000 (22:32 -0400)
darcs-hash:20060721023221-5007d-ba30b9ee61de8b4848d30fb0a9edfaa72730d635.gz

src/edu/berkeley/sbp/Atom.java
src/edu/berkeley/sbp/Repeat.java
src/edu/berkeley/sbp/Sequence.java
src/edu/berkeley/sbp/Walk.java
src/edu/berkeley/sbp/chr/CharAtom.java
src/edu/berkeley/sbp/meta/MetaGrammarBindings.java
src/edu/berkeley/sbp/misc/StringInput.java [deleted file]
src/edu/berkeley/sbp/util/TopologicalBag.java

index b9832b2..0989bc0 100644 (file)
@@ -8,25 +8,11 @@ import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.*;
 
 /** <font color=green>an element which matches exactly one input token</font> */
-public abstract class Atom<T> extends Element implements Topology<T> {
+public abstract class Atom<T> extends Element implements Topology<Atom<T>> {
 
-    protected abstract Topology<T> top();
+    public  abstract Topology<T>  underlying();
     public    abstract String       toString();
     public             StringBuffer toString(StringBuffer sb) { sb.append(this); return sb; }
 
-    // Topology Thunks //////////////////////////////////////////////////////////////////////////////
-
-    public Topology<T>       unwrap()                   { return top().unwrap(); }
-    public Topology<T>       empty()                    { return top().empty(); }
-    public boolean           contains(T v)              { return top().contains(v); }
-    public Topology<T>       intersect(Topology<T> t)   { return top().intersect(t); }
-    public Topology<T>       minus(Topology<T> t)       { return top().minus(t); }
-    public Topology<T>       union(Topology<T> t)       { return top().union(t); }
-    public Topology<T>       complement()               { return top().complement(); }
-    public boolean           disjoint(Topology<T> t)    { return top().disjoint(t); }
-    public boolean           containsAll(Topology<T> t) { return top().containsAll(t); }
-    public int               hashCode()                 { return top().hashCode(); }
-    public boolean           equals(Object o)           { return o != null && o instanceof Atom && ((Atom)o).top().equals(top()); }
-
 }
 
index 2028847..037e67d 100644 (file)
@@ -41,21 +41,13 @@ class Repeat extends Union {
             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 = new Invert(separator);
+                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 = new Invert(e);
+                s.follow = (Atom)e.complement();
         }
     }
 
-
-    /** 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; }
-    }
 }
index 5ee8433..1d56343 100644 (file)
@@ -20,7 +20,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     final Position          firstp;
 
     public Atom follow = null;
-    public final Topology follow() { return follow; }
+    public final Atom follow() { return follow; }
 
     // Static Constructors //////////////////////////////////////////////////////////////////////////////
 
index b58dbc5..b3432a9 100644 (file)
@@ -88,7 +88,7 @@ abstract class Walk<T> {
         public WalkTokenSet(Topology<Tok> cs)          { this.cs = cs; }
         public WalkTokenSet(Topology<Tok> cs, Cache c) { super(c); this.cs = cs; }
         public Topology<Tok> bottom(Element e)         { return cs; }
-        public Topology<Tok> walkAtom(Atom r)          { cs = cs.union(r); return cs; }
+        public Topology<Tok> walkAtom(Atom r)          { cs = cs.union(r.underlying()); return cs; }
     }
 
     static class First<Tok extends Input> extends WalkTokenSet<Tok> {
@@ -155,7 +155,7 @@ abstract class Walk<T> {
 
             if (e instanceof Sequence) {
                 Sequence s = (Sequence)e;
-                if (s.follow() != null) cs = cs.intersect(s.follow());
+                if (s.follow() != null) cs = cs.intersect(s.follow().underlying());
             }
 
             if (c != null && e==me) {
index c0df627..89a45ad 100644 (file)
@@ -10,9 +10,11 @@ import edu.berkeley.sbp.Input.Location;
 
 public class CharAtom extends Atom<Character> {
 
+    public CharAtom() { this(new CharTopology()); }
     public CharAtom(char a) { this(a,a); }
     public CharAtom(char a, char b) { this(new CharTopology(a, b)); }
     public CharAtom(CharTopology t) { this.t = t; }
+    public CharAtom(Topology<Character> t) { this(t instanceof CharTopology ? (CharTopology)t : new CharTopology(t)); }
 
     private CharTopology t;
     public  Topology<Character> top() { return t; }
@@ -50,4 +52,21 @@ public class CharAtom extends Atom<Character> {
 
     private static Union epsilon = new Union("()");
     static { epsilon.add(Sequence.empty); }
+
+    public Topology<Atom<Character>>       unwrap() { return this; }
+    public Topology<Atom<Character>>       empty()  { return new CharAtom(); }
+    public Topology<Character>             underlying()  { return top(); }
+
+    public boolean           contains(Atom<Character> v) { return top().containsAll(((CharAtom)v).top()); }
+    public boolean           disjoint(Topology<Atom<Character>> t) { return top().disjoint(((CharAtom)t).top()); }
+    public boolean           containsAll(Topology<Atom<Character>> t) { return top().containsAll(((CharAtom)t).top()); }
+
+    public Topology<Atom<Character>>       complement() { return new CharAtom(top().complement()); }
+    public Topology<Atom<Character>>       intersect(Topology<Atom<Character>> t) { return new CharAtom(top().intersect(((CharAtom)t).top())); }
+    public Topology<Atom<Character>>       minus(Topology<Atom<Character>> t) { return new CharAtom(top().minus(((CharAtom)t).top())); }
+    public Topology<Atom<Character>>       union(Topology<Atom<Character>> t) { return new CharAtom(top().union(((CharAtom)t).top())); }
+
+    public int     hashCode() { return top().hashCode(); }
+    public boolean equals(Object o) { return o != null && (o instanceof CharAtom) && ((CharAtom)o).top().equals(top()); }
+
 }
index 3790227..74e8a31 100644 (file)
@@ -68,7 +68,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             Atom ret = null;
             for(Seq[] ss : sequences)
                 for(Seq s : ss)
-                    ret = ret==null ? s.toAtom(cx) : infer(ret.union(s.toAtom(cx)));
+                    ret = ret==null ? s.toAtom(cx) : (Atom)ret.union(s.toAtom(cx));
             return ret;
         }
         public void build(Context cx, Union u, NonTerminalNode cnt) {
@@ -406,7 +406,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
     public static @bind.as("\r")          String lf() { return "\r"; }
 
     //static Atom infer(Element e)  { return infer((Topology<Character>)Atom.toAtom(e)); }
-    static Atom infer(Topology<Character> t) { return new CharAtom(new CharTopology(t)); }
+    static Atom infer(Object t) { return (Atom)t; }
 
     public static class Context {
         public HashMap<String,Union> map = new HashMap<String,Union>();
@@ -497,10 +497,12 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
         public String getLabel() { return label; }
     }
 
+    /*
     static class Invert extends Atom {
         private final Atom a;
         public Invert(Atom a) { this.a = a; }
         public Topology top() { return a.complement(); }
         public String toString() { return "~"+a; }
     }
+    */
 }
diff --git a/src/edu/berkeley/sbp/misc/StringInput.java b/src/edu/berkeley/sbp/misc/StringInput.java
deleted file mode 100644 (file)
index 1f3d0d3..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-package edu.berkeley.sbp.misc;
-import java.io.*;
-import java.util.*;
-import java.lang.reflect.*;
-import java.lang.ref.*;
-import edu.berkeley.sbp.*;
-import edu.berkeley.sbp.Input.Location;
-import edu.berkeley.sbp.util.*;
-
-/** an implementation of Input for streams of Java <tt>char</tt> values */
-public class StringInput {
-
-    public static final StringInput left = new StringInput(null, null) { public boolean equals(Object o) { return this==o; } };
-    public static final StringInput right = new StringInput(null, null) { public boolean equals(Object o) { return this==o; } };
-
-    public static final Atom leftBrace  = new StringAtom(new DiscreteTopology<StringInput>(left)) { public String toString() { return "{"; } };
-    public static final Atom rightBrace = new StringAtom(new DiscreteTopology<StringInput>(right)) { public String toString() { return "}"; } };
-
-    private static class StringAtom extends Atom {
-        private String s;
-        private Topology t;
-        public StringAtom(String s) { this.t = new DiscreteTopology<StringInput>(new StringInput(s, null)); this.s = s; }
-        public StringAtom(Topology<StringInput> t) { this.t = t; }
-        public String toString() { return "[atom \""+s+"\"]"; }
-        public Topology top() { return t; }
-    }
-
-    /** returns an element which exactly matches the string given */
-    public static Element string(String s) {
-        return new StringAtom(s);
-    }
-
-    public static Topology<StringInput> top() { return new DiscreteTopology<StringInput>(); }
-
-    public final String s;
-    public final Location location;
-    private StringInput(String s, Location loc)    { this.s = s; this.location = loc; }
-    public String result()                         { return s; }
-    public Location getLocation()                  { return location; }
-    public String  toString()                      { return "\'"+StringUtil.escapify(s)+"\'"; }
-
-    public boolean equals(Object o) { return o!=null && o instanceof StringInput && s.equals(((StringInput)o).s); }
-    public int     hashCode()       { return s==null ? 0 : s.hashCode(); }
-}
index 2595602..6e73681 100644 (file)
@@ -144,6 +144,8 @@ public class TopologicalBag<K,V> implements MapBag<Topology<K>,V>, VisitableMap<
             for(V vv : h.get(t)) al.add(vv);
             Object[] vs = new Object[al.size()];
             al.toArray(vs);
+            // XXX hack
+            if (t instanceof Atom) t = ((Atom)t).underlying();
             IntegerTopology it = (IntegerTopology)t;
             for(Range r : it.getRanges()) {
                 min_.add(r.isMinNegInf() ? Long.MIN_VALUE : r.getMin());