1 package edu.berkeley.sbp.misc;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
6 import java.lang.annotation.*;
7 import java.lang.reflect.*;
11 public static void main(String[] s) throws Exception {
12 Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
15 new ReflectiveMeta(MG.class,
22 MG.NonTerminalReference.class,
23 MG.StringLiteral.class,
27 MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
28 MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
29 Union meta = mgf.get("s").build(bc);
31 System.err.println("parsing " + s[1]);
32 Tree t = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
34 System.out.println(t.toPrettyString());
35 Reducer red = (Reducer)t.head();
36 System.out.println(red.reduce(t));
39 public static class ReflectiveMeta extends MetaGrammar.Meta {
40 private final Class _cl;
41 private final Class[] _inner;
42 public ReflectiveMeta(Class c, Class[] inner) {
46 private boolean match(Method m, String s) { return match(m.getAnnotation(tag.class), null, s); }
47 private boolean match(tag t, Class c, String s) {
48 if (t==null) return false;
49 if (t.value().equals(s)) return true;
50 if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true;
53 private boolean match(nonterminal t, Class c, String s) {
54 if (t==null) return false;
55 if (t.value().equals(s)) return true;
56 if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true;
59 private boolean match(Class c, String s, String nonTerminalName) {
60 if (match((tag)c.getAnnotation(tag.class), c, s)) return true;
61 if (match((nonterminal)c.getAnnotation(nonterminal.class), c, nonTerminalName)) return true;
64 public boolean match(Constructor con, String s, String nonTerminalName) {
65 Class c = con.getDeclaringClass();
66 if (match((tag)con.getAnnotation(tag.class), null, s)) return true;
67 if (match((nonterminal)con.getAnnotation(nonterminal.class), c, s)) return true;
70 public Object repeatTag() {
71 return new Reducer() {
72 public String toString() { return "[**]"; }
73 public Object reduce(Tree t) {
74 Object[] ret = new Object[t.numChildren()];
75 for(int i=0; i<t.numChildren(); i++) {
77 if (tc.head() != null && tc.head() instanceof Reducer)
78 ret[i] = ((Reducer)tc.head()).reduce(tc);
79 else if (tc.numChildren() == 0)
82 System.err.println("FIXME: don't know what to do about " + tc);
90 public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
91 Production p = new Production(tag, nonTerminalName, els, labels, drops);
92 for(Method m : _cl.getMethods())
93 if (new TargetMethod(m).isCompatible(p))
94 return new TargetMethod(m).makeSequence(p);
96 for(Constructor con : c.getConstructors())
97 if (new TargetConstructor(con).isCompatible(p))
98 return new TargetConstructor(con).makeSequence(p);
100 if (new TargetClass(c).isCompatible(p))
101 return new TargetClass(c).makeSequence(p);
102 throw new RuntimeException("could not find a Java method/class/ctor matching tag \""+tag+"\", nonterminal \""+nonTerminalName+"\"");
106 public static Object makeFlattener(final Method m, final Element[] els, final Object[] labels, final boolean[] drops) {
107 return new Reducer() {
108 public Object reduce(Tree t) {
109 Object[] o = new Object[m.getParameterTypes()];
111 for(int i=0; i<els.length; i++)
119 * Constructors, classes, and methods with this attribute will
120 * match every production of the nonterminal called "value()"
121 * that is arg-compatible. If value() is undefined, then the
122 * class/constructor/method name is used.
124 @Retention(RetentionPolicy.RUNTIME) public static @interface nonterminal { String value() default ""; }
127 * Constructors, classes, and methods with this attribute will
128 * match every tree tagged with "value()" that is arg-compatible.
129 * If value() is undefined, then the class/constructor/method
132 @Retention(RetentionPolicy.RUNTIME) public static @interface tag { String value() default ""; }
135 * If any parameter to a method or field in a class has a named
136 * arg-tag, that parameter/field matches the child of the tree
137 * which either has that label or else is a reference to a
138 * nonterminal with the corresponding name.
140 * The remaining non-named arg-tags match the remaining children
141 * of the tree in sequential order.
143 * If any arg-tagged parameters/fields remain, the match fails.
144 * If there were no arg-tagged parameters-fields, it is as if all
145 * of them were non-named and arg-tagged.
147 * A method/constructor is arg-compatible if all of its arguments
148 * are arg-compatible.
150 * A class is arg-compatible if all of its fields are
151 * arg-compatible, or if one of its constructors is arg-compatible.
154 @Retention(RetentionPolicy.RUNTIME) public static @interface arg { String value() default ""; }
156 public static class Production {
158 public String nonTerminal;
159 public Object[] labels;
160 public boolean[] drops;
161 public Element[] elements;
162 public int count = 0;
163 public Production(String tag, String nonTerminal, Element[] elements, Object[] labels, boolean[] drops) {
165 this.elements = elements;
166 this.nonTerminal = nonTerminal;
167 this.labels = labels;
169 for(int i=0; i<drops.length; i++)
175 public static abstract class Target {
176 public abstract String getName();
177 public abstract tag getTag();
178 public abstract nonterminal getNonTerminal();
179 public abstract int[] buildSequence(Production p);
180 public boolean isCompatible(Production p) {
183 (t.value().equals(p.tag) ||
184 (t.value().equals("") && p.tag.equals(getName()))))
185 return buildSequence(p)!=null;
187 nonterminal n = getNonTerminal();
189 (n.value().equals(p.nonTerminal) ||
190 (n.value().equals("") && p.nonTerminal.equals(getName()))))
191 return buildSequence(p)!=null;
195 public int[] buildSequence(Production p, String[] names, arg[] argtags) {
197 for(int i=0; i<argtags.length; i++)
198 if (argtags[i] != null)
201 // FIXME: can be smarter here
202 if (names.length==p.count) {
203 int[] ret = new int[p.count];
204 for(int i=0; i<p.count; i++) ret[i] = i;
206 } else if (argTagged==p.count) {
207 int[] ret = new int[argtags.length];
209 for(int i=0; i<argtags.length; i++)
210 ret[i] = argtags[i]==null ? -1 : (j++);
216 public Sequence makeSequence(Production p) {
217 return Sequence.rewritingSequence(new TargetReducer(p), p.elements, p.labels, p.drops);
219 public abstract Object plant(Object[] fields, int[] map);
220 public class TargetReducer implements Reducer {
221 private Production p;
223 public TargetReducer(Production p) {
225 this.map = buildSequence(p);
227 public String toString() { return "reducer-"+Target.this; }
228 public Object reduce(Tree t) {
229 Object[] objects = new Object[t.numChildren()];
230 for(int i=0; i<t.numChildren(); i++) {
231 Tree tc = t.child(i);
232 if (tc.head() != null && tc.head() instanceof Reducer)
233 objects[i] = ((Reducer)tc.head()).reduce(tc);
234 else if (tc.numChildren() == 0)
235 objects[i] = tc.head();
237 System.err.println("FIXME: don't know what to do about " + tc);
241 System.err.println("input tree: " + t);
242 return plant(objects, map);
247 public static interface Reducer {
248 public Object reduce(Tree t);
251 public static class TargetClass extends Target {
252 public final Class _class;
253 public TargetClass(Class _class) { this._class = _class; }
254 public String getName() { return _class.getSimpleName(); }
255 public tag getTag() { return (tag)_class.getAnnotation(tag.class); }
256 public nonterminal getNonTerminal() { return (nonterminal)_class.getAnnotation(nonterminal.class); }
257 public String toString() { return _class.getSimpleName(); }
258 public int[] buildSequence(Production p) {
259 Field[] f = _class.getDeclaredFields();
260 String[] names = new String[f.length];
261 arg[] argtags = new arg[f.length];
262 for(int i=0; i<f.length; i++) {
263 names[i] = f[i].getName();
264 argtags[i] = f[i].getAnnotation(arg.class);
266 int[] ret = buildSequence(p, names, argtags);
267 if (ret!=null) return ret;
268 for(Constructor c : _class.getConstructors())
269 if (new TargetConstructor(c).buildSequence(p)!=null)
270 return new TargetConstructor(c).buildSequence(p);
273 public Object plant(Object[] fields, int[] map) {
275 Object ret = _class.newInstance();
276 Field[] f = _class.getFields();
278 for(int i=0; i<f.length; i++)
280 Object tgt = Reflection.lub(fields[map[i]]);
281 if (f[i].getType() == String.class) tgt = stringify(tgt);
283 tgt = coerce(tgt, f[i].getType());
284 System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
288 } catch (Exception e) {
290 throw new RuntimeException(e);
295 public static String stringify(Object o) {
296 if (o==null) return "";
297 if (!(o instanceof Object[])) return o.toString();
298 Object[] arr = (Object[])o;
299 StringBuffer ret = new StringBuffer();
300 for(int i=0; i<arr.length; i++)
302 return ret.toString();
305 public static class TargetConstructor extends Target {
306 public final Constructor _ctor;
307 public TargetConstructor(Constructor _ctor) { this._ctor = _ctor; }
308 public String getName() { return _ctor.getName(); }
309 public tag getTag() { return (tag)_ctor.getAnnotation(tag.class); }
310 public nonterminal getNonTerminal() { return (nonterminal)_ctor.getAnnotation(nonterminal.class); }
311 public String toString() { return _ctor.getName(); }
312 public int[] buildSequence(Production p) {
313 Annotation[][] annotations = _ctor.getParameterAnnotations();
314 int len = annotations.length;
316 String name = _ctor.getDeclaringClass().getName();
318 if (name.indexOf('$') > name.lastIndexOf('.')) {
323 String[] names = new String[len];
324 arg[] argtags = new arg[len];
325 for(int i=0; i<names.length; i++)
326 for(Annotation a : annotations[i+ofs])
327 if (a instanceof arg)
328 argtags[i+ofs] = (arg)a;
329 return buildSequence(p, names, argtags);
331 public Object plant(Object[] fields, int[] map) {
333 Class[] argTypes = _ctor.getParameterTypes();
334 Object[] args = new Object[argTypes.length];
336 for(int i=0; i<args.length; i++)
338 Object tgt = Reflection.lub(fields[map[i]]);
339 if (argTypes[i] == String.class) tgt = stringify(tgt);
341 tgt = coerce(tgt, argTypes[i]);
342 System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
345 return _ctor.newInstance(args);
346 } catch (Exception e) {
347 throw new RuntimeException(e);
351 public static class TargetMethod extends Target {
352 public final Method _method;
353 public TargetMethod(Method _method) { this._method = _method; }
354 public String getName() { return _method.getName(); }
355 public String toString() { return _method.getName(); }
356 public tag getTag() { return (tag)_method.getAnnotation(tag.class); }
357 public nonterminal getNonTerminal() { return (nonterminal)_method.getAnnotation(nonterminal.class); }
358 public int[] buildSequence(Production p) {
359 Annotation[][] annotations = _method.getParameterAnnotations();
360 String[] names = new String[annotations.length];
361 arg[] argtags = new arg[annotations.length];
362 for(int i=0; i<names.length; i++)
363 for(Annotation a : annotations[i])
364 if (a instanceof arg)
366 int[] ret = buildSequence(p, names, argtags);
369 public Object plant(Object[] fields, int[] map) {
371 Class[] argTypes = _method.getParameterTypes();
372 Object[] args = new Object[argTypes.length];
374 for(int i=0; i<args.length; i++)
376 Object tgt = Reflection.lub(fields[map[i]]);
377 if (argTypes[i] == String.class) tgt = stringify(tgt);
379 tgt = coerce(tgt, argTypes[i]);
380 System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
383 System.err.println("invoking " + _method + " with " + Reflection.show(args));
384 return _method.invoke(null, args);
385 } catch (Exception e) {
386 throw new RuntimeException(e);
391 public static Object coerce(Object o, Class c) {
392 if (o==null) return null;
393 if (c.isInstance(o)) return o;
394 if (c == char.class) {
395 return o.toString().charAt(0);
397 if (c.isArray() && (c.getComponentType().isInstance(o))) {
398 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
402 if (o.getClass().isArray() && c.isArray()) {
404 for(int i=0; i<((Object[])o).length; i++) {
405 Object ob = (((Object[])o)[i]);
407 System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
412 System.err.println("hit with " + c.getComponentType().getName());
413 return Array.newInstance(c.getComponentType(), ((Object[])o).length);
419 public static class MG {
420 public static @tag("grammar") class Grammar {
421 public @arg("NonTerminal") NonTerminal[] nonterminals;
422 public String toString() {
424 for(NonTerminal nt : nonterminals) ret += nt + ", ";
428 public static @nonterminal class NonTerminal extends El {
429 public @arg("Word") String name;
430 public @arg("RHS") Seq[][] sequences;
431 private static int anon = 0;
432 public NonTerminal() { }
433 public NonTerminal(Seq[][] sequences) {
434 this.name = "anon" + (anon++);
435 this.sequences = sequences;
437 public Element build(Context cx) {
438 Element ret = cx.peek(name);
439 if (ret != null) return ret;
440 Union u = new Union(name);
442 HashSet<Sequence> bad = new HashSet<Sequence>();
443 for(Seq[] group : sequences) {
444 Union u2 = new Union(""+anon++);
445 HashSet<Sequence> bad2 = new HashSet<Sequence>();
446 for(Seq seq : group) {
447 Sequence built = seq.build(cx);
448 for(Sequence s : bad)
449 built = built.not(s);
454 u.add(Sequence.singleton(u2));
460 public static @tag void range(char c) { }
461 public static class Range {
462 public @tag("range") Range(char only) { first = only; last = only; }
463 public @tag("-") Range(char first, char last) { this.first = first; this.last = last; }
467 public static abstract class El {
468 public String getLabel() { return null; }
469 public String getOwnerTag() { return null; }
470 public boolean drop() { return false; }
471 public abstract Element build(Context cx);
473 public static class Drop extends El {
475 public Drop(El e) { this.e = e; }
476 public String getLabel() { return null; }
477 public boolean drop() { return true; }
478 public Element build(Context cx) { return e.build(cx); }
480 public static class Label extends El {
483 public Label(String label, El e) { this.e = e; this.label = label; }
484 public String getLabel() { return label; }
485 public Element build(Context cx) { return e.build(cx); }
487 public static /*abstract*/ class Seq {
488 HashSet<Seq> and = new HashSet<Seq>();
489 HashSet<Seq> not = new HashSet<Seq>();
493 public Seq(El e) { this(new El[] { e }); }
494 public Seq(El[] elements) { this.elements = elements; }
495 public Seq tag(String tag) { this.tag = tag; return this; }
496 public Seq follow(El follow) { this.follow = follow; return this; }
498 Seq ret = new Seq(elements);
505 public Seq and(Seq s) { and.add(s); return this; }
506 public Seq andnot(Seq s) { not.add(s); return this; }
507 public Seq separate(El sep) {
508 El[] elements = new El[this.elements.length * 2 - 1];
509 for(int i=0; i<this.elements.length; i++) {
510 elements[i*2] = this.elements[i];
511 if (i<this.elements.length-1)
512 elements[i*2+1] = new Drop(sep);
514 this.elements = elements;
517 public Sequence build(Context cx) {
518 boolean unwrap = false;
519 boolean dropAll = false;
520 if (tag!=null && tag.equals("[]")) unwrap = true;
521 if (tag!=null && "()".equals(tag)) dropAll = true;
522 Object[] labels = new Object[elements.length];
523 boolean[] drops = new boolean[elements.length];
524 Element[] els = new Element[elements.length];
525 for(int i=0; i<elements.length; i++) {
526 labels[i] = elements[i].getLabel();
527 drops[i] = elements[i].drop();
528 els[i] = elements[i].build(cx);
529 if (elements[i].getOwnerTag() != null)
530 tag = elements[i].getOwnerTag();
533 if (dropAll) ret = Sequence.drop(els, false);
534 else if (unwrap) ret = Sequence.unwrap(els, repeatTag(), drops);
535 else if (tag!=null) {
537 //ret = resolveTag(tag, bc.currentNonTerminal==null ? null : bc.currentNonTerminal.name, els, labels, drops);
538 //System.err.println("resolvetag " + tag + " => " + ret);
539 ret = Sequence.rewritingSequence(tag, els, labels, drops);
542 for(int i=0; i<els.length; i++)
544 if (idx==-1) idx = i;
545 else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
546 if (idx != -1) ret = Sequence.singleton(els, idx);
547 else ret = Sequence.drop(els, false);
549 if (this.follow != null)
550 ret.follow = MetaGrammar.infer(this.follow.build(cx));
554 public static @tag("&") Seq and(Seq s, El[] elements) { return s.and(seq(elements)); }
555 public static @tag("&~") Seq andnot(Seq s, El[] elements) { return s.andnot(seq(elements)); }
556 public static @tag("->") Seq arrow(Seq s, El e) { return s.follow(e); }
557 public static @tag("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); }
558 public static @tag("/") Seq slash(Seq s, El e) { return s.separate(e); }
560 public static @tag("ps") Seq seq(El[] elements) { return new Seq(elements); }
561 public static @tag Seq psx(Seq s) { return s; }
562 public static @tag(":") El colon(String s, El e) { return new Label(s, e); }
563 public static @tag(")") void close(String foo) { throw new Error("not supported"); }
564 public static @tag("()") El epsilon() { return new Constant(Union.epsilon); }
566 public static @tag("nonTerminal") class NonTerminalReference extends El {
567 public @arg String nonTerminal;
568 public Element build(Context cx) { return cx.get(nonTerminal); }
570 public static class StringLiteral extends Constant {
571 public @tag("literal") StringLiteral(String string) { super(CharRange.string(string)); }
573 public static class CharClass extends El {
575 public @tag("[") CharClass(Range[] ranges) { this.ranges = ranges; }
576 public Element build(Context cx) {
577 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
578 for(Range r : ranges) set.add(r.first, r.last);
579 return CharRange.set(set);
583 public static @tag("{") class Tree extends El {
584 public @arg Seq body;
585 public Element build(Context cx) {
590 public static class Rep extends El {
592 public boolean zero, many, max;
593 public Rep(El e, El sep, boolean zero, boolean many, boolean max) {
594 this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
595 public Element build(Context cx) {
597 ? Sequence.repeat(e.build(cx), zero, many, sep==null ? null : sep.build(cx), repeatTag())
599 ? Sequence.repeatMaximal(MetaGrammar.infer(e.build(cx)), zero, many, repeatTag())
600 : Sequence.repeatMaximal(e.build(cx), zero, many, MetaGrammar.infer(sep.build(cx)), repeatTag());
603 public static class Constant extends El {
605 public Constant(Element constant) { this.constant = constant; }
606 public Element build(Context cx) { return constant; }
608 public abstract static class PostProcess extends El {
610 public PostProcess(El e) { this.e = e; }
611 public Element build(Context cx) { return postProcess(e.build(cx)); }
612 public abstract Element postProcess(Element e);
615 // FIXME: it would be nice if we could hoist this into "Rep"
616 public static @tag("++") El plusmax(final El e) { return new Rep(e, null, false, true, true); }
617 public static @tag("+") El plus(final El e) { return new Rep(e, null, false, true, false); }
618 public static @tag("++/") El plusmaxfollow(final El e, final El sep) { return new Rep(e, sep, false, true, true); }
619 public static @tag("+/") El plusfollow(final El e, final El sep) { return new Rep(e, sep, false, true, false); }
620 public static @tag("**") El starmax(final El e) { return new Rep(e, null, true, true, true); }
621 public static @tag("*") El star(final El e) { return new Rep(e, null, true, true, false); }
622 public static @tag("**/") El starmaxfollow(final El e, final El sep) { return new Rep(e, sep, true, true, true); }
623 public static @tag("*/") El starfollow(final El e, final El sep) { return new Rep(e, sep, true, true, false); }
624 public static @tag("?") El question(final El e) { return new Rep(e, null, true, true, false); }
626 public static @tag("!") El bang(final El e) { return new Drop(e); }
628 public static @tag("^") El caret(final String s) {
629 return new Constant(CharRange.string(s)) {
630 public String getOwnerTag() { return s; }
634 public static @tag("~") El tilde(final El e) {
635 return new PostProcess(e) {
636 public Element postProcess(Element e) {
637 return MetaGrammar.infer((Topology<Character>)Atom.toAtom(e).complement());
640 public static @tag("^^") void doublecaret(final El e) { throw new Error("not implemented"); }
642 public static @tag("(") El subexpression(Seq[][] rhs) { return new NonTerminal(rhs); }
644 public static @nonterminal("Word") String word(String s) { return s; }
645 public static @nonterminal("Quoted") String quoted(String s) { return s; }
646 public static @nonterminal("escaped") String c(char c) { return c+""; }
647 public static @tag("\"\"") String emptystring() { return ""; }
648 public static @tag("\n") String retur() { return "\n"; }
649 public static @tag("\r") String lf() { return "\r"; }
652 public static class Context {
653 HashMap<String,Union> map = new HashMap<String,Union>();
654 public Union peek(String name) { return map.get(name); }
655 public void put(String name, Union u) { map.put(name, u); }
656 public Union get(String name) {
657 Union ret = map.get(name);
658 if (ret==null) map.put(name, ret = new Union(name));
663 public static Object repeatTag() { return null; }