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 MetaGrammar {
14 public static void main(String[] args) throws Exception {
15 if (args.length != 2) {
16 System.err.println("usage: java " + MetaGrammar.class.getName() + " grammarfile.g com.yourdomain.package.ClassName");
19 String className = args[1].substring(args[1].lastIndexOf('.')+1);
20 String packageName = args[1].substring(0, args[1].lastIndexOf('.'));
21 String fileName = packageName.replace('.', '/') + "/" + className + ".java";
23 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
24 StringBuffer out = new StringBuffer();
27 for(String s = br.readLine(); s != null; s = br.readLine()) {
28 if (s.indexOf("DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED") != -1 && s.indexOf("\"")==-1) skip = true;
29 if (s.indexOf("DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED") != -1 && s.indexOf("\"")==-1) break;
30 if (!skip) out.append(s+"\n");
33 out.append("\n // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED\n");
35 Tree t = MetaGrammarTree.meta;
36 Union u = MetaGrammar.make(t, "s");
39 System.err.println("== parsing with parsed grammar =================================================================================");
40 t = new CharParser((Union)u).parse(new FileInputStream(args[0])).expand1();
41 System.out.println(t.toPrettyString());
44 out.append("\n // DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED\n");
46 for(String s = br.readLine(); s != null; s = br.readLine()) out.append(s+"\n");
49 OutputStream os = new FileOutputStream(fileName);
50 PrintWriter p = new PrintWriter(new OutputStreamWriter(os));
51 p.println(out.toString());
58 public static class Production {
60 public boolean[] drops;
61 public Element[] elements;
63 public Production(String tag, Element[] elements, boolean[] drops) {
65 this.elements = elements;
67 for(int i=0; i<drops.length; i++)
73 public static class Target {
74 public int[] buildSequence(Production p) {
75 Annotation[][] annotations = _bindable.getArgAnnotations();
76 Class[] types = _bindable.getArgTypes();
77 String[] names = _bindable.getArgNames();
78 String name = _bindable.getSimpleName();
79 int len = annotations.length;
81 bind.arg[] argtags = new bind.arg[len];
82 for(int i=0; i<names.length; i++)
83 for(Annotation a : annotations[i+ofs])
84 if (a instanceof bind.arg)
85 argtags[i+ofs] = (bind.arg)a;
86 return Target.this.buildSequence(p, names, argtags, types);
88 private Bindable _bindable;
90 public Target(Object o) { this(Bindable.create(o)); }
91 public Target(Bindable b) { this._bindable = b; }
93 public String getName() { return _bindable.getSimpleName(); }
94 public bind getBind() { return (bind)_bindable.getAnnotation(bind.class); }
95 public bind.as getBindAs() { return (bind.as)_bindable.getAnnotation(bind.as.class); }
96 public String toString() { return _bindable.getSimpleName(); }
97 public boolean isRaw() { return _bindable.isAnnotationPresent(bind.raw.class); }
99 public boolean isCompatible(Production p) {
100 bind.as t = getBindAs();
104 (t.value().equals(p.tag)))
105 return buildSequence(p)!=null;
108 ((t.value().equals("") && getName().equals(p.tag))))
109 return buildSequence(p)!=null;
110 if (b != null && getName().equals(p.tag))
111 return buildSequence(p)!=null;
114 (t.value().equals(p.tag)))
115 return buildSequence(p)!=null;
117 ((t.value().equals("") && getName().equals(p.tag))))
118 return buildSequence(p)!=null;
119 if (b != null && getName().equals(p.tag))
120 return buildSequence(p)!=null;
125 public int[] buildSequence(Production p, String[] names, bind.arg[] argtags, Class[] types) {
127 boolean hasloc = types.length>0 && types[0]==Input.Region.class;
128 for(int i=0; i<argtags.length; i++) {
129 if (i==0 && types[0]==Input.Region.class) continue;
130 if (argtags[i] != null)
133 int numNames = names.length;
134 if (hasloc) numNames--;
136 // FIXME: can be smarter here
137 if (argTagged==p.count) {
138 int[] ret = new int[argtags.length];
140 for(int i=0; i<argtags.length; i++) {
141 if (i==0 && types[0]==Input.Region.class) continue;
142 if (argtags[i]==null) continue;
143 if (argtags[i].value().equals(""))
147 for(int k=0; k<names.length; k++)
148 if (argtags[i].value().equals(names[k])){
152 if (ret[i]==-1) return null;
156 } else if (numNames==p.count) {
157 int[] ret = new int[p.count];
158 for(int i=0; i<p.count; i++) ret[i] = i+(hasloc?1:0);
164 public Sequence makeSequence(Production p) {
165 return Sequence.rewritingSequence(new TargetReducer(buildSequence(p), _bindable, isRaw()),
166 p.elements, p.drops);
171 public static class TargetReducer implements Tree.TreeFunctor<Object,Object>, ToJava {
173 private Bindable _bindable;
174 private boolean _israw;
176 public void toJava(StringBuffer sb) {
177 sb.append("new MetaGrammar.TargetReducer(new int[] {");
178 for(int i=0; i<map.length; i++) sb.append((i+"")+(i<map.length-1 ? "," : ""));
180 _bindable.toJava(sb);
182 sb.append(_israw ? "true" : "false");
186 public TargetReducer(int[] map, Bindable b, boolean raw) {
191 public String toString() { return "reducer-"+_bindable.toString(); }
192 public Object invoke(Tree<Object> t) {
193 if (_israw) return _bindable.impose(new Object[] { t });
194 ArrayList ret = new ArrayList();
196 if (tc.head() != null && tc.head() instanceof Functor)
197 ret.add(((Tree.TreeFunctor<Object,Object>)tc.head()).invoke(tc));
198 else if (tc.numChildren() == 0)
201 System.err.println("FIXME: don't know what to do about " + tc);
205 Object[] o = (Object[])ret.toArray(new Object[0]);
207 for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
208 if (_bindable.getArgTypes().length > 0 &&
209 _bindable.getArgTypes()[0]==Input.Region.class)
211 Object[] o2 = new Object[max+1];
212 for(int i=0; i<o.length; i++) o2[map[i]] = o[i];
213 if (_bindable.getArgTypes().length > 0 &&
214 _bindable.getArgTypes()[0]==Input.Region.class)
215 o2[0] = t.getRegion();
216 return _bindable.impose(o2);
221 public static Union make() { return make(MetaGrammarTree.meta, "s"); }
222 public static Union make(Tree t, String s) { return make(t, s, new AnnotationGrammarBindingResolver(MetaGrammarBindings.class)); }
223 public static Union make(Tree t, String s, GrammarBindingResolver rm) {
224 Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
225 MetaGrammarBindings.GrammarNode g = (MetaGrammarBindings.GrammarNode)red.invoke(t);
226 return g.build(s, rm);