checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / Production.java
index 0abb996..bcd2407 100644 (file)
@@ -11,10 +11,13 @@ import java.io.*;
 
 public  class Production {
     public String tag;
+    public String nonTerminal;
     public boolean[] drops;
     public Element[] elements;
-    public String nonTerminal;
     public int count = 0;
+
+    public String tag() { return tag==null ? nonTerminal : tag; }
+
     public Production(String tag, Element[] elements, boolean[] drops) { this(tag, tag, elements, drops); }
     public Production(String tag, String nonTerminal, Element[] elements, boolean[] drops) {
         this.tag = tag;
@@ -25,4 +28,92 @@ public  class Production {
             if (!drops[i])
                 count++;
     }
+
+    public int[] buildSequence(Bindable _bindable) {
+        Annotation[][] annotations = _bindable.getArgAnnotations();
+        Class[]        types       = _bindable.getArgTypes();
+        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;
+
+        int argTagged = 0;
+        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==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++;
+                else {
+                    ret[i] = -1;
+                    for(int k=0; k<names.length; k++)
+                        if (argtags[i].value().equals(names[k])){
+                            ret[i] = k;
+                            break;
+                        }
+                    if (ret[i]==-1) return null;
+                }
+            }
+            return ret;
+        } else if (numNames==count) {
+            int[] ret = new int[count];
+            for(int i=0; i<count; i++) ret[i] = i+(hasloc?1:0);
+            return ret;
+        } else {
+            return null;
+        }
+    }
+
+    public boolean isCompatible(Object o) { return isCompatible(Bindable.create(o)); }
+    public boolean isCompatible(Bindable _bindable) {
+        bind.as t = _bindable.getAnnotation(bind.as.class);
+        bind b = _bindable.getAnnotation(bind.class);
+        
+        boolean ok = false;
+        if (t != null && (t.value().equals(tag))) ok = true;
+        if (t != null && ((t.value().equals("") && _bindable.getSimpleName().equals(tag)))) ok = true;
+        if (b != null && _bindable.getSimpleName().equals(tag)) ok = true;
+        
+        if (ok) return buildSequence(_bindable)!=null;
+        
+        return false;
+    }
+    
+    public Sequence makeSequence(Object o) { return makeSequence(Bindable.create(o)); }
+    public Sequence makeSequence(final Bindable _bindable) {
+
+        /*
+        if (_bindable.getArgTypes().length > 0 &&
+            _bindable.getArgTypes()[0] == Input.Region.class) {
+            Functor<Input.Region,Object> func = new Functor<Input.Region,Object>() {
+                int[] map = buildSequence(_bindable);
+                public Object invoke(final Input.Region region) { return _bindable.createBinding(map, region); }
+            };
+            return Sequence.newRegionRewritingSequence(func, elements, drops);
+        }
+        */
+
+        if (_bindable.isAnnotationPresent(bind.raw.class))
+            return Sequence.create(new RawBindingFunctor(tag(), _bindable.createBinding()), elements, drops, false);
+        int[] map = buildSequence(_bindable);
+        return Sequence.create(new BindingFunctor(tag(), _bindable.createBinding()), elements, drops, false);
+    }
+
 }