X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FDemo.java;h=c9c18f39b818087f1312edc5b7792873c4b9274b;hp=2b99ffbe3bf33c5c1ffee506e08aaddc8d42cf04;hb=683552e3855cac7417a6f456867a0c1dd96502a4;hpb=03e1f20f869819893205ec771571b2c62ab00421 diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java index 2b99ffb..c9c18f3 100644 --- a/src/edu/berkeley/sbp/misc/Demo.java +++ b/src/edu/berkeley/sbp/misc/Demo.java @@ -49,11 +49,11 @@ public class Demo { this(MG.class, new Class[] { MG.Grammar.class, - MG.NonTerminal.class, MG.AnonUn.class, MG.Range.class, MG.El.class, MG.Seq.class, + MG.NonTerminal.class, MG.NonTerminalReference.class, MG.StringLiteral.class, MG.XTree.class, @@ -71,41 +71,27 @@ public class Demo { 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; + //if (match((nonterminal)c.getAnnotation(tag.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; + //if (match((nonterminal)con.getAnnotation(tag.class), c, s)) return true; return false; } public Object repeatTag() { - return new Reducer() { - public String toString() { return ""; } - public Object reduce(Iterable t) { - ArrayList ret = new ArrayList(); - for(Tree tc : t) { - if (tc.head() != null && tc.head() instanceof Reducer) - ret.add(((Reducer)tc.head()).reduce(tc.children())); - else if (tc.numChildren() == 0) - ret.add(tc.head()); - else { - System.err.println("FIXME: don't know what to do about " + tc); - ret.add(null); - } - } - return ret.toArray(new Object[0]); - } - }; + return new Tree.ArrayBuildingTreeFunctor(); } public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) { Production p = new Production(tag, nonTerminalName, els, labels, drops); @@ -142,7 +128,9 @@ public class Demo { * 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 ""; } + */ @Retention(RetentionPolicy.RUNTIME) public static @interface raw { } @@ -196,10 +184,18 @@ public class Demo { } public static abstract class Target { - public abstract String getName(); - public abstract tag getTag(); - public abstract nonterminal getNonTerminal(); public abstract int[] buildSequence(Production p); + private Reflection.Bindable _bindable; + public Target(Reflection.Bindable b) { this._bindable = b; } + + public String getName() { return _bindable.getSimpleName(); } + public tag getTag() { return (tag)_bindable.getAnnotation(tag.class); } + //public nonterminal getNonTerminal() { return (nonterminal)_bindable.getAnnotation(tag.class); } + public String toString() { return _bindable.getSimpleName(); } + public Object plant(Object[] fields) { return _bindable.impose(fields); } + public boolean isRaw() { return _bindable.isAnnotationPresent(raw.class); } + public Object invokeRaw(Iterable> t) { return _bindable.impose(new Object[] { t }); } + public boolean isCompatible(Production p) { tag t = getTag(); if (t != null && @@ -207,7 +203,7 @@ public class Demo { (t.value().equals("") && getName().equals(p.tag)))) return buildSequence(p)!=null; - nonterminal n = getNonTerminal(); + tag n = getTag(); if (n != null && (n.value().equals(p.nonTerminal) || (n.value().equals("") && getName().equals(p.nonTerminal)))) @@ -215,6 +211,7 @@ public class Demo { return false; } + public int[] buildSequence(Production p, String[] names, arg[] argtags) { int argTagged = 0; for(int i=0; i t) { return null; } - public class TargetReducer implements Reducer { + + public class TargetReducer implements Tree.TreeFunctor { private Production p; private int[] map; private String name; @@ -252,12 +248,12 @@ public class Demo { this.name = name; } public String toString() { return name; } - public Object reduce(Iterable t) { + public Object invoke(Iterable> t) { if (isRaw()) return invokeRaw(t); ArrayList ret = new ArrayList(); for(Tree tc : t) { - if (tc.head() != null && tc.head() instanceof Reducer) - ret.add(((Reducer)tc.head()).reduce(tc.children())); + if (tc.head() != null && tc.head() instanceof Functor) + ret.add(((Tree.TreeFunctor)tc.head()).invoke(tc.children())); else if (tc.numChildren() == 0) ret.add(tc.head()); else { @@ -266,22 +262,19 @@ public class Demo { } } System.err.println("input tree: " + t); - return plant(ret.toArray(new Object[0]), map); + Object[] o = (Object[])ret.toArray(new Object[0]); + int max = 0; + for(int i=0; i t); - } - public static class TargetClass extends Target { public final Class _class; - public TargetClass(Class _class) { this._class = _class; } - public String getName() { return _class.getSimpleName(); } - public tag getTag() { return (tag)_class.getAnnotation(tag.class); } - public nonterminal getNonTerminal() { return (nonterminal)_class.getAnnotation(nonterminal.class); } - public String toString() { return _class.getSimpleName(); } + public TargetClass(Class _class) { super(Reflection.Bindable.create(_class)); this._class = _class; } public int[] buildSequence(Production p) { Field[] f = _class.getDeclaredFields(); String[] names = new String[f.length]; @@ -297,45 +290,11 @@ public class Demo { return new TargetConstructor(c).buildSequence(p); return null; } - public Object plant(Object[] fields, int[] map) { - try { - Object ret = _class.newInstance(); - Field[] f = _class.getFields(); - int j = 0; - for(int i=0; i t) { - try { - return _method.invoke(null, new Object[] { t }); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - public Object plant(Object[] fields, int[] map) { - try { - Class[] argTypes = _method.getParameterTypes(); - Object[] args = new Object[argTypes.length]; - int j = 0; - for(int i=0; i red = (Tree.TreeFunctor)t.head(); + MG.Grammar g = (MG.Grammar)red.invoke(t.children()); Context cx = new Context(g,rm); Union u = null; for(MG.NonTerminal nt : g.nonterminals) { @@ -539,8 +402,8 @@ public class Demo { } public static class NonTerminal extends Un { public String name = null; - public @nonterminal("NonTerminal") NonTerminal(@arg("Word") String name, - @arg("RHS") Seq[][] sequences) { + public @tag("=") NonTerminal(@arg("Word") String name, + @arg("RHS") Seq[][] sequences) { this.name = name; this.sequences = sequences; } @@ -757,9 +620,9 @@ public class Demo { //public static @tag("(") El subexpression(Seq[][] rhs) { return new NonTerminal(rhs); } - public static @nonterminal("Word") String word(String s) { return s; } - public static @nonterminal("Quoted") String quoted(String s) { return s; } - public static @nonterminal("escaped") String c(char c) { return c+""; } + public static @tag("Word") String word(String s) { return s; } + public static @tag("Quoted") String quoted(String s) { return s; } + public static @tag("escaped") String c(char c) { return c+""; } public static @tag("\"\"") String emptystring() { return ""; } public static @tag("\n") String retur() { return "\n"; } public static @tag("\r") String lf() { return "\r"; } @@ -785,8 +648,8 @@ public class Demo { } public Context(Tree t, ReflectiveMeta rm) { this.rm = rm; - Reducer red = (Reducer)t.head(); - this.grammar = (MG.Grammar)red.reduce(t.children()); + Tree.TreeFunctor red = (Tree.TreeFunctor)t.head(); + this.grammar = (MG.Grammar)red.invoke(t.children()); } public Union peek(String name) { return map.get(name); } public void put(String name, Union u) { map.put(name, u); }