Skip to content

Commit 5343c13

Browse files
committed
SI-8538 Document extension
Scaladoc for report extension point.
1 parent e320d81 commit 5343c13

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/library/scala/io/Source.scala

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,20 @@ object Source {
169169
createBufferedSource(is, reset = () => fromInputStream(is)(codec), close = () => is.close())(codec)
170170
}
171171

172-
/** The class `Source` implements an iterable representation of source data.
173-
* Calling method `reset` returns an identical, resetted source, where
174-
* possible.
172+
/** An iterable representation of source data.
173+
* It may be reset with the optional `reset` method.
174+
*
175+
* Subclasses must supply [[scala.io.Source@iter the underlying iterator]].
176+
*
177+
* Error handling may be customized by overriding the [[scala.io.Source@report report]] method.
178+
*
179+
* The [[scala.io.Source@ch current input]] and [[scala.io.Source@pos position]],
180+
* as well as the [[scala.io.Source@next next character]] methods delegate to
181+
* [[scala.io.Source$Positioner the positioner]].
182+
*
183+
* The default positioner encodes line and column numbers in the position passed to `report`.
184+
* This behavior can be changed by supplying a
185+
* [[scala.io.Source@withPositioning(pos:Source.this.Positioner):Source.this.type custom positioner]].
175186
*
176187
* @author Burak Emir
177188
* @version 1.0

test/junit/scala/io/SourceTest.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,34 @@ class SourceTest {
5353
s.reportError(s.pos, "That doesn't sound right.", ps)
5454
assertEquals("0030: THAT DOESN'T SOUND RIGHT.", out.toString(charSet))
5555
}
56+
@Test def canAltCustomizeReporting() = {
57+
class CapitalReporting(is: InputStream)(implicit codec: Codec) extends Source {
58+
override val iter = {
59+
val r = new InputStreamReader(is, codec.decoder)
60+
Iterator continually (codec wrap r.read()) takeWhile (_ != -1) map (_.toChar)
61+
}
62+
override def report(pos: Int, msg: String, out: PrintStream): Unit = {
63+
out print f"$pos%04x: ${msg.toUpperCase}"
64+
}
65+
private[this] var _pos: Int = _
66+
override def pos = _pos
67+
private[this] var _ch: Char = _
68+
override def ch = _ch
69+
override def next = {
70+
_ch = iter.next()
71+
_pos += 1
72+
_ch
73+
}
74+
}
75+
val s = new CapitalReporting(in)
76+
// skip to next line and report an error
77+
do {
78+
val c = s.next()
79+
} while (s.ch != '\n')
80+
s.next()
81+
val out = new ByteArrayOutputStream
82+
val ps = new PrintStream(out, true, charSet)
83+
s.reportError(s.pos, "That doesn't sound right.", ps)
84+
assertEquals("0030: THAT DOESN'T SOUND RIGHT.", out.toString(charSet))
85+
}
5686
}

0 commit comments

Comments
 (0)