checkpoint
authoradam <adam@megacz.com>
Sun, 2 Jul 2006 17:51:20 +0000 (13:51 -0400)
committeradam <adam@megacz.com>
Sun, 2 Jul 2006 17:51:20 +0000 (13:51 -0400)
darcs-hash:20060702175120-5007d-2117cd89d50661da17a605b1387168f928e900a2.gz

src/edu/berkeley/sbp/misc/Demo.java
src/edu/berkeley/sbp/tib/TibDoc.java
src/edu/berkeley/sbp/util/Reflection.java
src/edu/berkeley/sbp/util/StringUtil.java
tests/tibdoc.g

index c567edd..b3b9f4c 100644 (file)
@@ -213,7 +213,7 @@ public class Demo {
             nonterminal n = getNonTerminal();
             if (n != null &&
                 (n.value().equals(p.nonTerminal) ||
-                 (n.value().equals("") && p.nonTerminal.equals(getName()))))
+                 (n.value().equals("") && getName().equals(p.nonTerminal))))
                 return buildSequence(p)!=null;
 
             return false;
index a86fb2b..26f8174 100644 (file)
@@ -44,8 +44,8 @@ public class TibDoc {
         public Body   body;
         public void toHTML(ToHTML.HTML sb) { sb.tag("html", body); }
         public static class Header extends HashMap<String, Text[]> {
-            public static class kv { public String key; public Text[] val; }
-            public void attrs(kv[] kvs) { for(int i=0; i<kvs.length; i++) this.put(kvs[i].key, kvs[i].val); }
+            public static class KeyVal { public String key; public Text[] val; }
+            public void attrs(KeyVal[] KeyVals) { for(int i=0; i<KeyVals.length; i++) this.put(KeyVals[i].key, KeyVals[i].val); }
         }
         public static class Body implements ToHTML {
             public Section[] sections;
@@ -324,49 +324,46 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
 
     // Main //////////////////////////////////////////////////////////////////////////////
 
+    public static class Dump implements Reflection.Show {
+        public String toString() { return Reflection.show(this); }
+    }
+
     public static class TD {
 
-        public @nonterminal("Doc") static class Doc {
+        public @nonterminal static class Doc extends Dump {
             public @arg("head") Header head;
             public @arg("body") Body body;
-            public String toString() { return "doc:{"+head+","/*+body*/+"}"; }
         }
 
-        public @nonterminal("Header") static class Header {
-            public @arg("attrs") KV[] attrs;
+        public @nonterminal static class Header extends Dump {
+            public @arg("attrs") KeyVal[] attrs;
             // FIXME: it would be nice to be able to
-            // void kv(String, String) { ... } imperatively
-            public String toString() {
-                StringBuffer ret = new StringBuffer();
-                ret.append("<");
-                for(KV kv : attrs) ret .append(kv);
-                ret.append(">");
-                return ret.toString();
-            }
+            // void KeyVal(String, String) { ... } imperatively
         }
         
-        public @nonterminal("Body") static class Body {
+        public @nonterminal static class Body extends Dump {
             public Section[] sections;
             // FIXME: it would be nice to be able to
-            // void kv(String, String) { ... } imperatively
-            public String toString() {
-                StringBuffer ret = new StringBuffer();
-                ret.append("<");
-                for(Object kv : sections) ret .append(kv);
-                ret.append(">");
-                return ret.toString();
-            }
+            // void KeyVal(String, String) { ... } imperatively
         }
         
-        public @nonterminal("Section") static class Section {
-            public Object   @arg header;
-            public Object[] @arg paragraphs;
+        public @nonterminal("Section") static class Section extends Dump {
+            public String      header;
+            public Paragraph[] paragraphs;
         }
         
-        public @nonterminal("kv") static class KV {
+        public @nonterminal static class KeyVal extends Dump {
             public @arg("key") String key;
             public @arg("val") Object val;
-            public String toString() { return "KV["+key+"="+val+"]"; }
+        }
+
+        public static class Paragraph extends Dump { }
+        public @tag("P") static class P extends Paragraph {
+            Object text;
+        }
+        public @tag("HR") static class HR extends Paragraph { }
+        public @tag("Blockquote") static class Blockquote extends Paragraph {
+            Object text;
         }
     }
 
@@ -380,7 +377,11 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
                                             TibDoc.TD.Header.class,
                                             TibDoc.TD.Section.class,
                                             TibDoc.TD.Body.class,
-                                            TibDoc.TD.KV.class
+                                            TibDoc.TD.Paragraph.class,
+                                            TibDoc.TD.P.class,
+                                            TibDoc.TD.HR.class,
+                                            TibDoc.TD.Blockquote.class,
+                                            TibDoc.TD.KeyVal.class
                                         });
             Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
             MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
index c404ab5..247652a 100644 (file)
@@ -51,28 +51,28 @@ public final class Reflection {
         return (Object[])Array.newInstance(c, i);
     }
 
-    public static String indent(String s, int indent) {
-        if (s.indexOf('\n')==-1) return s;
-        StringBuffer ret = new StringBuffer();
-        for(int i=0; i<s.length(); i++) {
-            char c = s.charAt(i);
-            ret.append(c);
-            if (c=='\n')
-                for(int j=0; j<indent; j++)
-                    ret.append(' ');
-        }
-        return ret.toString();
-    }
-
     public static String show(Object o) {
         if (o==null) return "null";
+        if (o instanceof Show) {
+            StringBuffer ret = new StringBuffer();
+            for(Field f : o.getClass().getFields()) {
+                ret.append("\n" + f.getName() + " = ");
+                try {
+                    ret.append(show(f.get(o)));
+                } catch (Exception e) {
+                    ret.append("**"+e+"**");
+                    throw new RuntimeException(e);
+                }
+            }
+            return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}";
+        }
         if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
         Object[] arr = (Object[])o;
         StringBuffer ret = new StringBuffer();
         ret.append(o.getClass().getComponentType().getName());
-        ret.append("["+arr.length+"]:\n");
+        ret.append("["+arr.length+"]:");
         for(int i=0; i<arr.length; i++)
-            ret.append(indent(show(arr[i]), 4) + "\n");
+            ret.append(StringUtil.indent("\n"+show(arr[i]), 4));
         return ret.toString();
     }
 
@@ -197,4 +197,5 @@ public final class Reflection {
         return null;
     }
 
+    public static interface Show { }
 }
index 8c046ee..d8de66d 100644 (file)
@@ -69,4 +69,17 @@ public class StringUtil {
         }
         return sb.toString();
     }
+
+    public static String indent(String s, int indent) {
+        if (s.indexOf('\n')==-1) return s;
+        StringBuffer ret = new StringBuffer();
+        for(int i=0; i<s.length(); i++) {
+            char c = s.charAt(i);
+            ret.append(c);
+            if (c=='\n')
+                for(int j=0; j<indent; j++)
+                    ret.append(' ');
+        }
+        return ret.toString();
+    }
 }
index ce60b70..2afac83 100644 (file)
@@ -57,22 +57,22 @@ nw         = ~[\r\n\ ]
 s                   = Doc
 
 Doc                 = head:{Header} body:Body  /ws
-Header              = "header" attrs:{ kv */ ws }  /ws
+Header              = "header" attrs:{ KeyVal */ ws }  /ws
 Body                = {Section}*/ws
 Section             = SectionHeader Paragraph* /ws
 SectionHeader       = "==" SectionHeaderBody "=="
 SectionHeaderBody   =  "=" SectionHeaderBody "="
-                    >      !ws (Chars:: text:alphanum++) !ws
+                    >      !ws alphanum++ !ws
 
 sp       = " "**
 blank    = !sp "\n" !sp "\n" !ws
 
-kv           = key:word "=" val:text /ws
+KeyVal       = key:word "=" val:text /ws
 wp           = w++
 num          = [0-9]++
 Paragraph    = Blockquote:: { "\"\" "    text }
              > HR::         { "---" "-"*      }
-             >              { P:: t:text }
+             > P::          { text }
 
 onums        = nums !(". "|") ")
 any          = ~[]*