-
- public static boolean harsh = false;
-
- public static void main(String[] s) throws Exception {
-
- ReflectiveMeta m = new ReflectiveMeta();
- Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
- MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
- MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
-
- Union meta = mgf.get("s").build(bc);
- Tree t = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
-
- Union u = Demo.make(t, "s");
-
- System.err.println();
- System.err.println("== parsing with parsed grammar =================================================================================");
- t = new CharParser((Union)u).parse(new FileInputStream(s[1])).expand1();
- System.out.println(t.toPrettyString());
-
- System.err.println("== parsing with parsed-parsed grammar ==========================================================================");
- t = new CharParser(new Context(t, m).build()).parse(new FileInputStream(s[1])).expand1();
- System.out.println(t.toPrettyString());
- }
-
- public static class ReflectiveMetaPlain extends ReflectiveMeta {
- public Object repeatTag() { return null; }
- public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
- return null; }
- public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
- return Sequence.rewritingSequence(tag, els, labels, drops);
- }
- }
-
- public static class ReflectiveMeta extends MetaGrammar.Meta {
- private final Class _cl;
- private final Class[] _inner;
- public ReflectiveMeta() {
- this(MG.class,
- new Class[] {
- MG.Grammar.class,
- MG.NonTerminal.class,
- MG.AnonUn.class,
- MG.Range.class,
- MG.El.class,
- MG.Seq.class,
- MG.NonTerminalReference.class,
- MG.StringLiteral.class,
- MG.XTree.class,
- MG.CharClass.class
- });
- }
- public ReflectiveMeta(Class c, Class[] inner) {
- this._cl = c;
- this._inner = inner;
- }
- private boolean match(Method m, String s) { return match(m.getAnnotation(tag.class), null, s); }
- private boolean match(tag t, Class c, String s) {
- if (t==null) return false;
- if (t.value().equals(s)) return true;
- if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true;
- return false;
- }
- private boolean match(nonterminal t, Class c, String s) {
- if (t==null) return false;
- if (t.value().equals(s)) return true;
- if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true;
- return false;
- }
- private boolean match(Class c, String s, String nonTerminalName) {
- if (match((tag)c.getAnnotation(tag.class), c, s)) return true;
- if (match((nonterminal)c.getAnnotation(nonterminal.class), c, nonTerminalName)) return true;
- return false;
- }
- public boolean match(Constructor con, String s, String nonTerminalName) {
- Class c = con.getDeclaringClass();
- if (match((tag)con.getAnnotation(tag.class), null, s)) return true;
- if (match((nonterminal)con.getAnnotation(nonterminal.class), c, s)) return true;
- return false;
- }
- public Object repeatTag() {
- return new Tree.ArrayBuildingTreeFunctor<Object>();
- }
- public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
- Production p = new Production(tag, nonTerminalName, els, labels, drops);
- for(Method m : _cl.getMethods())
- if (new TargetMethod(m).isCompatible(p))
- return new TargetMethod(m).makeSequence(p);
- for(Class c : _inner)
- for(Constructor con : c.getConstructors())
- if (new TargetConstructor(con).isCompatible(p))
- return new TargetConstructor(con).makeSequence(p);
- for(Class c : _inner)
- if (new TargetClass(c).isCompatible(p))
- return new TargetClass(c).makeSequence(p);
- return null;
- }
- public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
- Sequence ret = tryResolveTag(tag, nonTerminalName, els, labels, drops);
- if (ret != null) return ret;
- String message = "could not find a Java method/class/ctor matching tag \""+tag+
- "\", nonterminal \""+nonTerminalName+"\" with " + els.length + " arguments";
- if (harsh) {
- throw new RuntimeException(message);
- } else {
- System.err.println(message);
- return Sequence.rewritingSequence(tag, els, labels, drops);
- }
- }
- }
-
-
- /**
- * Constructors, classes, and methods with this attribute will
- * match every production of the nonterminal called "value()"
- * that is arg-compatible. If value() is undefined, then the
- * class/constructor/method name is used.
- */
- @Retention(RetentionPolicy.RUNTIME) public static @interface nonterminal { String value() default ""; }
-
- @Retention(RetentionPolicy.RUNTIME) public static @interface raw { }
-
- /**
- * Constructors, classes, and methods with this attribute will
- * match every tree tagged with "value()" that is arg-compatible.
- * If value() is undefined, then the class/constructor/method
- * name is used.
- */
- @Retention(RetentionPolicy.RUNTIME) public static @interface tag { String value() default ""; }
-
- /**
- * If any parameter to a method or field in a class has a named
- * arg-tag, that parameter/field matches the child of the tree
- * which either has that label or else is a reference to a
- * nonterminal with the corresponding name.
- *
- * The remaining non-named arg-tags match the remaining children
- * of the tree in sequential order.
- *
- * If any arg-tagged parameters/fields remain, the match fails.
- * If there were no arg-tagged parameters-fields, it is as if all
- * of them were non-named and arg-tagged.
- *
- * A method/constructor is arg-compatible if all of its arguments
- * are arg-compatible.
- *
- * A class is arg-compatible if all of its fields are
- * arg-compatible, or if one of its constructors is arg-compatible.
- *
- */
- @Retention(RetentionPolicy.RUNTIME) public static @interface arg { String value() default ""; }
-
- public static class Production {
- public String tag;
- public String nonTerminal;
- public Object[] labels;
- public boolean[] drops;
- public Element[] elements;
- public int count = 0;
- public Production(String tag, String nonTerminal, Element[] elements, Object[] labels, boolean[] drops) {
- this.tag = tag;
- this.elements = elements;
- this.nonTerminal = nonTerminal;
- this.labels = labels;
- this.drops = drops;
- for(int i=0; i<drops.length; i++)
- if (!drops[i])
- count++;
- }
- }
-
- public static abstract class Target {
-
- private Reflection.Bindable _bindable;
- public Target(Reflection.Bindable b) { this._bindable = b; }
-
- public String getName() { return _bindable.getSimpleName(); }
- public tag getTag() { return (tag)_bindable.getAnnotation(tag.class); }
- public nonterminal getNonTerminal() { return (nonterminal)_bindable.getAnnotation(nonterminal.class); }
- public String toString() { return _bindable.getSimpleName(); }
- public Object plant(Object[] fields) { return _bindable.impose(fields); }
- public boolean isRaw() { return _bindable.isAnnotationPresent(raw.class); }
- public Object invokeRaw(Iterable<Tree<Object>> t) { _bindable.impose(new Object[] { t }); }
-
- public boolean isCompatible(Production p) {
- tag t = getTag();
- if (t != null &&
- (t.value().equals(p.tag) ||
- (t.value().equals("") && getName().equals(p.tag))))
- return buildSequence(p)!=null;
-
- nonterminal n = getNonTerminal();
- if (n != null &&
- (n.value().equals(p.nonTerminal) ||
- (n.value().equals("") && getName().equals(p.nonTerminal))))
- return buildSequence(p)!=null;
-
- return false;
- }
-
- public int[] buildSequence(Production p, String[] names, arg[] argtags) {
- int argTagged = 0;
- for(int i=0; i<argtags.length; i++)
- if (argtags[i] != null)
- argTagged++;
-
- // FIXME: can be smarter here
- if (names.length==p.count) {
- int[] ret = new int[p.count];
- for(int i=0; i<p.count; i++) ret[i] = i;
- return ret;
- } else if (argTagged==p.count) {
- int[] ret = new int[argtags.length];
- int j = 0;
- for(int i=0; i<argtags.length; i++)
- ret[i] = argtags[i]==null ? -1 : (j++);
- return ret;
- } else {
- return null;
- }
- }
- public Sequence makeSequence(Production p) {
- return Sequence.rewritingSequence(new TargetReducer(p, buildSequence(p), "reducer-"+this),
- p.elements, p.labels, p.drops);
- }