2c3255358f6119e269918b7a12d4909f8dcb77fb
[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();
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             int scalar = location.getScalar();
27             Token t = _next();
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();
33             if (now-then > 10) {
34                 then = now;
35                 System.out.print(s + "                                \r");
36             }
37             if (isCR()) { 
38                 line++;
39                 col = 1;
40             } else {
41                 col++;
42             }
43             location.next = new Cartesian.Location(col, line, scalar+1);
44             location.next.prev = location;
45             location = location.next;
46             return t;
47         }
48
49         public String showRegion(Input.Region<Token> region) {
50             return null;
51         }
52     }
53
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;
76             return 0;
77         }
78         public Input.Region<Tok> createRegion(Input.Location<Tok> loc) {
79             return new Region<Tok>(this, (Cartesian.Location<Tok>)loc); }
80     }
81
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);
90             return start+"-"+end;
91         }
92         public Region(Location<Tok> a, Location<Tok> b) {
93             switch(a.compareTo(b)) {
94                 case -1:
95                 case  0: start=a; end=b; return;
96                 case  1: start=b; end=a; return;
97                 default: throw new Error("impossible");
98             }
99         }
100     }
101 }