checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / Demo.java
index 82741d6..08a8a89 100644 (file)
@@ -46,19 +46,11 @@ public class Demo {
         private final Class _cl;
         private final Class[] _inner;
         public ReflectiveMeta() {
-            this(MG.class,
-                 new Class[] {
-                     MG.Grammar.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,
-                     MG.CharClass.class
-                 });
+            this(MG.class);
+        }
+        public ReflectiveMeta(Class c) {
+            this._cl = c;
+            this._inner = c.getDeclaredClasses();
         }
         public ReflectiveMeta(Class c, Class[] inner) {
             this._cl = c;
@@ -122,49 +114,51 @@ public class Demo {
     }
 
     
-    /**
-     *  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 ""; }
-    */
-
-    @Retention(RetentionPolicy.RUNTIME) public static @interface raw { }
-
-    /**
-     *  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.
-     */ 
-    public static class bind {
+    public static class bind {    
+        /**
+         *  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 ""; }
+        */
+
+        @Retention(RetentionPolicy.RUNTIME) public static @interface raw { }
+
+        /**
+         *  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 as         { 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 ""; }
+
+        /**
+         *  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 ""; }
+    }
 
     public static class Production {
         public String tag;
@@ -186,7 +180,19 @@ public class Demo {
     }
 
     public static abstract class Target {
-        public abstract int[] buildSequence(Production p);
+            public int[] buildSequence(Production p) {
+                Annotation[][] annotations = _bindable.getArgAnnotations();
+                String[]       names       = _bindable.getArgNames();
+                String name = _bindable.getSimpleName();
+                int len = annotations.length;
+                int ofs = 0;
+                bind.arg[] argtags  = new bind.arg[len];
+                for(int i=0; i<names.length; i++)
+                    for(Annotation a : annotations[i+ofs])
+                        if (a instanceof bind.arg)
+                            argtags[i+ofs] = (bind.arg)a;
+                return Target.this.buildSequence(p, names, argtags);
+            }
         private Reflection.Bindable _bindable;
         public Target(Reflection.Bindable b) { this._bindable = b; }
 
@@ -195,7 +201,7 @@ public class Demo {
         //public nonterminal getNonTerminal() { return (nonterminal)_bindable.getAnnotation(bind.as.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 boolean isRaw() { return _bindable.isAnnotationPresent(bind.raw.class); }
         public Object invokeRaw(Iterable<Tree<Object>> t) { return _bindable.impose(new Object[] { t }); }
 
         public boolean isCompatible(Production p) {
@@ -214,7 +220,7 @@ public class Demo {
             return false;
         }
 
-        public int[] buildSequence(Production p, String[] names, arg[] argtags) {
+        public int[] buildSequence(Production p, String[] names, bind.arg[] argtags) {
             int argTagged = 0;
             for(int i=0; i<argtags.length; i++)
                 if (argtags[i] != null)
@@ -277,60 +283,15 @@ public class Demo {
     public static class TargetClass extends Target {
         public final Class _class;
         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];
-            arg[]    argtags = new arg[f.length];
-            for(int i=0; i<f.length; i++) {
-                names[i]   = f[i].getName();
-                argtags[i] = f[i].getAnnotation(arg.class);
-            }
-            int[] ret = buildSequence(p, names, argtags);
-            if (ret!=null) return ret;
-            for(Constructor c : _class.getConstructors())
-                if (new TargetConstructor(c).buildSequence(p)!=null)
-                    return new TargetConstructor(c).buildSequence(p);
-            return null;
-        }
     }
 
     public static class TargetConstructor extends Target {
         public final Constructor _ctor;
         public TargetConstructor(Constructor _ctor) { super(Reflection.Bindable.create(_ctor)); this._ctor = _ctor; }
-        public int[] buildSequence(Production p) {
-            Annotation[][] annotations = _ctor.getParameterAnnotations();
-            int len = annotations.length;
-            int ofs = 0;
-            String name = _ctor.getDeclaringClass().getName();
-            /*
-            if (name.indexOf('$') > name.lastIndexOf('.')) {
-                len--;
-                ofs++;
-            }
-            */
-            String[] names   = new String[len];
-            arg[]    argtags = new arg[len];
-            for(int i=0; i<names.length; i++)
-                for(Annotation a : annotations[i+ofs])
-                    if (a instanceof arg)
-                        argtags[i+ofs] = (arg)a;
-            return buildSequence(p, names, argtags);
-        }
     }
     public static class TargetMethod extends Target {
         public final Method _method;
         public TargetMethod(Method _method) { super(Reflection.Bindable.create(_method)); this._method = _method; }
-        public int[] buildSequence(Production p) {
-            Annotation[][] annotations = _method.getParameterAnnotations();
-            String[] names   = new String[annotations.length];
-            arg[]    argtags = new arg[annotations.length];
-            for(int i=0; i<names.length; i++)
-                for(Annotation a : annotations[i])
-                    if (a instanceof arg)
-                        argtags[i] = (arg)a;
-            int[] ret = buildSequence(p, names, argtags);
-            return ret;
-        }
     }
 
     public static Union cached = null;
@@ -373,7 +334,7 @@ public class Demo {
                         return nt;
                 return null;
             }
-            public @arg("NonTerminal") NonTerminal[] nonterminals;
+            public @bind.arg("NonTerminal") NonTerminal[] nonterminals;
             public String toString() {
                 String ret = "[ ";
                 for(NonTerminal nt : nonterminals) ret += nt + ", ";
@@ -404,8 +365,8 @@ public class Demo {
         }
         public static class NonTerminal extends Un {
             public String  name = null;
-            public @bind.as("=") NonTerminal(@arg("Word") String name,
-                                         @arg("RHS") Seq[][] sequences) {
+            public @bind.as("=") NonTerminal(@bind.arg("Word") String name,
+                                         @bind.arg("RHS") Seq[][] sequences) {
                 this.name = name;
                 this.sequences = sequences;
             }
@@ -539,7 +500,7 @@ public class Demo {
         public static @bind.as("()")  El   epsilon()                         { return new Constant(Union.epsilon); }
 
         public static @bind.as("nonTerminal") class NonTerminalReference extends El {
-            public @arg String nonTerminal;
+            public @bind.arg String nonTerminal;
             public Element build(Context cx) {
                 return cx.get(nonTerminal);
             }
@@ -562,7 +523,7 @@ public class Demo {
         }
 
         public static @bind.as("{")           class XTree                 extends El {
-            public @arg Seq body;
+            public @bind.arg Seq body;
             public Element build(Context cx) {
                 throw new Error();
             }