checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammar.java
index a55481b..a4c620b 100644 (file)
@@ -52,26 +52,14 @@ public class MetaGrammar {
         p.flush();
         os.close();
     }
-
-
    
-    public static class Production {
-        public String tag;
-        public boolean[] drops;
-        public Element[] elements;
-        public int count = 0;
-        public Production(String tag, Element[] elements, boolean[] drops) {
-            this.tag = tag;
-            this.elements = elements;
-            this.drops = drops;
-            for(int i=0; i<drops.length; i++)
-                if (!drops[i])
-                    count++;
-        }
-    }
+    public static class TreeBindable {
 
-    public static class Target {
-        public int[] buildSequence(Production p) {
+        private Bindable _bindable;
+        public TreeBindable(Object o) { this(Bindable.create(o)); }
+        public TreeBindable(Bindable b) { this._bindable = b; }
+
+        private int[] buildSequence(Production p) {
             Annotation[][] annotations = _bindable.getArgAnnotations();
             Class[]        types       = _bindable.getArgTypes();
             String[]       names       = _bindable.getArgNames();
@@ -83,18 +71,14 @@ public class MetaGrammar {
                 for(Annotation a : annotations[i+ofs])
                     if (a instanceof bind.arg)
                         argtags[i+ofs] = (bind.arg)a;
-            return Target.this.buildSequence(p, names, argtags, types);
+            return TreeBindable.this.buildSequence(p, names, argtags, types);
         }
-        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 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 String  toString()  { return _bindable.getSimpleName(); }
+        public boolean isRaw()     { return _bindable.isAnnotationPresent(bind.raw.class); }
 
         public boolean isCompatible(Production p) {
             bind.as t = getBindAs();
@@ -122,7 +106,7 @@ public class MetaGrammar {
             return false;
         }
 
-        public int[] buildSequence(Production p, String[] names, bind.arg[] argtags, Class[] types) {
+        private int[] buildSequence(Production p, String[] names, bind.arg[] argtags, Class[] types) {
             int argTagged = 0;
             boolean hasloc = types.length>0 && types[0]==Input.Region.class;
             for(int i=0; i<argtags.length; i++) {
@@ -161,20 +145,62 @@ public class MetaGrammar {
                 return null;
             }
         }
-        public Sequence makeSequence(Production p) {
-            return Sequence.rewritingSequence(new TargetReducer(buildSequence(p), _bindable, isRaw()),
-                                              p.elements, p.drops);
+        public Sequence makeSequence(final Production p) {
+
+            if (_bindable.getArgTypes().length > 0 &&
+                _bindable.getArgTypes()[0] == Input.Region.class) {
+                Functor<Input.Region,Object> func = new Functor<Input.Region,Object>() {
+                    public Object invoke(final Input.Region region) {
+                        return 
+                        new TreeBindableReducer(buildSequence(p),
+                                                _bindable,
+                                                isRaw()) {
+                            public Object invoke(Iterable<Tree<Object>> t) {
+                                if (_israw) return _bindable.impose(new Object[] { t });
+                                ArrayList ret = new ArrayList();
+                                ret.add(region);
+                                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);
+                                    }
+                                }
+                                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+2];
+                                for(int i=0; i<o.length; i++) o2[map[i]+1] = o[i];
+                                o2[0] = region;
+                                return _bindable.impose(o2);
+                            }
+                        };
+                    }
+                };
+                return Sequence.regionRewritingSequence(func,
+                                                        p.elements,
+                                                        p.drops);
+            }
+
+            return Sequence.rewritingSequence(new TreeBindableReducer(buildSequence(p),
+                                                                      _bindable,
+                                                                      isRaw()),
+                                              p.elements,
+                                              p.drops);
         }
 
     }
 
-    public static class TargetReducer implements Tree.TreeFunctor<Object,Object>, ToJava {
-        private int[] map;
-        private Bindable _bindable;
-        private boolean _israw;
+    public static class TreeBindableReducer implements Tree.TreeFunctor<Object,Object>, ToJava {
+         int[] map;
+         Bindable _bindable;
+         boolean _israw;
 
         public void toJava(StringBuffer sb) {
-            sb.append("new MetaGrammar.TargetReducer(new int[] {");
+            sb.append("new MetaGrammar.TreeBindableReducer(new int[] {");
             for(int i=0; i<map.length; i++) sb.append((i+"")+(i<map.length-1 ? "," : ""));
             sb.append("}, ");
             _bindable.toJava(sb);
@@ -183,18 +209,18 @@ public class MetaGrammar {
             sb.append(")");
         }
         
-        public TargetReducer(int[] map, Bindable b, boolean raw) {
+        public TreeBindableReducer(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(Tree<Object> t) {
+        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));
+                    ret.add(((Tree.TreeFunctor<Object,Object>)tc.head()).invoke(tc.children()));
                 else if (tc.numChildren() == 0)
                     ret.add(tc.head());
                 else {
@@ -205,14 +231,8 @@ public class MetaGrammar {
             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);
-            if (_bindable.getArgTypes().length > 0 &&
-                _bindable.getArgTypes()[0]==Input.Region.class)
-                max++;
             Object[] o2 = new Object[max+1];
             for(int i=0; i<o.length; i++) o2[map[i]] = o[i];
-            if (_bindable.getArgTypes().length > 0 &&
-                _bindable.getArgTypes()[0]==Input.Region.class)
-                o2[0] = t.getRegion();
             return _bindable.impose(o2);
         }
     }
@@ -222,7 +242,7 @@ public class MetaGrammar {
     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.GrammarNode g = (MetaGrammarBindings.GrammarNode)red.invoke(t);
+        MetaGrammarBindings.GrammarNode g = (MetaGrammarBindings.GrammarNode)red.invoke(t.children());
         return g.build(s, rm);
     }