From 173e1696d10a91db6f5a919dbb2b2ed2c6e2a227 Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 20 Jul 2006 22:32:21 -0400 Subject: [PATCH] checkpoint darcs-hash:20060721023221-5007d-ba30b9ee61de8b4848d30fb0a9edfaa72730d635.gz --- src/edu/berkeley/sbp/Atom.java | 18 +------- src/edu/berkeley/sbp/Repeat.java | 12 +----- src/edu/berkeley/sbp/Sequence.java | 2 +- src/edu/berkeley/sbp/Walk.java | 4 +- src/edu/berkeley/sbp/chr/CharAtom.java | 19 +++++++++ src/edu/berkeley/sbp/meta/MetaGrammarBindings.java | 6 ++- src/edu/berkeley/sbp/misc/StringInput.java | 44 -------------------- src/edu/berkeley/sbp/util/TopologicalBag.java | 2 + 8 files changed, 32 insertions(+), 75 deletions(-) delete mode 100644 src/edu/berkeley/sbp/misc/StringInput.java diff --git a/src/edu/berkeley/sbp/Atom.java b/src/edu/berkeley/sbp/Atom.java index b9832b2..0989bc0 100644 --- a/src/edu/berkeley/sbp/Atom.java +++ b/src/edu/berkeley/sbp/Atom.java @@ -8,25 +8,11 @@ import edu.berkeley.sbp.*; import edu.berkeley.sbp.*; /** an element which matches exactly one input token */ -public abstract class Atom extends Element implements Topology { +public abstract class Atom extends Element implements Topology> { - protected abstract Topology top(); + public abstract Topology underlying(); public abstract String toString(); public StringBuffer toString(StringBuffer sb) { sb.append(this); return sb; } - // Topology Thunks ////////////////////////////////////////////////////////////////////////////// - - public Topology unwrap() { return top().unwrap(); } - public Topology empty() { return top().empty(); } - public boolean contains(T v) { return top().contains(v); } - public Topology intersect(Topology t) { return top().intersect(t); } - public Topology minus(Topology t) { return top().minus(t); } - public Topology union(Topology t) { return top().union(t); } - public Topology complement() { return top().complement(); } - public boolean disjoint(Topology t) { return top().disjoint(t); } - public boolean containsAll(Topology 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()); } - } diff --git a/src/edu/berkeley/sbp/Repeat.java b/src/edu/berkeley/sbp/Repeat.java index 2028847..037e67d 100644 --- a/src/edu/berkeley/sbp/Repeat.java +++ b/src/edu/berkeley/sbp/Repeat.java @@ -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 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/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index 5ee8433..1d56343 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -20,7 +20,7 @@ public abstract class Sequence extends Element implements Iterable { final Position firstp; public Atom follow = null; - public final Topology follow() { return follow; } + public final Atom follow() { return follow; } // Static Constructors ////////////////////////////////////////////////////////////////////////////// diff --git a/src/edu/berkeley/sbp/Walk.java b/src/edu/berkeley/sbp/Walk.java index b58dbc5..b3432a9 100644 --- a/src/edu/berkeley/sbp/Walk.java +++ b/src/edu/berkeley/sbp/Walk.java @@ -88,7 +88,7 @@ abstract class Walk { public WalkTokenSet(Topology cs) { this.cs = cs; } public WalkTokenSet(Topology cs, Cache c) { super(c); this.cs = cs; } public Topology bottom(Element e) { return cs; } - public Topology walkAtom(Atom r) { cs = cs.union(r); return cs; } + public Topology walkAtom(Atom r) { cs = cs.union(r.underlying()); return cs; } } static class First extends WalkTokenSet { @@ -155,7 +155,7 @@ abstract class Walk { 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) { diff --git a/src/edu/berkeley/sbp/chr/CharAtom.java b/src/edu/berkeley/sbp/chr/CharAtom.java index c0df627..89a45ad 100644 --- a/src/edu/berkeley/sbp/chr/CharAtom.java +++ b/src/edu/berkeley/sbp/chr/CharAtom.java @@ -10,9 +10,11 @@ import edu.berkeley.sbp.Input.Location; public class CharAtom extends Atom { + 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 t) { this(t instanceof CharTopology ? (CharTopology)t : new CharTopology(t)); } private CharTopology t; public Topology top() { return t; } @@ -50,4 +52,21 @@ public class CharAtom extends Atom { private static Union epsilon = new Union("()"); static { epsilon.add(Sequence.empty); } + + public Topology> unwrap() { return this; } + public Topology> empty() { return new CharAtom(); } + public Topology underlying() { return top(); } + + public boolean contains(Atom v) { return top().containsAll(((CharAtom)v).top()); } + public boolean disjoint(Topology> t) { return top().disjoint(((CharAtom)t).top()); } + public boolean containsAll(Topology> t) { return top().containsAll(((CharAtom)t).top()); } + + public Topology> complement() { return new CharAtom(top().complement()); } + public Topology> intersect(Topology> t) { return new CharAtom(top().intersect(((CharAtom)t).top())); } + public Topology> minus(Topology> t) { return new CharAtom(top().minus(((CharAtom)t).top())); } + public Topology> union(Topology> 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()); } + } diff --git a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java index 3790227..74e8a31 100644 --- a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java +++ b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java @@ -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)Atom.toAtom(e)); } - static Atom infer(Topology t) { return new CharAtom(new CharTopology(t)); } + static Atom infer(Object t) { return (Atom)t; } public static class Context { public HashMap map = new HashMap(); @@ -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 index 1f3d0d3..0000000 --- a/src/edu/berkeley/sbp/misc/StringInput.java +++ /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 char 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(left)) { public String toString() { return "{"; } }; - public static final Atom rightBrace = new StringAtom(new DiscreteTopology(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(new StringInput(s, null)); this.s = s; } - public StringAtom(Topology 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 top() { return new DiscreteTopology(); } - - 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(); } -} diff --git a/src/edu/berkeley/sbp/util/TopologicalBag.java b/src/edu/berkeley/sbp/util/TopologicalBag.java index 2595602..6e73681 100644 --- a/src/edu/berkeley/sbp/util/TopologicalBag.java +++ b/src/edu/berkeley/sbp/util/TopologicalBag.java @@ -144,6 +144,8 @@ public class TopologicalBag implements MapBag,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()); -- 1.7.10.4