X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FDemo.java;h=f163d93658d6e8434ff67a7571d046bda1b0657c;hp=92726187d3b8f47c9d6c45a13c0373eb54534c0c;hb=74e53cbf7227f5958ff7495abe31cb0462d62c4e;hpb=a96edcbb9051f33f65256dcd5adcbae6925956eb diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java index 9272618..f163d93 100644 --- a/src/edu/berkeley/sbp/misc/Demo.java +++ b/src/edu/berkeley/sbp/misc/Demo.java @@ -3,17 +3,482 @@ import edu.berkeley.sbp.util.*; import edu.berkeley.sbp.*; import edu.berkeley.sbp.chr.*; import java.util.*; +import java.lang.annotation.*; +import java.lang.reflect.*; import java.io.*; public class Demo { public static void main(String[] s) throws Exception { - Tree gram = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1(); - MetaGrammar g = (MetaGrammar)new MetaGrammar().walk(gram); - Union meta = g.done(); - //Tree out = new CharParser(meta).parse(new FileInputStream(s[1])).expand1(); - Forest out = new CharParser(meta).parse(new FileInputStream(s[1])); - GraphViz gv = new GraphViz(); - out.toGraphViz(gv); - gv.dump(new PrintWriter(new OutputStreamWriter(System.out))); + Tree 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 name.lastIndexOf('.')) { + len--; + ofs++; + } + */ + String[] names = new String[len]; + arg[] argtags = new arg[len]; + for(int i=0; i") Seq arrow(Seq s, El e) { return new Seq(); } + + public static @tag("nonTerminal") class NonTerminalReference extends El { public @arg String nonTerminal; } + public static @tag("literal") class StringLiteral extends El { public @arg String string; } + public static @tag("()") class Epsilon extends El { } + public static @tag("{") class Tree extends El { public @arg Seq body; } + public static class CharClass extends El { public @tag("[") CharClass(Range[] ranges) { } } + + public static @tag("++") void plusmax(El e) { } + public static @tag("+") void plus(El e) { } + public static @tag("++/") void plusmaxfollow(El e, El sep) { } + public static @tag("+/") void plusfollow(El e, El sep) { } + public static @tag("**") void starmax(El e) { } + public static @tag("*") void star(El e) { } + public static @tag("**/") void starmaxfollow(El e, El sep) { } + public static @tag("*/") void starfollow(El e, El sep) { } + public static @tag("!") void bang(El e) { } + public static @tag("?") void question(El e) { } + public static @tag("^") void caret(String s) { } + public static @tag("~") void tilde(El e) { } + public static @tag("^^") void doublecaret(El e) { } + public static @tag("(") void subexpression(Seq[][] rhs) { } + + public static @nonterminal("Word") String word(String s) { return null; } + public static @nonterminal("Quoted") String quoted(String s) { return null; } + public static @nonterminal("escaped") String c(char c) { return null; } + public static @tag("\"\"") String emptystring() { return null; } + public static @tag("\r") String lf() { return null; } + public static @tag("\n") String cr() { return null; } + } + }