Skip to content

Commit cd887c6

Browse files
committed
Some renamings and re-orderings
1 parent 0bbfaf6 commit cd887c6

File tree

3 files changed

+46
-44
lines changed

3 files changed

+46
-44
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -701,14 +701,14 @@ object Parsers {
701701
def bracesToIndented[T](body: => T): T = {
702702
val colonRequired = possibleColonOffset == in.lastOffset
703703
val (startOpening, endOpening) = startingElimRegion(colonRequired)
704-
val isOuterMost = in.sepRegions.isEmpty
704+
val isOutermost = in.currentRegion.isOutermost
705705
val savedPending = pendingPatches
706706
def allBraces(r: Region): Boolean = r match {
707-
case GlobalRegion => true
708-
case InBracesRegion(outer) => allBraces(outer)
707+
case Outermost => true
708+
case InBraces(outer) => allBraces(outer)
709709
case _ => false
710710
}
711-
var canRewrite = allBraces(in.sepRegions) && // test (1)
711+
var canRewrite = allBraces(in.currentRegion) && // test (1)
712712
!testChars(in.lastOffset - 3, " =>") // test(6)
713713
val t = enclosed(LBRACE, {
714714
canRewrite &= in.isAfterLineEnd // test (2)
@@ -730,7 +730,7 @@ object Parsers {
730730
patch(source, Span(startClosing, endClosing), "")
731731
}
732732
pendingPatches = applyPatch :: pendingPatches
733-
if (isOuterMost) {
733+
if (isOutermost) {
734734
pendingPatches.reverse.foreach(_())
735735
pendingPatches = Nil
736736
}

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

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,12 @@ object Scanners {
305305
* (the STRINGLIT appears twice in succession on the stack iff the
306306
* expression is a multiline string literal).
307307
*/
308-
var sepRegions: Region = GlobalRegion
308+
var currentRegion: Region = Outermost
309+
310+
def currentIndentWidth = currentRegion match {
311+
case r: Indented => r.width
312+
case _ => 0
313+
}
309314

310315
/** Indentation widths, innermost to outermost */
311316
var indent: IndentRegion = IndentRegion(IndentWidth.Zero, Set(), EMPTY, null)
@@ -330,16 +335,11 @@ object Scanners {
330335

331336
// Get next token ------------------------------------------------------------
332337

333-
/** Are we directly in a string interpolation expression?
334-
*/
335-
private def inStringInterpolation =
336-
sepRegions.isInstanceOf[InStringRegion]
337-
338338
/** Are we directly in a multiline string interpolation expression?
339339
* @pre inStringInterpolation
340340
*/
341-
private def inMultiLineInterpolation = sepRegions match {
342-
case InStringRegion(_, multiLine) => multiLine
341+
private def inMultiLineInterpolation = currentRegion match {
342+
case InString(multiLine, _) => multiLine
343343
case _ => false
344344
}
345345

@@ -353,27 +353,27 @@ object Scanners {
353353

354354
def adjustSepRegions(lastToken: Token): Unit = (lastToken: @switch) match {
355355
case LPAREN | LBRACKET =>
356-
sepRegions = InParensRegion(sepRegions, lastToken + 1)
356+
currentRegion = InParens(lastToken, currentRegion)
357357
case LBRACE =>
358-
sepRegions = InBracesRegion(sepRegions)
358+
currentRegion = InBraces(currentRegion)
359359
case RBRACE =>
360-
def dropBraces(): Unit = sepRegions match {
361-
case GlobalRegion =>
362-
case InBracesRegion(outer) =>
363-
sepRegions = outer
360+
def dropBraces(): Unit = currentRegion match {
361+
case Outermost =>
362+
case InBraces(outer) =>
363+
currentRegion = outer
364364
case _ =>
365-
sepRegions = sepRegions.outer
365+
currentRegion = currentRegion.enclosing
366366
dropBraces()
367367
}
368368
dropBraces()
369369
case RPAREN | RBRACKET =>
370-
sepRegions match {
371-
case InParensRegion(outer, closing) if closing == lastToken => sepRegions = outer
370+
currentRegion match {
371+
case InParens(prefix, outer) if prefix + 1 == lastToken => currentRegion = outer
372372
case _ =>
373373
}
374374
case STRINGLIT =>
375-
sepRegions match {
376-
case InStringRegion(outer, _) => sepRegions = outer
375+
currentRegion match {
376+
case InString(_, outer) => currentRegion = outer
377377
case _ =>
378378
}
379379
case _ =>
@@ -388,8 +388,11 @@ object Scanners {
388388
// Read a token or copy it from `next` tokenData
389389
if (next.token == EMPTY) {
390390
lastOffset = lastCharOffset
391-
if (inStringInterpolation) fetchStringPart() else fetchToken()
392-
if (token == ERROR) adjustSepRegions(STRINGLIT)
391+
currentRegion match {
392+
case InString(multiLine, _) => fetchStringPart(multiLine)
393+
case _ => fetchToken()
394+
}
395+
if (token == ERROR) adjustSepRegions(STRINGLIT) // make sure we exit enclosing string literal
393396
}
394397
else {
395398
this.copyFrom(next)
@@ -511,14 +514,14 @@ object Scanners {
511514
*
512515
* Indentation is _significant_ if indentSyntax is set, and we are not inside a
513516
* {...}, [...], (...), case ... => pair, nor in a if/while condition
514-
* (i.e. sepRegions is empty).
517+
* (i.e. currentRegion is empty).
515518
*
516519
* There are three rules:
517520
*
518521
* 1. Insert NEWLINE or NEWLINES if
519522
*
520523
* - the closest enclosing sepRegion is { ... } or for ... do/yield,
521-
* or we are on the toplevel, i.e. sepRegions is empty, and
524+
* or we are on the toplevel, i.e. currentRegion is empty, and
522525
* - the previous token can end a statement, and
523526
* - the current token can start a statement, and
524527
* - the current token is not a leading infix operator, and
@@ -562,9 +565,9 @@ object Scanners {
562565
* if the current indentation width and the indentation of the current token are incomparable.
563566
*/
564567
def handleNewLine(lastToken: Token) = {
565-
val indentIsSignificant = indentSyntax && sepRegions.isEmpty
566-
val newlineIsSeparating = sepRegions match {
567-
case GlobalRegion | InBracesRegion(_) => true
568+
val indentIsSignificant = indentSyntax && currentRegion.isOutermost
569+
val newlineIsSeparating = currentRegion match {
570+
case Outermost | InBraces(_) => true
568571
case _ => false
569572
}
570573
val curWidth = indentWidth(offset)
@@ -758,7 +761,7 @@ object Scanners {
758761
case '\"' =>
759762
def stringPart(multiLine: Boolean) = {
760763
getStringPart(multiLine)
761-
sepRegions = InStringRegion(sepRegions, multiLine)
764+
currentRegion = InString(multiLine, currentRegion)
762765
}
763766
def fetchDoubleQuote() = {
764767
if (token == INTERPOLATIONID) {
@@ -1129,9 +1132,9 @@ object Scanners {
11291132
}
11301133
}
11311134

1132-
private def fetchStringPart() = {
1135+
private def fetchStringPart(multiLine: Boolean) = {
11331136
offset = charOffset - 1
1134-
getStringPart(multiLine = inMultiLineInterpolation)
1137+
getStringPart(multiLine)
11351138
}
11361139

11371140
private def isTripleQuote(): Boolean =
@@ -1342,16 +1345,15 @@ object Scanners {
13421345
// end Scanner
13431346

13441347
abstract class Region {
1345-
def outer: Region
1346-
def isEmpty = false
1347-
}
1348-
case class InParensRegion(val outer: Region, closing: Token) extends Region
1349-
case class InBracesRegion(val outer: Region) extends Region
1350-
case class InStringRegion(val outer: Region, multiLine: Boolean) extends Region
1351-
case object GlobalRegion extends Region {
1352-
def outer = throw UnsupportedOperationException("GlobalRegion.outer")
1353-
override def isEmpty = true
1348+
def outer: Region | Null
1349+
def isOutermost = outer == null
1350+
def enclosing: Region = outer.asInstanceOf[Region]
13541351
}
1352+
case class InParens(prefix: Token, outer: Region) extends Region
1353+
case class InBraces(outer: Region) extends Region
1354+
case class InString(multiLine: Boolean, outer: Region) extends Region
1355+
case class Indented(width: IndentWidth, others: Set[IndentWidth], prefix: Token, outer: Region) extends Region
1356+
case object Outermost extends Region { val outer = null }
13551357

13561358
/** A class describing an indentation region.
13571359
* @param width The principal indendation width

compiler/src/dotty/tools/dotc/parsing/xml/MarkupParsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ object MarkupParsers {
398398

399399
def escapeToScala[A](op: => A, kind: String): A = {
400400
xEmbeddedBlock = false
401-
val res = saving(parser.in.sepRegions, parser.in.sepRegions = _) {
401+
val res = saving(parser.in.currentRegion, parser.in.currentRegion = _) {
402402
val lbrace = parser.in.newTokenData
403403
lbrace.token = LBRACE
404404
lbrace.offset = parser.in.charOffset - 1

0 commit comments

Comments
 (0)