checkpoint
[sbp.git] / src / edu / berkeley / sbp / Input.java
1 package edu.berkeley.sbp;
2 import java.io.*;
3 import java.util.*;
4 import java.lang.reflect.*;
5 import java.lang.ref.*;
6 import edu.berkeley.sbp.*;
7 import edu.berkeley.sbp.util.*;
8
9 /** a stream of tokens to be parsed */
10 public interface Input<Token> {
11
12     /** returns the token just beyond the current location and advances beyond it */
13     public Token           next() throws IOException;
14
15     /** returns the location the input stream is currently at */
16     public Location<Token> getLocation();
17
18     /** a location <i>between tokens<i> in the input stream */
19     public static interface Location<Tok> extends Comparable<Location> {
20         public String toString();
21
22         /** an implementation of Location for a cartesian grid (row, col) */
23         public static class Cartesian<Tok> implements Location<Tok>, Comparable<Location> {
24             protected final int row;
25             protected final int col;
26             public String toString() { return row+":"+col; }
27             public int getCol() { return col; }
28             public int getRow() { return row; }
29             public Cartesian(int col, int row) { this.row = row; this.col = col; }
30             public int compareTo(Location loc) throws ClassCastException {
31                 if (!(loc instanceof Cartesian)) throw new ClassCastException();
32                 Cartesian<Tok> c = (Cartesian<Tok>)loc;
33                 if (row < c.row) return -1;
34                 if (row > c.row) return  1;
35                 if (col < c.col) return -1;
36                 if (col > c.col) return  1;
37                 return 0;
38             }
39         }
40     }
41
42     public static class Region<Loc extends Location> /* implements Topology<Location<Tok>> */ {
43         public final Loc start;
44         public final Loc end;
45         public Region(Loc a, Loc b) {
46             switch(a.compareTo(b)) {
47                 case -1:
48                 case  0: start=a; end=b; return;
49                 case  1: start=b; end=a; return;
50                 default: throw new Error("impossible");
51             }
52         }
53     }
54 }
55
56