checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammar.java
index acda011..78c4b72 100644 (file)
@@ -75,16 +75,17 @@ public class MetaGrammar {
     public static class Target {
         public int[] buildSequence(Production p) {
             Annotation[][] annotations = _bindable.getArgAnnotations();
+            Class[]        types       = _bindable.getArgTypes();
             String[]       names       = _bindable.getArgNames();
-            String name = _bindable.getSimpleName();
-            int len = annotations.length;
-            int ofs = 0;
+            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);
+            return Target.this.buildSequence(p, names, argtags, types);
         }
         private Bindable _bindable;
 
@@ -123,17 +124,23 @@ public class MetaGrammar {
             return false;
         }
 
-        public int[] buildSequence(Production p, String[] names, bind.arg[] argtags) {
+        public int[] buildSequence(Production p, String[] names, bind.arg[] argtags, Class[] types) {
             int argTagged = 0;
-            for(int i=0; i<argtags.length; i++)
+            boolean hasloc = types.length>0 && types[0]==Input.Region.class;
+            for(int i=0; i<argtags.length; i++) {
+                if (i==0 && types[0]==Input.Region.class) continue;
                 if (argtags[i] != null)
                     argTagged++;
+            }
+            int numNames = names.length;
+            if (hasloc) numNames--;
 
             // FIXME: can be smarter here
             if (argTagged==p.count) {
                 int[] ret = new int[argtags.length];
                 int j = 0;
                 for(int i=0; i<argtags.length; i++) {
+                    if (i==0 && types[0]==Input.Region.class) continue;
                     if (argtags[i]==null) continue;
                     if (argtags[i].value().equals(""))
                         ret[i] = j++;
@@ -148,9 +155,9 @@ public class MetaGrammar {
                     }
                 }
                 return ret;
-            } else if (names.length==p.count) {
+            } else if (numNames==p.count) {
                 int[] ret = new int[p.count];
-                for(int i=0; i<p.count; i++) ret[i] = i;
+                for(int i=0; i<p.count; i++) ret[i] = i+(hasloc?1:0);
                 return ret;
             } else {
                 return null;
@@ -184,12 +191,12 @@ public class MetaGrammar {
             this._israw = raw;
         }
         public String toString() { return "reducer-"+_bindable.toString(); }
-        public Object invoke(Iterable<Tree<Object>> t) {
+        public Object invoke(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()));
+                    ret.add(((Tree.TreeFunctor<Object,Object>)tc.head()).invoke(tc));
                 else if (tc.numChildren() == 0)
                     ret.add(tc.head());
                 else {
@@ -200,8 +207,14 @@ 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);
         }
     }
@@ -211,7 +224,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.children());
+        MetaGrammarBindings.GrammarNode g = (MetaGrammarBindings.GrammarNode)red.invoke(t);
         return g.build(s, rm);
     }