1 package edu.berkeley.sbp.misc;
4 import java.lang.reflect.*;
5 import java.lang.ref.*;
6 import edu.berkeley.sbp.*;
7 import edu.berkeley.sbp.Input.Location;
8 import edu.berkeley.sbp.util.*;
10 public class Cartesian {
12 public static abstract class Input<Token> implements edu.berkeley.sbp.Input<Token> {
14 public abstract Token next() throws IOException;
15 public abstract boolean isCR();
18 private Cartesian.Location location = new Cartesian.Location(0, 1);
19 public edu.berkeley.sbp.Input.Location getLocation() { return location; }
21 public Token next(int numstates, int resets, int waits) throws IOException {
22 int line = location.getRow();
23 int col = location.getCol();
25 if (t==null) return null;
26 String s = " line "+line+", col " + col;
27 while(s.length() < 20) s += " ";
28 s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]";
29 long now = System.currentTimeMillis();
32 System.out.print(s + " \r");
40 location = new Cartesian.Location(col, line);
45 /** an implementation of Location for a cartesian grid (row, col) */
46 public static class Location<Tok> implements Input.Location<Tok>, Comparable<Input.Location> {
47 protected final int row;
48 protected final int col;
49 public String toString() { return row+":"+col; }
50 public int getCol() { return col; }
51 public int getRow() { return row; }
52 public Location(int col, int row) { this.row = row; this.col = col; }
53 public int compareTo(Input.Location loc) throws ClassCastException {
54 if (!(loc instanceof Cartesian.Location)) throw new ClassCastException(loc.getClass().getName());
55 Location<Tok> c = (Location<Tok>)loc;
56 if (row < c.row) return -1;
57 if (row > c.row) return 1;
58 if (col < c.col) return -1;
59 if (col > c.col) return 1;