checkpoint
authoradam <adam@megacz.com>
Sun, 15 Jan 2006 08:41:00 +0000 (03:41 -0500)
committeradam <adam@megacz.com>
Sun, 15 Jan 2006 08:41:00 +0000 (03:41 -0500)
darcs-hash:20060115084100-5007d-bb8e223b60a05a4dc1a66afa515a142e42e4d192.gz

src/edu/berkeley/sbp/Token.java
src/edu/berkeley/sbp/misc/CartesianInput.java [new file with mode: 0644]
src/edu/berkeley/sbp/misc/CharToken.java
src/edu/berkeley/sbp/tib/Tib.java

index 10ab90f..a662e8a 100644 (file)
@@ -5,7 +5,6 @@ import java.lang.reflect.*;
 import java.lang.ref.*;
 import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.util.*;
-import edu.berkeley.sbp.*;
 
 /** a token of input -- note that this represents an <i>actual input token</i> rather than an <tt>Element</tt> which <i>matches</i> a token */
 public interface Token {
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; }
+    }
+}
index 8efdb04..5d897de 100644 (file)
@@ -10,8 +10,6 @@ import edu.berkeley.sbp.util.*;
 /** an implementation of Token for streams of Java <tt>char</tt> values */
 public class CharToken implements IntegerMappable {
 
-    // Public //////////////////////////////////////////////////////////////////////////////
-
     public static class CharToStringParser extends Parser<CharToken,String> {
         public CharToStringParser(Union u) { super(u, new IntegerTopology<CharToken>()); }
         public Forest<String> shiftToken(CharToken ct, Location loc) {
@@ -106,78 +104,24 @@ public class CharToken implements IntegerMappable {
 
     public int toInt() { return (int)c; }
 
-    // Statics //////////////////////////////////////////////////////////////////////////////
-
-    public static class CartesianLocation implements Location {
-        public final int line;
-        public final int col;
-        public String toString()            { return line + ":" + col; }
-        public CartesianLocation(int line, int col) { this.line = line; this.col = col; }
-    }
-    
-    /** an implementation of Token.Stream for sequences of characters */
-    public static class Stream implements Token.Stream<CharToken> {
-        private final String message;
+    public static class Stream extends CartesianInput<CharToken> {
         private final Reader r;
-        private int line  = 1;
-        private int col   = 1;
-
+        
         public Stream(String s)                { this(new StringReader(s)); }
-
         public Stream(Reader r)                { this(r, null); }
-        public Stream(Reader r,      String s) { this.r = r; this.message = s; }
-
+        public Stream(Reader r,      String s) { this.r = r; }
         public Stream(InputStream i)           { this(i, null); }
         public Stream(InputStream i, String s) { this(new InputStreamReader(i), s); }
 
-        private Line currentLine = new Line();
-        private class Line {
-            public StringBuffer line = new StringBuffer();
-        }
-
-        private class LocWrap implements Location {
-            Line myline = Stream.this.currentLine;
-            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; }
-            public String getContext() {
-                StringBuffer spaces = new StringBuffer();
-                for(int i=0; i<col-1; i++) spaces.append(' ');
-                spaces.append('^');
-                return "  " + myline.line.toString() + "\n  " + spaces.toString();
-            }
-        }
-
-        long then = 0;
-        private Location location = new LocWrap(1, 1);
-        public Location getLocation() { return location; }
-        public CharToken next(int numstates, int resets, int waits) throws IOException {
+        boolean cr = false;
+        public boolean   isCR() { return cr; }
+        public CharToken next() throws IOException {
+            cr = false;
             int i = r.read();
             if (i==-1) return null;
             char c = (char)i;
-            location = new LocWrap(line, col);
-            CharToken ret = new CharToken(c);
-            String s = line + "";
-            while(s.length() < 4) s = " " + s;
-            s = "line "+s+", 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("  "+(message==null?"":message)+" " + s + "                                \r");
-            }
-            if (c=='\n') { 
-                currentLine = new Line();
-                line++;
-                col = 1;
-            } else {
-                currentLine.line.append(c);
-                col++;
-            }
-            return ret;
+            cr = c=='\n';
+            return new CharToken(c);
         }
     }
-
 }
index 401d9c0..881fc88 100644 (file)
@@ -42,7 +42,7 @@ public class Tib implements Token.Stream<CharToken> {
 
     int _row = 1;
     int _col = 0;
-    public Token.Location getLocation() { return new CharToken.CartesianLocation(_row, _col); }
+    public Token.Location getLocation() { return new CartesianInput.Location(_row, _col); }
     private BufferedReader br;
 
     boolean waiting = false;