+ public static @bind.as("Word") String word(String s) { return s; }
+ public static @bind.as("Quoted") String quoted(String s) { return s; }
+ public static @bind.as("escaped") String c(char c) {
+ if (c=='{') return CharAtom.left+"";
+ if (c=='}') return CharAtom.right+"";
+ return c+"";
+ }
+ public static @bind.as("EmptyString") String emptystring() { return ""; }
+ public static @bind.as("\n") String retur() { return "\n"; }
+ public static @bind.as("\r") String lf() { return "\r"; }
+
+ //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);
+ }
+ }