checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / MetaGrammar.java
index f5fff5d..e9c4413 100644 (file)
@@ -7,8 +7,6 @@ import java.io.*;
 
 public class MetaGrammar extends StringWalker {
 
-    public static Object repeatTag = null;
-
     public static Union make() throws Exception { return make(MetaGrammarTree.meta, "s"); }
     public static Union make(Tree<String> tree, String nt) throws Exception {
         Meta.MetaGrammarFile mgf = new Meta().new MetaGrammarFile(tree);
@@ -20,8 +18,8 @@ public class MetaGrammar extends StringWalker {
 
     private static Element  set(Range.Set r) { return CharRange.set(r); }
     private static Element  string(String s) { return CharRange.string(s); }
-    private static Atom infer(Element e)  { return infer((Topology<Character>)Atom.toAtom(e)); }
-    private static Atom infer(Topology<Character> t) { return new CharRange(new CharTopology(t)); }
+    /*private*/ static Atom infer(Element e)  { return infer((Topology<Character>)Atom.toAtom(e)); }
+    /*private*/ static Atom infer(Topology<Character> t) { return new CharRange(new CharTopology(t)); }
 
     private MetaGrammar() { }
 
@@ -39,42 +37,59 @@ public class MetaGrammar extends StringWalker {
 
     public static class BuildContext extends HashMap<String,Union> {
         private final Meta.MetaGrammarFile mgf;
+        public Meta.NonTerminal currentNonTerminal;
         public BuildContext(Meta.MetaGrammarFile mgf) { this.mgf = mgf; }
         public Union build(String s) {
             Union ret = get(s);
             if (ret != null) return ret;
-            Meta.MetaNonterminal mnt = mgf.get(s);
+            Meta.NonTerminal mnt = mgf.get(s);
             if (mnt==null) throw new Error("undeclared nonterminal \""+s+"\"");
             return mnt.build(this);
         }
     }
 
     public static class Meta {
-        public class MetaGrammarFile extends HashMap<String,MetaNonterminal> {
+        public Object repeatTag() { return null; }
+        public Sequence tryResolveTag(String s, String nonTerminalName, Element[] els, Object[] labels, boolean [] drops) {
+            //return resolveTag(s, nonTerminalName, els, labels, drops);
+            return null;
+        }
+        public Sequence resolveTag(String s, String nonTerminalName, Element[] els, Object[] labels, boolean [] drops) {
+            return Sequence.rewritingSequence(s, els, labels, drops);
+        }
+        public class MetaGrammarFile extends HashMap<String,NonTerminal> {
             public MetaGrammarFile(Tree<String> tree) {
                 if (!tree.head().equals("grammar")) throw new Error();
                 for(Tree<String> nt : tree.child(0))
-                    add(new MetaNonterminal(nt));
+                    add(new NonTerminal(nt));
             }
-            private void add(MetaNonterminal mnt) {
+            private void add(NonTerminal mnt) {
                 if (this.get(mnt.name)!=null) throw new Error("duplicate definition of nonterminal \""+mnt.name+"\"");
                 this.put(mnt.name, mnt);
             }
             public String toString() {
                 String ret = "";
-                for(MetaNonterminal mnt : this.values()) ret += mnt + "\n";
+                for(NonTerminal mnt : this.values()) ret += mnt + "\n";
                 return ret;
             }
         }
-        public class MetaNonterminal {
+        public class NonTerminal {
             public String    name;
             public MetaUnion rhs;
-            public MetaNonterminal(Tree<String> tree) {
+            public NonTerminal(Tree<String> 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 Union build(BuildContext bc) {
+                NonTerminal ont = bc.currentNonTerminal;
+                bc.currentNonTerminal = this;
+                try {
+                    return rhs.build(bc, name);
+                } finally {
+                    bc.currentNonTerminal = ont;
+                }
+            }
         }
         public MetaUnion rhs(Tree<String> t) {
             return t.numChildren()==1
@@ -137,9 +152,9 @@ public class MetaGrammar extends StringWalker {
         }
 
         public MetaSequence makeMetaSequence(Tree<String> t) {
-            if ("psx".equals(t.head())) return makeMetaConjunct(t.child(0));
-            if (t.head().equals("&"))  return new MetaAnd(makeMetaSequence(t.child(0)), makeMetaConjunct(t.child(1)), false);
-            if (t.head().equals("&~")) return new MetaAnd(makeMetaSequence(t.child(0)), makeMetaConjunct(t.child(1)), true);
+            if ("psx".equals(t.head())) return makeConjunct(t.child(0));
+            if (t.head().equals("&"))  return new MetaAnd(makeMetaSequence(t.child(0)), makeConjunct(t.child(1)), false);
+            if (t.head().equals("&~")) return new MetaAnd(makeMetaSequence(t.child(0)), makeConjunct(t.child(1)), true);
             return null;
         }
 
@@ -166,7 +181,7 @@ public class MetaGrammar extends StringWalker {
             public String toString() { return left + " &"+(not?"~":"")+" "+right; }
         }
 
-        public class MetaConjunct implements MetaSequence {
+        public class Conjunct implements MetaSequence {
             public boolean negated = false;
             public MetaClause[] elements;
             public HashMap<MetaClause,String> labelMap = new HashMap<MetaClause,String>();
@@ -184,6 +199,9 @@ public class MetaGrammar extends StringWalker {
                 boolean dropAll = false;
                 if (tag!=null && tag.equals("[]")) unwrap = true;
                 if (tag!=null && "()".equals(tag)) dropAll=true;
+
+                NonTerminal old = bc.currentNonTerminal;
+                bc.currentNonTerminal = null;
                 for(int i=0; i<elements.length; i++) {
                     int j = separator==null ? i : i*2;
                     els[j] = elements[i].build(bc);
@@ -194,25 +212,31 @@ public class MetaGrammar extends StringWalker {
                         drops[j+1] = true;
                     }
                 }
+                bc.currentNonTerminal = old;
+
                 Sequence ret = null;
                 if (dropAll)     ret = Sequence.drop(els, false);
-                else if (unwrap) ret = Sequence.unwrap(els, /*repeatTag FIXME*/null, drops);
+                else if (unwrap) ret = Sequence.unwrap(els, repeatTag(), drops);
                 else if (tag!=null) {
-                    ret = Sequence.rewritingSequence(tag, els, labels, drops);
+                    ret = resolveTag(tag, bc.currentNonTerminal==null ? null : bc.currentNonTerminal.name, els, labels, drops);
+                    System.err.println("resolvetag " + tag + " => " + ret);
                 } 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);
+                    ret = tryResolveTag(tag, bc.currentNonTerminal==null ? null : bc.currentNonTerminal.name, 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);
+                    }
                 }
                 if (this.followedBy != null)
                     ret.follow = infer(this.followedBy.build(bc));
                 return ret;
             }
-            private MetaConjunct(Tree<String> t) {
+            private Conjunct(Tree<String> t) {
                 elements = new MetaClause[t.numChildren()];
                 int i = 0; for(Tree<String> tt : t)
                     elements[i++] = makeMetaClause(tt, this);
@@ -227,34 +251,34 @@ public class MetaGrammar extends StringWalker {
                 return ret;
             }
         }
-            public MetaConjunct makeMetaConjunct(Tree<String> t) {
-                //System.err.println("makeMetaConjunct("+t+")");
+            public Conjunct makeConjunct(Tree<String> t) {
+                //System.err.println("makeConjunct("+t+")");
                 if ("/".equals(t.head())) {
-                    MetaConjunct ret = makeMetaConjunct(t.child(0));
+                    Conjunct ret = makeConjunct(t.child(0));
                     ret.separator = makeMetaClause(t.child(1), ret);
                     return ret;
                 }
                 if ("->".equals(t.head())) {
-                    MetaConjunct ret = makeMetaConjunct(t.child(0));
+                    Conjunct ret = makeConjunct(t.child(0));
                     ret.followedBy = makeMetaClause(t.child(1), ret);
                     return ret;
                 }
                 if ("::".equals(t.head())) {
-                    MetaConjunct ret = makeMetaConjunct(t.child(1));
+                    Conjunct ret = makeConjunct(t.child(1));
                     ret.tag = string(t.child(0));
                     return ret;
                 }
                 if ("ps".equals(t.head())) {
-                    return new MetaConjunct(t.child(0));
+                    return new Conjunct(t.child(0));
                 }
-                return new MetaConjunct(t);
+                return new Conjunct(t);
             }
 
-            public MetaClause makeMetaClause(Tree<String> t, MetaConjunct c) {
+            public MetaClause makeMetaClause(Tree<String> t, Conjunct c) {
                 //System.err.println("MetaClause.makeMetaClause("+t+")");
-                if (t==null) return new MetaEpsilon();
-                if (t.head()==null) return new MetaEpsilon();
-                if (t.head().equals("{")) throw new Error("metatree: " + t);
+                if (t==null) return new Epsilon();
+                if (t.head()==null) return new Epsilon();
+                if (t.head().equals("{")) return new MetaTree(makeConjunct(t.child(0)));
                 if (t.head().equals("*")) return new MetaRepeat(makeMetaClause(t.child(0), c), false, null, true, true);
                 if (t.head().equals("+")) return new MetaRepeat(makeMetaClause(t.child(0), c), false, null, false, true);
                 if (t.head().equals("?")) return new MetaRepeat(makeMetaClause(t.child(0), c), false, null, true, false);
@@ -264,15 +288,15 @@ public class MetaGrammar extends StringWalker {
                 if (t.head().equals("+/")) return new MetaRepeat(makeMetaClause(t.child(0), c), false, makeMetaClause(t.child(1), c), false, true);
                 if (t.head().equals("**/")) return new MetaRepeat(makeMetaClause(t.child(0), c), true, makeMetaClause(t.child(1), c), true, true);
                 if (t.head().equals("++/")) return new MetaRepeat(makeMetaClause(t.child(0), c), true, makeMetaClause(t.child(1), c), false, true);
-                if (t.head().equals("()")) return new MetaEpsilon();
+                if (t.head().equals("()")) return new Epsilon();
                 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("literal")) return new StringLiteral(t.child(0));
+                if (t.head().equals("nonTerminal")) return new NonTerminalReference(t.child(0));
+                if (t.head().equals(")")) return new SelfReference();
+                if (t.head().equals("(")) return new Parens(t.child(0));
                 if (t.head().equals("~")) return new MetaInvert(t.child(0), c);
                 if (t.head().equals("!")) { MetaClause mc = makeMetaClause(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("^")) { c.tag = string(t.child(0)); return new StringLiteral(t.child(0)); }
                 if (t.head().equals("^^")) throw new Error("carets: " + t);
                 if (t.head().equals(":")) {
                     String name = string(t.child(0));
@@ -295,11 +319,11 @@ public class MetaGrammar extends StringWalker {
                 public Element build(BuildContext bc) {
                     return !maximal
                         ? (separator==null
-                           ? Sequence.repeat(element.build(bc), zero, many, null, null)
-                           : Sequence.repeat(element.build(bc), zero, many, separator.build(bc), null))
+                           ? Sequence.repeat(element.build(bc), zero, many, null, repeatTag())
+                           : Sequence.repeat(element.build(bc), zero, many, separator.build(bc), repeatTag()))
                         : (separator==null
-                           ? Sequence.repeatMaximal(infer(element.build(bc)), zero, many, null)
-                           : Sequence.repeatMaximal(element.build(bc), zero, many, infer(separator.build(bc)), null));
+                           ? Sequence.repeatMaximal(infer(element.build(bc)), zero, many, repeatTag())
+                           : Sequence.repeatMaximal(element.build(bc), zero, many, infer(separator.build(bc)), repeatTag()));
                 }
                 public MetaRepeat(MetaClause element, boolean maximal, MetaClause separator, boolean zero, boolean many) {
                     this.separator = separator;
@@ -315,26 +339,37 @@ public class MetaGrammar extends StringWalker {
                         (separator==null?"":(" /"+separator));
                 }
             }
-        public class MetaEpsilon extends MetaClause {
+        public class Epsilon extends MetaClause {
             public String toString() { return "()"; }
             public Element build(BuildContext bc) { return Union.epsilon; }
         }
-            public class MetaParens extends MetaClause {
+            public class Parens extends MetaClause {
                 public MetaUnion body;
-                public MetaParens(Tree<String> t) { this.body = rhs(t); }
+                public Parens(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 = makeMetaConjunct(t); }
+
+            public class MetaTree extends MetaClause {
+                public Conjunct body;
+                public MetaTree(Conjunct c) { this.body = c; }
                 public String toString() { return "{ " + body + " }"; }
                 public Element build(BuildContext bc) {
-                    return new Union("{}");// body.buildSequence();
+                    Union u = new Union();
+                    Union u2 = new Union();
+                    Sequence seq = body.buildSequence(bc);
+                    u2.add(seq);
+                    u.add(Sequence.singleton(new Element[] { CharRange.leftBrace,
+                                                             new NonTerminalReference("ws").build(bc),
+                                                             u2,
+                                                             new NonTerminalReference("ws").build(bc),
+                                                             CharRange.rightBrace }
+                                             , 2));
+                    //u.add(seq);
+                    return u;
                 }
             }
-            */
+
             public class MetaRange extends MetaClause {
                 Range.Set range = new Range.Set();
                 public String toString() { return range.toString(); }
@@ -350,25 +385,26 @@ public class MetaGrammar extends StringWalker {
                     }
                 }
             }
-            public class MetaStringLiteral extends MetaClause {
+            public class StringLiteral 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 StringLiteral(Tree<String> literal) { this.literal = string(literal); this.drop = true; }
                 public String toString() { return "\""+StringUtil.escapify(literal, "\"\r\n\\")+"\""; }
             }
-            public class MetaNonterminalReference extends MetaClause {
+            public class NonTerminalReference extends MetaClause {
                 public String name;
-                public MetaNonterminalReference(Tree<String> name) { this.name = string(name); }
+                public NonTerminalReference(Tree<String> name) { this.name = string(name); }
+                public NonTerminalReference(String name) { this.name = name; }
                 public Element build(BuildContext bc) { return bc.build(name); }
                 public String toString() { return name; } 
             }
-            public class MetaSelfReference extends MetaClause {
+            public class SelfReference extends MetaClause {
                 public String toString() { return "(*)"; } 
                 public Element build(BuildContext bc) { return new Union("(*)"); /* FIXME */ }
             }
             public class MetaInvert extends MetaClause {
                 public MetaClause element;
-                public MetaInvert(Tree<String> t, MetaConjunct c) { this.element = makeMetaClause(t, c); }
+                public MetaInvert(Tree<String> t, Conjunct c) { this.element = makeMetaClause(t, c); }
                 public String toString() { return "~"+element; }
                 public Element build(BuildContext bc) { return infer((Topology<Character>)Atom.toAtom(element.build(bc)).complement()); }
             }