Skip to content

Commit 26032db

Browse files
authored
Merge pull request #8679 from dotty-staging/change-sepregions
Have specific SepRegions for cases and conditions
2 parents 0af439e + 49b011d commit 26032db

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ object Parsers {
7474
if source.isSelfContained then new ScriptParser(source)
7575
else new Parser(source)
7676

77+
private val InCase: Region => Region = Scanners.InCase
78+
private val InCond: Region => Region = Scanners.InBraces
79+
7780
abstract class ParserCommon(val source: SourceFile)(implicit ctx: Context) {
7881

7982
val in: ScannerCommon
@@ -611,10 +614,10 @@ object Parsers {
611614

612615
def commaSeparated[T](part: () => T): List[T] = tokenSeparated(COMMA, part)
613616

614-
def inSepRegion[T](opening: Token, closing: Token)(op: => T): T = {
615-
in.adjustSepRegions(opening)
616-
try op finally in.adjustSepRegions(closing)
617-
}
617+
def inSepRegion[T](f: Region => Region)(op: => T): T =
618+
val cur = in.currentRegion
619+
in.currentRegion = f(cur)
620+
try op finally in.currentRegion = cur
618621

619622
/** Parse `body` while checking (under -noindent) that a `{` is not missing before it.
620623
* This is done as follows:
@@ -1785,7 +1788,7 @@ object Parsers {
17851788
var t: Tree = atSpan(in.offset) { Parens(inParens(exprInParens())) }
17861789
val enclosedInParens = !toBeContinued(altToken)
17871790
if !enclosedInParens then
1788-
t = inSepRegion(LBRACE, RBRACE) {
1791+
t = inSepRegion(InCond) {
17891792
expr1Rest(postfixExprRest(simpleExprRest(t)), Location.ElseWhere)
17901793
}
17911794
if in.token == altToken then
@@ -1803,7 +1806,7 @@ object Parsers {
18031806
if in.isNestedStart then
18041807
try expr() finally newLinesOpt()
18051808
else
1806-
inSepRegion(LBRACE, RBRACE)(expr())
1809+
inSepRegion(InCond)(expr())
18071810
if rewriteToOldSyntax(t.span.startPos) then
18081811
revertToParens(t)
18091812
if altToken == THEN && in.isNewLine then
@@ -2470,7 +2473,7 @@ object Parsers {
24702473
if (in.token == INDENT)
24712474
inBracesOrIndented(enumerators())
24722475
else {
2473-
val ts = inSepRegion(LBRACE, RBRACE)(enumerators())
2476+
val ts = inSepRegion(InCond)(enumerators())
24742477
if (rewriteToOldSyntax(Span(start)) && ts.nonEmpty)
24752478
if (ts.head.sourcePos.startLine != ts.last.sourcePos.startLine) {
24762479
patch(source, Span(forEnd), " {")
@@ -2514,7 +2517,7 @@ object Parsers {
25142517
* ExprCaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Expr
25152518
*/
25162519
def caseClause(exprOnly: Boolean = false): CaseDef = atSpan(in.offset) {
2517-
val (pat, grd) = inSepRegion(LPAREN, RPAREN) {
2520+
val (pat, grd) = inSepRegion(InCase) {
25182521
accept(CASE)
25192522
(pattern(), guard())
25202523
}
@@ -2526,7 +2529,7 @@ object Parsers {
25262529
/** TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]
25272530
*/
25282531
def typeCaseClause(): CaseDef = atSpan(in.offset) {
2529-
val pat = inSepRegion(LPAREN, RPAREN) {
2532+
val pat = inSepRegion(InCase) {
25302533
accept(CASE)
25312534
infixType()
25322535
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,7 @@ object Scanners {
14021402
case class InString(multiLine: Boolean, outer: Region) extends Region
14031403
case class InParens(prefix: Token, outer: Region) extends Region
14041404
case class InBraces(outer: Region) extends Region
1405+
case class InCase(outer: Region) extends Region
14051406

14061407
/** A class describing an indentation region.
14071408
* @param width The principal indendation width

0 commit comments

Comments
 (0)