copyright notices/updates
[sbp.git] / src / edu / berkeley / sbp / meta / Production.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
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.*;
9 import java.util.*;
10 import java.lang.annotation.*;
11 import java.lang.reflect.*;
12 import java.io.*;
13
14 public  class Production {
15     public String tag;
16     public String nonTerminal;
17     public boolean[] drops;
18     public Element[] elements;
19     public int count = 0;
20
21     public String tag() { return tag==null ? nonTerminal : tag; }
22
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) {
25         this.tag = tag;
26         this.elements = elements;
27         this.drops = drops;
28         this.nonTerminal = nonTerminal;
29         for(int i=0; i<drops.length; i++)
30             if (!drops[i])
31                 count++;
32     }
33
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;
40         int ofs                    = 0;
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;
46
47         int argTagged = 0;
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)
52                 argTagged++;
53         }
54         int numNames = names.length;
55         if (hasloc) numNames--;
56
57         // FIXME: can be smarter here
58         if (argTagged==count) {
59             int[] ret = new int[argtags.length];
60             int j = 0;
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(""))
65                     ret[i] = j++;
66                 else {
67                     ret[i] = -1;
68                     for(int k=0; k<names.length; k++)
69                         if (argtags[i].value().equals(names[k])){
70                             ret[i] = k;
71                             break;
72                         }
73                     if (ret[i]==-1) return null;
74                 }
75             }
76             return ret;
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);
80             return ret;
81         } else {
82             return null;
83         }
84     }
85
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);
90         
91         boolean ok = false;
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;
95         
96         if (ok) return buildSequence(_bindable)!=null;
97         
98         return false;
99     }
100     
101     public Sequence makeSequence(Object o) { return makeSequence(Bindable.create(o)); }
102     public Sequence makeSequence(final Bindable _bindable) {
103
104         /*
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); }
110             };
111             return Sequence.newRegionRewritingSequence(func, elements, drops);
112         }
113         */
114
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);
119     }
120
121 }