copyright notices/updates
[sbp.git] / src / edu / berkeley / sbp / misc / Cartesian.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp.misc;
4 import java.io.*;
5 import java.util.*;
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.*;
11
12 public class Cartesian {
13
14     public static abstract class Input<Token> implements edu.berkeley.sbp.Input<Token> {
15
16         public abstract Token   _next() throws IOException;
17         public abstract boolean isCR();
18
19         long then = 0;
20         private Cartesian.Location location = new Cartesian.Location(0, 1);
21         public  edu.berkeley.sbp.Input.Location    getLocation() { return location; }
22
23         public Token next() throws IOException {
24             int line  = location.getRow();
25             int col   = location.getCol();
26             Token t = _next();
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();
32             if (now-then > 10) {
33                 then = now;
34                 System.out.print(s + "                                \r");
35             }
36             if (isCR()) { 
37                 line++;
38                 col = 1;
39             } else {
40                 col++;
41             }
42             location = new Cartesian.Location(col, line);
43             return t;
44         }
45     }
46
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;
62             return 0;
63         }
64         public Input.Region<Tok> createRegion(Input.Location<Tok> loc) {
65             return new Region<Tok>(this, (Cartesian.Location<Tok>)loc); }
66     }
67
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);
73             return start+"-"+end;
74         }
75         public Region(Location<Tok> a, Location<Tok> b) {
76             switch(a.compareTo(b)) {
77                 case -1:
78                 case  0: start=a; end=b; return;
79                 case  1: start=b; end=a; return;
80                 default: throw new Error("impossible");
81             }
82         }
83     }
84 }