checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammar.java
index aa02d5f..d2aae07 100644 (file)
@@ -16,9 +16,6 @@ public class MetaGrammar {
             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";
@@ -35,15 +32,6 @@ public class MetaGrammar {
 
         out.append("\n        // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED\n");
 
-        /*
-        GrammarCompiler m = new GrammarCompiler();
-        Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(args[0])).expand1();
-        MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
-        MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
-
-        Union meta = mgf.get("s").build(bc);
-        Tree t = new CharParser(meta).parse(new FileInputStream(args[0])).expand1();
-        */
         Tree t = MetaGrammarTree.meta;
         Union u = MetaGrammar.make(t, "s");
 
@@ -51,14 +39,6 @@ public class MetaGrammar {
         System.err.println("== parsing with parsed grammar =================================================================================");
         t = new CharParser((Union)u).parse(new FileInputStream(args[0])).expand1();
         System.out.println(t.toPrettyString());
-        //Forest<String> fs = new CharParser(make()).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();
 
         t.toJava(out);
         out.append("\n        // DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED\n");
@@ -73,231 +53,12 @@ public class MetaGrammar {
         os.close();
     }
 
-
-   
-    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) {
-            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])
-                    count++;
-        }
-    }
-
-    public static class Target {
-        public int[] buildSequence(Production p) {
-            Annotation[][] annotations = _bindable.getArgAnnotations();
-            String[]       names       = _bindable.getArgNames();
-            String name = _bindable.getSimpleName();
-            int len = annotations.length;
-            int ofs = 0;
-            bind.arg[] argtags  = new bind.arg[len];
-            for(int i=0; i<names.length; i++)
-                for(Annotation a : annotations[i+ofs])
-                    if (a instanceof bind.arg)
-                        argtags[i+ofs] = (bind.arg)a;
-            return Target.this.buildSequence(p, names, argtags);
-        }
-        private Bindable _bindable;
-
-        public Target(Object o) { this(Bindable.create(o)); }
-        public Target(Bindable b) { this._bindable = b; }
-
-        public String getName() { return _bindable.getSimpleName(); }
-        public bind getBind() { return (bind)_bindable.getAnnotation(bind.class); }
-        public bind.as getBindAs() { return (bind.as)_bindable.getAnnotation(bind.as.class); }
-        public String toString() { return _bindable.getSimpleName(); }
-        public boolean isRaw() { return _bindable.isAnnotationPresent(bind.raw.class); }
-
-        public boolean isCompatible(Production p) {
-            bind.as t = getBindAs();
-            if (t != null &&
-                (t.value().equals(p.tag) ||
-                 (t.value().equals("") && getName().equals(p.tag))))
-                return buildSequence(p)!=null;
-
-            bind b = getBind();
-            System.out.println(_bindable.getClass().getSimpleName() + ": " + _bindable.getSimpleName());
-            if (b != null && getName().equals(p.tag))
-                return buildSequence(p)!=null;
-
-            bind.as n = getBindAs();
-            if (n != null &&
-                (n.value().equals(p.nonTerminal) ||
-                 (n.value().equals("") && getName().equals(p.nonTerminal))))
-                return buildSequence(p)!=null;
-
-            if (b != null && getName().equals(p.nonTerminal))
-                return buildSequence(p)!=null;
-
-            return false;
-        }
-
-        public int[] buildSequence(Production p, String[] names, bind.arg[] argtags) {
-            int argTagged = 0;
-            for(int i=0; i<argtags.length; i++)
-                if (argtags[i] != null)
-                    argTagged++;
-
-            // FIXME: can be smarter here
-            if (names.length==p.count) {
-                int[] ret = new int[p.count];
-                for(int i=0; i<p.count; i++) ret[i] = i;
-                return ret;
-            } else if (argTagged==p.count) {
-                int[] ret = new int[argtags.length];
-                int j = 0;
-                for(int i=0; i<argtags.length; i++)
-                    ret[i] = argtags[i]==null ? -1 : (j++);
-                return ret;
-            } else {
-                return null;
-            }
-        }
-        public Sequence makeSequence(Production p) {
-            return Sequence.rewritingSequence(new TargetReducer(buildSequence(p), _bindable, isRaw()),
-                                              p.elements, p.labels, p.drops);
-        }
-
-    }
-
-    public static class TargetReducer implements Tree.TreeFunctor<Object,Object>, ToJava {
-        private int[] map;
-        private Bindable _bindable;
-        private boolean _israw;
-
-        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 ? "," : ""));
-            sb.append("}, ");
-            _bindable.toJava(sb);
-            sb.append(", ");
-            sb.append(_israw ? "true" : "false");
-            sb.append(")");
-        }
-        
-        public TargetReducer(int[] map, Bindable b, boolean raw) {
-            this.map = map;
-            this._bindable = b;
-            this._israw = raw;
-        }
-        public String toString() { return "reducer-"+_bindable.toString(); }
-        public Object invoke(Iterable<Tree<Object>> t) {
-            if (_israw) return _bindable.impose(new Object[] { t });
-            ArrayList ret = new ArrayList();
-            for(Tree tc : t) {
-                if (tc.head() != null && tc.head() instanceof Functor)
-                    ret.add(((Tree.TreeFunctor<Object,Object>)tc.head()).invoke(tc.children()));
-                else if (tc.numChildren() == 0)
-                    ret.add(tc.head());
-                else {
-                    System.err.println("FIXME: don't know what to do about " + tc);
-                    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);
-            Object[] o2 = new Object[max+1];
-            for(int i=0; i<o.length; i++) o2[map[i]] = o[i];
-            return _bindable.impose(o2);
-        }
-    }
-
-
-    public static Union cached = null;
-    public static Union make() {
-        /*
-        if (cached != null) return cached;
-        try {
-            GrammarBindingResolver m = new GrammarBindingResolver();
-            Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream("tests/meta.g")).expand1();
-            MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
-            MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
-            Union meta = mgf.get("s").build(bc);
-            Tree t = new CharParser(meta).parse(new FileInputStream("tests/meta.g")).expand1();
-            return cached = make(t, "s");
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
-        */
-        return make(MetaGrammarTree.meta, "s");
-    }
-    public static Union make(Tree t, String s) { return make(t, s, new AnnotationGrammarBindingResolver()); }
+    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) {
-        System.out.println("Head: " + t.head());
         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.nonterminals) {
-            System.out.println(nt.name);
-            Union el = (Union)cx.get(nt.name);
-            StringBuffer st = new StringBuffer();
-            el.toString(st);
-            System.err.println(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.nonterminals) {
-                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)); }
 }