checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / MetaGrammar.java
index eacdcf6..2917f7b 100644 (file)
@@ -10,7 +10,7 @@ import java.io.*;
 
 public class MetaGrammar {
     
-    public static boolean harsh = false;
+    public static boolean harsh = true;
 
     public static void main(String[] args) throws Exception {
         if (args.length != 2) {
@@ -97,32 +97,6 @@ public class MetaGrammar {
             this._cl = c;
             this._inner = inner;
         }
-        private boolean match(Method m, String s) { return match(m.getAnnotation(bind.as.class), null, s); }
-        private boolean match(bind.as 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((bind.as)c.getAnnotation(bind.as.class), c, s)) return true;
-            //if (match((nonterminal)c.getAnnotation(bind.as.class), c, nonTerminalName)) return true;
-            return false;
-        }
-        public boolean match(Constructor con, String s, String nonTerminalName) {
-            Class c = con.getDeclaringClass();
-            if (match((bind.as)con.getAnnotation(bind.as.class), null, s)) return true;
-            //if (match((nonterminal)con.getAnnotation(bind.as.class), c, s)) return true;
-            return false;
-        }
         public Object repeatTag() {
             return new Tree.ArrayBuildingTreeFunctor<Object>();
         }
@@ -194,7 +168,10 @@ public class MetaGrammar {
         public Target(Bindable b) { this._bindable = b; }
 
         public String getName() { return _bindable.getSimpleName(); }
-        public bind.as getBindAs() { return (bind.as)_bindable.getAnnotation(bind.as.class); }
+        public bind getBind() {
+            return (bind)_bindable.getAnnotation(bind.class); }
+        public bind.as getBindAs() {
+            return (bind.as)_bindable.getAnnotation(bind.as.class); }
         //public nonterminal getNonTerminal() { return (nonterminal)_bindable.getAnnotation(bind.as.class); }
         public String toString() { return _bindable.getSimpleName(); }
         public boolean isRaw() { return _bindable.isAnnotationPresent(bind.raw.class); }
@@ -206,12 +183,20 @@ public class MetaGrammar {
                  (t.value().equals("") && getName().equals(p.tag))))
                 return buildSequence(p)!=null;
 
+            bind b = getBind();
+            System.out.println(_bindable.getClass().getSimpleName() + ": " + _bindable.getSimpleName());
+            if (b != null && getName().equals(p.tag))
+                return buildSequence(p)!=null;
+
             bind.as n = getBindAs();
             if (n != null &&
                 (n.value().equals(p.nonTerminal) ||
                  (n.value().equals("") && getName().equals(p.nonTerminal))))
                 return buildSequence(p)!=null;
 
+            if (b != null && getName().equals(p.nonTerminal))
+                return buildSequence(p)!=null;
+
             return false;
         }
 
@@ -309,6 +294,7 @@ public class MetaGrammar {
     }
     public static Union make(Tree t, String s) { return make(t, s, new ReflectiveMeta()); }
     public static Union make(Tree t, String s, ReflectiveMeta rm) {
+        System.out.println("Head: " + t.head());
         Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
         MG.Grammar g = (MG.Grammar)red.invoke(t.children());
         Context cx = new Context(g,rm);
@@ -327,14 +313,14 @@ public class MetaGrammar {
 
 
     public static class MG {
-        public static @bind.as("grammar") class Grammar {
+        public static @bind class Grammar {
             public NonTerminal get(String s) {
                 for(NonTerminal nt : nonterminals)
                     if (nt.name.equals(s))
                         return nt;
                 return null;
             }
-            public @bind.arg("NonTerminal") NonTerminal[] nonterminals;
+            public @bind.arg NonTerminal[] nonterminals;
             public String toString() {
                 String ret = "[ ";
                 for(NonTerminal nt : nonterminals) ret += nt + ", ";
@@ -365,7 +351,7 @@ public class MetaGrammar {
         }
         public static class NonTerminal extends Un {
             public String  name = null;
-            public @bind.as("=") NonTerminal(@bind.arg("Word") String name,
+            public @bind.as("NonTerminal") NonTerminal(@bind.arg("Word") String name,
                                          @bind.arg("RHS") Seq[][] sequences) {
                 this.name = name;
                 this.sequences = sequences;
@@ -473,13 +459,16 @@ public class MetaGrammar {
                 else if (tag!=null) {
                     ret = cx.rm.resolveTag(tag, cx.cnt, els, labels, drops);
                 } else {
-                    int idx = -1;
-                    for(int i=0; i<els.length; i++)
-                        if (!drops[i])
-                            if (idx==-1) idx = i;
-                            else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
-                    if (idx != -1) ret = Sequence.singleton(els, idx);
-                    else           ret = Sequence.drop(els, false);
+                    ret = cx.rm.tryResolveTag(tag, cx.cnt, els, labels, drops);
+                    if (ret == null) {
+                        int idx = -1;
+                        for(int i=0; i<els.length; i++)
+                            if (!drops[i])
+                                if (idx==-1) idx = i;
+                                else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
+                        if (idx != -1) ret = Sequence.singleton(els, idx);
+                        else           ret = Sequence.drop(els, false);
+                    }
                 }
                 if (this.follow != null)
                     ret.follow = infer(this.follow.build(cx));