+ //static Atom infer(Element e) { return infer((Topology<Character>)Atom.toAtom(e)); }
+ static Atom infer(Object t) { return (Atom)t; }
+
+ public static class Context {
+ public HashMap<String,Union> map = new HashMap<String,Union>();
+ public GrammarNode grammar;
+ public String cnt = null;
+ public Grammar.Bindings rm;
+ public Context(GrammarNode g, Grammar.Bindings rm) {
+ this.grammar = g;
+ this.rm = rm;
+ }
+ public Union build() {
+ Union ret = null;
+ for(NonTerminalNode nt : grammar.values()) {
+ Union u = get(nt.name);
+ if ("s".equals(nt.name))
+ ret = u;
+ }
+ return ret;
+ }
+ public Context(Tree t, Grammar.Bindings rm) {
+ this.rm = rm;
+ TreeFunctor<Object,Object> red = (TreeFunctor<Object,Object>)t.head();
+ this.grammar = (GrammarNode)red.invoke(t);
+ }
+ public Union peek(String name) { return map.get(name); }
+ public void put(String name, Union u) { map.put(name, u); }
+ public Union get(String name) {
+ Union ret = map.get(name);
+ if (ret != null) return ret;
+ ret = new Union(name);
+ map.put(name, ret);
+ NonTerminalNode nt = grammar.get(name);
+ if (nt==null) {
+ throw new Error("warning could not find " + name);
+ } else {
+ String old = cnt;
+ cnt = name;
+ nt.build(this, ret, nt);
+ cnt = old;
+ }
+ return ret;
+ }
+
+ }
+
+ public static abstract class ElementNode {
+ public String getLabel() { return null; }
+ public String getOwnerTag() { return null; }
+ public boolean drop() { return false; }
+ public Atom toAtom(Context cx) { throw new Error("can't convert a " + this.getClass().getName() + " to an atom"); }
+ public abstract Element build(Context cx, NonTerminalNode cnt);
+ }
+
+ public static abstract class ElementNodeWrapper extends ElementNode {
+ protected ElementNode _e;
+ public ElementNodeWrapper(ElementNode e) { this._e = e; }
+ public String getLabel() { return _e.getLabel(); }
+ public String getOwnerTag() { return _e.getOwnerTag(); }
+ public boolean drop() { return _e.drop(); }
+ public Atom toAtom(Context cx) { return _e.toAtom(cx); }
+ public Element build(Context cx, NonTerminalNode cnt) { return _e.build(cx, cnt); }
+ }
+
+ public static class Constant extends ElementNode {
+ Element constant;
+ public Constant(Element constant) { this.constant = constant; }
+ public Element build(Context cx, NonTerminalNode cnt) { return constant; }
+ public Atom toAtom(Context cx) {
+ if (constant instanceof Atom) return ((Atom)constant);
+ return super.toAtom(cx);
+ }
+ }
+
+ public abstract static class PostProcess extends ElementNodeWrapper {
+ public PostProcess(ElementNode e) { super(e); }
+ public Element build(Context cx, NonTerminalNode cnt) { return postProcess(_e.build(cx, cnt)); }
+ public abstract Element postProcess(Element e);
+ }
+
+ public static class Drop extends ElementNodeWrapper {
+ public Drop(ElementNode e) { super(e); }
+ public boolean drop() { return true; }
+ }
+
+ public static class Label extends ElementNodeWrapper {
+ public String label;
+ public Label(String label, ElementNode e) { super(e); this.label = label; }
+ 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; }
+ }
+ */