checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / MetaGrammar.java
index 972d3a1..869e0fa 100644 (file)
@@ -7,8 +7,10 @@ import java.io.*;
 
 public class MetaGrammar extends StringWalker {
 
+    public static Object repeatTag = null;
+
     /** an atom which tracks the possible tokenset of some element, provided that element can only match single-token sequences */
-    static class Infer<T extends Input> extends Atom<T> {
+    public static class Infer<T extends Input> extends Atom<T> {
         private final Element e;
         public Infer(Element e) { this.e = e; }
         public Topology<T> top() { return (Topology<T>)toAtom(e); }
@@ -16,7 +18,7 @@ public class MetaGrammar extends StringWalker {
     }
 
     /** an atom which tracks the inverse of some other atom */
-    static class Invert<T extends Input> extends Atom<T> {
+    public 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(); }
@@ -28,12 +30,13 @@ public class MetaGrammar extends StringWalker {
         static final Topology leftright = CharRange.rightBrace.union(CharRange.leftBrace);
         public Hack(Atom<T> a) { this.a = a; }
         public Topology<T> top() { return a.minus(leftright); }
-        public String toString() { return "~"+a; }
+        public String toString() { return a.toString(); }
     }
 
-
     public static Union make() throws Exception {
-        return ((MetaGrammar)new MetaGrammar().walk(meta)).done();
+        Meta.MetaGrammarFile mgf = new MetaGrammar.Meta.MetaGrammarFile(meta);
+        BuildContext bc = new BuildContext(mgf);
+        return mgf.get("s").build(bc);
     }
     public String toString() {
         StringBuffer ret = new StringBuffer();
@@ -54,12 +57,12 @@ public class MetaGrammar extends StringWalker {
     private HashMap<String,Union> nt;
     private int anon = 0;
     private String startSymbol;
-    private boolean strings;
+    private static boolean strings;
 
-    private Element  set(Range.Set r) { if (strings) throw new Error(); return CharRange.set(r); }
-    private Element  string(String s) { return strings ? StringInput.string(s) : CharRange.string(s); }
-    private Atom     leftBrace()      { return strings ? StringInput.leftBrace : CharRange.leftBrace; }
-    private Atom     rightBrace()     { return strings ? StringInput.rightBrace : CharRange.rightBrace; }
+    private static Element  set(Range.Set r) { if (strings) throw new Error(); return CharRange.set(r); }
+    private static Element  string(String s) { return strings ? StringInput.string(s) : CharRange.string(s); }
+    private static Atom     leftBrace()      { return strings ? StringInput.leftBrace : CharRange.leftBrace; }
+    private static Atom     rightBrace()     { return strings ? StringInput.rightBrace : CharRange.rightBrace; }
 
     public MetaGrammar() { this("s", false); }
     public MetaGrammar(String s) { this(s, false); }
@@ -107,12 +110,12 @@ public class MetaGrammar extends StringWalker {
         return n;
     }
 
-    public String string(Iterable<Tree<String>> children) {
+    public static String string(Iterable<Tree<String>> children) {
         String ret = "";
         for(Tree<String> t : children) ret += string(t);
         return ret;
     }
-    public String string(Tree<String> tree) {
+    public static String string(Tree<String> tree) {
         String ret = "";
         if (tree.head()!=null) ret += tree.head();
         ret += string(tree.children());
@@ -125,6 +128,319 @@ public class MetaGrammar extends StringWalker {
     public Sequence sequence(Object o, boolean lame) {
         return new PreSequence((Element[])Reflection.lub((Object[])o), null).buildSequence(null, lame, false);
     }
+
+    public static class BuildContext extends HashMap<String,Union> {
+        private final Meta.MetaGrammarFile mgf;
+        public BuildContext(Meta.MetaGrammarFile mgf) { this.mgf = mgf; }
+        public Union build(String s) {
+            Union ret = get(s);
+            if (ret != null) return ret;
+            return mgf.get(s).build(this);
+        }
+    }
+
+    public static class Meta {
+        public static class MetaGrammarFile extends HashMap<String,MetaNonterminal> {
+            public MetaGrammarFile(Tree<String> tree) {
+                if (!tree.head().equals("grammar")) throw new Error();
+                for(Tree<String> nt : tree.child(0))
+                    add(new MetaNonterminal(nt));
+            }
+            private void add(MetaNonterminal mnt) {
+                this.put(mnt.name, mnt);
+            }
+            public String toString() {
+                String ret = "";
+                for(MetaNonterminal mnt : this.values()) ret += mnt + "\n";
+                return ret;
+            }
+        }
+        public static class MetaNonterminal {
+            public String    name;
+            public MetaUnion rhs;
+            public MetaNonterminal(Tree<String> tree) {
+                System.err.println("MetaNonterminal("+tree+")");
+                name = string(tree.child(0));
+                rhs = rhs(tree.child(1));
+            }
+            public String toString() { return name + " = " + rhs; }
+            public Union build(BuildContext bc) { return rhs.build(bc, name); }
+        }
+        public static MetaUnion rhs(Tree<String> t) {
+            return t.numChildren()==1
+                ? new MetaUnion(t.child(0), false)
+                : new MetaUnion(t, true);
+        }
+        public static class MetaUnion implements MetaSequence {
+            public boolean prioritized;
+            public MetaSequence[] sequences;
+            public Sequence buildSequence(BuildContext bc) {
+                return Sequence.singleton(new Element[] { buildAnon(bc) }, 0);
+            }
+            public Union buildAnon(BuildContext bc) {
+                String s = "";
+                for(int i=0; i<sequences.length; i++)
+                    s += (i>0?"\n                "+(prioritized?">":"|")+" ":"")+sequences[i].buildSequence(bc);
+                return build(bc, s);
+            }
+            public Union build(BuildContext bc, String name) {
+                Union u = bc.get(name);
+                if (u != null) return u;
+                u = new Union(name);
+                bc.put(name, u);
+                HashSet<Sequence> seqs = new HashSet<Sequence>();
+                for(MetaSequence s : sequences) {
+                    Sequence seq = s.buildSequence(bc);
+                    if (seq != null) {
+                        Sequence oseq = seq;
+                        if (prioritized)
+                            for(Sequence seqprev : seqs)
+                                seq = seq.not(seqprev);
+                        u.add(seq);
+                        seqs.add(oseq);
+                    }
+                }
+                return u;
+            }
+            public MetaUnion(Tree<String> t, boolean prioritized) {
+                System.err.println("MetaUnion("+t+")");
+                //System.err.println("metaunion: " + t);
+                this.prioritized = prioritized;
+                int i = 0;
+                this.sequences = new MetaSequence[t.numChildren()];
+                for(Tree<String> tt : t)
+                    sequences[i++] = prioritized
+                        ? new MetaUnion(tt, false)
+                        : makeMetaSequence(tt);
+            }
+            public String toString() {
+                String ret = "\n     ";
+                for(int i=0; i<sequences.length; i++) {
+                    ret += sequences[i];
+                    if (i<sequences.length-1)
+                        ret += (prioritized ? "\n    > " : "\n    | ");
+                }
+                return ret;
+            }
+        }
+
+        public interface MetaSequence {
+            public abstract Sequence buildSequence(BuildContext bc);
+        }
+
+        public static MetaSequence makeMetaSequence(Tree<String> t) {
+            System.err.println("MetaSequence("+t+")");
+            if ("psx".equals(t.head())) return MetaConjunct.make(t.child(0));
+            if (t.head().equals("&"))  return new MetaAnd(makeMetaSequence(t.child(0)), MetaConjunct.make(t.child(1)), false);
+            if (t.head().equals("&~")) return new MetaAnd(makeMetaSequence(t.child(0)), MetaConjunct.make(t.child(1)), true);
+            return null;
+        }
+
+        public static class MetaAnd implements MetaSequence {
+            boolean not;
+            MetaSequence left;
+            MetaSequence right;
+            public Sequence buildSequence(BuildContext bc) {
+                Sequence ret = left.buildSequence(bc);
+                if (not) ret.not(right.buildSequence(bc)); else ret.and(right.buildSequence(bc));
+                return ret;
+            }
+            public MetaAnd(MetaSequence left, MetaSequence right, boolean not) {
+                this.left = left;
+                this.right = right;
+                this.not = not;
+            }
+            public String toString() { return left + " &"+(not?"~":"")+" " /* FIXME */; }
+        }
+
+        public static class MetaConjunct implements MetaSequence {
+            public boolean negated = false;
+            public MetaClause[] elements;
+            public MetaClause followedBy = null;
+            public String tag;
+            public MetaClause separator;
+            public Sequence buildSequence(BuildContext bc) {
+                Element[] els = new Element[elements.length + (separator==null?0:(elements.length-1))];
+                boolean[] drops = new boolean[elements.length + (separator==null?0:(elements.length-1))];
+                boolean unwrap = false;
+                boolean dropAll = false;
+                if (tag!=null && tag.equals("[]")) unwrap = true;
+                if (tag!=null && "()".equals(tag)) dropAll=true;
+                for(int i=0; i<elements.length; i++) {
+                    int j = separator==null ? i : i*2;
+                    els[j] = elements[i].build(bc);
+                    drops[j] = elements[i].drop;
+                    if (separator!=null && i<elements.length-1) {
+                        els[j+1] = separator.build(bc);
+                        drops[j+1] = true;
+                    }
+                }
+                Sequence ret = null;
+                if (dropAll)     ret = Sequence.drop(els, false);
+                else if (unwrap) ret = Sequence.unwrap(els, /*repeatTag FIXME*/null, drops);
+                else if (tag!=null) ret = Sequence.rewritingSequence(tag, els, new Object[els.length], drops);
+                else {
+                    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);
+                }
+                if (this.followedBy != null) ret.follow = new Hack(new Infer(this.followedBy.build(bc)));
+                return ret;
+            }
+            private MetaConjunct(Tree<String> t) {
+                System.err.println("MetaConjunct("+t+")");
+                elements = new MetaClause[t.numChildren()];
+                int i = 0; for(Tree<String> tt : t)
+                    elements[i++] = MetaClause.make(tt, this);
+            }
+            public String toString() {
+                String ret = (tag==null ? "" : (tag+":: ")) + (negated ? "~" : "");
+                if (elements.length > 1) ret += "(";
+                for(MetaClause mc : elements) ret += (" " + mc + " ");
+                if (separator != null) ret += " /" + separator;
+                if (followedBy != null) ret += " -> " + followedBy;
+                if (elements.length > 1) ret += ")";
+                return ret;
+            }
+            public static MetaConjunct make(Tree<String> t) {
+                System.err.println("MetaConjunct.make("+t+")");
+                if ("/".equals(t.head())) {
+                    MetaConjunct ret = make(t.child(0));
+                    ret.separator = MetaClause.make(t.child(1), ret);
+                    return ret;
+                }
+                if ("->".equals(t.head())) {
+                    MetaConjunct ret = make(t.child(0));
+                    ret.followedBy = MetaClause.make(t.child(1), ret);
+                    return ret;
+                }
+                if ("::".equals(t.head())) {
+                    MetaConjunct ret = make(t.child(1));
+                    ret.tag = string(t.child(0));
+                    return ret;
+                }
+                if ("ps".equals(t.head())) {
+                    return new MetaConjunct(t.child(0));
+                }
+                return new MetaConjunct(t);
+            }
+        }
+
+        public static abstract class MetaClause {
+            public String label = null;
+            public boolean drop = false;
+            public boolean lift = false;
+            public abstract Element build(BuildContext bc);
+            public static MetaClause make(Tree<String> t, MetaConjunct c) {
+                System.err.println("MetaClause.make("+t+")");
+                if (t.head().equals("{")) throw new Error("metatree: " + t);
+                if (t.head().equals("*")) return new MetaRepeat(make(t.child(0), c), false, null, true, true);
+                if (t.head().equals("+")) return new MetaRepeat(make(t.child(0), c), false, null, false, true);
+                if (t.head().equals("?")) return new MetaRepeat(make(t.child(0), c), false, null, true, false);
+                if (t.head().equals("**")) return new MetaRepeat(make(t.child(0), c), true, null, true, true);
+                if (t.head().equals("++")) return new MetaRepeat(make(t.child(0), c), true, null, false, true);
+                if (t.head().equals("*/")) return new MetaRepeat(make(t.child(0), c), false, make(t.child(1), c), true, true);
+                if (t.head().equals("+/")) return new MetaRepeat(make(t.child(0), c), false, make(t.child(1), c), false, true);
+                if (t.head().equals("**/")) return new MetaRepeat(make(t.child(0), c), true, make(t.child(1), c), true, true);
+                if (t.head().equals("++/")) return new MetaRepeat(make(t.child(0), c), true, make(t.child(1), c), false, true);
+                if (t.head().equals("()")) return new MetaEpsilon();
+                if (t.head().equals("[")) return new MetaRange(t.child(0));
+                if (t.head().equals("literal")) return new MetaStringLiteral(t.child(0));
+                if (t.head().equals("nonTerminal")) return new MetaNonterminalReference(t.child(0));
+                if (t.head().equals(")")) return new MetaSelfReference();
+                if (t.head().equals("(")) return new MetaParens(t.child(0));
+                if (t.head().equals("~")) return new MetaInvert(t.child(0), c);
+                if (t.head().equals("!")) { MetaClause mc = make(t.child(0), c); mc.drop = true; return mc; }
+                if (t.head().equals("^")) { c.tag = string(t.child(0)); return new MetaStringLiteral(t.child(0)); }
+                if (t.head().equals("^^")) throw new Error("carets: " + t);
+                throw new Error("unknown: " + t);
+            }
+            public static class MetaRepeat extends MetaClause {
+                public MetaClause element, separator;
+                public boolean maximal, zero, many;
+                public Element build(BuildContext bc) {
+                    return new Repeat(element.build(bc), zero, many, separator==null?null:separator.build(bc), maximal);
+                }
+                public MetaRepeat(MetaClause element, boolean maximal, MetaClause separator, boolean zero, boolean many) {
+                    this.separator = separator;
+                    this.element = element;
+                    this.maximal = maximal;
+                    this.zero = zero;
+                    this.many = many;
+                }
+                public String toString() {
+                    return element+
+                        ((zero&&!many)?"?":zero?"*":"+")+
+                        (!maximal?"":zero?"*":"+")+
+                        (separator==null?"":(" /"+separator));
+                }
+            }
+            public static class MetaParens extends MetaClause {
+                public MetaUnion body;
+                public MetaParens(Tree<String> t) { this.body = rhs(t); }
+                public String toString() { return "( " + body + " )"; }
+                public Element build(BuildContext bc) { return body.buildAnon(bc); }
+            }
+            /*
+            public static class MetaTree extends MetaClause {
+                public MetaConjunct body;
+                public MetaTree(Tree<String> t) { this.body = MetaConjunct.make(t); }
+                public String toString() { return "{ " + body + " }"; }
+                public Element build(BuildContext bc) {
+                    return new Union("{}");// body.buildSequence();
+                }
+            }
+            */
+            public static class MetaEpsilon extends MetaClause {
+                public String toString() { return "()"; }
+                public Element build(BuildContext bc) { return Union.epsilon; }
+            }
+            public static class MetaRange extends MetaClause {
+                Range.Set range = new Range.Set();
+                public String toString() { return range.toString(); }
+                public Element build(BuildContext bc) { return set(range); }
+                public MetaRange(Tree<String> t) {
+                    for(Tree<String> tt : t) {
+                        if (tt.head().equals("range")) {
+                            range.add(tt.child(0).head().charAt(0));
+                        } else if (tt.head().equals("-")) {
+                            range.add(new Range(string(tt.child(0)).charAt(0),
+                                                string(tt.child(1)).charAt(0)));
+                        }
+                    }
+                    //set = set(ret);
+                }
+            }
+            public static class MetaStringLiteral extends MetaClause {
+                public String literal;
+                public Element build(BuildContext bc) { return string(literal); }
+                public MetaStringLiteral(Tree<String> literal) { this.literal = string(literal); this.drop = true; }
+                public String toString() { return "\""+StringUtil.escapify(literal, "\"\r\n\\")+"\""; }
+            }
+            public static class MetaNonterminalReference extends MetaClause {
+                public String name;
+                public MetaNonterminalReference(Tree<String> name) { this.name = string(name); }
+                public Element build(BuildContext bc) { return bc.build(name); }
+                public String toString() { return name; } 
+            }
+            public static class MetaSelfReference extends MetaClause {
+                public String toString() { return "(*)"; } 
+                public Element build(BuildContext bc) { return new Union("(*)"); /* FIXME */ }
+            }
+            public static class MetaInvert extends MetaClause {
+                public MetaClause element;
+                public MetaInvert(Tree<String> t, MetaConjunct c) { this.element = make(t, c); }
+                public String toString() { return "~"+element; }
+                public Element build(BuildContext bc) { return new Hack(new Invert(new Infer(element.build(bc)))); }
+            }
+        }
+
+    }
+
     public Object walk(Tree<String> tree) {
         String head = tree.head();
         if (tree.numChildren()==0) return super.walk(tree);
@@ -166,7 +482,7 @@ public class MetaGrammar extends StringWalker {
         else if ("range".equals(head)) return new Range(walk(tree, 0).toString().charAt(0), walk(tree,0).toString().charAt(0));
         else if ("gram".equals(head)) return walk(tree, 0);
         else if ("psy".equals(head)) return (PreSequence)walk(tree, 0);
-        else if ("->".equals(head)) { PreSequence p = (PreSequence)walk(tree, 0); p.noFollow = (Element)walk(tree, 1); return p; }
+        else if ("->".equals(head)) { PreSequence p = (PreSequence)walk(tree, 0); p.follow = (Element)walk(tree, 1); return p; }
         else if ("/".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1));
         else if (" /".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1));
         else if ("~".equals(head)) return new Hack(new Invert(new Infer((Element)walk(tree, 0))));
@@ -218,7 +534,7 @@ public class MetaGrammar extends StringWalker {
     //////////////////////////////////////////////////////////////////////////////
 
     public class PreSequence {
-        public Element noFollow = null;
+        public Element follow = null;
         public final HashSet<Sequence> and  = new HashSet<Sequence>();
         public final HashSet<Sequence> not  = new HashSet<Sequence>();
         public /*final*/ Object tag;
@@ -330,7 +646,7 @@ public class MetaGrammar extends StringWalker {
             for(Sequence s : and) ret = ret.and(s);
             for(Sequence s : not) ret = ret.not(s);
             set.add(ret);
-            if (this.noFollow != null) ret.noFollow = new Invert(new Infer(this.noFollow));
+            if (this.follow != null) ret.follow = new Infer(this.follow);
             return ret;
         }
     }
@@ -340,7 +656,9 @@ public class MetaGrammar extends StringWalker {
             System.err.println("usage: java " + MetaGrammar.class.getName() + " grammarfile.g com.yourdomain.package.ClassName");
             System.exit(-1);
         }
-
+        //StringBuffer sbs = new StringBuffer();
+        //((MetaGrammar)new MetaGrammar().walk(meta)).nt.get("e").toString(sbs);
+        //System.err.println(sbs);
         String className   = args[1].substring(args[1].lastIndexOf('.')+1);
         String packageName = args[1].substring(0, args[1].lastIndexOf('.'));
         String fileName    = packageName.replace('.', '/') + "/" + className + ".java";
@@ -356,17 +674,35 @@ public class MetaGrammar extends StringWalker {
         }
 
         out.append("\n        // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED\n");
-        new CharParser(MetaGrammar.make()).parse(new FileInputStream(args[0])).expand1().toJava(out);
+        Tree<String> ts = new CharParser(MetaGrammar.make()).parse(new FileInputStream(args[0])).expand1();
+
+        Meta.MetaGrammarFile mgf = new MetaGrammar.Meta.MetaGrammarFile(ts);
+        BuildContext bc = new BuildContext(mgf);
+        for(Meta.MetaNonterminal nt : mgf.values()) {
+            StringBuffer sb = new StringBuffer();
+            nt.build(bc).toString(sb);
+            System.err.println(sb);
+        }
+
+        Forest<String> fs = new CharParser(mgf.get("s").build(new BuildContext(mgf))).parse(new FileInputStream(args[0]));
+        System.out.println(fs.expand1());
+        //GraphViz gv = new GraphViz();
+        //fs.toGraphViz(gv);
+        //FileOutputStream fox = new FileOutputStream("out.dot");
+        //gv.dump(fox);
+        //fox.close();
+
+        ts.toJava(out);
         out.append("\n        // DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED\n");
 
         for(String s = br.readLine(); s != null; s = br.readLine()) out.append(s+"\n");
         br.close();
 
-        OutputStream os = new FileOutputStream(fileName);
-        PrintWriter p = new PrintWriter(new OutputStreamWriter(os));
-        p.println(out.toString());
-        p.flush();
-        os.close();
+        //OutputStream os = new FileOutputStream(fileName);
+        //PrintWriter p = new PrintWriter(new OutputStreamWriter(os));
+        //p.println(out.toString());
+        //p.flush();
+        //os.close();
     }
 
     public static final Tree meta =
@@ -555,6 +891,75 @@ public class MetaGrammar extends StringWalker {
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED
 new edu.berkeley.sbp.Tree(null, "grammar", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "=", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "s", new edu.berkeley.sbp.Tree[] { })}),
         new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "psx", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "ps", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "!", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "nonTerminal", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "w", new edu.berkeley.sbp.Tree[] { }),
@@ -1244,3 +1649,72 @@ new edu.berkeley.sbp.Tree(null, "grammar", new edu.berkeley.sbp.Tree[] { new edu
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+