Skip to content

Commit fb7ee27

Browse files
committed
Have specific SepRegions for cases and conditions
1 parent 06f311c commit fb7ee27

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

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

Lines changed: 13 additions & 5 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
@@ -616,6 +619,11 @@ object Parsers {
616619
try op finally in.adjustSepRegions(closing)
617620
}
618621

622+
def inSepRegion[T](f: Region => Region)(op: => T): T =
623+
val cur = in.currentRegion
624+
in.currentRegion = f(cur)
625+
try op finally in.currentRegion = cur
626+
619627
/** Parse `body` while checking (under -noindent) that a `{` is not missing before it.
620628
* This is done as follows:
621629
* If the next token S is indented relative to the current region,
@@ -1785,7 +1793,7 @@ object Parsers {
17851793
var t: Tree = atSpan(in.offset) { Parens(inParens(exprInParens())) }
17861794
val enclosedInParens = !toBeContinued(altToken)
17871795
if !enclosedInParens then
1788-
t = inSepRegion(LBRACE, RBRACE) {
1796+
t = inSepRegion(InCond) {
17891797
expr1Rest(postfixExprRest(simpleExprRest(t)), Location.ElseWhere)
17901798
}
17911799
if in.token == altToken then
@@ -1803,7 +1811,7 @@ object Parsers {
18031811
if in.isNestedStart then
18041812
try expr() finally newLinesOpt()
18051813
else
1806-
inSepRegion(LBRACE, RBRACE)(expr())
1814+
inSepRegion(InCond)(expr())
18071815
if rewriteToOldSyntax(t.span.startPos) then
18081816
revertToParens(t)
18091817
if altToken == THEN && in.isNewLine then
@@ -2470,7 +2478,7 @@ object Parsers {
24702478
if (in.token == INDENT)
24712479
inBracesOrIndented(enumerators())
24722480
else {
2473-
val ts = inSepRegion(LBRACE, RBRACE)(enumerators())
2481+
val ts = inSepRegion(InCond)(enumerators())
24742482
if (rewriteToOldSyntax(Span(start)) && ts.nonEmpty)
24752483
if (ts.head.sourcePos.startLine != ts.last.sourcePos.startLine) {
24762484
patch(source, Span(forEnd), " {")
@@ -2514,7 +2522,7 @@ object Parsers {
25142522
* ExprCaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Expr
25152523
*/
25162524
def caseClause(exprOnly: Boolean = false): CaseDef = atSpan(in.offset) {
2517-
val (pat, grd) = inSepRegion(LPAREN, RPAREN) {
2525+
val (pat, grd) = inSepRegion(InCase) {
25182526
accept(CASE)
25192527
(pattern(), guard())
25202528
}
@@ -2526,7 +2534,7 @@ object Parsers {
25262534
/** TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]
25272535
*/
25282536
def typeCaseClause(): CaseDef = atSpan(in.offset) {
2529-
val pat = inSepRegion(LPAREN, RPAREN) {
2537+
val pat = inSepRegion(InCase) {
25302538
accept(CASE)
25312539
infixType()
25322540
}

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)