Skip to content

Commit fe88598

Browse files
committed
Check for trailing commas in parser instead of scanner
1 parent 317b08c commit fe88598

File tree

6 files changed

+162
-37
lines changed

6 files changed

+162
-37
lines changed

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

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,18 @@ object Parsers {
568568
* If the parser consumes a `part` that is not followed by a comma or this expected
569569
* token, issue a syntax error and try to recover at the next safe point.
570570
*/
571-
def commaSeparated[T](part: () => T, expectedEnd: Token = EMPTY): List[T] = {
572-
val ts = new ListBuffer[T] += part()
573-
while (in.token == COMMA) {
571+
def commaSeparated[T](part: () => T, expectedEnd: Token, readFirst: Boolean = true): List[T] = {
572+
val ts = new ListBuffer[T]
573+
if (readFirst) ts += part()
574+
var done = false
575+
while (in.token == COMMA && !done) {
574576
in.nextToken()
575-
ts += part()
577+
if (in.isAfterLineEnd && (in.token == OUTDENT || (expectedEnd != EMPTY && in.token == expectedEnd))) {
578+
// skip the trailing comma
579+
done = true
580+
} else {
581+
ts += part()
582+
}
576583
}
577584
if (expectedEnd != EMPTY && in.token != expectedEnd) {
578585
// As a side effect, will skip to the nearest safe point, which might be a comma
@@ -1400,14 +1407,7 @@ object Parsers {
14001407
else
14011408
Function(params, t)
14021409
}
1403-
def funTypeArgsRest(first: Tree, following: () => Tree) = {
1404-
val buf = new ListBuffer[Tree] += first
1405-
while (in.token == COMMA) {
1406-
in.nextToken()
1407-
buf += following()
1408-
}
1409-
buf.toList
1410-
}
1410+
14111411
var isValParamList = false
14121412

14131413
val t =
@@ -1423,11 +1423,10 @@ object Parsers {
14231423
val ts = funArgType() match {
14241424
case Ident(name) if name != tpnme.WILDCARD && in.isColon() =>
14251425
isValParamList = true
1426-
funTypeArgsRest(
1427-
typedFunParam(paramStart, name.toTermName, imods),
1428-
() => typedFunParam(in.offset, ident(), imods))
1426+
typedFunParam(paramStart, name.toTermName, imods) :: commaSeparated(
1427+
() => typedFunParam(in.offset, ident(), imods), RPAREN, readFirst = false)
14291428
case t =>
1430-
funTypeArgsRest(t, funArgType)
1429+
t :: commaSeparated(funArgType, RPAREN, readFirst = false)
14311430
}
14321431
accept(RPAREN)
14331432
if isValParamList || in.isArrow then
@@ -3130,7 +3129,7 @@ object Parsers {
31303129
*/
31313130
def importClause(leading: Token, mkTree: ImportConstr): List[Tree] = {
31323131
val offset = accept(leading)
3133-
commaSeparated(importExpr(mkTree)) match {
3132+
commaSeparated(importExpr(mkTree), EMPTY) match {
31343133
case t :: rest =>
31353134
// The first import should start at the start offset of the keyword.
31363135
val firstPos =
@@ -3207,9 +3206,9 @@ object Parsers {
32073206
}
32083207
else ImportSelector(from)
32093208

3210-
def importSelectors(idOK: Boolean): List[ImportSelector] =
3209+
def importSelector(idOK: Boolean)(): ImportSelector =
32113210
val isWildcard = in.token == USCORE || in.token == GIVEN || isIdent(nme.raw.STAR)
3212-
val selector = atSpan(in.offset) {
3211+
atSpan(in.offset) {
32133212
in.token match
32143213
case USCORE => wildcardSelector()
32153214
case GIVEN => givenSelector()
@@ -3219,13 +3218,6 @@ object Parsers {
32193218
if !idOK then syntaxError(i"named imports cannot follow wildcard imports")
32203219
namedSelector(termIdent())
32213220
}
3222-
val rest =
3223-
if in.token == COMMA then
3224-
in.nextToken()
3225-
importSelectors(idOK = idOK && !isWildcard)
3226-
else
3227-
Nil
3228-
selector :: rest
32293221

32303222
def importSelection(qual: Tree): Tree =
32313223
if in.isIdent(nme.as) && qual.isInstanceOf[RefTree] then
@@ -3243,7 +3235,7 @@ object Parsers {
32433235
case GIVEN =>
32443236
mkTree(qual, givenSelector() :: Nil)
32453237
case LBRACE =>
3246-
mkTree(qual, inBraces(importSelectors(idOK = true)))
3238+
mkTree(qual, inBraces(commaSeparated(importSelector(idOK = true), RBRACE)))
32473239
case _ =>
32483240
if isIdent(nme.raw.STAR) then
32493241
mkTree(qual, wildcardSelector() :: Nil)
@@ -3300,7 +3292,7 @@ object Parsers {
33003292
var lhs = first match {
33013293
case id: Ident if in.token == COMMA =>
33023294
in.nextToken()
3303-
id :: commaSeparated(() => termIdent())
3295+
id :: commaSeparated(() => termIdent(), EMPTY)
33043296
case _ =>
33053297
first :: Nil
33063298
}
@@ -3571,7 +3563,7 @@ object Parsers {
35713563
val id = termIdent()
35723564
if (in.token == COMMA) {
35733565
in.nextToken()
3574-
val ids = commaSeparated(() => termIdent())
3566+
val ids = commaSeparated(() => termIdent(), EMPTY)
35753567
PatDef(mods1, id :: ids, TypeTree(), EmptyTree)
35763568
}
35773569
else {
@@ -3775,7 +3767,7 @@ object Parsers {
37753767
val derived =
37763768
if (isIdent(nme.derives)) {
37773769
in.nextToken()
3778-
commaSeparated(() => convertToTypeId(qualId()))
3770+
commaSeparated(() => convertToTypeId(qualId()), EMPTY)
37793771
}
37803772
else Nil
37813773
possibleTemplateStart()

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,6 @@ object Scanners {
660660
insert(OUTDENT, offset)
661661
currentRegion = r.outer
662662
case _ =>
663-
lookAhead()
664-
if isAfterLineEnd
665-
&& (token == RPAREN || token == RBRACKET || token == RBRACE || token == OUTDENT)
666-
then
667-
() /* skip the trailing comma */
668-
else
669-
reset()
670663
case END =>
671664
if !isEndMarker then token = IDENTIFIER
672665
case COLON =>

tests/neg/t11900.check

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Error: tests/neg/t11900.scala:44:16 ---------------------------------------------------------------------------------
2+
44 | a => a + 1, // error: weird comma
3+
| ^
4+
| end of statement expected but ',' found
5+
-- Error: tests/neg/t11900.scala:48:16 ---------------------------------------------------------------------------------
6+
48 | println("a"), // error: weird comma
7+
| ^
8+
| end of statement expected but ',' found
9+
-- Error: tests/neg/t11900.scala:52:16 ---------------------------------------------------------------------------------
10+
52 | println("b"), // error: weird comma
11+
| ^
12+
| end of statement expected but ',' found
13+
-- [E032] Syntax Error: tests/neg/t11900.scala:64:8 --------------------------------------------------------------------
14+
64 | _*, // error
15+
| ^
16+
| pattern expected
17+
|
18+
| longer explanation available when compiling with `-explain`

tests/neg/t11900.scala

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
trait t11900 {
3+
// cf pos/trailing-commas
4+
//
5+
import scala.collection.{
6+
immutable,
7+
mutable,
8+
}
9+
10+
def h[A,
11+
]: List[A] = Nil
12+
13+
def u(
14+
x: Int,
15+
y: Int,
16+
)(using List[Int],
17+
Set[Int],
18+
)(using l: List[Int],
19+
s : Set[Int],
20+
): Int = 1
21+
22+
def g = List(
23+
1,
24+
2,
25+
3,
26+
)
27+
28+
def star =
29+
List(1, 2, 3, 4, 5) match {
30+
case List(
31+
1,
32+
2,
33+
3,
34+
) => false
35+
case List(
36+
1,
37+
2,
38+
_*,
39+
) => true
40+
}
41+
42+
def f =
43+
List(1, 2, 3).map {
44+
a => a + 1, // error: weird comma
45+
}
46+
47+
class A() {
48+
println("a"), // error: weird comma
49+
}
50+
51+
def b() = {
52+
println("b"), // error: weird comma
53+
}
54+
55+
def starcrossed =
56+
List(1, 2, 3, 4, 5) match {
57+
case List(
58+
1,
59+
2,
60+
3,
61+
) => false
62+
case List(
63+
1,
64+
_*, // error
65+
2,
66+
) => true
67+
}
68+
69+
def p(p: (Int,
70+
String,
71+
)
72+
): Unit
73+
74+
def q: (Int,
75+
String,
76+
)
77+
78+
val z = 42
79+
}

tests/neg/trailingCommas.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,27 @@ object `package` {
5656
case class Foo(foo: Any)
5757
case class Bar(foo: Any)
5858
}
59+
60+
// Unparenthesized lists
61+
trait Deriv1[T]
62+
object Deriv1 {
63+
def derived[T]: Deriv1[T] = new Deriv1[T] {}
64+
}
65+
66+
trait Deriv2[T]
67+
object Deriv2 {
68+
def derived[T]: Deriv2[T] = new Deriv2[T] {}
69+
}
70+
71+
class Derives1 derives Deriv1, Deriv2,
72+
object End // error: an identifier expected, but 'object' found
73+
74+
class Derives2 derives Deriv1,
75+
Deriv2,
76+
object End2 // error: an identifier expected, but 'object' found
77+
78+
val a,
79+
b,
80+
c,
81+
= (1, 2, 3) // error
82+
val x, y, z, = (1, 2, 3) // error // error

tests/pos/comma-separated.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
trait Bar[T]
2+
object Bar {
3+
def derived[T]: Bar[T] = new Bar[T] {}
4+
}
5+
6+
trait Baz[T]
7+
object Baz {
8+
def derived[T]: Baz[T] = new Baz[T] {}
9+
}
10+
11+
class Foo derives Bar, Baz
12+
13+
class Foo2 derives Bar,
14+
Baz
15+
16+
val x, y, z = (1, 2, 3)
17+
val a,
18+
b,
19+
c = (1, 2, 3)

0 commit comments

Comments
 (0)