1 package edu.berkeley.sbp;
4 import java.lang.reflect.*;
5 import java.lang.ref.*;
6 import edu.berkeley.sbp.util.*;
7 import edu.berkeley.sbp.*;
8 import edu.berkeley.sbp.*;
10 /** an element which matches exactly one input token */
11 public abstract class Atom<T> extends Element implements Topology<T> {
13 protected abstract Topology<T> top();
14 public abstract String toString();
15 public StringBuffer toString(StringBuffer sb) { sb.append(this); return sb; }
17 // Topology Thunks //////////////////////////////////////////////////////////////////////////////
19 public Topology<T> unwrap() { return top().unwrap(); }
20 public Topology<T> empty() { return top().empty(); }
21 public boolean contains(T v) { return top().contains(v); }
22 public Topology<T> intersect(Topology<T> t) { return top().intersect(t); }
23 public Topology<T> minus(Topology<T> t) { return top().minus(t); }
24 public Topology<T> union(Topology<T> t) { return top().union(t); }
25 public Topology<T> complement() { return top().complement(); }
26 public boolean disjoint(Topology<T> t) { return top().disjoint(t); }
27 public boolean containsAll(Topology<T> t) { return top().containsAll(t); }
28 public int hashCode() { return top().hashCode(); }
29 public boolean equals(Object o) { return o != null && o instanceof Atom && ((Atom)o).top().equals(top()); }
31 // Subclasses //////////////////////////////////////////////////////////////////////////////
33 /** an atom which tracks the possible tokenset of some element, provided that element can only match single-token sequences */
34 public static class Infer<T extends Input> extends Atom<T> {
35 private final Element e;
36 public Infer(Element e) { this.e = e; }
37 public Topology<T> top() { return (Topology<T>)toAtom(e); }
38 public String toString() { return e.toString(); }
41 /** an atom which tracks the inverse of some other atom */
42 public static class Invert<T extends Input> extends Atom<T> {
43 private final Atom<T> a;
44 public Invert(Atom<T> a) { this.a = a; }
45 public Topology<T> top() { return ((Topology<T>)a.top()).complement(); }
46 public String toString() { return "~"+a; }
49 static Topology toAtom(Element e) {
50 if (e instanceof Atom) return (Atom)e;
51 if (e instanceof Sequence) return ((Sequence)e).toAtom();
53 for(Sequence s : (Union)e)
54 ret = ret==null ? toAtom(s) : ret.union(s.toAtom());