MAJOR: huge revamp of Sequence, Result, Reduction, Parser, Node, GSS
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammarBindings.java
index ca24fd0..927cd62 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
+
 package edu.berkeley.sbp.meta;
 import edu.berkeley.sbp.util.*;
 import edu.berkeley.sbp.*;
@@ -9,99 +11,164 @@ import java.lang.annotation.*;
 import java.lang.reflect.*;
 import java.io.*;
 
-/** The java classes typically used to represent a parsed grammar AST */
-public class MetaGrammarBindings {
+/** The java classes typically used to represent a parsed grammar AST; each inner class is a type of AST node. */
+public class MetaGrammarBindings extends AnnotationGrammarBindings {
+
+    public MetaGrammarBindings() { super(MetaGrammarBindings.class); }
 
+    // FIXME ugly ugly ugly scary dangerous
+    public static String prefix = "";
+    
     /** A grammar (a set of nonterminals) */
-    public static class Grammar extends HashMap<String,NonTerminal> {
-        public @bind Grammar(NonTerminal[] nonterminals) {
-            for(NonTerminal nt : nonterminals) this.put(nt.name, nt); }
+    public static class GrammarNode extends HashMap<String,NonTerminalNode> {
+        public NonTerminalNode[] getNonTerminals() {
+            return (NonTerminalNode[])values().toArray(new NonTerminalNode[0]);
+        }
+        public GrammarNode(NonTerminalNode[] nonterminals) {
+            for(NonTerminalNode nt : nonterminals) {
+                if (nt==null) continue;
+                if (this.get(nt.name)!=null)
+                    throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
+                this.put(nt.name, nt);
+            }
+        }
+        public @bind.as("Grammar") GrammarNode(Object[] nt) { add(nt); }
+        private void add(Object[] obs) {
+            for(Object o : obs) {
+                if (o==null) continue;
+                else if (o instanceof Object[]) add((Object[])o);
+                else if (o instanceof NonTerminalNode) {
+                    NonTerminalNode nt = (NonTerminalNode)o;
+                    if (this.get(nt.name)!=null)
+                        throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
+                    this.put(nt.name, nt);
+                }
+                else if (o instanceof GrammarNode) add(((GrammarNode)o).getNonTerminals());
+            }
+        }
         public String toString() {
             String ret = "[ ";
-            for(NonTerminal nt : values()) ret += nt + ", ";
+            for(NonTerminalNode nt : values()) ret += nt + ", ";
             return ret + " ]";
         }
+        public Union build(String s, Grammar.Bindings rm) {
+            Context cx = new Context(this,rm);
+            Union u = null;
+            for(MetaGrammarBindings.NonTerminalNode nt : values()) {
+                Union el = (Union)cx.get(nt.name);
+                StringBuffer st = new StringBuffer();
+                el.toString(st);
+                if (nt.name.equals(s)) u = el;
+            }
+            return u;
+        }
     }
 
-    public abstract static class Un extends El {
+    public abstract static class UnionNode extends ElementNode {
         public Seq[][] sequences;
-        public void build(MetaGrammar.Context cx, Union u) {
+        public Atom toAtom(Context cx) {
+            Atom ret = null;
+            for(Seq[] ss : sequences)
+                for(Seq s : ss)
+                    ret = ret==null ? s.toAtom(cx) : (Atom)ret.union(s.toAtom(cx));
+            return ret;
+        }
+        public void build(Context cx, Union u, NonTerminalNode cnt) {
             HashSet<Sequence> bad2 = new HashSet<Sequence>();
             for(int i=0; i<sequences.length; i++) {
                 Seq[] group = sequences[i];
-                Union u2 = new Union();
+                Union u2 = new Union(null, false);
                 if (sequences.length==1) u2 = u;
-                for(int j=0; j<group.length; j++) {
-                    group[j].build(cx, u2, false);
-                }
+                for(int j=0; j<group.length; j++)
+                    group[j].build(cx, u2, cnt);
                 if (sequences.length==1) break;
-                Sequence seq = Sequence.singleton(u2);
-                for(Sequence s : bad2) {
-                    s.lame = true;
-                    seq = seq.not(s);
-                }
+                Sequence seq = Sequence.create(u2);
+                for(Sequence s : bad2) seq = seq.andnot(s);
                 u.add(seq);
-                bad2.add(Sequence.singleton(u2));
+                bad2.add(Sequence.create(u2));
             }
         }
     }
 
-    public static class NonTerminal extends Un {
+    public static @bind.as("#import") GrammarNode poundimport(String fileName, String as) {
+        if (as==null) as = "";
+        else if ("".equals(as)) { }
+        else as = as +".";
+
+        try {
+            Tree t = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream("tests/"+fileName)).expand1();
+            TreeFunctor<Object,Object> red = (TreeFunctor<Object,Object>)t.head();
+            String oldprefix = prefix;
+            prefix = as;
+            GrammarNode gn = (GrammarNode)red.invoke(t);
+            prefix = oldprefix;
+            return gn;
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static class NonTerminalNode extends UnionNode {
         public boolean rep;
         public String  name = null;
         public String sep = null;
-        public @bind NonTerminal(@bind.arg String name, @bind.arg Seq[][] sequences) { this(name, sequences, false); }
-        public NonTerminal(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
-        public NonTerminal(String name, Seq[][] sequences, boolean rep, String sep) {
-            this.name = name;
+        public NonTerminalNode[] getNonTerminals() { return new NonTerminalNode[] { this }; }
+        public @bind.as("NonTerminal") NonTerminalNode(@bind.arg String name, @bind.arg Seq[][] sequences) {
+            this(name, sequences, false); }
+        public NonTerminalNode(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
+        public NonTerminalNode(String name, Seq[][] sequences, boolean rep, String sep) {
+            this.name = prefix + name;
             this.sequences = sequences;
             this.rep = rep;
-            this.sep = sep;
+            this.sep = sep==null?null:(prefix + sep);
         }
-        public Element build(MetaGrammar.Context cx) { return cx.get(name); }
-        public void build(MetaGrammar.Context cx, Union u) {
-            if (!rep) { super.build(cx, u); return; }
+        public Element build(Context cx, NonTerminalNode cnt) { return cx.get(name); }
+        public void build(Context cx, Union u, NonTerminalNode cnt) {
+            if (!rep) { super.build(cx, u, this); return; }
             HashSet<Sequence> bad2 = new HashSet<Sequence>();
 
-            Union urep = new Union();
-            urep.add(Sequence.empty);
-            urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
+            Union urep = new Union(null, false);
+            urep.add(Sequence.create());
+            if (sep != null)
+                urep.add(Sequence.create(new Element[] { cx.get(sep), u }, 1));
+            else
+                urep.add(Sequence.create(new Element[] { u }, 0));
 
             for(int i=0; i<sequences.length; i++) {
                 Seq[] group = sequences[i];
-                Union u2 = new Union();
+                Union u2 = new Union(null, false);
                 if (sequences.length==1) u2 = u;
                 for(int j=0; j<group.length; j++) {
-                    Union u3 = new Union();
-                    group[j].build(cx, u3, false);
-                    Sequence s = Sequence.unwrap(new Element[] { u3, urep },
-                                                 cx.rm.repeatTag(),
-                                                 new boolean[] { false, false });
+                    Union u3 = new Union(null, false);
+                    group[j].build(cx, u3, this);
+                    Sequence s = Sequence.create(cx.rm.repeatTag(),
+                                                 new Element[] { u3, urep },
+                                                 new boolean[] { false, false },
+                                                 true);
                     u2.add(s);
                 }
                 if (sequences.length==1) break;
-                Sequence seq = Sequence.singleton(u2);
-                for(Sequence s : bad2) {
-                    s.lame = true;
-                    seq = seq.not(s);
-                }
+                Sequence seq = Sequence.create(u2);
+                for(Sequence s : bad2) seq = seq.andnot(s);
                 u.add(seq);
-                bad2.add(Sequence.singleton(u2));
+                bad2.add(Sequence.create(u2));
             }
         }
     }
-    public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg Seq[][] sequences) { return new NonTerminal(name, sequences, true); }
-    public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
-        return new NonTerminal(name, sequences, true, sep);
-    }
 
-    public static class AnonUn extends Un {
-        public @bind.as("(") AnonUn(Seq[][] sequences) {
+    public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg Seq[][] sequences) {
+        return new NonTerminalNode(name, sequences, true); }
+    public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
+        return new NonTerminalNode(name, sequences, true, sep); }
+
+    public static class AnonUnionNode extends UnionNode {
+        public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
             this.sequences = sequences;
         }
-        public Element build(MetaGrammar.Context cx) {
-            Union ret = new Union();
-            build(cx, ret);
+        public Element build(Context cx, NonTerminalNode cnt) {
+            Union ret = new Union(null, false);
+            build(cx, ret, cnt);
             return ret;
         }
     }
@@ -112,57 +179,42 @@ public class MetaGrammarBindings {
         public char first;
         public char last;
     }
-    public static abstract class El {
-        public String getLabel() { return null; }
-        public String getOwnerTag() { return null; }
-        public boolean drop() { return false; }
-        public abstract Element build(MetaGrammar.Context cx);
-    }
-    public static class Drop extends El {
-        public El e;
-        public Drop(El e) { this.e = e; }
-        public String getLabel() { return null; }
-        public boolean drop() { return true; }
-        public String getOwnerTag() { return e.getOwnerTag(); }
-        public Element build(MetaGrammar.Context cx) { return e.build(cx); }
-    }
-    public static class Label extends El {
-        public String label;
-        public El e;
-        public Label(String label, El e) { this.e = e; this.label = label; }
-        public String getLabel() { return label; }
-        public String getOwnerTag() { return e.getOwnerTag(); }
-        public Element build(MetaGrammar.Context cx) { return e.build(cx); }
-    }
+
     public static /*abstract*/ class Seq {
         HashSet<Seq> and = new HashSet<Seq>();
         HashSet<Seq> not = new HashSet<Seq>();
-        El[] elements;
-        El follow;
+        ElementNode[] elements;
+        ElementNode follow;
         String tag = null;
-        boolean lame;
-        public void append(El e) {
-            El[] elements = new El[this.elements.length+1];
+        public void append(ElementNode e) {
+            ElementNode[] elements = new ElementNode[this.elements.length+1];
             System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
             this.elements = elements;
             elements[elements.length-1] = e;
         }
-        public Seq(El e) { this(new El[] { e }); }
-        public Seq(El[] elements) { this.elements = elements; }
-        public Seq tag(String tag) { this.tag = tag; return this; }
-        public Seq follow(El follow) { this.follow = follow; return this; }
+        public Seq(ElementNode e) { this(new ElementNode[] { e }); }
+        public Seq(ElementNode[] elements) { this.elements = elements; }
+        public Atom toAtom(Context cx) {
+            if (elements.length != 1) throw new Error("you attempted to use ->, **, ++, or a similar character-class operator on a [potentially] multicharacter production");
+            return elements[0].toAtom(cx);
+        }
+        public Seq tag(String tag) { this.tag = prefix+tag; return this; }
+        public Seq follow(ElementNode follow) {
+            this.follow = follow;
+            return this;
+       }
         public Seq dup() {
             Seq ret = new Seq(elements);
             ret.and.addAll(and);
             ret.not.addAll(not);
             ret.follow = follow;
-            ret.tag = tag;
+            ret.tag = prefix+tag;
             return ret;
         }
-        public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
-        public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
-        public Seq separate(El sep) {
-            El[] elements = new El[this.elements.length * 2 - 1];
+        public Seq and(Seq s) { and.add(s); return this; }
+        public Seq andnot(Seq s) { not.add(s); return this; }
+        public Seq separate(ElementNode sep) {
+            ElementNode[] elements = new ElementNode[this.elements.length * 2 - 1];
             for(int i=0; i<this.elements.length; i++) {
                 elements[i*2]   = this.elements[i];
                 if (i<this.elements.length-1)
@@ -171,162 +223,292 @@ public class MetaGrammarBindings {
             this.elements = elements;
             return this;
         }
-        public Sequence build(MetaGrammar.Context cx, Union u, boolean lame) {
-            Sequence ret = build0(cx, lame || this.lame);
-            for(Seq s : and) { Sequence dork = s.build(cx, u, true); ret = ret.and(dork); }
-            for(Seq s : not) { Sequence dork = s.build(cx, u, true); ret = ret.not(dork); }
-            u.add(ret);
-            ret.lame = lame;
+        public Sequence build(Context cx, Union u, NonTerminalNode cnt) {
+            Sequence ret = build0(cx, cnt);
+            for(Seq s : and) { Sequence dork = s.build(cx, null, cnt); ret = ret.and(dork); }
+            for(Seq s : not) { Sequence dork = s.build(cx, null, cnt); ret = ret.andnot(dork); }
+            if (u!=null) u.add(ret);
             return ret;
         }
-        public Sequence build0(MetaGrammar.Context cx, boolean lame) {
-            boolean unwrap = false;
-            boolean dropAll = lame;
-            if (tag!=null && tag.equals("[]")) unwrap  = true;
-            if (tag!=null && "()".equals(tag)) dropAll = true;
-            Object[] labels = new Object[elements.length];
+        public Sequence build0(Context cx, NonTerminalNode cnt) {
             boolean[] drops = new boolean[elements.length];
             Element[] els = new Element[elements.length];
             for(int i=0; i<elements.length; i++) {
-                labels[i] = elements[i].getLabel();
                 drops[i]  = elements[i].drop();
-                els[i] = elements[i].build(cx);
+                els[i] = elements[i].build(cx, cnt);
                 if (elements[i].getOwnerTag() != null)
                     tag = elements[i].getOwnerTag();
             }
             Sequence ret = null;
-            if (dropAll)     ret = Sequence.drop(els, false);
-            else if (unwrap) ret = Sequence.unwrap(els, cx.rm.repeatTag(), drops);
-            else {
-                ret = cx.rm.tryResolveTag(tag, cx.cnt, els, labels, drops);
-                if (ret == null) {
-                    int idx = -1;
-                    for(int i=0; i<els.length; i++)
-                        if (!drops[i])
-                            if (idx==-1) idx = i;
-                            else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
-                    if (idx != -1) ret = Sequence.singleton(els, idx);
-                    else           ret = Sequence.drop(els, false);
-                }
+            Production prod = new Production(tag, (cnt==null?null:cnt.name), els, drops);
+            ret = cx.rm.createSequence(prod);
+            if (ret == null) {
+                int idx = -1;
+                for(int i=0; i<els.length; i++)
+                    if (!drops[i])
+                        if (idx==-1) idx = i;
+                        else throw new Error("multiple non-dropped elements in sequence: " + Sequence.create(els, null));
+                if (idx != -1) ret = Sequence.create(els, idx);
+                else           ret = Sequence.create(els, null);
             }
             if (this.follow != null)
-                ret.follow = infer(this.follow.build(cx));
-            ret.lame = this.lame;
+                ret = ret.followedBy(this.follow.toAtom(cx));
             return ret;
         }
     }
-    public static @bind.as("&")   Seq  and2(Seq s,        Seq a) { return s.and(a); }
-    public static @bind.as("&~")  Seq  andnot2(Seq s,     Seq a) { return s.andnot(a); }
-    public static @bind.as("->")  Seq  arrow(Seq s, El e)                { return s.follow(e); }
-    public static @bind.as("::")  Seq  tag(String tagname, Seq s)        { return s.tag(tagname); }
-    public static @bind.as("/")   Seq  slash(Seq s, El e)                { return s.separate(e); }
-
-    public static Seq  seq(El[] elements)               { return new Seq(elements); }
-    public static @bind.as("Elements")  Seq  seq2(El[] elements)               { return new Seq(elements); }
+    public static @bind.as("&")   Seq  and2(Seq s,        Seq a)   { return s.and(a); }
+    public static @bind.as("&~")  Seq  andnot2(Seq s,     Seq a)   { return s.andnot(a); }
+    public static @bind.as("->")  Seq  arrow(Seq s, ElementNode e) { return s.follow(e); }
+    public static @bind.as("::")  Seq  tag(String tagname, Seq s)  { return s.tag(tagname); }
+    public static @bind.as("/")   Seq  slash(Seq s, ElementNode e) { return s.separate(e); }
+
+    public static Seq  seq(ElementNode[] elements)               { return new Seq(elements); }
+    public static @bind.as("Elements")  Seq  seq2(ElementNode[] elements)               { return new Seq(elements); }
     public static @bind.as        Seq  psx(Seq s)                        { return s; }
-    public static @bind.as(":")   El   colon(String s, El e)             { return new Label(s, e); }
+    public static @bind.as(":")   ElementNode   colon(String s, ElementNode e)             { return new Label(s, e); }
     public static @bind.as(")")   void close(String foo)                 { throw new Error("not supported"); }
-    public static @bind.as("()")  El   epsilon()                         { return new Constant(Union.epsilon); }
+    public static @bind.as("()")  ElementNode   epsilon()                         { return new Constant(epsilon); }
+
+    private static Union epsilon = new Union("()");
+    static { epsilon.add(Sequence.create()); }
 
-    public static @bind class NonTerminalReference extends El {
-        public @bind.arg String nonTerminal;
-        public Element build(MetaGrammar.Context cx) { return cx.get(nonTerminal); }
+    public static class NonTerminalReferenceNode extends ElementNode {
+        public String nonTerminal;
+        public NonTerminalReferenceNode() { }
+        public @bind.as("NonTerminalReference") NonTerminalReferenceNode(String nonTerminal) {
+            this.nonTerminal = prefix + nonTerminal;
+        }
+        public Atom toAtom(Context cx) {
+            return cx.grammar.get(nonTerminal).toAtom(cx);
+        }
+        public Element build(Context cx, NonTerminalNode cnt) {
+            if (!this.nonTerminal.startsWith(prefix)) nonTerminal = prefix + nonTerminal;
+            Element ret = cx.get(nonTerminal);
+            if (ret == null) throw new RuntimeException("unknown nonterminal \""+nonTerminal+"\"");
+            return ret;
+        }
     }
 
     public static class Literal extends Constant {
-        public @bind Literal(@bind.arg String string) { super(CharRange.string(string)); }
+        private String string;
+        public @bind Literal(@bind.arg String string) {
+            super(CharAtom.string(string));
+            this.string = string;
+        }
         public boolean drop() { return true; }
+        public Atom toAtom(Context cx) {
+            if (string.length()!=1) return super.toAtom(cx);
+            edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
+            set.add(string.charAt(0), string.charAt(0));
+            return CharAtom.set(set);
+        }
     }
 
-    public static                     class CharClass            extends El {
+    public static                     class CharClass            extends ElementNode {
         Range[] ranges;
         public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
-        public Element build(MetaGrammar.Context cx) {
+        public Atom toAtom(Context cx) {
+            edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
+            for(Range r : ranges)
+                set.add(r.first, r.last);
+            return CharAtom.set(set);
+        }
+        public Element build(Context cx, NonTerminalNode cnt) {
             edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
             for(Range r : ranges)
                 set.add(r.first, r.last);
-            return CharRange.set(set);
+            return CharAtom.set(set);
         }
     }
 
-    public static @bind.as("{")           class XTree                 extends El {
+    public static @bind.as("\\{") ElementNode   leftBrace() {
+        return new Drop(new CharClass(new Range[] { new Range(CharAtom.left, CharAtom.left) })); }
+    public static @bind.as("\\}") ElementNode   rightBrace() {
+        return new Drop(new CharClass(new Range[] { new Range(CharAtom.right, CharAtom.right) })); }
+
+    public static @bind.as("{")           class XTree                 extends ElementNode {
         public @bind.arg Seq body;
-        public Element build(MetaGrammar.Context cx) {
-            Union u = new Union();
-            Sequence s = body.build(cx, u, false);
-            Union u2 = new Union();
-            u2.add(Sequence.singleton(new Element[] {
-                CharRange.leftBrace,
+        public Element build(Context cx, NonTerminalNode cnt) {
+            Union u = new Union(null, false);
+            Sequence s = body.build(cx, u, null);
+            Union u2 = new Union(null, false);
+            u2.add(Sequence.create(new Element[] {
+                CharAtom.leftBrace,
                 cx.get("ws"),
                 u,
                 cx.get("ws"),
-                CharRange.rightBrace
+                CharAtom.rightBrace
             }, 2));
             return u2;
         }
     }
 
-    public static class Rep extends El {
-        public El e, sep;
+    public static class Rep extends ElementNode {
+        public ElementNode e, sep;
         public boolean zero, many, max;
-        public Rep(El e, El sep, boolean zero, boolean many, boolean max) {
+        public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
             this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
-        public Element build(MetaGrammar.Context cx) {
+        public Atom toAtom(Context cx) {
+            if (sep != null) return super.toAtom(cx);
+            return e.toAtom(cx);
+        }
+        public Element build(Context cx, NonTerminalNode cnt) {
             return (!max)
-                ? Sequence.repeat(e.build(cx),        zero, many, sep==null ? null : sep.build(cx), cx.rm.repeatTag())
+                ? Repeat.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
                 : sep==null
-                ? Sequence.repeatMaximal(infer(e.build(cx)), zero, many,                                   cx.rm.repeatTag())
-                : Sequence.repeatMaximal(e.build(cx),                    zero, many, infer(sep.build(cx)), cx.rm.repeatTag());
+                ? Repeat.repeatMaximal(e.toAtom(cx), zero, many, cx.rm.repeatTag())
+                : Repeat.repeatMaximal(e.build(cx, null), zero, many, sep.toAtom(cx), cx.rm.repeatTag());
         }
     }
-    public static class Constant extends El {
-        Element constant;
-        public Constant(Element constant) { this.constant = constant; }
-        public Element build(MetaGrammar.Context cx) { return constant; }
-    }
-    public abstract static class PostProcess extends El {
-        El e;
-        public PostProcess(El e) { this.e = e; }
-        public Element build(MetaGrammar.Context cx) { return postProcess(e.build(cx)); }
-        public abstract Element postProcess(Element e);
-    }
 
     // FIXME: it would be nice if we could hoist this into "Rep"
-    public static @bind.as("++")  El plusmax(final El e)                     { return new Rep(e, null, false, true, true); }
-    public static @bind.as("+")   El plus(final El e)                        { return new Rep(e, null, false, true, false); }
-    public static @bind.as("++/") El plusmaxfollow(final El e, final El sep) { return new Rep(e, sep,  false, true, true); }
-    public static @bind.as("+/")  El plusfollow(final El e, final El sep)    { return new Rep(e, sep,  false, true, false); }
-    public static @bind.as("**")  El starmax(final El e)                     { return new Rep(e, null, true,  true, true); }
-    public static @bind.as("*")   El star(final El e)                        { return new Rep(e, null, true,  true, false); }
-    public static @bind.as("**/") El starmaxfollow(final El e, final El sep) { return new Rep(e, sep,  true,  true, true); }
-    public static @bind.as("*/")  El starfollow(final El e, final El sep)    { return new Rep(e, sep,  true,  true, false); }
-    public static @bind.as("?")   El question(final El e)                    { return new Rep(e, null, true,  true, false); }
-
-    public static @bind.as("!")   El bang(final El e)                        { return new Drop(e); }
-
-    public static @bind.as("^")   El caret(final String s) {
-        return new Drop(new Constant(CharRange.string(s)) {
-                public String getOwnerTag() { return s; }
-            });
+    public static @bind.as("++")  ElementNode plusmax(final ElementNode e)                     
+    { return new Rep(e, null, false, true, true); }
+    public static @bind.as("+")   ElementNode plus(final ElementNode e)                        
+    { return new Rep(e, null, false, true, false); }
+    public static @bind.as("++/") ElementNode plusmaxfollow(final ElementNode e, final ElementNode sep) 
+    { return new Rep(e, sep,  false, true, true); }
+    public static @bind.as("+/")  ElementNode plusfollow(final ElementNode e, final ElementNode sep)    
+    { return new Rep(e, sep,  false, true, false); }
+    public static @bind.as("**")  ElementNode starmax(final ElementNode e)                     
+    { return new Rep(e, null, true,  true, true); }
+    public static @bind.as("*")   ElementNode star(final ElementNode e)                        
+    { return new Rep(e, null, true,  true, false); }
+    public static @bind.as("**/") ElementNode starmaxfollow(final ElementNode e, final ElementNode sep) 
+    { return new Rep(e, sep,  true,  true, true); }
+    public static @bind.as("*/")  ElementNode starfollow(final ElementNode e, final ElementNode sep)    
+    { return new Rep(e, sep,  true,  true, false); }
+    public static @bind.as("?")   ElementNode question(final ElementNode e)                    
+    { return new Rep(e, null, true,  true, false); }
+    public static @bind.as("!")   ElementNode bang(final ElementNode e)                        
+    { return new Drop(e); }
+
+    public static @bind.as("^")   ElementNode caret(final String s) {
+        final String thePrefix = prefix;
+        return new Constant(CharAtom.string(s)) {
+                public String getOwnerTag() { return thePrefix+s; }
+                public boolean drop() { return true; }
+            };
     }
 
-    public static @bind.as("~")   El tilde(final El e) {
-        return new PostProcess(e) {
-                public Element postProcess(Element e) {
-                    return infer((Topology<Character>)Atom.toAtom(e).complement()); 
+    public static @bind.as("~")   ElementNode tilde(final ElementNode e) {
+        return new ElementNodeWrapper(e) {
+                public Atom toAtom(Context cx) {
+                    return infer((Topology<Character>)e.toAtom(cx).complement()/*.minus(CharAtom.braces)*/);
+                }
+                public Element build(Context cx, NonTerminalNode cnt) {
+                    return infer((Topology<Character>)e.toAtom(cx).complement()/*.minus(CharAtom.braces)*/);
                 } }; }
 
-    public static @bind.as("^^")  void doublecaret(final El e)                 { throw new Error("not implemented"); }
-
-    //public static @bind.as("(")   El subexpression(Seq[][] rhs)                { return new NonTerminal(rhs); }
-
     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) { return c+""; }
+    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(Topology<Character> t) { return new CharRange(new CharTopology(t)); }
+    //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; }
+    }
+    */
 }