diff src/clojure/lang/LineNumberingPushbackReader.java @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/lang/LineNumberingPushbackReader.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,75 @@
     1.4 +/**
     1.5 + * Copyright (c) Rich Hickey. All rights reserved.
     1.6 + * The use and distribution terms for this software are covered by the
     1.7 + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.8 + * which can be found in the file epl-v10.html at the root of this distribution.
     1.9 + * By using this software in any fashion, you are agreeing to be bound by
    1.10 + * the terms of this license.
    1.11 + * You must not remove this notice, or any other, from this software.
    1.12 + */
    1.13 +
    1.14 +package clojure.lang;
    1.15 +
    1.16 +import java.io.PushbackReader;
    1.17 +import java.io.Reader;
    1.18 +import java.io.LineNumberReader;
    1.19 +import java.io.IOException;
    1.20 +
    1.21 +
    1.22 +public class LineNumberingPushbackReader extends PushbackReader{
    1.23 +
    1.24 +// This class is a PushbackReader that wraps a LineNumberReader. The code
    1.25 +// here to handle line terminators only mentions '\n' because
    1.26 +// LineNumberReader collapses all occurrences of CR, LF, and CRLF into a
    1.27 +// single '\n'.
    1.28 +
    1.29 +private static final int newline = (int) '\n';
    1.30 +
    1.31 +private boolean _atLineStart = true;
    1.32 +private boolean _prev;
    1.33 +
    1.34 +public LineNumberingPushbackReader(Reader r){
    1.35 +	super(new LineNumberReader(r));
    1.36 +}
    1.37 +
    1.38 +public int getLineNumber(){
    1.39 +	return ((LineNumberReader) in).getLineNumber() + 1;
    1.40 +}
    1.41 +
    1.42 +public int read() throws IOException{
    1.43 +    int c = super.read();
    1.44 +    _prev = _atLineStart;
    1.45 +    _atLineStart = (c == newline) || (c == -1);
    1.46 +    return c;
    1.47 +}
    1.48 +
    1.49 +public void unread(int c) throws IOException{
    1.50 +    super.unread(c);
    1.51 +    _atLineStart = _prev;
    1.52 +}
    1.53 +
    1.54 +public String readLine() throws IOException{
    1.55 +    int c = read();
    1.56 +    String line;
    1.57 +    switch (c) {
    1.58 +    case -1:
    1.59 +        line = null;
    1.60 +        break;
    1.61 +    case newline:
    1.62 +        line = "";
    1.63 +        break;
    1.64 +    default:
    1.65 +        String first = String.valueOf((char) c);
    1.66 +        String rest = ((LineNumberReader)in).readLine();
    1.67 +        line = (rest == null) ? first : first + rest;
    1.68 +        _prev = false;
    1.69 +        _atLineStart = true;
    1.70 +        break;
    1.71 +    }
    1.72 +    return line;
    1.73 +}
    1.74 +
    1.75 +public boolean atLineStart(){
    1.76 +    return _atLineStart;
    1.77 +}
    1.78 +}