checkpoint
[sbp.git] / src / edu / berkeley / sbp / meta / Production.java
1 package edu.berkeley.sbp.meta;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.bind.*;
7 import java.util.*;
8 import java.lang.annotation.*;
9 import java.lang.reflect.*;
10 import java.io.*;
11
12 public  class Production {
13     public String tag;
14     public boolean[] drops;
15     public Element[] elements;
16     public String nonTerminal;
17     public int count = 0;
18     public Production(String tag, Element[] elements, boolean[] drops) { this(tag, tag, elements, drops); }
19     public Production(String tag, String nonTerminal, Element[] elements, boolean[] drops) {
20         this.tag = tag;
21         this.elements = elements;
22         this.drops = drops;
23         this.nonTerminal = nonTerminal;
24         for(int i=0; i<drops.length; i++)
25             if (!drops[i])
26                 count++;
27     }
28
29     public int[] buildSequence(Bindable _bindable) {
30         Annotation[][] annotations = _bindable.getArgAnnotations();
31         Class[]        types       = _bindable.getArgTypes();
32         String[]       names       = _bindable.getArgNames();
33         String name                = _bindable.getSimpleName();
34         int len                    = annotations.length;
35         int ofs                    = 0;
36         bind.arg[] argtags  = new bind.arg[len];
37         for(int i=0; i<names.length; i++)
38             for(Annotation a : annotations[i+ofs])
39                 if (a instanceof bind.arg)
40                     argtags[i+ofs] = (bind.arg)a;
41
42         int argTagged = 0;
43         boolean hasloc = types.length>0 && types[0]==Input.Region.class;
44         for(int i=0; i<argtags.length; i++) {
45             if (i==0 && types[0]==Input.Region.class) continue;
46             if (argtags[i] != null)
47                 argTagged++;
48         }
49         int numNames = names.length;
50         if (hasloc) numNames--;
51
52         // FIXME: can be smarter here
53         if (argTagged==count) {
54             int[] ret = new int[argtags.length];
55             int j = 0;
56             for(int i=0; i<argtags.length; i++) {
57                 if (i==0 && types[0]==Input.Region.class) continue;
58                 if (argtags[i]==null) continue;
59                 if (argtags[i].value().equals(""))
60                     ret[i] = j++;
61                 else {
62                     ret[i] = -1;
63                     for(int k=0; k<names.length; k++)
64                         if (argtags[i].value().equals(names[k])){
65                             ret[i] = k;
66                             break;
67                         }
68                     if (ret[i]==-1) return null;
69                 }
70             }
71             return ret;
72         } else if (numNames==count) {
73             int[] ret = new int[count];
74             for(int i=0; i<count; i++) ret[i] = i+(hasloc?1:0);
75             return ret;
76         } else {
77             return null;
78         }
79     }
80
81 }