From 1177435c53540af2247a2fbd7b6fbcf059accbb8 Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 15 Jan 2006 03:41:00 -0500 Subject: [PATCH] checkpoint darcs-hash:20060115084100-5007d-bb8e223b60a05a4dc1a66afa515a142e42e4d192.gz --- src/edu/berkeley/sbp/Token.java | 1 - src/edu/berkeley/sbp/misc/CartesianInput.java | 55 ++++++++++++++++++ src/edu/berkeley/sbp/misc/CharToken.java | 74 +++---------------------- src/edu/berkeley/sbp/tib/Tib.java | 2 +- 4 files changed, 65 insertions(+), 67 deletions(-) create mode 100644 src/edu/berkeley/sbp/misc/CartesianInput.java diff --git a/src/edu/berkeley/sbp/Token.java b/src/edu/berkeley/sbp/Token.java index 10ab90f..a662e8a 100644 --- a/src/edu/berkeley/sbp/Token.java +++ b/src/edu/berkeley/sbp/Token.java @@ -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 actual input token rather than an Element which matches 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 index 0000000..cf477cc --- /dev/null +++ b/src/edu/berkeley/sbp/misc/CartesianInput.java @@ -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 implements Token.Stream { + + 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; } + } +} diff --git a/src/edu/berkeley/sbp/misc/CharToken.java b/src/edu/berkeley/sbp/misc/CharToken.java index 8efdb04..5d897de 100644 --- a/src/edu/berkeley/sbp/misc/CharToken.java +++ b/src/edu/berkeley/sbp/misc/CharToken.java @@ -10,8 +10,6 @@ import edu.berkeley.sbp.util.*; /** an implementation of Token for streams of Java char values */ public class CharToken implements IntegerMappable { - // Public ////////////////////////////////////////////////////////////////////////////// - public static class CharToStringParser extends Parser { public CharToStringParser(Union u) { super(u, new IntegerTopology()); } public Forest 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 { - private final String message; + public static class Stream extends CartesianInput { 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 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); } } - } diff --git a/src/edu/berkeley/sbp/tib/Tib.java b/src/edu/berkeley/sbp/tib/Tib.java index 401d9c0..881fc88 100644 --- a/src/edu/berkeley/sbp/tib/Tib.java +++ b/src/edu/berkeley/sbp/tib/Tib.java @@ -42,7 +42,7 @@ public class Tib implements Token.Stream { 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; -- 1.7.10.4