X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FDemo.java;h=c567eddaadf5c34af41b77f8ab969b17b54294f8;hp=845b1cbd49a093330d86e6448d2577e54dfcb5aa;hb=bb4357ee026741cacecdfbfdcdd2d44699306536;hpb=03f91bd299c8c8724fe966f527b7410d2cea675d diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java index 845b1cb..c567edd 100644 --- a/src/edu/berkeley/sbp/misc/Demo.java +++ b/src/edu/berkeley/sbp/misc/Demo.java @@ -8,27 +8,16 @@ import java.lang.reflect.*; import java.io.*; public class Demo { + + public static boolean harsh = false; + public static void main(String[] s) throws Exception { + + ReflectiveMeta m = new ReflectiveMeta(); Tree res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1(); - - ReflectiveMeta m = - new ReflectiveMeta(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 - }); 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()); @@ -60,6 +49,21 @@ public class Demo { 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; @@ -108,7 +112,7 @@ public class Demo { } }; } - public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) { + 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)) @@ -120,20 +124,21 @@ public class Demo { 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+"\" with " + els.length + " arguments"); + 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); + } } } - /* - 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