X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FInput.java;h=bf0d50deb2e02f14ec73b0e88d1f9e1f6d0b3604;hp=be41cbebeff1af501c5a65d25c5318a14f1210e9;hb=449c39e0dafd7c736bfcd8d56bbd08b7a99e25a4;hpb=ac3843911c47a601ffd679d2e075b519d3a18d6a diff --git a/src/edu/berkeley/sbp/Input.java b/src/edu/berkeley/sbp/Input.java index be41cbe..bf0d50d 100644 --- a/src/edu/berkeley/sbp/Input.java +++ b/src/edu/berkeley/sbp/Input.java @@ -1,3 +1,5 @@ +// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license + package edu.berkeley.sbp; import java.io.*; import java.util.*; @@ -6,7 +8,7 @@ import java.lang.ref.*; import edu.berkeley.sbp.*; import edu.berkeley.sbp.util.*; -/** a stream of tokens to be parsed */ +/** a stream of Tokens to be parsed */ public interface Input { /** returns the token just beyond the current location and advances beyond it */ @@ -14,43 +16,52 @@ public interface Input { /** returns the location the input stream is currently at */ public Location getLocation(); + + /** should return a short string describing where the input is coming from */ + public String getName(); + + /** + * Optional: If possible, this method will return a + * rendering of the input region (for example, if the input is a + * region of characters, it would be those characters) -- + * otherwise, returns null. In any case, the string returned + * will be no more than maxLength characters long; + * typically ellipses will be inserted to perform truncation. + */ + public abstract String showRegion(Region r, int maxLength); + + /** a location (position) in the input stream -- between tokens */ + public static interface Location extends Comparable { + + /** return the region between this location and loc */ + public Region createRegion(Location loc); - /** a location between tokens in the input stream */ - public static interface Location extends Comparable { public String toString(); - /** an implementation of Location for a cartesian grid (row, col) */ - public static class Cartesian implements Location, Comparable { - protected final int row; - protected final int col; - public String toString() { return row+":"+col; } - public int getCol() { return col; } - public int getRow() { return row; } - public Cartesian(int col, int row) { this.row = row; this.col = col; } - public int compareTo(Location loc) throws ClassCastException { - if (!(loc instanceof Cartesian)) throw new ClassCastException(); - Cartesian c = (Cartesian)loc; - if (row < c.row) return -1; - if (row > c.row) return 1; - if (col < c.col) return -1; - if (col > c.col) return 1; - return 0; - } - } + /** the location following this one */ + public Location next(); + + /** the location preceding this one */ + public Location prev(); } - public static class Region /* implements Topology> */ { - public final Loc start; - public final Loc end; - public Region(Loc a, Loc b) { - switch(a.compareTo(b)) { - case -1: - case 0: start=a; end=b; return; - case 1: start=b; end=a; return; - default: throw new Error("impossible"); - } - } + /** a contiguous set of Locations */ + public static interface Region /* implements Topology> */ { + + /** + * the toString() method of Region should return a <80char + * "rendition" of the input region, if possible + */ + public abstract String toString(); + + /** The location of the start of this region */ + public abstract Location getStart(); + + /** The location of the end of this region */ + public abstract Location getEnd(); + } + }