-
- 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);
- System.err.println("parsing " + s[1]);
- Tree t = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
- System.out.println("tree:\n" + t.toPrettyString());
-
- Reducer red = (Reducer)t.head();
- MG.Grammar g = (MG.Grammar)red.reduce(t);
- System.out.println(g);
-
- Context cx = new Context(g,m);
- Element u = null;
- for(MG.NonTerminal nt : g.nonterminals) {
- Union el = (Union)cx.get(nt.name);
- StringBuffer st = new StringBuffer();
- el.toString(st);
- System.err.println(st);
- if (nt.name.equals("s")) u = el;
- }
- 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 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.Tree.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 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 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 ""; }
-
- /**
- * 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 ""; }