Skip to content

Alternative trailing comma handling #14757

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
Mar 23, 2022
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
50 changes: 32 additions & 18 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -559,19 +559,24 @@ object Parsers {
def inDefScopeBraces[T](body: => T, rewriteWithColon: Boolean = false): T =
inBracesOrIndented(body, rewriteWithColon)

/**
* @param readFirst If true, assume we have not read the first `part`. Otherwise,
* expect that we have (i.e the next thing to expect is a [[COMMA]]).
*/
def commaSeparated[T](part: () => T, readFirst: Boolean = true): List[T] =
/** <part> {`,` <part>} */
def commaSeparated[T](part: () => T): List[T] =
in.currentRegion.withCommasExpected {
val ts = new ListBuffer[T]
if (readFirst) ts += part()
commaSeparatedRest(part(), part)
}

/** {`,` <part>}
*
* currentRegion.commasExpected has to be set separately.
*/
def commaSeparatedRest[T](leading: T, part: () => T): List[T] =
if in.token == COMMA then
val ts = new ListBuffer[T] += leading
while in.token == COMMA do
in.nextToken()
ts += part()
ts.toList
}
else leading :: Nil

def inSepRegion[T](f: Region => Region)(op: => T): T =
val cur = in.currentRegion
Expand Down Expand Up @@ -1402,13 +1407,15 @@ object Parsers {
else {
if isErased then imods = addModifier(imods)
val paramStart = in.offset
val ts = funArgType() match {
case Ident(name) if name != tpnme.WILDCARD && in.isColon() =>
isValParamList = true
typedFunParam(paramStart, name.toTermName, imods) :: commaSeparated(
() => typedFunParam(in.offset, ident(), imods), readFirst = false)
case t =>
t :: commaSeparated(funArgType, readFirst = false)
val ts = in.currentRegion.withCommasExpected {
funArgType() match
case Ident(name) if name != tpnme.WILDCARD && in.isColon() =>
isValParamList = true
commaSeparatedRest(
typedFunParam(paramStart, name.toTermName, imods),
() => typedFunParam(in.offset, ident(), imods))
case t =>
commaSeparatedRest(t, funArgType)
}
accept(RPAREN)
if isValParamList || in.isArrow then
Expand Down Expand Up @@ -3188,8 +3195,7 @@ object Parsers {
}
else ImportSelector(from)

def importSelector(idOK: Boolean)(): ImportSelector =
val isWildcard = in.token == USCORE || in.token == GIVEN || isIdent(nme.raw.STAR)
def importSelector(idOK: Boolean): ImportSelector =
atSpan(in.offset) {
in.token match
case USCORE => wildcardSelector()
Expand All @@ -3201,6 +3207,14 @@ object Parsers {
namedSelector(termIdent())
}

def importSelectors(): List[ImportSelector] =
var idOK = true
commaSeparated { () =>
val isWildcard = in.token == USCORE || in.token == GIVEN || isIdent(nme.raw.STAR)
try importSelector(idOK)
finally idOK &= !isWildcard
}

def importSelection(qual: Tree): Tree =
if in.isIdent(nme.as) && qual.isInstanceOf[RefTree] then
qual match
Expand All @@ -3217,7 +3231,7 @@ object Parsers {
case GIVEN =>
mkTree(qual, givenSelector() :: Nil)
case LBRACE =>
mkTree(qual, inBraces(commaSeparated(importSelector(idOK = true))))
mkTree(qual, inBraces(importSelectors()))
case _ =>
if isIdent(nme.raw.STAR) then
mkTree(qual, wildcardSelector() :: Nil)
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 @@ -700,6 +700,7 @@ object Scanners {
case _ =>
lookAhead()
if isAfterLineEnd
&& currentRegion.commasExpected
&& (token == RPAREN || token == RBRACKET || token == RBRACE || token == OUTDENT)
then
() /* skip the trailing comma */
Expand Down
2 changes: 2 additions & 0 deletions tests/neg/imports.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import scala.collection.{*, View} // error
object test