1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp.misc;
6 import java.lang.reflect.*;
7 import java.lang.ref.*;
8 import edu.berkeley.sbp.*;
9 import edu.berkeley.sbp.Input.Location;
10 import edu.berkeley.sbp.util.*;
12 public class Cartesian {
14 public static abstract class Input<Token> implements edu.berkeley.sbp.Input<Token> {
16 public abstract Token _next() throws IOException;
17 public abstract boolean isCR();
20 private Cartesian.Location location = new Cartesian.Location(0, 1);
21 public edu.berkeley.sbp.Input.Location getLocation() { return location; }
23 public Token next() throws IOException {
24 int line = location.getRow();
25 int col = location.getCol();
27 if (t==null) return null;
28 String s = " line "+line+", col " + col;
29 while(s.length() < 20) s += " ";
30 //s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]";
31 long now = System.currentTimeMillis();
34 System.out.print(s + " \r");
42 location = new Cartesian.Location(col, line);
47 /** an implementation of Location for a cartesian grid (row, col) */
48 public static class Location<Tok> implements Input.Location<Tok>, Comparable<Input.Location> {
49 protected final int row;
50 protected final int col;
51 public String toString() { return row+":"+col; }
52 public int getCol() { return col; }
53 public int getRow() { return row; }
54 public Location(int col, int row) { this.row = row; this.col = col; }
55 public int compareTo(Input.Location loc) throws ClassCastException {
56 if (!(loc instanceof Cartesian.Location)) throw new ClassCastException(loc.getClass().getName());
57 Location<Tok> c = (Location<Tok>)loc;
58 if (row < c.row) return -1;
59 if (row > c.row) return 1;
60 if (col < c.col) return -1;
61 if (col > c.col) return 1;
64 public Input.Region<Tok> createRegion(Input.Location<Tok> loc) {
65 return new Region<Tok>(this, (Cartesian.Location<Tok>)loc); }
68 public static class Region<Tok> implements Input.Region<Tok> {
69 public final Location<Tok> start;
70 public final Location<Tok> end;
71 public String toString() {
72 if (start.row==end.row) return start.row+":"+(start.col+"-"+end.col);
75 public Region(Location<Tok> a, Location<Tok> b) {
76 switch(a.compareTo(b)) {
78 case 0: start=a; end=b; return;
79 case 1: start=b; end=a; return;
80 default: throw new Error("impossible");