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