cf477ccc105bdafe3500f568049f3cb6c8f3aa65
[sbp.git] / src / edu / berkeley / sbp / misc / CartesianInput.java
1 package edu.berkeley.sbp.misc;
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.Token.Location;
8 import edu.berkeley.sbp.util.*;
9
10 public abstract class CartesianInput<Tok> implements Token.Stream<Tok> {
11
12     private int line  = 1;
13     private int col   = 0;
14
15     public abstract Tok     next() throws IOException;
16     public abstract boolean isCR();
17
18     long then = 0;
19     private Token.Location location = new LocWrap(line, col);
20     public Token.Location getLocation() { return location; }
21     public Tok next(int numstates, int resets, int waits) throws IOException {
22         Tok t = next();
23         if (t==null) return null;
24         String s = "  line "+line+", col " + col;
25         while(s.length() < 20) s += " ";
26         s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]";
27         long now = System.currentTimeMillis();
28         if (now-then > 10) {
29             then = now;
30             System.out.print(s + "                                \r");
31         }
32         if (isCR()) { 
33             line++;
34             col = 1;
35         } else {
36             col++;
37         }
38         location = new LocWrap(line, col);
39         return t;
40     }
41
42     public static class Location implements Token.Location {
43         public final int line;
44         public final int col;
45         public String toString()            { return line + ":" + col; }
46         public Location(int line, int col)  { this.line = line; this.col = col; }
47     }
48
49     private class LocWrap implements Token.Location {
50         public final int line;
51         public final int col;
52         public String toString()            { return line + ":" + col; }
53         public LocWrap(int line, int col) { this.line = line; this.col = col; }
54     }
55 }