checkpoint
authoradam <adam@megacz.com>
Thu, 13 Jul 2006 09:08:46 +0000 (05:08 -0400)
committeradam <adam@megacz.com>
Thu, 13 Jul 2006 09:08:46 +0000 (05:08 -0400)
darcs-hash:20060713090846-5007d-29c31d94e929cead6018306da206dfdd2d6040f7.gz

14 files changed:
src/edu/berkeley/sbp/Forest.java
src/edu/berkeley/sbp/GSS.java
src/edu/berkeley/sbp/Input.java
src/edu/berkeley/sbp/Parser.java
src/edu/berkeley/sbp/Sequence.java
src/edu/berkeley/sbp/Tree.java
src/edu/berkeley/sbp/bind/Bindable.java
src/edu/berkeley/sbp/chr/CharParser.java
src/edu/berkeley/sbp/meta/MetaGrammar.java
src/edu/berkeley/sbp/meta/MetaGrammarBindings.java
src/edu/berkeley/sbp/misc/Cartesian.java
src/edu/berkeley/sbp/misc/Demo.java
src/edu/berkeley/sbp/misc/RegressionTests.java
src/edu/berkeley/sbp/util/TreeBuilder.java

index cadf343..b00f069 100644 (file)
@@ -77,11 +77,11 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/
         public InnerAmbiguous(Forest<?> f) { this.f = f; }
     }
 
         public InnerAmbiguous(Forest<?> f) { this.f = f; }
     }
 
-    static        <T> Forest<T> singleton(Input.Location loc, Position p) {
+    static        <T> Forest<T> singleton(Input.Region loc, Position p) {
         return create(loc, null, new Forest[] { }, false, true, p); }
         return create(loc, null, new Forest[] { }, false, true, p); }
-    static        <T> Forest<T> singleton(Input.Location loc, Forest<T> body, Position p) { return body; }
-    static        <T> Forest<T> leaf(Input.Location loc, T tag, Position p) { return create(loc, tag, null, false, false, p); }
-    public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton, Position p) {
+    static        <T> Forest<T> singleton(Input.Region loc, Forest<T> body, Position p) { return body; }
+    static        <T> Forest<T> leaf(Input.Region loc, T tag, Position p) { return create(loc, tag, null, false, false, p); }
+    public static <T> Forest<T> create(Input.Region loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton, Position p) {
         return new MyBody<T>(loc, tag, tokens, unwrap, singleton, p);
     }
     // Body //////////////////////////////////////////////////////////////////////////////
         return new MyBody<T>(loc, tag, tokens, unwrap, singleton, p);
     }
     // Body //////////////////////////////////////////////////////////////////////////////
@@ -121,14 +121,14 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/
             ivbc.invoke(this, b, c);
         }
 
             ivbc.invoke(this, b, c);
         }
 
-        private final Input.Location    location;
+        private final Input.Region    location;
         private final T                 tag;
         private final Forest<T>[]       tokens;
         private final boolean           unwrap;
         private final boolean           singleton;
         private final Sequence.Position reduction;
 
         private final T                 tag;
         private final Forest<T>[]       tokens;
         private final boolean           unwrap;
         private final boolean           singleton;
         private final Sequence.Position reduction;
 
-        private MyBody(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton, Position reduction) {
+        private MyBody(Input.Region loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton, Position reduction) {
             this.location = loc;
             this.tag = tag;
             this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
             this.location = loc;
             this.tag = tag;
             this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
@@ -143,7 +143,7 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/
         }
         private void gather(TaskList tl, HashSet<Tree<T>>[] ht, HashSet<Tree<T>> target, Tree[] trees, int i) {
             if (i==ht.length) {
         }
         private void gather(TaskList tl, HashSet<Tree<T>>[] ht, HashSet<Tree<T>> target, Tree[] trees, int i) {
             if (i==ht.length) {
-                target.add(new Tree<T>(null, tag, trees));
+                target.add(new Tree<T>(location, tag, trees));
                 return;
             }
             for(Tree<T> tree : ht[i]) {
                 return;
             }
             for(Tree<T> tree : ht[i]) {
@@ -153,7 +153,7 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/
                     System.arraycopy(trees, 0, trees2, 0, trees.length-1);
                     for(int j=0; j<tree.numChildren(); j++)
                         trees2[trees.length-1+j] = tree.child(j);
                     System.arraycopy(trees, 0, trees2, 0, trees.length-1);
                     for(int j=0; j<tree.numChildren(); j++)
                         trees2[trees.length-1+j] = tree.child(j);
-                    target.add(new Tree<T>(null, tag, trees2));
+                    target.add(new Tree<T>(location, tag, trees2));
                 } else {
                     trees[i] = tree;
                     gather(tl, ht, target, trees, i+1);
                 } else {
                     trees[i] = tree;
                     gather(tl, ht, target, trees, i+1);
@@ -190,9 +190,9 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/
 
             } else {
                 tokens[i].visit(new TreeBuilder<T>(h.toss) {
 
             } else {
                 tokens[i].visit(new TreeBuilder<T>(h.toss) {
-                    public void start(T head, Input.Location loc) { }
+                    public void start(T head, Input.Region loc) { }
                     public void addTree(Tree<T> t) { toks.add(t); }
                     public void addTree(Tree<T> t) { toks.add(t); }
-                    public void finish(T head, Input.Location loc) {
+                    public void finish(T head, Input.Region loc) {
                         int old = h.toks.size();
                         h.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint)));
                         expand(i+1, h);
                         int old = h.toks.size();
                         h.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint)));
                         expand(i+1, h);
index 7af8b07..9c4b62b 100644 (file)
@@ -342,7 +342,7 @@ public class GSS {
                     for(Node child : ((Forest.Ref<?>)result).parents) {
                         if (only != null && child!=only) continue;
                         holder[pos] = result;
                     for(Node child : ((Forest.Ref<?>)result).parents) {
                         if (only != null && child!=only) continue;
                         holder[pos] = result;
-                        if (pos==0) child.finish(r, r.rewrite(phase().getLocation()), target);
+                        if (pos==0) child.finish(r, r.rewrite(new Input.Region(child.phase().getLocation(), phase().getLocation())), target);
                         else        child.reduce(r, pos-1, target, null);
                     }
 
                         else        child.reduce(r, pos-1, target, null);
                     }
 
index 846fd9a..407d843 100644 (file)
@@ -23,6 +23,7 @@ public interface Input<Token> {
     public static class Region<Loc extends Location> /* implements Topology<Location<Tok>> */ {
         public final Loc start;
         public final Loc end;
     public static class Region<Loc extends Location> /* implements Topology<Location<Tok>> */ {
         public final Loc start;
         public final Loc end;
+        public String toString() { return start+"-"+end; }
         public Region(Loc a, Loc b) {
             switch(a.compareTo(b)) {
                 case -1:
         public Region(Loc a, Loc b) {
             switch(a.compareTo(b)) {
                 case -1:
index c5ebd7e..0d14563 100644 (file)
@@ -15,7 +15,7 @@ public abstract class Parser<Tok, Result> {
     protected Parser(Table<Tok> pt)               { this.pt = pt; }
 
     /** implement this method to create the output forest corresponding to a lone shifted input token */
     protected Parser(Table<Tok> pt)               { this.pt = pt; }
 
     /** implement this method to create the output forest corresponding to a lone shifted input token */
-    public abstract Forest<Result> shiftToken(Tok t, Input.Location loc);
+    public abstract Forest<Result> shiftToken(Input.Location oldloc, Tok t, Input.Location newloc);
 
     public boolean helpgc = true;
 
 
     public boolean helpgc = true;
 
@@ -29,9 +29,10 @@ public abstract class Parser<Tok, Result> {
         current.newNode(null, Forest.leaf(null, null, null), pt.start, true);
         int count = 1;
         for(int idx=0;;idx++) {
         current.newNode(null, Forest.leaf(null, null, null), pt.start, true);
         int count = 1;
         for(int idx=0;;idx++) {
+            Input.Location oldloc = loc;
             loc = input.getLocation();
             current.reduce();
             loc = input.getLocation();
             current.reduce();
-            Forest forest = current.token==null ? null : shiftToken((Tok)current.token, loc);
+            Forest forest = current.token==null ? null : shiftToken(oldloc, (Tok)current.token, loc);
             GSS.Phase next = gss.new Phase<Tok>(current, this, current, input.next(), loc, forest);
             if (!helpgc) {
                 FileOutputStream fos = new FileOutputStream("out-"+idx+".dot");
             GSS.Phase next = gss.new Phase<Tok>(current, this, current, input.next(), loc, forest);
             if (!helpgc) {
                 FileOutputStream fos = new FileOutputStream("out-"+idx+".dot");
index 61a18e8..0ac3500 100644 (file)
@@ -85,7 +85,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         return epsilonForm;
     }
 
         return epsilonForm;
     }
 
-    protected abstract <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p);
+    protected abstract <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p);
 
 
     // Position //////////////////////////////////////////////////////////////////////////////
 
 
     // Position //////////////////////////////////////////////////////////////////////////////
@@ -127,8 +127,8 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
         // Position /////////////////////////////////////////////////////////////////////////////////
 
 
         // Position /////////////////////////////////////////////////////////////////////////////////
 
-        final <T> Forest<T> rewrite(Input.Location loc) { return rewrite(loc, true); }
-        private final <T> Forest<T> rewrite(Input.Location loc, boolean epsilonCheck) {
+        final <T> Forest<T> rewrite(Input.Region loc) { return rewrite(loc, true); }
+        private final <T> Forest<T> rewrite(Input.Region loc, boolean epsilonCheck) {
             if (epsilonCheck && this==firstp()) return epsilonForm();
             for(int i=0; i<pos; i++) if (holder[i]==null) throw new Error("realbad " + i);
             for(int i=pos; i<elements.length; i++) {
             if (epsilonCheck && this==firstp()) return epsilonForm();
             for(int i=0; i<pos; i++) if (holder[i]==null) throw new Error("realbad " + i);
             for(int i=pos; i<elements.length; i++) {
@@ -180,7 +180,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         private final Object result;
         public Constant(Element[] e, Object result) { super(e); this.result = result; }
         Sequence _clone() { return new Constant(elements, result); }
         private final Object result;
         public Constant(Element[] e, Object result) { super(e); this.result = result; }
         Sequence _clone() { return new Constant(elements, result); }
-        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
+        public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
             return (Forest<T>)Forest.leaf(loc, result, p);
         }
         static class Drop extends Constant {
             return (Forest<T>)Forest.leaf(loc, result, p);
         }
         static class Drop extends Constant {
@@ -199,7 +199,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         private final int idx;
         public Singleton(Element e) { this(new Element[] { e }, 0); }
         public Singleton(Element[] e, int idx) { super(e); this.idx = idx; }
         private final int idx;
         public Singleton(Element e) { this(new Element[] { e }, 0); }
         public Singleton(Element[] e, int idx) { super(e); this.idx = idx; }
-        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) { return (Forest<T>)Forest.singleton(loc, args[idx], p); }
+        public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) { return (Forest<T>)Forest.singleton(loc, args[idx], p); }
         Sequence _clone() { return new Singleton(elements,idx); }
     }
 
         Sequence _clone() { return new Singleton(elements,idx); }
     }
 
@@ -210,7 +210,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         public Unwrap(Element[] e, Object tag)                  { super(e); this.drops = null; this.tag = tag; }
         public Unwrap(Element[] e, Object tag, boolean[] drops) { super(e); this.drops = drops; this.tag = tag; }
         Sequence _clone() { return new Unwrap(elements, drops); }
         public Unwrap(Element[] e, Object tag)                  { super(e); this.drops = null; this.tag = tag; }
         public Unwrap(Element[] e, Object tag, boolean[] drops) { super(e); this.drops = drops; this.tag = tag; }
         Sequence _clone() { return new Unwrap(elements, drops); }
-        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
+        public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
             for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
             if (drops==null) return Forest.create(loc, (T)tag, args, true, false, p);
             int count = 0;
             for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
             if (drops==null) return Forest.create(loc, (T)tag, args, true, false, p);
             int count = 0;
@@ -234,7 +234,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             this.drops = drops == null ? new boolean[e.length] : drops;
             for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
         }
             this.drops = drops == null ? new boolean[e.length] : drops;
             for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
         }
-        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
+        public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
             Forest<T>[] args2 = new Forest[count];
             int j = 0;
             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
             Forest<T>[] args2 = new Forest[count];
             int j = 0;
             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
index 86a2ffa..c54fa6a 100644 (file)
@@ -14,7 +14,7 @@ public class Tree<T>
 
     final T           head;
           Tree<T>[]   children;
 
     final T           head;
           Tree<T>[]   children;
-    final Input.Location    location;
+    final Input.Region    location;
 
     public T                 head()        { return head; }
     public int               numChildren() { return children.length; }
 
     public T                 head()        { return head; }
     public int               numChildren() { return children.length; }
@@ -22,10 +22,10 @@ public class Tree<T>
     public Iterator<Tree<T>> iterator()    { return new ArrayIterator(children); }
     public Tree<T>           child(int i)  { return children[i]; }
 
     public Iterator<Tree<T>> iterator()    { return new ArrayIterator(children); }
     public Tree<T>           child(int i)  { return children[i]; }
 
-    public Input.Location    getLocation() { return location; }
+    public Input.Region    getRegion() { return location; }
 
 
-    public Tree(Input.Location loc, T head)                   { this(loc, head, null); }
-    public Tree(Input.Location loc, T head, Tree<T>[] children) {
+    public Tree(Input.Region loc, T head)                   { this(loc, head, null); }
+    public Tree(Input.Region loc, T head, Tree<T>[] children) {
         this.location = loc;
         this.head = head;
 
         this.location = loc;
         this.head = head;
 
@@ -36,13 +36,14 @@ public class Tree<T>
 
     protected String headToString() { return head==null?null:head.toString(); }
     protected String headToJava()   {
 
     protected String headToString() { return head==null?null:head.toString(); }
     protected String headToJava()   {
+      // FIXME
         if (head==null) return null;
         if (head instanceof ToJava) {
             StringBuffer sb = new StringBuffer();
             ((ToJava)head).toJava(sb);
             return sb.toString();
         }
         if (head==null) return null;
         if (head instanceof ToJava) {
             StringBuffer sb = new StringBuffer();
             ((ToJava)head).toJava(sb);
             return sb.toString();
         }
-        return head==null?"null":("\""+StringUtil.toJavaString(head.toString())+"\"");
+        return (head==null?"null":("\""+StringUtil.toJavaString(head.toString())+"\""));
     }
     protected String left()   { return "{"; }
     protected String right()  { return "}"; }
     }
     protected String left()   { return "{"; }
     protected String right()  { return "}"; }
@@ -59,7 +60,7 @@ public class Tree<T>
     public boolean isTransparent() { return false; }
     public boolean isHidden() { return false; }
 
     public boolean isTransparent() { return false; }
     public boolean isHidden() { return false; }
 
-    public static interface TreeFunctor<T,R> extends Functor<Iterable<Tree<T>>, R> {
+    public static interface TreeFunctor<T,R> extends Functor<Tree<T>, R> {
     }
 
     public static class ArrayBuildingTreeFunctor<T> implements TreeFunctor<T,T[]>, ToJava {
     }
 
     public static class ArrayBuildingTreeFunctor<T> implements TreeFunctor<T,T[]>, ToJava {
@@ -67,11 +68,11 @@ public class Tree<T>
         public void toJava(StringBuffer sb) { sb.append("new Tree.ArrayBuildingTreeFunctor()"); }
         public String toString() { return ""; }
         
         public void toJava(StringBuffer sb) { sb.append("new Tree.ArrayBuildingTreeFunctor()"); }
         public String toString() { return ""; }
         
-        public T[] invoke(Iterable<Tree<T>> t) {
+        public T[] invoke(Tree<T> t) {
             ArrayList ret = new ArrayList();
             for(Tree tc : t) {
                 if (tc.head() != null && tc.head() instanceof Functor)
             ArrayList ret = new ArrayList();
             for(Tree tc : t) {
                 if (tc.head() != null && tc.head() instanceof Functor)
-                    ret.add(((Functor<Iterable<Tree>,Object>)tc.head()).invoke(tc.children()));
+                    ret.add(((Functor<Iterable<Tree>,Object>)tc.head()).invoke(tc));
                 else if (tc.numChildren() == 0)
                     ret.add(tc.head());
                 else {
                 else if (tc.numChildren() == 0)
                     ret.add(tc.head());
                 else {
index 0e2da13..4a10187 100644 (file)
@@ -20,6 +20,7 @@ public abstract class Bindable implements ToJava {
 
     public abstract Annotation[][] getArgAnnotations();
     public abstract String[]       getArgNames();
 
     public abstract Annotation[][] getArgAnnotations();
     public abstract String[]       getArgNames();
+    public abstract Class[]        getArgTypes();
 
     public static Bindable create(Object o) {
         if (o instanceof Class) return new BindableClass((Class)o);
 
     public static Bindable create(Object o) {
         if (o instanceof Class) return new BindableClass((Class)o);
@@ -37,6 +38,7 @@ public abstract class Bindable implements ToJava {
         public Object impose(Object[] fields) { return Reflection.impose(_method, fields); }
         public Annotation[][] getArgAnnotations() { return _method.getParameterAnnotations(); }
         public String[]       getArgNames() { return new String[_method.getParameterTypes().length]; }
         public Object impose(Object[] fields) { return Reflection.impose(_method, fields); }
         public Annotation[][] getArgAnnotations() { return _method.getParameterAnnotations(); }
         public String[]       getArgNames() { return new String[_method.getParameterTypes().length]; }
+        public Class[]        getArgTypes() { return _method.getParameterTypes(); }
         public void toJava(StringBuffer sb) {
             sb.append("Bindable.create(");
             sb.append(_method.getDeclaringClass().getName().replace('$','.'));
         public void toJava(StringBuffer sb) {
             sb.append("Bindable.create(");
             sb.append(_method.getDeclaringClass().getName().replace('$','.'));
@@ -57,6 +59,7 @@ public abstract class Bindable implements ToJava {
         public Object impose(Object[] fields) { return Reflection.impose(_constructor, fields); }
         public Annotation[][] getArgAnnotations() { return _constructor.getParameterAnnotations(); }
         public String[]       getArgNames() { return new String[_constructor.getParameterTypes().length]; }
         public Object impose(Object[] fields) { return Reflection.impose(_constructor, fields); }
         public Annotation[][] getArgAnnotations() { return _constructor.getParameterAnnotations(); }
         public String[]       getArgNames() { return new String[_constructor.getParameterTypes().length]; }
+        public Class[]        getArgTypes() { return _constructor.getParameterTypes(); }
         public void toJava(StringBuffer sb) {
             sb.append("Bindable.create(");
             sb.append(_constructor.getDeclaringClass().getName().replace('$','.'));
         public void toJava(StringBuffer sb) {
             sb.append("Bindable.create(");
             sb.append(_constructor.getDeclaringClass().getName().replace('$','.'));
@@ -87,6 +90,13 @@ public abstract class Bindable implements ToJava {
                 ret[i] = fields[i].getName();
             return ret;
         }
                 ret[i] = fields[i].getName();
             return ret;
         }
+        public Class[]       getArgTypes() {
+            Field[] fields = _class.getFields();
+            Class[] ret = new Class[fields.length];
+            for(int i=0; i<fields.length; i++)
+                ret[i] = fields[i].getType();
+            return ret;
+        }
         public void toJava(StringBuffer sb) {
             sb.append("Bindable.create(");
             sb.append(_class.getName().replace('$','.'));
         public void toJava(StringBuffer sb) {
             sb.append("Bindable.create(");
             sb.append(_class.getName().replace('$','.'));
index b3ba6cb..0a05011 100644 (file)
@@ -15,8 +15,8 @@ public class CharParser extends Parser<Character,String> {
 
     public CharParser(Union u) { super(u, new CharTopology()); }
 
 
     public CharParser(Union u) { super(u, new CharTopology()); }
 
-    public Forest<String> shiftToken(Character ct, Location loc) {
-        return Forest.create(loc, ct.toString(), null, false, false, null);
+    public Forest<String> shiftToken(Location oldloc, Character ct, Location newloc) {
+        return Forest.create(new Input.Region(oldloc, newloc), ct.toString(), null, false, false, null);
     }
 
 }
     }
 
 }
index acda011..78c4b72 100644 (file)
@@ -75,16 +75,17 @@ public class MetaGrammar {
     public static class Target {
         public int[] buildSequence(Production p) {
             Annotation[][] annotations = _bindable.getArgAnnotations();
     public static class Target {
         public int[] buildSequence(Production p) {
             Annotation[][] annotations = _bindable.getArgAnnotations();
+            Class[]        types       = _bindable.getArgTypes();
             String[]       names       = _bindable.getArgNames();
             String[]       names       = _bindable.getArgNames();
-            String name = _bindable.getSimpleName();
-            int len = annotations.length;
-            int ofs = 0;
+            String name                = _bindable.getSimpleName();
+            int len                    = annotations.length;
+            int ofs                    = 0;
             bind.arg[] argtags  = new bind.arg[len];
             for(int i=0; i<names.length; i++)
                 for(Annotation a : annotations[i+ofs])
                     if (a instanceof bind.arg)
                         argtags[i+ofs] = (bind.arg)a;
             bind.arg[] argtags  = new bind.arg[len];
             for(int i=0; i<names.length; i++)
                 for(Annotation a : annotations[i+ofs])
                     if (a instanceof bind.arg)
                         argtags[i+ofs] = (bind.arg)a;
-            return Target.this.buildSequence(p, names, argtags);
+            return Target.this.buildSequence(p, names, argtags, types);
         }
         private Bindable _bindable;
 
         }
         private Bindable _bindable;
 
@@ -123,17 +124,23 @@ public class MetaGrammar {
             return false;
         }
 
             return false;
         }
 
-        public int[] buildSequence(Production p, String[] names, bind.arg[] argtags) {
+        public int[] buildSequence(Production p, String[] names, bind.arg[] argtags, Class[] types) {
             int argTagged = 0;
             int argTagged = 0;
-            for(int i=0; i<argtags.length; i++)
+            boolean hasloc = types.length>0 && types[0]==Input.Region.class;
+            for(int i=0; i<argtags.length; i++) {
+                if (i==0 && types[0]==Input.Region.class) continue;
                 if (argtags[i] != null)
                     argTagged++;
                 if (argtags[i] != null)
                     argTagged++;
+            }
+            int numNames = names.length;
+            if (hasloc) numNames--;
 
             // FIXME: can be smarter here
             if (argTagged==p.count) {
                 int[] ret = new int[argtags.length];
                 int j = 0;
                 for(int i=0; i<argtags.length; i++) {
 
             // FIXME: can be smarter here
             if (argTagged==p.count) {
                 int[] ret = new int[argtags.length];
                 int j = 0;
                 for(int i=0; i<argtags.length; i++) {
+                    if (i==0 && types[0]==Input.Region.class) continue;
                     if (argtags[i]==null) continue;
                     if (argtags[i].value().equals(""))
                         ret[i] = j++;
                     if (argtags[i]==null) continue;
                     if (argtags[i].value().equals(""))
                         ret[i] = j++;
@@ -148,9 +155,9 @@ public class MetaGrammar {
                     }
                 }
                 return ret;
                     }
                 }
                 return ret;
-            } else if (names.length==p.count) {
+            } else if (numNames==p.count) {
                 int[] ret = new int[p.count];
                 int[] ret = new int[p.count];
-                for(int i=0; i<p.count; i++) ret[i] = i;
+                for(int i=0; i<p.count; i++) ret[i] = i+(hasloc?1:0);
                 return ret;
             } else {
                 return null;
                 return ret;
             } else {
                 return null;
@@ -184,12 +191,12 @@ public class MetaGrammar {
             this._israw = raw;
         }
         public String toString() { return "reducer-"+_bindable.toString(); }
             this._israw = raw;
         }
         public String toString() { return "reducer-"+_bindable.toString(); }
-        public Object invoke(Iterable<Tree<Object>> t) {
+        public Object invoke(Tree<Object> t) {
             if (_israw) return _bindable.impose(new Object[] { t });
             ArrayList ret = new ArrayList();
             for(Tree tc : t) {
                 if (tc.head() != null && tc.head() instanceof Functor)
             if (_israw) return _bindable.impose(new Object[] { t });
             ArrayList ret = new ArrayList();
             for(Tree tc : t) {
                 if (tc.head() != null && tc.head() instanceof Functor)
-                    ret.add(((Tree.TreeFunctor<Object,Object>)tc.head()).invoke(tc.children()));
+                    ret.add(((Tree.TreeFunctor<Object,Object>)tc.head()).invoke(tc));
                 else if (tc.numChildren() == 0)
                     ret.add(tc.head());
                 else {
                 else if (tc.numChildren() == 0)
                     ret.add(tc.head());
                 else {
@@ -200,8 +207,14 @@ public class MetaGrammar {
             Object[] o = (Object[])ret.toArray(new Object[0]);
             int max = 0;
             for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
             Object[] o = (Object[])ret.toArray(new Object[0]);
             int max = 0;
             for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
+            if (_bindable.getArgTypes().length > 0 &&
+                _bindable.getArgTypes()[0]==Input.Region.class)
+                max++;
             Object[] o2 = new Object[max+1];
             for(int i=0; i<o.length; i++) o2[map[i]] = o[i];
             Object[] o2 = new Object[max+1];
             for(int i=0; i<o.length; i++) o2[map[i]] = o[i];
+            if (_bindable.getArgTypes().length > 0 &&
+                _bindable.getArgTypes()[0]==Input.Region.class)
+                o2[0] = t.getRegion();
             return _bindable.impose(o2);
         }
     }
             return _bindable.impose(o2);
         }
     }
@@ -211,7 +224,7 @@ public class MetaGrammar {
     public static Union make(Tree t, String s) { return make(t, s, new AnnotationGrammarBindingResolver(MetaGrammarBindings.class)); }
     public static Union make(Tree t, String s, GrammarBindingResolver rm) {
         Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
     public static Union make(Tree t, String s) { return make(t, s, new AnnotationGrammarBindingResolver(MetaGrammarBindings.class)); }
     public static Union make(Tree t, String s, GrammarBindingResolver rm) {
         Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
-        MetaGrammarBindings.GrammarNode g = (MetaGrammarBindings.GrammarNode)red.invoke(t.children());
+        MetaGrammarBindings.GrammarNode g = (MetaGrammarBindings.GrammarNode)red.invoke(t);
         return g.build(s, rm);
     }
 
         return g.build(s, rm);
     }
 
index b70ddb8..1d5eeb7 100644 (file)
@@ -366,7 +366,7 @@ public class MetaGrammarBindings {
         public Context(Tree t, GrammarBindingResolver rm) {
             this.rm = rm;
             Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
         public Context(Tree t, GrammarBindingResolver rm) {
             this.rm = rm;
             Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
-            this.grammar = (GrammarNode)red.invoke(t.children());
+            this.grammar = (GrammarNode)red.invoke(t);
         }
         public Union peek(String name) { return map.get(name); }
         public void  put(String name, Union u) { map.put(name, u); }
         }
         public Union peek(String name) { return map.get(name); }
         public void  put(String name, Union u) { map.put(name, u); }
index 6627fbf..676a4cf 100644 (file)
@@ -51,7 +51,7 @@ public class Cartesian {
         public int getRow() { return row; }
         public Location(int col, int row) { this.row = row; this.col = col; }
         public int compareTo(Input.Location loc) throws ClassCastException {
         public int getRow() { return row; }
         public Location(int col, int row) { this.row = row; this.col = col; }
         public int compareTo(Input.Location loc) throws ClassCastException {
-            if (!(loc instanceof Cartesian)) throw new ClassCastException();
+            if (!(loc instanceof Cartesian.Location)) throw new ClassCastException(loc.getClass().getName());
             Location<Tok> c = (Location<Tok>)loc;
             if (row < c.row) return -1;
             if (row > c.row) return  1;
             Location<Tok> c = (Location<Tok>)loc;
             if (row < c.row) return -1;
             if (row > c.row) return  1;
index 0bafd76..566e05d 100644 (file)
@@ -48,7 +48,7 @@ public class Demo {
 
         // below is ugly voodoo which will go away very soon.  ignore it.
         Tree.TreeFunctor tf = (Tree.TreeFunctor)tree.head();
 
         // below is ugly voodoo which will go away very soon.  ignore it.
         Tree.TreeFunctor tf = (Tree.TreeFunctor)tree.head();
-        Math.Expr e = (Math.Expr)tf.invoke(tree.children());
+        Math.Expr e = (Math.Expr)tf.invoke(tree);
         // above is ugly voodoo which will go away very soon.  ignore it.
 
         System.out.println("done!");
         // above is ugly voodoo which will go away very soon.  ignore it.
 
         System.out.println("done!");
index c91a369..e4aa179 100644 (file)
@@ -62,7 +62,7 @@ public class RegressionTests {
             System.err.println("expanding...");
 
             Tree t = r2.expand1();
             System.err.println("expanding...");
 
             Tree t = r2.expand1();
-            TestCase[] expanded = (TestCase[])((Functor)t.head()).invoke(t.children());
+            TestCase[] expanded = (TestCase[])((Functor)t.head()).invoke(t);
             System.err.println("executing...");
             for(TestCase tc : expanded) {
                 tc.execute();
             System.err.println("executing...");
             for(TestCase tc : expanded) {
                 tc.execute();
index fbdd107..9fae34f 100644 (file)
@@ -7,8 +7,8 @@ public abstract class TreeBuilder<T> implements Invokable<Forest.Body<T>,Boolean
     public boolean toss;
     protected T head;
     public TreeBuilder(boolean toss) { this.toss = toss; }
     public boolean toss;
     protected T head;
     public TreeBuilder(boolean toss) { this.toss = toss; }
-    public abstract void start(T head, Input.Location loc);
-    public abstract void finish(T head, Input.Location loc);
+    public abstract void start(T head, Input.Region loc);
+    public abstract void finish(T head, Input.Region loc);
     public abstract void addTree(Tree<T> t);
     public void invoke(Forest.Body<T> bod, Boolean o, Integer i) {
         if (i==null) {
     public abstract void addTree(Tree<T> t);
     public void invoke(Forest.Body<T> bod, Boolean o, Integer i) {
         if (i==null) {