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 boolean[] drops;
15 public Element[] elements;
16 public String nonTerminal;
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) {
21 this.elements = elements;
23 this.nonTerminal = nonTerminal;
24 for(int i=0; i<drops.length; i++)
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;
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;
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)
49 int numNames = names.length;
50 if (hasloc) numNames--;
52 // FIXME: can be smarter here
53 if (argTagged==count) {
54 int[] ret = new int[argtags.length];
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(""))
63 for(int k=0; k<names.length; k++)
64 if (argtags[i].value().equals(names[k])){
68 if (ret[i]==-1) return null;
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);
81 public boolean isCompatible(Object o) { return isCompatible(Bindable.create(o)); }
82 public boolean isCompatible(Bindable _bindable) {
83 bind.as t = _bindable.getAnnotation(bind.as.class);
84 bind b = _bindable.getAnnotation(bind.class);
87 if (t != null && (t.value().equals(tag))) ok = true;
88 if (t != null && ((t.value().equals("") && _bindable.getSimpleName().equals(tag)))) ok = true;
89 if (b != null && _bindable.getSimpleName().equals(tag)) ok = true;
91 if (ok) return buildSequence(_bindable)!=null;
96 public Sequence makeSequence(Object o) { return makeSequence(Bindable.create(o)); }
97 public Sequence makeSequence(final Bindable _bindable) {
99 if (_bindable.getArgTypes().length > 0 &&
100 _bindable.getArgTypes()[0] == Input.Region.class) {
101 Functor<Input.Region,Object> func = new Functor<Input.Region,Object>() {
102 int[] map = buildSequence(_bindable);
103 public Object invoke(final Input.Region region) { return _bindable.createBinding(map, region); }
105 return Sequence.regionRewritingSequence(func, elements, drops);
108 if (_bindable.isAnnotationPresent(bind.raw.class))
109 return Sequence.rewritingSequence(new Tree.RawBindingFunctor(_bindable.createBinding()), elements, drops);
110 int[] map = buildSequence(_bindable);
111 return Sequence.rewritingSequence(new Tree.BindingFunctor(_bindable.createBinding()), elements, drops);