Mercurial > lasercutter
comparison 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 |
comparison
equal
deleted
inserted
replaced
9:35cf337adfcf | 10:ef7dbbd6452c |
---|---|
1 /** | |
2 * Copyright (c) Rich Hickey. All rights reserved. | |
3 * The use and distribution terms for this software are covered by the | |
4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | |
5 * which can be found in the file epl-v10.html at the root of this distribution. | |
6 * By using this software in any fashion, you are agreeing to be bound by | |
7 * the terms of this license. | |
8 * You must not remove this notice, or any other, from this software. | |
9 */ | |
10 | |
11 package clojure.lang; | |
12 | |
13 import java.io.PushbackReader; | |
14 import java.io.Reader; | |
15 import java.io.LineNumberReader; | |
16 import java.io.IOException; | |
17 | |
18 | |
19 public class LineNumberingPushbackReader extends PushbackReader{ | |
20 | |
21 // This class is a PushbackReader that wraps a LineNumberReader. The code | |
22 // here to handle line terminators only mentions '\n' because | |
23 // LineNumberReader collapses all occurrences of CR, LF, and CRLF into a | |
24 // single '\n'. | |
25 | |
26 private static final int newline = (int) '\n'; | |
27 | |
28 private boolean _atLineStart = true; | |
29 private boolean _prev; | |
30 | |
31 public LineNumberingPushbackReader(Reader r){ | |
32 super(new LineNumberReader(r)); | |
33 } | |
34 | |
35 public int getLineNumber(){ | |
36 return ((LineNumberReader) in).getLineNumber() + 1; | |
37 } | |
38 | |
39 public int read() throws IOException{ | |
40 int c = super.read(); | |
41 _prev = _atLineStart; | |
42 _atLineStart = (c == newline) || (c == -1); | |
43 return c; | |
44 } | |
45 | |
46 public void unread(int c) throws IOException{ | |
47 super.unread(c); | |
48 _atLineStart = _prev; | |
49 } | |
50 | |
51 public String readLine() throws IOException{ | |
52 int c = read(); | |
53 String line; | |
54 switch (c) { | |
55 case -1: | |
56 line = null; | |
57 break; | |
58 case newline: | |
59 line = ""; | |
60 break; | |
61 default: | |
62 String first = String.valueOf((char) c); | |
63 String rest = ((LineNumberReader)in).readLine(); | |
64 line = (rest == null) ? first : first + rest; | |
65 _prev = false; | |
66 _atLineStart = true; | |
67 break; | |
68 } | |
69 return line; | |
70 } | |
71 | |
72 public boolean atLineStart(){ | |
73 return _atLineStart; | |
74 } | |
75 } |