1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp.meta;
4 import edu.berkeley.sbp.util.*;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.chr.*;
7 import edu.berkeley.sbp.misc.*;
8 import edu.berkeley.sbp.bind.*;
10 import java.lang.annotation.*;
11 import java.lang.reflect.*;
14 public class Production {
16 public String nonTerminal;
17 public boolean[] drops;
18 public Element[] elements;
21 public String tag() { return tag==null ? nonTerminal : tag; }
23 public Production(String tag, Element[] elements, boolean[] drops) { this(tag, tag, elements, drops); }
24 public Production(String tag, String nonTerminal, Element[] elements, boolean[] drops) {
26 this.elements = elements;
28 this.nonTerminal = nonTerminal;
29 for(int i=0; i<drops.length; i++)
34 public int[] buildSequence(Bindable _bindable) {
35 Annotation[][] annotations = _bindable.getArgAnnotations();
36 Class[] types = _bindable.getArgTypes();
37 String[] names = _bindable.getArgNames();
38 String name = _bindable.getSimpleName();
39 int len = annotations.length;
41 bind.arg[] argtags = new bind.arg[len];
42 for(int i=0; i<names.length; i++)
43 for(Annotation a : annotations[i+ofs])
44 if (a instanceof bind.arg)
45 argtags[i+ofs] = (bind.arg)a;
48 boolean hasloc = types.length>0 && types[0]==Input.Region.class;
49 for(int i=0; i<argtags.length; i++) {
50 if (i==0 && types[0]==Input.Region.class) continue;
51 if (argtags[i] != null)
54 int numNames = names.length;
55 if (hasloc) numNames--;
57 // FIXME: can be smarter here
58 if (argTagged==count) {
59 int[] ret = new int[argtags.length];
61 for(int i=0; i<argtags.length; i++) {
62 if (i==0 && types[0]==Input.Region.class) continue;
63 if (argtags[i]==null) continue;
64 if (argtags[i].value().equals(""))
68 for(int k=0; k<names.length; k++)
69 if (argtags[i].value().equals(names[k])){
73 if (ret[i]==-1) return null;
77 } else if (numNames==count) {
78 int[] ret = new int[count];
79 for(int i=0; i<count; i++) ret[i] = i+(hasloc?1:0);
86 public boolean isCompatible(Object o) { return isCompatible(Bindable.create(o)); }
87 public boolean isCompatible(Bindable _bindable) {
88 bind.as t = _bindable.getAnnotation(bind.as.class);
89 bind b = _bindable.getAnnotation(bind.class);
92 if (t != null && (t.value().equals(tag))) ok = true;
93 if (t != null && ((t.value().equals("") && _bindable.getSimpleName().equals(tag)))) ok = true;
94 if (b != null && _bindable.getSimpleName().equals(tag)) ok = true;
96 if (ok) return buildSequence(_bindable)!=null;
101 public Sequence makeSequence(Object o) { return makeSequence(Bindable.create(o)); }
102 public Sequence makeSequence(final Bindable _bindable) {
105 if (_bindable.getArgTypes().length > 0 &&
106 _bindable.getArgTypes()[0] == Input.Region.class) {
107 Functor<Input.Region,Object> func = new Functor<Input.Region,Object>() {
108 int[] map = buildSequence(_bindable);
109 public Object invoke(final Input.Region region) { return _bindable.createBinding(map, region); }
111 return Sequence.newRegionRewritingSequence(func, elements, drops);
115 if (_bindable.isAnnotationPresent(bind.raw.class))
116 return Sequence.create(new RawBindingFunctor(tag(), _bindable.createBinding()), elements, drops, false);
117 int[] map = buildSequence(_bindable);
118 return Sequence.create(new BindingFunctor(tag(), _bindable.createBinding()), elements, drops, false);