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()); }
-
}
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; }
- }
}
final Position firstp;
public Atom follow = null;
- public final Topology follow() { return follow; }
+ public final Atom follow() { return follow; }
// Static Constructors //////////////////////////////////////////////////////////////////////////////
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> {
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) {
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; }
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()); }
+
}
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) {
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>();
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; }
}
+ */
}
+++ /dev/null
-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(); }
-}
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());