- public static void main(String[] s) throws Exception {
- Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
-
- MetaGrammar.Meta m =
- new ReflectiveMeta(MG.class,
- new Class[] {
- MG.Grammar.class,
- MG.NonTerminal.class,
- MG.Range.class,
- MG.El.class,
- MG.Seq.class,
- MG.NonTerminalReference.class,
- MG.StringLiteral.class,
- MG.Epsilon.class,
- MG.Tree.class,
- MG.CharClass.class
- });
- MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
- MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
- Union meta = mgf.get("s").build(bc);
-
- System.err.println("parsing " + s[1]);
- Tree t = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
-
- System.out.println(t.toPrettyString());
- Reducer red = (Reducer)t.head();
- System.out.println(red.reduce(t));
- }
-
- public static class ReflectiveMeta extends MetaGrammar.Meta {
- private final Class _cl;
- private final Class[] _inner;
- 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 Reducer() {
- public String toString() { return "[**]"; }
- public Object reduce(Tree t) {
- Object[] ret = new Object[t.numChildren()];
- for(int i=0; i<t.numChildren(); i++) {
- Tree tc = t.child(i);
- if (tc.head() != null && tc.head() instanceof Reducer)
- ret[i] = ((Reducer)tc.head()).reduce(tc);
- else if (tc.numChildren() == 0)
- ret[i] = tc.head();
- else {
- System.err.println("FIXME: don't know what to do about " + tc);
- ret[i] = null;
- }
- }
- return ret;
- }
- };
- }
- public Sequence resolveTag(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);
- throw new RuntimeException("could not find a Java method/class/ctor matching tag \""+tag+"\", nonterminal \""+nonTerminalName+"\"");
- }
- }
- /*
- public static Object makeFlattener(final Method m, final Element[] els, final Object[] labels, final boolean[] drops) {
- return new Reducer() {
- public Object reduce(Tree t) {
- Object[] o = new Object[m.getParameterTypes()];
- int j = 0;
- for(int i=0; i<els.length; i++)
- }
- };
- }
- */
-
-
- /**
- * 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 ""; }
-
- /**
- * 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; // FIXME: use this
- this.nonTerminal = nonTerminal;
- this.labels = labels;
- this.drops = drops;
- for(int i=0; i<drops.length; i++)
- if (!drops[i])
- count++;
- }
- }