/** 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) {
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);
}
}
-
}