checkpoint
authoradam <adam@megacz.com>
Sat, 8 Jul 2006 05:27:04 +0000 (01:27 -0400)
committeradam <adam@megacz.com>
Sat, 8 Jul 2006 05:27:04 +0000 (01:27 -0400)
darcs-hash:20060708052704-5007d-d1677271293aaa97696b5d264bb4c361414ce62e.gz

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

index 1f7cc38..c96bdd4 100644 (file)
@@ -13,10 +13,12 @@ public class Ambiguous extends Exception {
         // FEATURE: more legible printout desperately needed
         StringBuffer sb = new StringBuffer();
         sb.append("unresolved ambiguity ");
         // 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);
         }
         for(Tree<?> result : ambiguity.expand(false)) {
             sb.append("\n\n");
             result.toPrettyString(sb);
         }
+        */
         return sb.toString();
     }
 }
         return sb.toString();
     }
 }
index 9846edd..ee01a45 100644 (file)
@@ -31,8 +31,6 @@ public class AnnotationGrammarBindingResolver extends GrammarBindingResolver {
 
     public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
         MetaGrammar.Production p = new MetaGrammar.Production(tag, nonTerminalName, els, labels, drops);
 
     public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
         MetaGrammar.Production p = new MetaGrammar.Production(tag, nonTerminalName, els, labels, drops);
-            if (labels != null && labels.length > 0 && "numeric".equals(labels[0]))
-                System.out.println("here! ");
         for(Method m : _allMethods)
             if (new MetaGrammar.Target(m).isCompatible(p))
                 return new MetaGrammar.Target(m).makeSequence(p);
         for(Method m : _allMethods)
             if (new MetaGrammar.Target(m).isCompatible(p))
                 return new MetaGrammar.Target(m).makeSequence(p);
@@ -54,7 +52,6 @@ public class AnnotationGrammarBindingResolver extends GrammarBindingResolver {
         if (harsh) {
             throw new RuntimeException(message);
         } else {
         if (harsh) {
             throw new RuntimeException(message);
         } else {
-            System.err.println(message);
             return Sequence.rewritingSequence(tag, els, labels, drops);
         }
     }
             return Sequence.rewritingSequence(tag, els, labels, drops);
         }
     }
index a1e37bf..77635d0 100644 (file)
@@ -217,6 +217,4 @@ public class MetaGrammar {
         return g.build(s, rm);
     }
 
         return g.build(s, rm);
     }
 
-
-
 }
 }
index dc35dfc..9dc7ab3 100644 (file)
@@ -36,14 +36,14 @@ public class MetaGrammarBindings {
 
     public abstract static class UnionNode extends ElementNode {
         public Seq[][] sequences;
 
     public abstract static class UnionNode extends ElementNode {
         public Seq[][] sequences;
-        public void build(Context cx, Union u) {
+        public void build(Context cx, Union u, NonTerminalNode cnt) {
             HashSet<Sequence> bad2 = new HashSet<Sequence>();
             for(int i=0; i<sequences.length; i++) {
                 Seq[] group = sequences[i];
                 Union u2 = new Union();
                 if (sequences.length==1) u2 = u;
                 for(int j=0; j<group.length; j++) {
             HashSet<Sequence> bad2 = new HashSet<Sequence>();
             for(int i=0; i<sequences.length; i++) {
                 Seq[] group = sequences[i];
                 Union u2 = new Union();
                 if (sequences.length==1) u2 = u;
                 for(int j=0; j<group.length; j++) {
-                    group[j].build(cx, u2, false);
+                    group[j].build(cx, u2, false, cnt);
                 }
                 if (sequences.length==1) break;
                 Sequence seq = Sequence.singleton(u2);
                 }
                 if (sequences.length==1) break;
                 Sequence seq = Sequence.singleton(u2);
@@ -69,9 +69,9 @@ public class MetaGrammarBindings {
             this.rep = rep;
             this.sep = sep;
         }
             this.rep = rep;
             this.sep = sep;
         }
-        public Element build(Context cx) { return cx.get(name); }
-        public void build(Context cx, Union u) {
-            if (!rep) { super.build(cx, u); return; }
+        public Element build(Context cx, NonTerminalNode cnt) { return cx.get(name); }
+        public void build(Context cx, Union u, NonTerminalNode cnt) {
+            if (!rep) { super.build(cx, u, this); return; }
             HashSet<Sequence> bad2 = new HashSet<Sequence>();
 
             Union urep = new Union();
             HashSet<Sequence> bad2 = new HashSet<Sequence>();
 
             Union urep = new Union();
@@ -84,7 +84,7 @@ public class MetaGrammarBindings {
                 if (sequences.length==1) u2 = u;
                 for(int j=0; j<group.length; j++) {
                     Union u3 = new Union();
                 if (sequences.length==1) u2 = u;
                 for(int j=0; j<group.length; j++) {
                     Union u3 = new Union();
-                    group[j].build(cx, u3, false);
+                    group[j].build(cx, u3, false, this);
                     Sequence s = Sequence.unwrap(new Element[] { u3, urep },
                                                  cx.rm.repeatTag(),
                                                  new boolean[] { false, false });
                     Sequence s = Sequence.unwrap(new Element[] { u3, urep },
                                                  cx.rm.repeatTag(),
                                                  new boolean[] { false, false });
@@ -110,9 +110,9 @@ public class MetaGrammarBindings {
         public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
             this.sequences = sequences;
         }
         public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
             this.sequences = sequences;
         }
-        public Element build(Context cx) {
+        public Element build(Context cx, NonTerminalNode cnt) {
             Union ret = new Union();
             Union ret = new Union();
-            build(cx, ret);
+            build(cx, ret, cnt);
             return ret;
         }
     }
             return ret;
         }
     }
@@ -128,7 +128,7 @@ public class MetaGrammarBindings {
         public String getLabel() { return null; }
         public String getOwnerTag() { return null; }
         public boolean drop() { return false; }
         public String getLabel() { return null; }
         public String getOwnerTag() { return null; }
         public boolean drop() { return false; }
-        public abstract Element build(Context cx);
+        public abstract Element build(Context cx, NonTerminalNode cnt);
     }
 
     public static class Drop extends ElementNode {
     }
 
     public static class Drop extends ElementNode {
@@ -137,7 +137,7 @@ public class MetaGrammarBindings {
         public String getLabel() { return null; }
         public boolean drop() { return true; }
         public String getOwnerTag() { return e.getOwnerTag(); }
         public String getLabel() { return null; }
         public boolean drop() { return true; }
         public String getOwnerTag() { return e.getOwnerTag(); }
-        public Element build(Context cx) { return e.build(cx); }
+        public Element build(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); }
     }
 
     public static class Label extends ElementNode {
     }
 
     public static class Label extends ElementNode {
@@ -146,7 +146,7 @@ public class MetaGrammarBindings {
         public Label(String label, ElementNode e) { this.e = e; this.label = label; }
         public String getLabel() { return label; }
         public String getOwnerTag() { return e.getOwnerTag(); }
         public Label(String label, ElementNode e) { this.e = e; this.label = label; }
         public String getLabel() { return label; }
         public String getOwnerTag() { return e.getOwnerTag(); }
-        public Element build(Context cx) { return e.build(cx); }
+        public Element build(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); }
     }
 
     public static /*abstract*/ class Seq {
     }
 
     public static /*abstract*/ class Seq {
@@ -186,15 +186,15 @@ public class MetaGrammarBindings {
             this.elements = elements;
             return this;
         }
             this.elements = elements;
             return this;
         }
-        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); }
+        public Sequence build(Context cx, Union u, boolean lame, NonTerminalNode cnt) {
+            Sequence ret = build0(cx, lame || this.lame, cnt);
+            for(Seq s : and) { Sequence dork = s.build(cx, u, true, cnt); ret = ret.and(dork); }
+            for(Seq s : not) { Sequence dork = s.build(cx, u, true, cnt); ret = ret.not(dork); }
             u.add(ret);
             ret.lame = lame;
             return ret;
         }
             u.add(ret);
             ret.lame = lame;
             return ret;
         }
-        public Sequence build0(Context cx, boolean lame) {
+        public Sequence build0(Context cx, boolean lame, NonTerminalNode cnt) {
             boolean dropAll = lame;
             if (tag!=null && "()".equals(tag)) dropAll = true;
             Object[] labels = new Object[elements.length];
             boolean dropAll = lame;
             if (tag!=null && "()".equals(tag)) dropAll = true;
             Object[] labels = new Object[elements.length];
@@ -203,14 +203,14 @@ public class MetaGrammarBindings {
             for(int i=0; i<elements.length; i++) {
                 labels[i] = elements[i].getLabel();
                 drops[i]  = elements[i].drop();
             for(int i=0; i<elements.length; i++) {
                 labels[i] = elements[i].getLabel();
                 drops[i]  = elements[i].drop();
-                els[i] = elements[i].build(cx);
+                els[i] = elements[i].build(cx, cnt);
                 if (elements[i].getOwnerTag() != null)
                     tag = elements[i].getOwnerTag();
             }
             Sequence ret = null;
             if (dropAll)     ret = Sequence.drop(els, false);
             else {
                 if (elements[i].getOwnerTag() != null)
                     tag = elements[i].getOwnerTag();
             }
             Sequence ret = null;
             if (dropAll)     ret = Sequence.drop(els, false);
             else {
-                ret = cx.rm.tryResolveTag(tag, cx.cnt, els, labels, drops);
+                ret = cx.rm.tryResolveTag(tag, cnt==null?null:cnt.name, els, labels, drops);
                 if (ret == null) {
                     int idx = -1;
                     for(int i=0; i<els.length; i++)
                 if (ret == null) {
                     int idx = -1;
                     for(int i=0; i<els.length; i++)
@@ -222,7 +222,7 @@ public class MetaGrammarBindings {
                 }
             }
             if (this.follow != null)
                 }
             }
             if (this.follow != null)
-                ret.follow = infer(this.follow.build(cx));
+                ret.follow = infer(this.follow.build(cx, null));
             ret.lame = this.lame;
             return ret;
         }
             ret.lame = this.lame;
             return ret;
         }
@@ -242,7 +242,11 @@ public class MetaGrammarBindings {
 
     public static @bind.as("NonTerminalReference") class NonTerminalReferenceNode extends ElementNode {
         public @bind.arg String nonTerminal;
 
     public static @bind.as("NonTerminalReference") class NonTerminalReferenceNode extends ElementNode {
         public @bind.arg String nonTerminal;
-        public Element build(Context cx) { return cx.get(nonTerminal); }
+        public Element build(Context cx, NonTerminalNode cnt) {
+            Element ret = cx.get(nonTerminal);
+            if (ret == null) throw new RuntimeException("unkown nonterminal \""+nonTerminal+"\"");
+            return ret;
+        }
     }
 
     public static class Literal extends Constant {
     }
 
     public static class Literal extends Constant {
@@ -253,7 +257,7 @@ public class MetaGrammarBindings {
     public static                     class CharClass            extends ElementNode {
         Range[] ranges;
         public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
     public static                     class CharClass            extends ElementNode {
         Range[] ranges;
         public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
-        public Element build(Context cx) {
+        public Element build(Context cx, NonTerminalNode cnt) {
             edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
             for(Range r : ranges)
                 set.add(r.first, r.last);
             edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
             for(Range r : ranges)
                 set.add(r.first, r.last);
@@ -263,9 +267,9 @@ public class MetaGrammarBindings {
 
     public static @bind.as("{")           class XTree                 extends ElementNode {
         public @bind.arg Seq body;
 
     public static @bind.as("{")           class XTree                 extends ElementNode {
         public @bind.arg Seq body;
-        public Element build(Context cx) {
+        public Element build(Context cx, NonTerminalNode cnt) {
             Union u = new Union();
             Union u = new Union();
-            Sequence s = body.build(cx, u, false);
+            Sequence s = body.build(cx, u, false, null);
             Union u2 = new Union();
             u2.add(Sequence.singleton(new Element[] {
                 CharRange.leftBrace,
             Union u2 = new Union();
             u2.add(Sequence.singleton(new Element[] {
                 CharRange.leftBrace,
@@ -283,23 +287,23 @@ public class MetaGrammarBindings {
         public boolean zero, many, max;
         public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
             this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
         public boolean zero, many, max;
         public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
             this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
-        public Element build(Context cx) {
+        public Element build(Context cx, NonTerminalNode cnt) {
             return (!max)
             return (!max)
-                ? Sequence.repeat(e.build(cx),        zero, many, sep==null ? null : sep.build(cx), cx.rm.repeatTag())
+                ? Sequence.repeat(e.build(cx, null),        zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
                 : sep==null
                 : sep==null
-                ? Sequence.repeatMaximal(infer(e.build(cx)), zero, many,                                   cx.rm.repeatTag())
-                : Sequence.repeatMaximal(e.build(cx),                    zero, many, infer(sep.build(cx)), 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());
         }
     }
     public static class Constant extends ElementNode {
         Element constant;
         public Constant(Element constant) { this.constant = constant; }
         }
     }
     public static class Constant extends ElementNode {
         Element constant;
         public Constant(Element constant) { this.constant = constant; }
-        public Element build(Context cx) { return constant; }
+        public Element build(Context cx, NonTerminalNode cnt) { return constant; }
     }
     public abstract static class PostProcess extends ElementNode {
         ElementNode e;
         public PostProcess(ElementNode e) { this.e = e; }
     }
     public abstract static class PostProcess extends ElementNode {
         ElementNode e;
         public PostProcess(ElementNode e) { this.e = e; }
-        public Element build(Context cx) { return postProcess(e.build(cx)); }
+        public Element build(Context cx, NonTerminalNode cnt) { return postProcess(e.build(cx, cnt)); }
         public abstract Element postProcess(Element e);
     }
 
         public abstract Element postProcess(Element e);
     }
 
@@ -374,7 +378,7 @@ public class MetaGrammarBindings {
             } else {
                 String old = cnt;
                 cnt = name;
             } else {
                 String old = cnt;
                 cnt = name;
-                nt.build(this, ret);
+                nt.build(this, ret, nt);
                 cnt = old;
             }
             return ret;
                 cnt = old;
             }
             return ret;
index 1646a17..c91a369 100644 (file)
@@ -96,7 +96,6 @@ public class RegressionTests {
         }
         public static class TestCaseMakerHelper extends MetaGrammarBindings {
             public static @bind.as("grammaro") @bind.raw Object grammaro(Iterable<Tree> t) {
         }
         public static class TestCaseMakerHelper extends MetaGrammarBindings {
             public static @bind.as("grammaro") @bind.raw Object grammaro(Iterable<Tree> t) {
-                System.out.println("working on " + t);
                 return MetaGrammar.make(t.iterator().next(), "s", new TaggingGrammarBindingResolver());
             }
             //public static @bind.as("tca")           Object tca(Object[] o) throws IOException {
                 return MetaGrammar.make(t.iterator().next(), "s", new TaggingGrammarBindingResolver());
             }
             //public static @bind.as("tca")           Object tca(Object[] o) throws IOException {
index 1057673..3245d76 100644 (file)
@@ -338,13 +338,13 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             public void toHTML(HTML h) { body.toHTML(h); }
         }
 
             public void toHTML(HTML h) { body.toHTML(h); }
         }
 
-        public @bind.as("H") static class Header extends Dump {
+        public @bind static class Header extends Dump {
             public @bind.arg KeyVal[] attrs;
             // FIXME: it would be nice to be able to
             // void KeyVal(String, String) { ... } imperatively
         }
         
             public @bind.arg KeyVal[] attrs;
             // FIXME: it would be nice to be able to
             // void KeyVal(String, String) { ... } imperatively
         }
         
-        public @bind.as("B") static class Body extends Dump implements ToHTML {
+        public @bind static class Body extends Dump implements ToHTML {
             public Section[] sections;
             public void toHTML(HTML h) { for(Section s : sections) s.toHTML(h); }
         }
             public Section[] sections;
             public void toHTML(HTML h) { for(Section s : sections) s.toHTML(h); }
         }
index 6a4361f..9d4837a 100644 (file)
@@ -3,6 +3,7 @@ import java.io.*;
 import java.lang.reflect.*;
 import java.lang.annotation.*;
 
 import java.lang.reflect.*;
 import java.lang.annotation.*;
 
+// FIXME: decent error reporting
 /** Random reflection-related utilities */
 public final class Reflection {
     
 /** Random reflection-related utilities */
 public final class Reflection {
     
@@ -219,7 +220,6 @@ public final class Reflection {
                 if (f[i].getType() == String.class) tgt = stringify(tgt);
                 // FUGLY
                 tgt = coerce(tgt, f[i].getType());
                 if (f[i].getType() == String.class) tgt = stringify(tgt);
                 // FUGLY
                 tgt = coerce(tgt, f[i].getType());
-                System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
                 f[i].set(ret, tgt);
             }
             return ret;
                 f[i].set(ret, tgt);
             }
             return ret;
@@ -239,10 +239,8 @@ public final class Reflection {
                 if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
                 // FUGLY
                 tgt = Reflection.coerce(tgt, argTypes[i]);
                 if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
                 // FUGLY
                 tgt = Reflection.coerce(tgt, argTypes[i]);
-                System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
                 args[i] = tgt;
             }
                 args[i] = tgt;
             }
-            //System.err.println("invoking " + _method + " with " + Reflection.show(args));
             return _method.invoke(null, args);
         } catch (Exception e) {
             throw new RuntimeException(e);
             return _method.invoke(null, args);
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -259,7 +257,6 @@ public final class Reflection {
                 if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
                 // FUGLY
                 tgt = Reflection.coerce(tgt, argTypes[i]);
                 if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
                 // FUGLY
                 tgt = Reflection.coerce(tgt, argTypes[i]);
-                System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
                 args[i] = tgt;
             }
             return _ctor.newInstance(args);
                 args[i] = tgt;
             }
             return _ctor.newInstance(args);
@@ -315,12 +312,10 @@ public final class Reflection {
             for(int i=0; i<((Object[])o).length; i++) {
                 Object ob = (((Object[])o)[i]);
                 if (ob != null) {
             for(int i=0; i<((Object[])o).length; i++) {
                 Object ob = (((Object[])o)[i]);
                 if (ob != null) {
-                    //System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
                     ok = false;
                 }
             }
             if (ok) {
                     ok = false;
                 }
             }
             if (ok) {
-                //System.err.println("hit with " + c.getComponentType().getName());
                 return Array.newInstance(c.getComponentType(), ((Object[])o).length);
             }
         }
                 return Array.newInstance(c.getComponentType(), ((Object[])o).length);
             }
         }
index 80c8cef..f455d89 100644 (file)
@@ -57,9 +57,9 @@ nw         = ~[\r\n\ ]
 s                   = Doc
 
 Doc                 = head:Header body:Body  /ws
 s                   = Doc
 
 Doc                 = head:Header body:Body  /ws
-Header              = H:: { "header" { KeyVal */ ws } /ws }
-Body                = B:: {Section}*/ws
-Section             = SectionHeader ws! (Paragraph Paragraph)
+Header              = { "header" { KeyVal */ ws } /ws }
+Body                = {Section}*/ws
+Section             = SectionHeader ws! Paragraph*
 SectionHeader       = "==" SectionHeaderBody "=="
 SectionHeaderBody   =  "=" SectionHeaderBody "="
                     >      ws! alphanum++ ws!
 SectionHeader       = "==" SectionHeaderBody "=="
 SectionHeaderBody   =  "=" SectionHeaderBody "="
                     >      ws! alphanum++ ws!