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();
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();
26 int scalar = location.getScalar();
28 if (t==null) return null;
29 String s = " line "+line+", col " + col;
30 while(s.length() < 20) s += " ";
31 //s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]";
32 long now = System.currentTimeMillis();
35 System.err.print(s + " \r");
43 location.next = new Cartesian.Location(col, line, scalar+1);
44 location.next.prev = location;
45 location = location.next;
49 public String showRegion(Input.Region<Token> region) {
54 /** an implementation of Location for a cartesian grid (row, col) */
55 public static class Location<Tok> implements Input.Location<Tok>, Comparable<Input.Location> {
56 protected final int row;
57 protected final int col;
58 protected final int scalar;
59 Location<Tok> next = null;
60 Location<Tok> prev = null;
61 public Location<Tok> next() { return next; }
62 public Location<Tok> prev() { return prev; }
63 public String toString() { return row+":"+col; }
64 public int getCol() { return col; }
65 public int getRow() { return row; }
66 public int getScalar() { return scalar; }
67 public Location() { this(-1, 1, 0); }
68 public Location(int col, int row, int scalar) { this.row = row; this.col = col; this.scalar = scalar; }
69 public int compareTo(Input.Location loc) throws ClassCastException {
70 if (!(loc instanceof Cartesian.Location)) throw new ClassCastException(loc.getClass().getName());
71 Location<Tok> c = (Location<Tok>)loc;
72 if (row < c.row) return -1;
73 if (row > c.row) return 1;
74 if (col < c.col) return -1;
75 if (col > c.col) return 1;
78 public Input.Region<Tok> createRegion(Input.Location<Tok> loc) {
79 return new Region<Tok>(this, (Cartesian.Location<Tok>)loc); }
82 public static class Region<Tok> implements Input.Region<Tok> {
83 public final Location<Tok> start;
84 public final Location<Tok> end;
85 public Location<Tok> getStart() { return start; }
86 public Location<Tok> getEnd() { return end; }
87 public String toString() {
88 if (start.row==end.row && start.col==end.col) return start+"";
89 if (start.row==end.row) return start.row+":"+(start.col+"-"+end.col);
92 public Region(Location<Tok> a, Location<Tok> b) {
93 switch(a.compareTo(b)) {
95 case 0: start=a; end=b; return;
96 case 1: start=b; end=a; return;
97 default: throw new Error("impossible");