- 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]);
- res = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
-
- System.out.println(res);
- }
-
- 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 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 ""; }