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.*;
8 import java.lang.annotation.*;
9 import java.lang.reflect.*;
12 public class Production {
14 public String nonTerminal;
15 public boolean[] drops;
16 public Element[] elements;
19 public String tag() { return tag==null ? nonTerminal : tag; }
21 public Production(String tag, Element[] elements, boolean[] drops) { this(tag, tag, elements, drops); }
22 public Production(String tag, String nonTerminal, Element[] elements, boolean[] drops) {
24 this.elements = elements;
26 this.nonTerminal = nonTerminal;
27 for(int i=0; i<drops.length; i++)
32 public int[] buildSequence(Bindable _bindable) {
33 Annotation[][] annotations = _bindable.getArgAnnotations();
34 Class[] types = _bindable.getArgTypes();
35 String[] names = _bindable.getArgNames();
36 String name = _bindable.getSimpleName();
37 int len = annotations.length;
39 bind.arg[] argtags = new bind.arg[len];
40 for(int i=0; i<names.length; i++)
41 for(Annotation a : annotations[i+ofs])
42 if (a instanceof bind.arg)
43 argtags[i+ofs] = (bind.arg)a;
46 boolean hasloc = types.length>0 && types[0]==Input.Region.class;
47 for(int i=0; i<argtags.length; i++) {
48 if (i==0 && types[0]==Input.Region.class) continue;
49 if (argtags[i] != null)
52 int numNames = names.length;
53 if (hasloc) numNames--;
55 // FIXME: can be smarter here
56 if (argTagged==count) {
57 int[] ret = new int[argtags.length];
59 for(int i=0; i<argtags.length; i++) {
60 if (i==0 && types[0]==Input.Region.class) continue;
61 if (argtags[i]==null) continue;
62 if (argtags[i].value().equals(""))
66 for(int k=0; k<names.length; k++)
67 if (argtags[i].value().equals(names[k])){
71 if (ret[i]==-1) return null;
75 } else if (numNames==count) {
76 int[] ret = new int[count];
77 for(int i=0; i<count; i++) ret[i] = i+(hasloc?1:0);
84 public boolean isCompatible(Object o) { return isCompatible(Bindable.create(o)); }
85 public boolean isCompatible(Bindable _bindable) {
86 bind.as t = _bindable.getAnnotation(bind.as.class);
87 bind b = _bindable.getAnnotation(bind.class);
90 if (t != null && (t.value().equals(tag))) ok = true;
91 if (t != null && ((t.value().equals("") && _bindable.getSimpleName().equals(tag)))) ok = true;
92 if (b != null && _bindable.getSimpleName().equals(tag)) ok = true;
94 if (ok) return buildSequence(_bindable)!=null;
99 public Sequence makeSequence(Object o) { return makeSequence(Bindable.create(o)); }
100 public Sequence makeSequence(final Bindable _bindable) {
102 if (_bindable.getArgTypes().length > 0 &&
103 _bindable.getArgTypes()[0] == Input.Region.class) {
104 Functor<Input.Region,Object> func = new Functor<Input.Region,Object>() {
105 int[] map = buildSequence(_bindable);
106 public Object invoke(final Input.Region region) { return _bindable.createBinding(map, region); }
108 return Sequence.regionRewritingSequence(func, elements, drops);
111 if (_bindable.isAnnotationPresent(bind.raw.class))
112 return Sequence.rewritingSequence(new RawBindingFunctor(tag(), _bindable.createBinding()), elements, drops);
113 int[] map = buildSequence(_bindable);
114 return Sequence.rewritingSequence(new BindingFunctor(tag(), _bindable.createBinding()), elements, drops);