X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FCartesianInput.java;fp=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FCartesianInput.java;h=cf477ccc105bdafe3500f568049f3cb6c8f3aa65;hb=1177435c53540af2247a2fbd7b6fbcf059accbb8;hp=0000000000000000000000000000000000000000;hpb=d684010debf9069af108981d9451932c15b1ec75;p=sbp.git 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; } + } +}