corrected the reporting+alignment of error locations
[sbp.git] / src / edu / berkeley / sbp / misc / Cartesian.java
index 6627fbf..5665019 100644 (file)
@@ -11,21 +11,21 @@ public class Cartesian {
 
     public static abstract class Input<Token> implements edu.berkeley.sbp.Input<Token> {
 
-        public abstract Token   next() throws IOException;
+        public abstract Token   _next() throws IOException;
         public abstract boolean isCR();
 
         long then = 0;
         private Cartesian.Location location = new Cartesian.Location(0, 1);
         public  edu.berkeley.sbp.Input.Location    getLocation() { return location; }
 
-        public Token next(int numstates, int resets, int waits) throws IOException {
+        public Token next() throws IOException {
             int line  = location.getRow();
             int col   = location.getCol();
-            Token t = next();
+            Token t = _next();
             if (t==null) return null;
             String s = "  line "+line+", col " + col;
             while(s.length() < 20) s += " ";
-            s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]";
+            //s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]";
             long now = System.currentTimeMillis();
             if (now-then > 10) {
                 then = now;
@@ -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 {
-            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;
@@ -59,5 +59,24 @@ public class Cartesian {
             if (col > c.col) return  1;
             return 0;
         }
+        public Input.Region<Tok> createRegion(Input.Location<Tok> loc) {
+            return new Region<Tok>(this, (Cartesian.Location<Tok>)loc); }
+    }
+
+    public static class Region<Tok> implements Input.Region<Tok> {
+        public final Location<Tok> start;
+        public final Location<Tok> end;
+        public String toString() {
+            if (start.row==end.row) return start.row+":"+(start.col+"-"+end.col);
+            return start+"-"+end;
+        }
+        public Region(Location<Tok> a, Location<Tok> b) {
+            switch(a.compareTo(b)) {
+                case -1:
+                case  0: start=a; end=b; return;
+                case  1: start=b; end=a; return;
+                default: throw new Error("impossible");
+            }
+        }
     }
 }