Skip to content

Use commaSeparated helper in more places #14741

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

def commaSeparated[T](part: () => T): List[T] =
/**
* @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] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to avoid boolean parameters. In this case we can do that by having two methods commaSeparated and commaSeparatedRest. The second takes the first node as parameter.

in.currentRegion.withCommasExpected {
val ts = new ListBuffer[T] += part()
val ts = new ListBuffer[T]
if (readFirst) ts += part()
while in.token == COMMA do
in.nextToken()
ts += part()
Expand Down Expand Up @@ -1384,14 +1389,7 @@ object Parsers {
else
Function(params, t)
}
def funTypeArgsRest(first: Tree, following: () => Tree) = {
val buf = new ListBuffer[Tree] += first
while (in.token == COMMA) {
in.nextToken()
buf += following()
}
buf.toList
}

var isValParamList = false

val t =
Expand All @@ -1407,11 +1405,10 @@ object Parsers {
val ts = funArgType() match {
case Ident(name) if name != tpnme.WILDCARD && in.isColon() =>
isValParamList = true
funTypeArgsRest(
typedFunParam(paramStart, name.toTermName, imods),
() => typedFunParam(in.offset, ident(), imods))
typedFunParam(paramStart, name.toTermName, imods) :: commaSeparated(
() => typedFunParam(in.offset, ident(), imods), readFirst = false)
case t =>
funTypeArgsRest(t, funArgType)
t :: commaSeparated(funArgType, readFirst = false)
}
accept(RPAREN)
if isValParamList || in.isArrow then
Expand Down Expand Up @@ -3191,9 +3188,9 @@ object Parsers {
}
else ImportSelector(from)

def importSelectors(idOK: Boolean): List[ImportSelector] =
def importSelector(idOK: Boolean)(): ImportSelector =
val isWildcard = in.token == USCORE || in.token == GIVEN || isIdent(nme.raw.STAR)
val selector = atSpan(in.offset) {
atSpan(in.offset) {
in.token match
case USCORE => wildcardSelector()
case GIVEN => givenSelector()
Expand All @@ -3203,13 +3200,6 @@ object Parsers {
if !idOK then syntaxError(i"named imports cannot follow wildcard imports")
namedSelector(termIdent())
}
val rest =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that changes the accepted set of programs since it does not propagate isWildcard to following import selectors.

if in.token == COMMA then
in.nextToken()
importSelectors(idOK = idOK && !isWildcard)
else
Nil
selector :: rest

def importSelection(qual: Tree): Tree =
if in.isIdent(nme.as) && qual.isInstanceOf[RefTree] then
Expand All @@ -3227,7 +3217,7 @@ object Parsers {
case GIVEN =>
mkTree(qual, givenSelector() :: Nil)
case LBRACE =>
mkTree(qual, inBraces(importSelectors(idOK = true)))
mkTree(qual, inBraces(commaSeparated(importSelector(idOK = true))))
case _ =>
if isIdent(nme.raw.STAR) then
mkTree(qual, wildcardSelector() :: Nil)
Expand Down