001: /*
002: * $Id: SourceReader.java,v 1.2.4.1 2007/02/06 18:49:26 scgmille Exp $
003: */
004: package sisc.reader;
005:
006: import java.io.IOException;
007: import java.io.PushbackReader;
008: import java.io.Reader;
009:
010: public class SourceReader extends PushbackReader {
011:
012: public SourceReader(Reader in, String file) {
013: super (in);
014: line = 1;
015: column = 1;
016: sourceFile = file;
017: }
018:
019: public int line, column;
020: public String sourceFile;
021:
022: protected void maintainLineColumn(int c) {
023: if (c == '\n') {
024: line++;
025: column = 1;
026: } else if (c == '\t')
027: column += 8;
028: else
029: column++;
030: }
031:
032: //Does this even work?
033: protected void unmaintainLineColumn(int c) {
034: if (c == '\n') {
035: line--;
036: column = -1;
037: } else if (c == '\t')
038: column -= 8;
039: else
040: column--;
041: }
042:
043: public int read() throws IOException {
044: int c = super .read();
045: maintainLineColumn(c);
046: return c;
047: }
048:
049: public int read(char[] buffer) throws IOException {
050: return read(buffer, 0, buffer.length);
051: }
052:
053: //TODO: Could be made more efficient by not doing character by character scan
054: public int read(char[] buffer, int offset, int length)
055: throws IOException {
056: int count = super .read(buffer, offset, length);
057: for (int i = offset; i < count; i++) {
058: maintainLineColumn(buffer[i]);
059: }
060: return count;
061: }
062:
063: public void unread(int c) throws IOException {
064: super .unread(c);
065: unmaintainLineColumn(c);
066: }
067:
068: public void unread(char[] buffer) throws IOException {
069: unread(buffer, 0, buffer.length);
070: }
071:
072: public void unread(char[] buffer, int offset, int length)
073: throws IOException {
074: super .unread(buffer, offset, length);
075: for (int i = offset; i < length; i++) {
076: unmaintainLineColumn(buffer[i]);
077: }
078: }
079:
080: }
081: /*
082: * The contents of this file are subject to the Mozilla Public
083: * License Version 1.1 (the "License"); you may not use this file
084: * except in compliance with the License. You may obtain a copy of
085: * the License at http://www.mozilla.org/MPL/
086: *
087: * Software distributed under the License is distributed on an "AS
088: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
089: * implied. See the License for the specific language governing
090: * rights and limitations under the License.
091: *
092: * The Original Code is the Second Interpreter of Scheme Code (SISC).
093: *
094: * The Initial Developer of the Original Code is Scott G. Miller.
095: * Portions created by Scott G. Miller are Copyright (C) 2000-2007
096: * Scott G. Miller. All Rights Reserved.
097: *
098: * Contributor(s):
099: * Matthias Radestock
100: *
101: * Alternatively, the contents of this file may be used under the
102: * terms of the GNU General Public License Version 2 or later (the
103: * "GPL"), in which case the provisions of the GPL are applicable
104: * instead of those above. If you wish to allow use of your
105: * version of this file only under the terms of the GPL and not to
106: * allow others to use your version of this file under the MPL,
107: * indicate your decision by deleting the provisions above and
108: * replace them with the notice and other provisions required by
109: * the GPL. If you do not delete the provisions above, a recipient
110: * may use your version of this file under either the MPL or the
111: * GPL.
112: */
|