Skip to content

Commit 75a21cc

Browse files
committed
Go for olympic style points
1 parent a50b5f3 commit 75a21cc

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ object Scanners {
268268
/** The current region. This is initially an Indented region with zero indentation width. */
269269
var currentRegion: Region = Indented(IndentWidth.Zero, Set(), EMPTY, null)
270270

271+
// begin
272+
/* Initialization: read first char, then first token */
273+
nextChar()
274+
nextToken()
275+
currentRegion = topLevelRegion(indentWidth(offset))
276+
// end begin
277+
271278
// Get next token ------------------------------------------------------------
272279

273280
/** Are we directly in a multiline string interpolation expression?
@@ -438,19 +445,19 @@ object Scanners {
438445
def indentWidth(offset: Offset): IndentWidth =
439446
import IndentWidth.{Run, Conc}
440447
def recur(idx: Int, ch: Char, n: Int, k: IndentWidth => IndentWidth): IndentWidth =
441-
if (idx < 0) k(Run(ch, n))
442-
else {
443-
val nextChar = buf(idx)
444-
if (nextChar == LF) k(Run(ch, n))
445-
else if (nextChar == ' ' || nextChar == '\t')
446-
if (nextChar == ch)
448+
inline def runK = k(Run(ch, n))
449+
inline def konkat: IndentWidth => IndentWidth = if n == 0 then k else Conc(_, Run(ch, n))
450+
if idx < 0 then runK
451+
else
452+
buf(idx) match
453+
case LF => runK
454+
case nextChar @ (' ' | '\t') =>
455+
if nextChar == ch then
447456
recur(idx - 1, ch, n + 1, k)
448-
else {
449-
val k1: IndentWidth => IndentWidth = if (n == 0) k else Conc(_, Run(ch, n))
450-
recur(idx - 1, nextChar, 1, k1)
451-
}
452-
else recur(idx - 1, ' ', 0, identity)
453-
}
457+
else
458+
recur(idx - 1, nextChar, 1, konkat)
459+
case _ =>
460+
recur(idx - 1, ' ', 0, identity)
454461
recur(offset - 1, ' ', 0, identity)
455462
end indentWidth
456463

@@ -1419,11 +1426,6 @@ object Scanners {
14191426

14201427
nextToken()
14211428
}
1422-
1423-
/* Initialization: read first char, then first token */
1424-
nextChar()
1425-
nextToken()
1426-
currentRegion = topLevelRegion(indentWidth(offset))
14271429
}
14281430
end Scanner
14291431

0 commit comments

Comments
 (0)