checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / Demo.java
index 845b1cb..c567edd 100644 (file)
@@ -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<String> 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<els.length; i++)
-                }
-            };
-    }
-    */
 
     
     /**
@@ -461,7 +466,7 @@ public class Demo {
                     Union u2 = new Union();
                     if (sequences.length==1) u2 = u;
                     for(int j=0; j<group.length; j++) {
-                        group[j].build(cx, u2);
+                        group[j].build(cx, u2, false);
                     }
                     if (sequences.length==1) break;
                     Sequence seq = Sequence.singleton(u2);
@@ -555,16 +560,17 @@ public class Demo {
                 this.elements = elements;
                 return this;
             }
-            public Sequence build(Context cx, Union u) {
-                Sequence ret = build0(cx);
-                for(Seq s : and) { Sequence dork = s.build(cx, u); dork.lame = true; ret = ret.and(dork); }
-                for(Seq s : not) { Sequence dork = s.build(cx, u); dork.lame = true; ret = ret.not(dork); }
+            public Sequence build(Context cx, Union u, boolean lame) {
+                Sequence ret = build0(cx, lame || this.lame);
+                for(Seq s : and) { Sequence dork = s.build(cx, u, true); ret = ret.and(dork); }
+                for(Seq s : not) { Sequence dork = s.build(cx, u, true); ret = ret.not(dork); }
                 u.add(ret);
+                ret.lame = lame;
                 return ret;
             }
-            public Sequence build0(Context cx) {
+            public Sequence build0(Context cx, boolean lame) {
                 boolean unwrap = false;
-                boolean dropAll = false;
+                boolean dropAll = lame;
                 if (tag!=null && tag.equals("[]")) unwrap  = true;
                 if (tag!=null && "()".equals(tag)) dropAll = true;
                 Object[] labels = new Object[elements.length];