Skip to content

Have specific SepRegions for cases and conditions #8679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ object Parsers {
if source.isSelfContained then new ScriptParser(source)
else new Parser(source)

private val InCase: Region => Region = Scanners.InCase
private val InCond: Region => Region = Scanners.InBraces

abstract class ParserCommon(val source: SourceFile)(implicit ctx: Context) {

val in: ScannerCommon
Expand Down Expand Up @@ -611,10 +614,10 @@ object Parsers {

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

def inSepRegion[T](opening: Token, closing: Token)(op: => T): T = {
in.adjustSepRegions(opening)
try op finally in.adjustSepRegions(closing)
}
def inSepRegion[T](f: Region => Region)(op: => T): T =
val cur = in.currentRegion
in.currentRegion = f(cur)
try op finally in.currentRegion = cur

/** Parse `body` while checking (under -noindent) that a `{` is not missing before it.
* This is done as follows:
Expand Down Expand Up @@ -1785,7 +1788,7 @@ object Parsers {
var t: Tree = atSpan(in.offset) { Parens(inParens(exprInParens())) }
val enclosedInParens = !toBeContinued(altToken)
if !enclosedInParens then
t = inSepRegion(LBRACE, RBRACE) {
t = inSepRegion(InCond) {
expr1Rest(postfixExprRest(simpleExprRest(t)), Location.ElseWhere)
}
if in.token == altToken then
Expand All @@ -1803,7 +1806,7 @@ object Parsers {
if in.isNestedStart then
try expr() finally newLinesOpt()
else
inSepRegion(LBRACE, RBRACE)(expr())
inSepRegion(InCond)(expr())
if rewriteToOldSyntax(t.span.startPos) then
revertToParens(t)
if altToken == THEN && in.isNewLine then
Expand Down Expand Up @@ -2470,7 +2473,7 @@ object Parsers {
if (in.token == INDENT)
inBracesOrIndented(enumerators())
else {
val ts = inSepRegion(LBRACE, RBRACE)(enumerators())
val ts = inSepRegion(InCond)(enumerators())
if (rewriteToOldSyntax(Span(start)) && ts.nonEmpty)
if (ts.head.sourcePos.startLine != ts.last.sourcePos.startLine) {
patch(source, Span(forEnd), " {")
Expand Down Expand Up @@ -2514,7 +2517,7 @@ object Parsers {
* ExprCaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Expr
*/
def caseClause(exprOnly: Boolean = false): CaseDef = atSpan(in.offset) {
val (pat, grd) = inSepRegion(LPAREN, RPAREN) {
val (pat, grd) = inSepRegion(InCase) {
accept(CASE)
(pattern(), guard())
}
Expand All @@ -2526,7 +2529,7 @@ object Parsers {
/** TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]
*/
def typeCaseClause(): CaseDef = atSpan(in.offset) {
val pat = inSepRegion(LPAREN, RPAREN) {
val pat = inSepRegion(InCase) {
accept(CASE)
infixType()
}
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,7 @@ object Scanners {
case class InString(multiLine: Boolean, outer: Region) extends Region
case class InParens(prefix: Token, outer: Region) extends Region
case class InBraces(outer: Region) extends Region
case class InCase(outer: Region) extends Region

/** A class describing an indentation region.
* @param width The principal indendation width
Expand Down