checkpoint
authoradam <adam@megacz.com>
Sat, 15 Jul 2006 05:32:04 +0000 (01:32 -0400)
committeradam <adam@megacz.com>
Sat, 15 Jul 2006 05:32:04 +0000 (01:32 -0400)
darcs-hash:20060715053204-5007d-7e165220811d26d9c4fd154cd5981450f77f578c.gz

Makefile
src/edu/berkeley/sbp/GSS.java
src/edu/berkeley/sbp/bind/Bindable.java
src/edu/berkeley/sbp/meta/MetaGrammarBindings.java
src/edu/berkeley/sbp/tib/TibDoc.java
tests/tibdoc.g

index 1154ad2..da56cde 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,10 @@
 
-java = java
+java = java -Xmx900m
 
 tibdoc: edu.berkeley.sbp.jar
        $(java) -cp $< edu.berkeley.sbp.tib.TibDoc \
                tests/tibdoc.g \
-               tests/tib.in
+               tests/bitstream.tib
 
 demo: edu.berkeley.sbp.jar
        $(java) -cp $< edu.berkeley.sbp.misc.Demo \
index 9c4b62b..8412471 100644 (file)
@@ -338,7 +338,9 @@ public class GSS {
                 Forest[] holder = r.holder;
                 Forest old = holder[pos];
 
-                for(Forest result : results())
+                HashSet<Forest> rr = new HashSet<Forest>();
+                for(Forest result : results()) rr.add(result);
+                for(Forest result : rr)
                     for(Node child : ((Forest.Ref<?>)result).parents) {
                         if (only != null && child!=only) continue;
                         holder[pos] = result;
index ccaa51d..8f5cc86 100644 (file)
@@ -22,49 +22,19 @@ public abstract class Bindable implements ToJava {
     public abstract String[]       getArgNames();
     public abstract Class[]        getArgTypes();
 
-    public static Bindable create(Object o) {
-        if (o instanceof Class) return new BindableClass((Class)o);
-        if (o instanceof Method) return new BindableMethod((Method)o);
-        if (o instanceof Constructor) return new BindableConstructor((Constructor)o);
-        return null;
-    }
-
     public Binding createBinding() { return new SimpleBinding(); }
     public Binding createBinding(final int[] map) { return new SimpleBinding(map); }
     public Binding createBinding(final int[] map, Object prepend) { return new SimpleBinding(map, prepend); }
 
-    public class SimpleBinding implements Binding, ToJava {
-        private int[] map = null;
-        private Object prepend = null;
-        public SimpleBinding() { }
-        public SimpleBinding(int[] map) { this.map = map; }
-        public SimpleBinding(int[] map, Object prepend) { this.map = map; this.prepend = prepend; }
-
-        public Object invoke(Object[] o) {
-            if (map==null) return impose(o);
-            int max = 0;
-            for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
-            Object[] o2 = new Object[max+1];
-            for(int i=0; i<o.length; i++) o2[map[i]+(prepend==null?0:1)] = o[i];
-            if (prepend != null) o2[0] = prepend;
-            return impose(o2);
-        }
 
-        public void toJava(StringBuffer sb) {
-            Bindable.this.toJava(sb);
-            sb.append(".createBinding(");
-            if (map != null) {
-                sb.append("new int[] {");
-                for(int i=0; i<map.length; i++) {
-                    sb.append(i);
-                    if (i<map.length-1) sb.append(",");
-                }
-                sb.append("}");
-            }
-            sb.append(")");
-        }
+    public static Bindable create(Object o) {
+        if (o instanceof Class) return new BindableClass((Class)o);
+        if (o instanceof Method) return new BindableMethod((Method)o);
+        if (o instanceof Constructor) return new BindableConstructor((Constructor)o);
+        return null;
     }
 
+    // Subclasses //////////////////////////////////////////////////////////////////////////////
 
     private static class BindableMethod extends Bindable {
         private final Method _method;
@@ -142,6 +112,8 @@ public abstract class Bindable implements ToJava {
     }
 
 
+    // Helpers //////////////////////////////////////////////////////////////////////////////
+
     private static void appendClassArray(StringBuffer sb, Class[] c) {
         sb.append("new Class[] {");
         for(int i=0; i<c.length; i++) {
@@ -157,4 +129,40 @@ public abstract class Bindable implements ToJava {
         if (!c.isArray()) return c.getName().replace('$','.');
         return makeClass(c.getComponentType())+"[]";
     }
+
+
+    // Creating Bindings //////////////////////////////////////////////////////////////////////////////
+
+    private class SimpleBinding implements Binding, ToJava {
+        private int[] map = null;
+        private Object prepend = null;
+        public SimpleBinding() { }
+        public SimpleBinding(int[] map) { this.map = map; }
+        public SimpleBinding(int[] map, Object prepend) { this.map = map; this.prepend = prepend; }
+
+        public Object invoke(Object[] o) {
+            if (map==null) return impose(o);
+            int max = 0;
+            for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
+            Object[] o2 = new Object[max+1];
+            for(int i=0; i<o.length; i++) o2[map[i]+(prepend==null?0:1)] = o[i];
+            if (prepend != null) o2[0] = prepend;
+            return impose(o2);
+        }
+
+        public void toJava(StringBuffer sb) {
+            Bindable.this.toJava(sb);
+            sb.append(".createBinding(");
+            if (map != null) {
+                sb.append("new int[] {");
+                for(int i=0; i<map.length; i++) {
+                    sb.append(i);
+                    if (i<map.length-1) sb.append(",");
+                }
+                sb.append("}");
+            }
+            sb.append(")");
+        }
+    }
+
 }
index b381360..e3299cb 100644 (file)
@@ -116,7 +116,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             this.name = prefix + name;
             this.sequences = sequences;
             this.rep = rep;
-            this.sep = prefix + sep;
+            this.sep = sep==null?null:(prefix + sep);
         }
         public Element build(Context cx, NonTerminalNode cnt) { return cx.get(name); }
         public void build(Context cx, Union u, NonTerminalNode cnt) {
@@ -125,7 +125,10 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
 
             Union urep = new Union();
             urep.add(Sequence.empty);
-            urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
+            if (sep != null)
+                urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
+            else
+                urep.add(Sequence.singleton(new Element[] { u }, 0));
 
             for(int i=0; i<sequences.length; i++) {
                 Seq[] group = sequences[i];
@@ -318,10 +321,10 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
         public Element build(Context cx, NonTerminalNode cnt) {
             return (!max)
-                ? Sequence.repeat(e.build(cx, null),        zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
+                ? Sequence.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
                 : sep==null
-                ? Sequence.repeatMaximal(infer(e.build(cx, null)), zero, many,                                   cx.rm.repeatTag())
-                : Sequence.repeatMaximal(e.build(cx, null),                    zero, many, infer(sep.build(cx, null)), cx.rm.repeatTag());
+                ? Sequence.repeatMaximal(infer(e.build(cx, null)), zero, many, cx.rm.repeatTag())
+                : Sequence.repeatMaximal(e.build(cx, null), zero, many, infer(sep.build(cx, null)), cx.rm.repeatTag());
         }
     }
 
@@ -403,7 +406,6 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
             map.put(name, ret);
             NonTerminalNode nt = grammar.get(name);
             if (nt==null) {
-                //System.err.println("*** warning could not find " + name);
                 throw new Error("warning could not find " + name);
             } else {
                 String old = cnt;
index ee1a861..2b65690 100644 (file)
@@ -394,7 +394,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             public P() { }
             public P(Text[] text) { this.text = text; }
             public P(String string) { this.text = new Text[] { new Chars(string) }; }
-            public void toHTML(HTML h) { if (text != null) for (Text t : text) if (t != null) t.toHTML(h); }
+            public void toHTML(HTML h) { if (text != null) h.append(text); }
             public String toString() {
                 StringBuffer sb = new StringBuffer();
                 ToHTML.HTML h = new ToHTML.HTML(sb);
@@ -408,7 +408,6 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             public @bind.arg String password;
         }
 
-        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; }
 
@@ -448,20 +447,20 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
         }
 
         public abstract static class Text extends Dump implements ToHTML { }
+
+        public @bind static class Space extends Chars {
+            public Space() { super(" "); }
+        }
         public @bind static class Chars extends Text {
             public String text;
             public Chars() { }
             public Chars(String text) { this.text = text; }
-            public void toHTML(HTML h) { h.appendText(" " + text + " "); }
+            public void toHTML(HTML h) { h.appendText(text); }
             public String toString() { return text; }
         }
         public @bind static class Block extends Text {
             public Text[] text;
-            public void toHTML(HTML h) {
-                for(Text t : text)
-                    if (t != null)
-                        t.toHTML(h);
-            }
+            public void toHTML(HTML h) { h.append(text); }
         }
         public static class TextWrap extends Text {
             public @bind.arg Text[] text;
@@ -469,7 +468,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
                 if (htmlTag()!=null) {
                     h.openTag(htmlTag(), htmlTagParams());
                 }
-                for(Text t : text) t.toHTML(h);
+                h.append(text);
                 if (htmlTag()!=null) {
                     h.closeTag(htmlTag());
                 }
@@ -477,7 +476,8 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             public String   htmlTag() { return null; }
             public Object[] htmlTagParams() { return null; }
         }
-        public static @bind class Verbatim   extends Text {
+
+        public static @bind class Verbatim  extends Text {
             public @bind.arg String s;
             public void toHTML(HTML h) {
                 h.openTag("div", new Object[] { "class", "terminal" });
@@ -493,6 +493,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
                 h.closeTag("div");
             }
         }
+
         //public @bind class Blockquote extends TextWrap { }
         public static @bind class Underline extends TextWrap { public String htmlTag() { return "u"; } }
         public static @bind class Footnote extends TextWrap { public String htmlTag() { return "small"; } }
@@ -510,23 +511,20 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
         public static @bind class Today extends Command { public void toHTML(HTML h) { } }
         public static @bind class LineBreak extends Command { public void toHTML(HTML h) { h.tag("br"); } }
 
-        public static abstract class Glyph extends Text implements ToHTML { }
-        public static @bind.as("emdash") class Emdash extends Glyph { public void toHTML(HTML h) { h.appendLiterally("&mdash;"); } }
-        public static @bind.as("ellipses") class Ellipses extends Glyph { public void toHTML(HTML h) { h.appendLiterally("&#8230;"); } }
-        public static @bind.as("r") class RegTm extends Glyph { public void toHTML(HTML h) { h.appendLiterally("&#xAE;"); } }
-        public static @bind.as("c") class Copyright extends Glyph { public void toHTML(HTML h) { h.appendLiterally("&#xA9;"); } }
-        public static @bind.as("tm") class Tm extends Glyph { public void toHTML(HTML h) { h.appendLiterally("&;"); } }
-        public static @bind.as("euro") class Euro extends Glyph { public void toHTML(HTML h) { h.appendLiterally("&euro;"); } }
+        public static @bind.as("emdash") Entity emdash() { return new Entity("mdash"); }
+        public static @bind.as("ellipses") Entity ellipses() { return new Entity("#8230"); }
+        public static @bind.as("r") Entity r() { return new Entity("#xAE"); }
+        public static @bind.as("c") Entity c() { return new Entity("#xA9"); }
+        public static @bind.as("tm") Entity tm() { return new Entity(""); }
+        public static @bind.as("euro") Entity euro() { return new Entity("euro"); }
 
-        public static @bind.as("#") Text comment() { return new Chars(""); }
         public static class Entity extends Text implements ToHTML {
-            public int code;
             public String name;
-            public Entity(String name, int code) { this.name = name; this.code = code; }
+            public String code;
+            public Entity(String code) { this.name = ""; this.code = code; }
+            public Entity(String name, int code) { this.code = "#"+code; this.name = name; }
             public Entity(String name, String abbrev, int code) { this(name, code); }
-            public void toHTML(HTML h) {
-                h.appendLiterally("&#x"+Integer.toString(code, 16)+";");
-            }
+            public void toHTML(HTML h) { h.appendLiterally("&"+code+";"); }
         }
 
         public static Entity[] entities = new Entity[] {
@@ -609,16 +607,24 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             return null;
         }
 
+        public static @bind class Quotes extends Text {
+            public Text[] text;
+            public void toHTML(HTML h) {
+                h.append("\"");
+                h.append(text);
+                h.append("\"");
+            }
+        }
+
         public static class Link extends Text {
             public Text[] t;
             public Url u;
-            public Input.Region region;
-            public @bind.as("link")  Link(Input.Region region, @bind.arg Text[] t, @bind.arg Url u)  { this.region = region; this.t = t; this.u = u; }
-            public Link(String s, Url u) { this(null,new Text[] { new Chars(s) }, u); }
+            public @bind.as("link")  Link(@bind.arg Text[] t, @bind.arg Url u)  { this.t = t; this.u = u; }
+            public Link(String s, Url u) { this(new Text[] { new Chars(s) }, u); }
             public void toHTML(HTML h) {
                 h.tag("a",
                       new Object[] { "href", u==null ? "" : u.toString() },
-                      new P(/*t*/region+""));
+                      new P(t));
             }
         }
 
@@ -647,25 +653,33 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             public String   pass;
             public String   port;
             public String   path;
-            public @bind.as("URL") Url(String method, String[] login, Host host, String port, String path) {
+            public String   ref;
+            public @bind.as("URL") Url(String method, String[] login, Host host, String port, String path, String ref) {
                 this.method = method;
                 this.user = login==null ? null : login.length >= 1 ? login[0] : null;
                 this.pass = login==null ? null : login.length >= 2 ? login[1] : null;
                 this.host = host;
                 this.port = port;
                 this.path = path;
+                this.ref  = ref;
             }
             public void toHTML(HTML h) { new Link(toString(), this).toHTML(h); }
             public String toString() {
-                return method + "://" + host + "/" + path;
+                return method + "://" + host + "/" + path + (ref==null ? "" : ("#"+ref));
             }
         }
         public static class Email extends Url {
             public @bind.as("Mailto") Email(String email) {
-                super("mailto", null, new Host(email.substring(email.indexOf('@'))), "25", email.substring(email.indexOf('@')));
+                super("mailto",
+                      null,
+                      new Host(email.substring(email.indexOf('@'))),
+                      "25",
+                      email.substring(email.indexOf('@')),
+                      null
+                      );
             }
             public @bind.as("email")  Email(String user, Host host) {
-                super("mailto", null, host, "25", user);
+                super("mailto", null, host, "25", user, null);
             }
             public void toHTML(HTML h) {
                 h.tag("a",
@@ -678,12 +692,17 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
         public static @bind.as("lf")        String lf() { return "\r"; }
         public static @bind.as("cr")        String cr() { return "\n"; }
         public static @bind.as("\"\"")      String empty() { return ""; }
-        public static @bind.as("urlescape") char urlescape(char a, char b) { return ((char)((a-'0') * 16 + (b-'0'))); }
+        public static @bind.as("urlescape") char   urlescape(char a, char b) { return ((char)((a-'0') * 16 + (b-'0'))); }
+        public static @bind String urlpath(String[] s) {
+            StringBuffer sb = new StringBuffer();
+            for(String st : s) sb.append(st);
+            return sb.toString();
+        }
     }
 
     public static void main(String[] s) throws Exception {
-        try {
 
+        try {
             Tree<String> res = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream(s[0])).expand1();
             
             AnnotationGrammarBindings resolver = new AnnotationGrammarBindings(TD.class);
@@ -706,27 +725,6 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             p.println(sb);
             p.flush();
             p.close();
-            
-            
-
-            /*
-            System.out.println("parsing " + s[0]);
-            Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
-            MetaGrammar gram = new Tib.Grammar(TibDoc.class);
-            gram = (MetaGrammar)gram.walk(res);
-            System.out.println("\nparsing " + s[1]);
-            Forest f = new CharParser(gram.done()).parse(new Tib(new FileInputStream(s[1])));
-            System.out.println();
-            System.out.println(f.expand1().toPrettyString());
-            System.out.println();
-
-
-            FileOutputStream fos = new FileOutputStream("out.html");
-            PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
-            p.println(sb);
-            p.flush();
-            p.close();
-            */
         } catch (Ambiguous a) {
             FileOutputStream fos = new FileOutputStream("/Users/megacz/Desktop/out.dot");
             PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
index 072c625..8a0928a 100644 (file)
@@ -63,7 +63,7 @@ Body                = { Section } */ws
 Section             = SectionHeader ws! Paragraph*
 SectionHeader       = "==" SectionHeaderBody "=="
 SectionHeaderBody   =  "=" SectionHeaderBody "="
-                    >      ws! alphanum++ ws!
+                    >      ws! text ws!
 
 sp       = " "**
 blank    = sp! "\n" sp! "\n" ws!
@@ -74,6 +74,8 @@ num          = [0-9]++
 
 Paragraph   = { Blockquote:: "\"\" "    text }
             > HR::         { "---" "-"*      }
+            > P::          { UL:: uli+/ws }
+            > P::          { OL:: oli+/ws }
             > P::          { text            }
 
 onums        = nums (". "|") ")!
@@ -95,7 +97,7 @@ Item*/ws     =
              > structured                 
              > styled                     
              > (Chars:: alphanum++)       
-             > "\"" text "\""             
+             > Quotes:: "\"" text "\""         
              > (Symbol:: sym++)           
              > { Block:: text }
 
@@ -123,7 +125,8 @@ styled       = Underline::     "__" text "__"
 
 block         = { text }
 
-link          = text:({ text }|word)  "->" href:(url|email)
+link          = text:({ text }|word)  "->" ws! href:href
+href          = url | email | {href}
 
 structured    = command & "\\" ([a-zA-Z0-9]++)! block?
               > glyph
@@ -161,7 +164,8 @@ url          = Mailto:: "mailto" ":"   email -> ~urlv
                   host:host
                   port:(":" nums)?
                   path:("/" urlpath)?
-                     -> ~urlv
+                  ref:("#" urlpath)?
+                     -> ~(urlv|[\#])
 url_login    = Login:: username:username password:(":" password) "@"
 method       = [+\-.a-z0-9]+
 domain       = (part +/ ".") -> ~"."