checkpoint
authoradam <adam@megacz.com>
Sat, 8 Jul 2006 06:31:11 +0000 (02:31 -0400)
committeradam <adam@megacz.com>
Sat, 8 Jul 2006 06:31:11 +0000 (02:31 -0400)
darcs-hash:20060708063111-5007d-b5f040a1be2cc38e9060307115de76ffb3f2c285.gz

src/edu/berkeley/sbp/Ambiguous.java
src/edu/berkeley/sbp/meta/MetaGrammarBindings.java
src/edu/berkeley/sbp/tib/TibDoc.java
src/edu/berkeley/sbp/util/Reflection.java
tests/tibdoc.g

index c96bdd4..1f7cc38 100644 (file)
@@ -13,12 +13,10 @@ public class Ambiguous extends Exception {
         // FEATURE: more legible printout desperately needed
         StringBuffer sb = new StringBuffer();
         sb.append("unresolved ambiguity ");
-        /*
         for(Tree<?> result : ambiguity.expand(false)) {
             sb.append("\n\n");
             result.toPrettyString(sb);
         }
-        */
         return sb.toString();
     }
 }
index 401536f..b70ddb8 100644 (file)
@@ -15,7 +15,12 @@ public class MetaGrammarBindings {
     /** A grammar (a set of nonterminals) */
     public static class GrammarNode extends HashMap<String,NonTerminalNode> {
         public @bind.as("Grammar") GrammarNode(NonTerminalNode[] nonterminals) {
-            for(NonTerminalNode nt : nonterminals) this.put(nt.name, nt); }
+            for(NonTerminalNode nt : nonterminals) {
+                if (this.get(nt.name)!=null)
+                    throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
+                this.put(nt.name, nt);
+            }
+        }
         public String toString() {
             String ret = "[ ";
             for(NonTerminalNode nt : values()) ret += nt + ", ";
@@ -327,7 +332,7 @@ public class MetaGrammarBindings {
     public static @bind.as("~")   ElementNode tilde(final ElementNode e) {
         return new PostProcess(e) {
                 public Element postProcess(Element e) {
-                    return infer((Topology<Character>)Atom.toAtom(e).complement()); 
+                    return infer((Topology<Character>)Atom.toAtom(e).complement().minus(CharRange.braces));
                 } }; }
 
     public static @bind.as("Word")        String word(String s) { return s; }
index 3245d76..469c166 100644 (file)
@@ -384,16 +384,17 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             public @bind.arg String password;
         }
 
-        public @bind.as("Euro") Object euro() { return null; }
-        public @bind.as("Citation") Object cite(Object o) { return new Chars("*cite*"); }
-        public @bind.as("Symbol") Object sym(Object o) { return null; }
+        public static @bind.as("Euro") Object euro() { return null; }
+        public static @bind.as("Citation") Object cite(Object o) { return new Chars("*cite*"); }
+        public static @bind.as("Symbol") Object sym(Object o) { return null; }
 
         public static abstract class List extends Text {
-            public @bind.arg Text[] points;
+            public @bind.arg Text[][] points;
             public abstract String tag();
             public void toHTML(ToHTML.HTML sb) {
                 sb.append("<"+tag()+">\n");
-                for(Text t : points) sb.tag("li", t);
+                for(Text[] t : points)
+                    sb.tag("li", t);
                 sb.append("</"+tag()+">\n");
             }
         }
@@ -409,6 +410,16 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
        public static class Blockquote extends Paragraph {
             Text[] text;
             public @bind Blockquote(Text[] t) { this.text = t; }
+            public @bind Blockquote(Text[] t, Text[] t2) {
+                if (t2==null) {
+                    this.text = t;
+                } else {
+                    Text[] t3 = new Text[t.length + t2.length];
+                    System.arraycopy(t,  0, t3, 0, t.length);
+                    System.arraycopy(t2, 0, t3, t.length, t2.length);
+                    this.text = t3;
+                }
+            }
             public void toHTML(HTML h) { h.tag("blockquote", new P(text)); }
         }
 
index 9d4837a..a7c2539 100644 (file)
@@ -211,60 +211,68 @@ public final class Reflection {
         return null;
     }
     public static Object impose(Class _class, Object[] fields) {
-        try {
-            Object ret = _class.newInstance();
-            Field[] f = _class.getFields();
-            int j = 0;
-            for(int i=0; i<f.length; i++) {
-                Object tgt = Reflection.lub(fields[i]);
-                if (f[i].getType() == String.class) tgt = stringify(tgt);
-                // FUGLY
-                tgt = coerce(tgt, f[i].getType());
+        Object ret = null;
+        try { ret = _class.newInstance(); }
+        catch (Exception e) { rethrow(e, "while trying to instantiate a " + _class.getName()); }
+        Field[] f = _class.getFields();
+        int j = 0;
+        for(int i=0; i<f.length; i++) {
+            Object tgt = Reflection.lub(fields[i]);
+            if (f[i].getType() == String.class) tgt = stringify(tgt);
+            // FUGLY
+            tgt = coerce(tgt, f[i].getType());
+            try {
                 f[i].set(ret, tgt);
+            } catch (Exception e) {
+                rethrow(e, "while setting \n    " +
+                        f[i] +
+                        "\n     to " + show(tgt));
             }
-            return ret;
-        } catch (Exception e) {
-            e.printStackTrace();
-            throw new RuntimeException(e);
         }
+        return ret;
+    }
+
+    public static Object rethrow(Exception e, String message) {
+        StackTraceElement[] st = e.getStackTrace();
+        RuntimeException re = new RuntimeException(e.getMessage() + "\n  " + message);
+        re.setStackTrace(st);
+        throw re;
     }
 
     public static Object impose(Method _method, Object[] fields) {
+        Object[] args = mkArgs(_method.getParameterTypes(), fields);
         try {
-            Class[] argTypes = _method.getParameterTypes();
-            Object[] args = new Object[argTypes.length];
-            int j = 0;
-            for(int i=0; i<args.length; i++) {
-                Object tgt = Reflection.lub(fields[i]);
-                if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
-                // FUGLY
-                tgt = Reflection.coerce(tgt, argTypes[i]);
-                args[i] = tgt;
-            }
             return _method.invoke(null, args);
         } catch (Exception e) {
-            throw new RuntimeException(e);
+            return rethrow(e, "while trying to invoke \n    " +
+                           _method +
+                           "\n    with arguments " + show(args));
         }
     }
-
     public static Object impose(Constructor _ctor, Object[] fields) {
+        Object[] args = mkArgs(_ctor.getParameterTypes(), fields);
         try {
-            Class[] argTypes = _ctor.getParameterTypes();
-            Object[] args = new Object[argTypes.length];
-            int j = 0;
-            for(int i=0; i<args.length; i++) {
-                Object tgt = Reflection.lub(fields[i]);
-                if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
-                // FUGLY
-                tgt = Reflection.coerce(tgt, argTypes[i]);
-                args[i] = tgt;
-            }
             return _ctor.newInstance(args);
         } catch (Exception e) {
-            throw new RuntimeException(e);
+            return rethrow(e, "while trying to invoke \n    " +
+                           _ctor +
+                           "\n    with arguments " + show(args));
         }
     }
 
+    private static Object[] mkArgs(Class[] argTypes, Object[] fields) {
+        int j = 0;
+        Object[] args = new Object[argTypes.length];
+        for(int i=0; i<args.length; i++) {
+            Object tgt = Reflection.lub(fields[i]);
+            if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
+            // FUGLY
+            tgt = Reflection.coerce(tgt, argTypes[i]);
+            args[i] = tgt;
+        }
+        return args;
+    }
+
     public static String stringify(Object o) {
         if (o==null) return "";
         if (!(o instanceof Object[])) return o.toString();
@@ -276,6 +284,13 @@ public final class Reflection {
     }
 
     public static Object coerce(Object o, Class c) {
+        try {
+            return coerce0(o, c);
+        } catch (Exception e) {
+            return (Object[])rethrow(e, "while trying to coerce " + show(o) + " to a " + c.getName());
+        }
+    }
+    public static Object coerce0(Object o, Class c) {
         if (o==null) return null;
         if (c.isInstance(o)) return o;
         if (c == char.class) {
index f455d89..f93c5dd 100644 (file)
@@ -56,9 +56,9 @@ nw         = ~[\r\n\ ]
 
 s                   = Doc
 
-Doc                 = head:Header body:Body  /ws
+Doc                 = head:Header ws! body:Body
 Header              = { "header" { KeyVal */ ws } /ws }
-Body                = {Section}*/ws
+Body                = { Section } */ws
 Section             = SectionHeader ws! Paragraph*
 SectionHeader       = "==" SectionHeaderBody "=="
 SectionHeaderBody   =  "=" SectionHeaderBody "="
@@ -67,11 +67,11 @@ SectionHeaderBody   =  "=" SectionHeaderBody "="
 sp       = " "**
 blank    = sp! "\n" sp! "\n" ws!
 
-KeyVal       = key:word "=" val:text /ws
+KeyVal       = key:bareword "=" val:text /ws
 wp           = w++
 num          = [0-9]++
 
-Paragraph   = Blockquote:: { "\"\" "    text }
+Paragraph   = { Blockquote:: "\"\" "    text }
             > HR::         { "---" "-"*      }
             > P::          { text            }
 
@@ -94,7 +94,9 @@ Item*/ws     =
              > (Chars:: alphanum++)       
              > "\"" text "\""             
              > (Symbol:: sym++)           
-//             > { Block:: text }           
+             > { Block:: text }           
+
+word = Chars:: bareword
 
 blockquote = "adsfafewag"
 //blockquote   = Blockquote:: "\"\"" (block | text "\"\"")
@@ -134,7 +136,7 @@ command      = Today::     "\\today"
 // subtypes of url (ftp, etc) as conjunctions, but the "master pattern"
 // only gets parsed once
 
-urlpath      = urlchar*
+urlpath      = urlchar* -> ~urlv           // this ~urlv should be handled by url! bug!
 username     = [a-zA-Z0-9;/?:&=$\-_.+]++
 password     = [a-zA-Z0-9;/?:&=$\-_.+]++
 urlc         = [a-zA-Z0-9;/?:&=$\-_.+@]
@@ -166,7 +168,7 @@ host         = IP::  nums "." nums "." nums "." nums
 
 // Tokens ///////////////////////////////////////////////////////////////////
 
-word       = alphanum++
+bareword   = alphanum++
            | quoted
 
 quoted     = "\"" ((~[\"\\] | escaped)+) "\""
@@ -181,5 +183,6 @@ escaped    = lf:: "\\n"
 alpha      = [a-zA-Z]
 alphanum   = [a-zA-Z0-9]
 sym        = ~[a-zA-Z0-9\ \r\n=\">]
+//sym        = [,()]