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,
23 MG.NonTerminalReference.class,
24 MG.StringLiteral.class,
28 MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
29 MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
30 Union meta = mgf.get("s").build(bc);
32 System.err.println("parsing " + s[1]);
33 Tree t = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
34 System.out.println("tree:\n" + t.toPrettyString());
36 Reducer red = (Reducer)t.head();
37 MG.Grammar g = (MG.Grammar)red.reduce(t);
38 System.out.println(g);
40 Context cx = new Context(g,m);
42 for(MG.NonTerminal nt : g.nonterminals) {
43 Union el = (Union)cx.get(nt.name);
44 StringBuffer st = new StringBuffer();
46 System.err.println(st);
47 if (nt.name.equals("s")) u = el;
51 System.err.println("== parsing with parsed grammar =================================================================================");
52 t = new CharParser((Union)u).parse(new FileInputStream(s[1])).expand1();
53 System.out.println(t.toPrettyString());
55 System.err.println("== parsing with parsed-parsed grammar ==========================================================================");
56 t = new CharParser(new Context(t, m).build()).parse(new FileInputStream(s[1])).expand1();
57 System.out.println(t.toPrettyString());
60 public static class ReflectiveMeta extends MetaGrammar.Meta {
61 private final Class _cl;
62 private final Class[] _inner;
63 public ReflectiveMeta(Class c, Class[] inner) {
67 private boolean match(Method m, String s) { return match(m.getAnnotation(tag.class), null, s); }
68 private boolean match(tag t, Class c, String s) {
69 if (t==null) return false;
70 if (t.value().equals(s)) return true;
71 if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true;
74 private boolean match(nonterminal t, Class c, String s) {
75 if (t==null) return false;
76 if (t.value().equals(s)) return true;
77 if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true;
80 private boolean match(Class c, String s, String nonTerminalName) {
81 if (match((tag)c.getAnnotation(tag.class), c, s)) return true;
82 if (match((nonterminal)c.getAnnotation(nonterminal.class), c, nonTerminalName)) return true;
85 public boolean match(Constructor con, String s, String nonTerminalName) {
86 Class c = con.getDeclaringClass();
87 if (match((tag)con.getAnnotation(tag.class), null, s)) return true;
88 if (match((nonterminal)con.getAnnotation(nonterminal.class), c, s)) return true;
91 public Object repeatTag() {
92 return new Reducer() {
93 public String toString() { return "[**]"; }
94 public Object reduce(Tree t) {
95 Object[] ret = new Object[t.numChildren()];
96 for(int i=0; i<t.numChildren(); i++) {
98 if (tc.head() != null && tc.head() instanceof Reducer)
99 ret[i] = ((Reducer)tc.head()).reduce(tc);
100 else if (tc.numChildren() == 0)
103 System.err.println("FIXME: don't know what to do about " + tc);
111 public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
112 Production p = new Production(tag, nonTerminalName, els, labels, drops);
113 for(Method m : _cl.getMethods())
114 if (new TargetMethod(m).isCompatible(p))
115 return new TargetMethod(m).makeSequence(p);
116 for(Class c : _inner)
117 for(Constructor con : c.getConstructors())
118 if (new TargetConstructor(con).isCompatible(p))
119 return new TargetConstructor(con).makeSequence(p);
120 for(Class c : _inner)
121 if (new TargetClass(c).isCompatible(p))
122 return new TargetClass(c).makeSequence(p);
123 throw new RuntimeException("could not find a Java method/class/ctor matching tag \""+tag+"\", nonterminal \""+nonTerminalName+"\" with " + els.length + " arguments");
127 public static Object makeFlattener(final Method m, final Element[] els, final Object[] labels, final boolean[] drops) {
128 return new Reducer() {
129 public Object reduce(Tree t) {
130 Object[] o = new Object[m.getParameterTypes()];
132 for(int i=0; i<els.length; i++)
140 * Constructors, classes, and methods with this attribute will
141 * match every production of the nonterminal called "value()"
142 * that is arg-compatible. If value() is undefined, then the
143 * class/constructor/method name is used.
145 @Retention(RetentionPolicy.RUNTIME) public static @interface nonterminal { String value() default ""; }
148 * Constructors, classes, and methods with this attribute will
149 * match every tree tagged with "value()" that is arg-compatible.
150 * If value() is undefined, then the class/constructor/method
153 @Retention(RetentionPolicy.RUNTIME) public static @interface tag { String value() default ""; }
156 * If any parameter to a method or field in a class has a named
157 * arg-tag, that parameter/field matches the child of the tree
158 * which either has that label or else is a reference to a
159 * nonterminal with the corresponding name.
161 * The remaining non-named arg-tags match the remaining children
162 * of the tree in sequential order.
164 * If any arg-tagged parameters/fields remain, the match fails.
165 * If there were no arg-tagged parameters-fields, it is as if all
166 * of them were non-named and arg-tagged.
168 * A method/constructor is arg-compatible if all of its arguments
169 * are arg-compatible.
171 * A class is arg-compatible if all of its fields are
172 * arg-compatible, or if one of its constructors is arg-compatible.
175 @Retention(RetentionPolicy.RUNTIME) public static @interface arg { String value() default ""; }
177 public static class Production {
179 public String nonTerminal;
180 public Object[] labels;
181 public boolean[] drops;
182 public Element[] elements;
183 public int count = 0;
184 public Production(String tag, String nonTerminal, Element[] elements, Object[] labels, boolean[] drops) {
186 this.elements = elements;
187 this.nonTerminal = nonTerminal;
188 this.labels = labels;
190 for(int i=0; i<drops.length; i++)
196 public static abstract class Target {
197 public abstract String getName();
198 public abstract tag getTag();
199 public abstract nonterminal getNonTerminal();
200 public abstract int[] buildSequence(Production p);
201 public boolean isCompatible(Production p) {
204 (t.value().equals(p.tag) ||
205 (t.value().equals("") && p.tag.equals(getName()))))
206 return buildSequence(p)!=null;
208 nonterminal n = getNonTerminal();
210 (n.value().equals(p.nonTerminal) ||
211 (n.value().equals("") && p.nonTerminal.equals(getName()))))
212 return buildSequence(p)!=null;
216 public int[] buildSequence(Production p, String[] names, arg[] argtags) {
218 for(int i=0; i<argtags.length; i++)
219 if (argtags[i] != null)
222 // FIXME: can be smarter here
223 if (names.length==p.count) {
224 int[] ret = new int[p.count];
225 for(int i=0; i<p.count; i++) ret[i] = i;
227 } else if (argTagged==p.count) {
228 int[] ret = new int[argtags.length];
230 for(int i=0; i<argtags.length; i++)
231 ret[i] = argtags[i]==null ? -1 : (j++);
237 public Sequence makeSequence(Production p) {
238 return Sequence.rewritingSequence(new TargetReducer(p), p.elements, p.labels, p.drops);
240 public abstract Object plant(Object[] fields, int[] map);
241 public class TargetReducer implements Reducer {
242 private Production p;
244 public TargetReducer(Production p) {
246 this.map = buildSequence(p);
248 public String toString() { return "reducer-"+Target.this; }
249 public Object reduce(Tree t) {
250 Object[] objects = new Object[t.numChildren()];
251 for(int i=0; i<t.numChildren(); i++) {
252 Tree tc = t.child(i);
253 if (tc.head() != null && tc.head() instanceof Reducer)
254 objects[i] = ((Reducer)tc.head()).reduce(tc);
255 else if (tc.numChildren() == 0)
256 objects[i] = tc.head();
258 System.err.println("FIXME: don't know what to do about " + tc);
262 System.err.println("input tree: " + t);
263 return plant(objects, map);
268 public static interface Reducer {
269 public Object reduce(Tree t);
272 public static class TargetClass extends Target {
273 public final Class _class;
274 public TargetClass(Class _class) { this._class = _class; }
275 public String getName() { return _class.getSimpleName(); }
276 public tag getTag() { return (tag)_class.getAnnotation(tag.class); }
277 public nonterminal getNonTerminal() { return (nonterminal)_class.getAnnotation(nonterminal.class); }
278 public String toString() { return _class.getSimpleName(); }
279 public int[] buildSequence(Production p) {
280 Field[] f = _class.getDeclaredFields();
281 String[] names = new String[f.length];
282 arg[] argtags = new arg[f.length];
283 for(int i=0; i<f.length; i++) {
284 names[i] = f[i].getName();
285 argtags[i] = f[i].getAnnotation(arg.class);
287 int[] ret = buildSequence(p, names, argtags);
288 if (ret!=null) return ret;
289 for(Constructor c : _class.getConstructors())
290 if (new TargetConstructor(c).buildSequence(p)!=null)
291 return new TargetConstructor(c).buildSequence(p);
294 public Object plant(Object[] fields, int[] map) {
296 Object ret = _class.newInstance();
297 Field[] f = _class.getFields();
299 for(int i=0; i<f.length; i++)
301 Object tgt = Reflection.lub(fields[map[i]]);
302 if (f[i].getType() == String.class) tgt = stringify(tgt);
304 tgt = coerce(tgt, f[i].getType());
305 System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
309 } catch (Exception e) {
311 throw new RuntimeException(e);
316 public static String stringify(Object o) {
317 if (o==null) return "";
318 if (!(o instanceof Object[])) return o.toString();
319 Object[] arr = (Object[])o;
320 StringBuffer ret = new StringBuffer();
321 for(int i=0; i<arr.length; i++)
323 return ret.toString();
326 public static class TargetConstructor extends Target {
327 public final Constructor _ctor;
328 public TargetConstructor(Constructor _ctor) { this._ctor = _ctor; }
329 public String getName() { return _ctor.getName(); }
330 public tag getTag() { return (tag)_ctor.getAnnotation(tag.class); }
331 public nonterminal getNonTerminal() { return (nonterminal)_ctor.getAnnotation(nonterminal.class); }
332 public String toString() { return _ctor.getName(); }
333 public int[] buildSequence(Production p) {
334 Annotation[][] annotations = _ctor.getParameterAnnotations();
335 int len = annotations.length;
337 String name = _ctor.getDeclaringClass().getName();
339 if (name.indexOf('$') > name.lastIndexOf('.')) {
344 String[] names = new String[len];
345 arg[] argtags = new arg[len];
346 for(int i=0; i<names.length; i++)
347 for(Annotation a : annotations[i+ofs])
348 if (a instanceof arg)
349 argtags[i+ofs] = (arg)a;
350 return buildSequence(p, names, argtags);
352 public Object plant(Object[] fields, int[] map) {
354 Class[] argTypes = _ctor.getParameterTypes();
355 Object[] args = new Object[argTypes.length];
357 for(int i=0; i<args.length; i++)
359 Object tgt = Reflection.lub(fields[map[i]]);
360 if (argTypes[i] == String.class) tgt = stringify(tgt);
362 tgt = coerce(tgt, argTypes[i]);
363 System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
366 return _ctor.newInstance(args);
367 } catch (Exception e) {
368 throw new RuntimeException(e);
372 public static class TargetMethod extends Target {
373 public final Method _method;
374 public TargetMethod(Method _method) { this._method = _method; }
375 public String getName() { return _method.getName(); }
376 public String toString() { return _method.getName(); }
377 public tag getTag() { return (tag)_method.getAnnotation(tag.class); }
378 public nonterminal getNonTerminal() { return (nonterminal)_method.getAnnotation(nonterminal.class); }
379 public int[] buildSequence(Production p) {
380 Annotation[][] annotations = _method.getParameterAnnotations();
381 String[] names = new String[annotations.length];
382 arg[] argtags = new arg[annotations.length];
383 for(int i=0; i<names.length; i++)
384 for(Annotation a : annotations[i])
385 if (a instanceof arg)
387 int[] ret = buildSequence(p, names, argtags);
390 public Object plant(Object[] fields, int[] map) {
392 Class[] argTypes = _method.getParameterTypes();
393 Object[] args = new Object[argTypes.length];
395 for(int i=0; i<args.length; i++)
397 Object tgt = Reflection.lub(fields[map[i]]);
398 if (argTypes[i] == String.class) tgt = stringify(tgt);
400 tgt = coerce(tgt, argTypes[i]);
401 System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
404 System.err.println("invoking " + _method + " with " + Reflection.show(args));
405 return _method.invoke(null, args);
406 } catch (Exception e) {
407 throw new RuntimeException(e);
412 public static Object coerce(Object o, Class c) {
413 if (o==null) return null;
414 if (c.isInstance(o)) return o;
415 if (c == char.class) {
416 return o.toString().charAt(0);
418 if (c.isArray() && (c.getComponentType().isInstance(o))) {
419 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
423 if (o.getClass().isArray() && c.isArray()) {
425 for(int i=0; i<((Object[])o).length; i++) {
426 Object ob = (((Object[])o)[i]);
428 System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
433 System.err.println("hit with " + c.getComponentType().getName());
434 return Array.newInstance(c.getComponentType(), ((Object[])o).length);
440 public static class MG {
441 public static @tag("grammar") class Grammar {
442 public NonTerminal get(String s) {
443 for(NonTerminal nt : nonterminals)
444 if (nt.name.equals(s))
448 public @arg("NonTerminal") NonTerminal[] nonterminals;
449 public String toString() {
451 for(NonTerminal nt : nonterminals) ret += nt + ", ";
455 public abstract static class Un extends El {
456 public Seq[][] sequences;
457 public void build(Context cx, Union u) {
458 HashSet<Sequence> bad2 = new HashSet<Sequence>();
459 for(int i=0; i<sequences.length; i++) {
460 Seq[] group = sequences[i];
461 Union u2 = new Union();
462 if (sequences.length==1) u2 = u;
463 for(int j=0; j<group.length; j++) {
464 group[j].build(cx, u2);
466 if (sequences.length==1) break;
467 Sequence seq = Sequence.singleton(u2);
468 for(Sequence s : bad2) {
473 bad2.add(Sequence.singleton(u2));
477 public static class NonTerminal extends Un {
478 public String name = null;
479 public @nonterminal("NonTerminal") NonTerminal(@arg("Word") String name,
480 @arg("RHS") Seq[][] sequences) {
482 this.sequences = sequences;
484 public Element build(Context cx) { return cx.get(name); }
487 public static class AnonUn extends Un {
488 public @tag("(") AnonUn(Seq[][] sequences) {
489 this.sequences = sequences;
491 public Element build(Context cx) {
492 Union ret = new Union();
498 //public static @tag void range(char c) { }
499 public static class Range {
500 public @tag("range") Range(char only) { first = only; last = only; }
501 public @tag("-") Range(char first, char last) { this.first = first; this.last = last; }
505 public static abstract class El {
506 public String getLabel() { return null; }
507 public String getOwnerTag() { return null; }
508 public boolean drop() { return false; }
509 public abstract Element build(Context cx);
511 public static class Drop extends El {
513 public Drop(El e) { this.e = e; }
514 public String getLabel() { return null; }
515 public boolean drop() { return true; }
516 public String getOwnerTag() { return e.getOwnerTag(); }
517 public Element build(Context cx) { return e.build(cx); }
519 public static class Label extends El {
522 public Label(String label, El e) { this.e = e; this.label = label; }
523 public String getLabel() { return label; }
524 public String getOwnerTag() { return e.getOwnerTag(); }
525 public Element build(Context cx) { return e.build(cx); }
527 public static /*abstract*/ class Seq {
528 HashSet<Seq> and = new HashSet<Seq>();
529 HashSet<Seq> not = new HashSet<Seq>();
534 public Seq(El e) { this(new El[] { e }); }
535 public Seq(El[] elements) { this.elements = elements; }
536 public Seq tag(String tag) { this.tag = tag; return this; }
537 public Seq follow(El follow) { this.follow = follow; return this; }
539 Seq ret = new Seq(elements);
546 public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
547 public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
548 public Seq separate(El sep) {
549 El[] elements = new El[this.elements.length * 2 - 1];
550 for(int i=0; i<this.elements.length; i++) {
551 elements[i*2] = this.elements[i];
552 if (i<this.elements.length-1)
553 elements[i*2+1] = new Drop(sep);
555 this.elements = elements;
558 public Sequence build(Context cx, Union u) {
559 Sequence ret = build0(cx);
560 for(Seq s : and) { Sequence dork = s.build(cx, u); dork.lame = true; ret = ret.and(dork); }
561 for(Seq s : not) { Sequence dork = s.build(cx, u); dork.lame = true; ret = ret.not(dork); }
565 public Sequence build0(Context cx) {
566 boolean unwrap = false;
567 boolean dropAll = false;
568 if (tag!=null && tag.equals("[]")) unwrap = true;
569 if (tag!=null && "()".equals(tag)) dropAll = true;
570 Object[] labels = new Object[elements.length];
571 boolean[] drops = new boolean[elements.length];
572 Element[] els = new Element[elements.length];
573 for(int i=0; i<elements.length; i++) {
574 labels[i] = elements[i].getLabel();
575 drops[i] = elements[i].drop();
576 els[i] = elements[i].build(cx);
577 if (elements[i].getOwnerTag() != null)
578 tag = elements[i].getOwnerTag();
581 if (dropAll) ret = Sequence.drop(els, false);
582 else if (unwrap) ret = Sequence.unwrap(els, cx.rm.repeatTag(), drops);
583 else if (tag!=null) {
584 ret = cx.rm.resolveTag(tag, cx.cnt, els, labels, drops);
587 for(int i=0; i<els.length; i++)
589 if (idx==-1) idx = i;
590 else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
591 if (idx != -1) ret = Sequence.singleton(els, idx);
592 else ret = Sequence.drop(els, false);
594 if (this.follow != null)
595 ret.follow = MetaGrammar.infer(this.follow.build(cx));
596 ret.lame = this.lame;
600 public static @tag("&") Seq and(Seq s, El[] elements) { return s.and(seq(elements)); }
601 public static @tag("&~") Seq andnot(Seq s, El[] elements) { return s.andnot(seq(elements)); }
602 public static @tag("->") Seq arrow(Seq s, El e) { return s.follow(e); }
603 public static @tag("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); }
604 public static @tag("/") Seq slash(Seq s, El e) { return s.separate(e); }
606 public static @tag("ps") Seq seq(El[] elements) { return new Seq(elements); }
607 public static @tag Seq psx(Seq s) { return s; }
608 public static @tag(":") El colon(String s, El e) { return new Label(s, e); }
609 public static @tag(")") void close(String foo) { throw new Error("not supported"); }
610 public static @tag("()") El epsilon() { return new Constant(Union.epsilon); }
612 public static @tag("nonTerminal") class NonTerminalReference extends El {
613 public @arg String nonTerminal;
614 public Element build(Context cx) {
615 return cx.get(nonTerminal);
618 public static class StringLiteral extends Constant {
619 public @tag("literal") StringLiteral(String string) { super(CharRange.string(string)); }
620 public boolean drop() { return true; }
622 public static class CharClass extends El {
624 public @tag("[") CharClass(Range[] ranges) { this.ranges = ranges; }
625 public Element build(Context cx) {
626 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
627 for(Range r : ranges)
628 set.add(r.first, r.last);
629 return CharRange.set(set);
633 public static @tag("{") class Tree extends El {
634 public @arg Seq body;
635 public Element build(Context cx) {
640 public static class Rep extends El {
642 public boolean zero, many, max;
643 public Rep(El e, El sep, boolean zero, boolean many, boolean max) {
644 this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
645 public Element build(Context cx) {
647 ? Sequence.repeat(e.build(cx), zero, many, sep==null ? null : sep.build(cx), cx.rm.repeatTag())
649 ? Sequence.repeatMaximal(MetaGrammar.infer(e.build(cx)), zero, many, cx.rm.repeatTag())
650 : Sequence.repeatMaximal(e.build(cx), zero, many, MetaGrammar.infer(sep.build(cx)), cx.rm.repeatTag());
653 public static class Constant extends El {
655 public Constant(Element constant) { this.constant = constant; }
656 public Element build(Context cx) { return constant; }
658 public abstract static class PostProcess extends El {
660 public PostProcess(El e) { this.e = e; }
661 public Element build(Context cx) { return postProcess(e.build(cx)); }
662 public abstract Element postProcess(Element e);
665 // FIXME: it would be nice if we could hoist this into "Rep"
666 public static @tag("++") El plusmax(final El e) { return new Rep(e, null, false, true, true); }
667 public static @tag("+") El plus(final El e) { return new Rep(e, null, false, true, false); }
668 public static @tag("++/") El plusmaxfollow(final El e, final El sep) { return new Rep(e, sep, false, true, true); }
669 public static @tag("+/") El plusfollow(final El e, final El sep) { return new Rep(e, sep, false, true, false); }
670 public static @tag("**") El starmax(final El e) { return new Rep(e, null, true, true, true); }
671 public static @tag("*") El star(final El e) { return new Rep(e, null, true, true, false); }
672 public static @tag("**/") El starmaxfollow(final El e, final El sep) { return new Rep(e, sep, true, true, true); }
673 public static @tag("*/") El starfollow(final El e, final El sep) { return new Rep(e, sep, true, true, false); }
674 public static @tag("?") El question(final El e) { return new Rep(e, null, true, true, false); }
676 public static @tag("!") El bang(final El e) { return new Drop(e); }
678 public static @tag("^") El caret(final String s) {
679 return new Drop(new Constant(CharRange.string(s)) {
680 public String getOwnerTag() { return s; }
684 public static @tag("~") El tilde(final El e) {
685 return new PostProcess(e) {
686 public Element postProcess(Element e) {
687 return MetaGrammar.infer((Topology<Character>)Atom.toAtom(e).complement());
690 public static @tag("^^") void doublecaret(final El e) { throw new Error("not implemented"); }
692 //public static @tag("(") El subexpression(Seq[][] rhs) { return new NonTerminal(rhs); }
694 public static @nonterminal("Word") String word(String s) { return s; }
695 public static @nonterminal("Quoted") String quoted(String s) { return s; }
696 public static @nonterminal("escaped") String c(char c) { return c+""; }
697 public static @tag("\"\"") String emptystring() { return ""; }
698 public static @tag("\n") String retur() { return "\n"; }
699 public static @tag("\r") String lf() { return "\r"; }
702 public static class Context {
703 HashMap<String,Union> map = new HashMap<String,Union>();
704 private MG.Grammar grammar;
705 public String cnt = null;
706 private ReflectiveMeta rm;
707 public Context(MG.Grammar g, ReflectiveMeta rm) {
711 public Union build() {
713 for(MG.NonTerminal nt : grammar.nonterminals) {
714 Union u = get(nt.name);
715 if ("s".equals(nt.name))
720 public Context(Tree t, ReflectiveMeta rm) {
722 Reducer red = (Reducer)t.head();
723 this.grammar = (MG.Grammar)red.reduce(t);
725 public Union peek(String name) { return map.get(name); }
726 public void put(String name, Union u) { map.put(name, u); }
727 public Union get(String name) {
728 Union ret = map.get(name);
729 if (ret != null) return ret;
730 ret = new Union(name);
732 MG.NonTerminal nt = grammar.get(name);
734 System.err.println("*** warning could not find " + name);