7a2df1aee721d512ea2acce0f85bb1c52b7cdadd
[sbp.git] / src / edu / berkeley / sbp / Input.java
1 // (C) 2006-2007 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import java.io.*;
5 import java.util.*;
6 import edu.berkeley.sbp.util.*;
7
8 // FEATURE: Region implements Topology<Location<Tok>>
9
10 /** <font color=purple>a stream of <tt>Token</tt>s to be parsed</font> */
11 public interface Input<Token> {
12
13     /** the current location within the input stream */
14     public Location<Token> getLocation();
15
16     /** returns the token just beyond the current location and advances beyond it */
17     public Token next() throws IOException;
18    
19     /** a short string describing where the input is coming from, such as a filename */
20     public String getName();
21
22     /**
23      *  <b>Optional:</b> <i>If possible</i>, this method will return a
24      *  rendering of the input region (for example, if the input is a
25      *  region of characters, it would be those characters) --
26      *  otherwise, returns null.  In any case, the string returned
27      *  will be no more than <tt>maxLength</tt> characters long;
28      *  typically ellipses will be inserted to perform truncation.
29      */
30     public abstract String showRegion(Region<Token> r, int maxLength);
31
32     /** <font color=purple>a location (position) in the input stream <i>between tokens</i></font> */
33     public static interface Location<Token> extends Comparable<Location> {
34
35         /** return the region between this location and <tt>loc</tt> */
36         public Region<Token> createRegion(Location<Token> loc);
37
38         public String toString();
39
40         /** the location following this one */
41         public Location next();
42
43         /** the location preceding this one */
44         public Location prev();
45     }
46
47     /** <font color=purple>a contiguous set of <tt>Location</tt>s</font> */
48     public static interface Region<Token> {
49
50         /** should return less than 80 chars if possible */
51         public abstract String toString();
52         
53         /** The location of the start of this region */
54         public abstract Location<Token> getStart();
55
56         /** The location of the end of this region */
57         public abstract Location<Token> getEnd();
58
59     }
60
61 }
62
63