checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammar.java
index 414d705..acda011 100644 (file)
@@ -58,15 +58,13 @@ public class MetaGrammar {
     public static class Production {
         public String tag;
         public String nonTerminal;
-        public Object[] labels;
         public boolean[] drops;
         public Element[] elements;
         public int count = 0;
-        public Production(String tag, String nonTerminal, Element[] elements, Object[] labels, boolean[] drops) {
+        public Production(String tag, String nonTerminal, Element[] elements, boolean[] drops) {
             this.tag = tag;
             this.elements = elements;
             this.nonTerminal = nonTerminal;
-            this.labels = labels;
             this.drops = drops;
             for(int i=0; i<drops.length; i++)
                 if (!drops[i])
@@ -160,7 +158,7 @@ public class MetaGrammar {
         }
         public Sequence makeSequence(Production p) {
             return Sequence.rewritingSequence(new TargetReducer(buildSequence(p), _bindable, isRaw()),
-                                              p.elements, p.labels, p.drops);
+                                              p.elements, p.drops);
         }
 
     }
@@ -172,8 +170,7 @@ public class MetaGrammar {
 
         public void toJava(StringBuffer sb) {
             sb.append("new MetaGrammar.TargetReducer(new int[] {");
-            for(int i=0; i<map.length; i++)
-                sb.append((i+"")+(i<map.length-1 ? "," : ""));
+            for(int i=0; i<map.length; i++) sb.append((i+"")+(i<map.length-1 ? "," : ""));
             sb.append("}, ");
             _bindable.toJava(sb);
             sb.append(", ");
@@ -200,7 +197,6 @@ public class MetaGrammar {
                     ret.add(null);
                 }
             }
-            //System.err.println("input tree: " + t);
             Object[] o = (Object[])ret.toArray(new Object[0]);
             int max = 0;
             for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
@@ -211,68 +207,12 @@ public class MetaGrammar {
     }
 
 
-    public static Union cached = null;
     public static Union make() { return make(MetaGrammarTree.meta, "s"); }
     public static Union make(Tree t, String s) { return make(t, s, new AnnotationGrammarBindingResolver(MetaGrammarBindings.class)); }
     public static Union make(Tree t, String s, GrammarBindingResolver rm) {
         Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
-        MetaGrammarBindings.Grammar g = (MetaGrammarBindings.Grammar)red.invoke(t.children());
-        Context cx = new Context(g,rm);
-        Union u = null;
-        for(MetaGrammarBindings.NonTerminal nt : g.values()) {
-            Union el = (Union)cx.get(nt.name);
-            StringBuffer st = new StringBuffer();
-            el.toString(st);
-            if (nt.name.equals(s)) u = el;
-        }
-        return u;
+        MetaGrammarBindings.GrammarNode g = (MetaGrammarBindings.GrammarNode)red.invoke(t.children());
+        return g.build(s, rm);
     }
 
-
-
-    public static class Context {
-        public HashMap<String,Union> map = new HashMap<String,Union>();
-        public MetaGrammarBindings.Grammar grammar;
-        public String cnt = null;
-        public GrammarBindingResolver rm;
-        public Context(MetaGrammarBindings.Grammar g, GrammarBindingResolver rm) {
-            this.grammar = g;
-            this.rm = rm;
-        }
-        public Union build() {
-            Union ret = null;
-            for(MetaGrammarBindings.NonTerminal nt : grammar.values()) {
-                Union u = get(nt.name);
-                if ("s".equals(nt.name))
-                    ret = u;
-            }
-            return ret;
-        }
-        public Context(Tree t, GrammarBindingResolver rm) {
-            this.rm = rm;
-            Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
-            this.grammar = (MetaGrammarBindings.Grammar)red.invoke(t.children());
-        }
-        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);
-            MetaGrammarBindings.NonTerminal nt = grammar.get(name);
-            if (nt==null) {
-                System.err.println("*** warning could not find " + name);
-            } else {
-                String old = cnt;
-                cnt = name;
-                nt.build(this, ret);
-                cnt = old;
-            }
-            return ret;
-        }
-
-    }
-    /*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)); }
 }