checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / CartesianInput.java
diff --git a/src/edu/berkeley/sbp/misc/CartesianInput.java b/src/edu/berkeley/sbp/misc/CartesianInput.java
new file mode 100644 (file)
index 0000000..cf477cc
--- /dev/null
@@ -0,0 +1,55 @@
+package edu.berkeley.sbp.misc;
+import java.io.*;
+import java.util.*;
+import java.lang.reflect.*;
+import java.lang.ref.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.Token.Location;
+import edu.berkeley.sbp.util.*;
+
+public abstract class CartesianInput<Tok> implements Token.Stream<Tok> {
+
+    private int line  = 1;
+    private int col   = 0;
+
+    public abstract Tok     next() throws IOException;
+    public abstract boolean isCR();
+
+    long then = 0;
+    private Token.Location location = new LocWrap(line, col);
+    public Token.Location getLocation() { return location; }
+    public Tok next(int numstates, int resets, int waits) throws IOException {
+        Tok 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 + "]";
+        long now = System.currentTimeMillis();
+        if (now-then > 10) {
+            then = now;
+            System.out.print(s + "                                \r");
+        }
+        if (isCR()) { 
+            line++;
+            col = 1;
+        } else {
+            col++;
+        }
+        location = new LocWrap(line, col);
+        return t;
+    }
+
+    public static class Location implements Token.Location {
+        public final int line;
+        public final int col;
+        public String toString()            { return line + ":" + col; }
+        public Location(int line, int col)  { this.line = line; this.col = col; }
+    }
+
+    private class LocWrap implements Token.Location {
+        public final int line;
+        public final int col;
+        public String toString()            { return line + ":" + col; }
+        public LocWrap(int line, int col) { this.line = line; this.col = col; }
+    }
+}