Skip to content

Commit 3b428f5

Browse files
committed
Move trailing comma handling to Parser
1 parent f76e5ed commit 3b428f5

File tree

8 files changed

+191
-50
lines changed

8 files changed

+191
-50
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,15 @@ object Parsers {
567567
in.currentRegion.withCommasExpected {
568568
val ts = new ListBuffer[T]
569569
if (readFirst) ts += part()
570-
while in.token == COMMA do
570+
var done = false
571+
while in.token == COMMA && !done do
572+
val start = in.offset
571573
in.nextToken()
572-
ts += part()
574+
if in.isAfterLineEnd && (in.token == RPAREN || in.token == RBRACKET || in.token == RBRACE || in.token == OUTDENT) then
575+
// skip the trailing comma
576+
done = true
577+
else
578+
ts += part()
573579
ts.toList
574580
}
575581

@@ -3271,7 +3277,7 @@ object Parsers {
32713277
*/
32723278
def patDefOrDcl(start: Offset, mods: Modifiers): Tree = atSpan(start, nameStart) {
32733279
val first = pattern2()
3274-
var lhs = first match {
3280+
val lhs = first match {
32753281
case id: Ident if in.token == COMMA =>
32763282
in.nextToken()
32773283
id :: commaSeparated(() => termIdent())

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -698,13 +698,6 @@ object Scanners {
698698
case r: Indented if isEnclosedInParens(r.outer) =>
699699
insert(OUTDENT, offset)
700700
case _ =>
701-
lookAhead()
702-
if isAfterLineEnd
703-
&& (token == RPAREN || token == RBRACKET || token == RBRACE || token == OUTDENT)
704-
then
705-
() /* skip the trailing comma */
706-
else
707-
reset()
708701
case END =>
709702
if !isEndMarker then token = IDENTIFIER
710703
case COLON =>

docs/_docs/internals/syntax.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ comment ::= ‘/*’ “any sequence of characters; nested comments ar
8484
| ‘//’ “any sequence of characters up to end of line”
8585
8686
nl ::= “new line character”
87+
<,> ::= [ ‘,’ nl ]
8788
semi ::= ‘;’ | nl {nl}
8889
```
8990

@@ -168,7 +169,7 @@ FunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type
168169
FunTypeArgs ::= InfixType
169170
| ‘(’ [ FunArgTypes ] ‘)’
170171
| FunParamClause
171-
FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } ‘)’
172+
FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } <,> ‘)’
172173
TypedFunParam ::= id ‘:’ Type
173174
MatchType ::= InfixType `match` <<< TypeCaseClauses >>>
174175
InfixType ::= RefinedType {id [nl] RefinedType} InfixOp(t1, op, t2)
@@ -181,7 +182,7 @@ SimpleType ::= SimpleLiteral
181182
SimpleType1 ::= id Ident(name)
182183
| Singleton ‘.’ id Select(t, name)
183184
| Singleton ‘.’ ‘type’ SingletonTypeTree(p)
184-
| ‘(’ Types ‘)’ Tuple(ts)
185+
| ‘(’ Types <,> ‘)’ Tuple(ts)
185186
| Refinement RefinedTypeTree(EmptyTree, refinement)
186187
| ‘$’ ‘{’ Block ‘}’ -- unless inside quoted pattern
187188
| ‘$’ ‘{’ Pattern ‘}’ -- only inside quoted pattern
@@ -196,7 +197,7 @@ FunArgType ::= Type
196197
FunArgTypes ::= FunArgType { ‘,’ FunArgType }
197198
ParamType ::= [‘=>’] ParamValueType
198199
ParamValueType ::= Type [‘*’] PostfixOp(t, "*")
199-
TypeArgs ::= ‘[’ Types ‘]’ ts
200+
TypeArgs ::= ‘[’ Types <,> ‘]’ ts
200201
Refinement ::= ‘{’ [RefineDcl] {semi [RefineDcl]} ‘}’ ds
201202
TypeBounds ::= [‘>:’ Type] [‘<:’ Type] TypeBoundsTree(lo, hi)
202203
TypeParamBounds ::= TypeBounds {‘:’ Type} ContextBounds(typeBounds, tps)
@@ -249,7 +250,7 @@ SimpleExpr ::= SimpleRef
249250
| quoteId -- only inside splices
250251
| ‘new’ ConstrApp {‘with’ ConstrApp} [TemplateBody] New(constr | templ)
251252
| ‘new’ TemplateBody
252-
| ‘(’ ExprsInParens ‘)’ Parens(exprs)
253+
| ‘(’ ExprsInParens <,> ‘)’ Parens(exprs)
253254
| SimpleExpr ‘.’ id Select(expr, id)
254255
| SimpleExpr ‘.’ MatchClause
255256
| SimpleExpr TypeArgs TypeApply(expr, args)
@@ -264,8 +265,8 @@ Quoted ::= ‘'’ ‘{’ Block ‘}’
264265
ExprsInParens ::= ExprInParens {‘,’ ExprInParens}
265266
ExprInParens ::= PostfixExpr ‘:’ Type -- normal Expr allows only RefinedType here
266267
| Expr
267-
ParArgumentExprs ::= ‘(’ [‘using’] ExprsInParens ‘)’ exprs
268-
| ‘(’ [ExprsInParens ‘,’] PostfixExpr ‘*’ ‘)’ exprs :+ Typed(expr, Ident(wildcardStar))
268+
ParArgumentExprs ::= ‘(’ [‘using’] ExprsInParens <,> ‘)’ exprs
269+
| ‘(’ [ExprsInParens ‘,’] PostfixExpr ‘*’ <,> ‘)’ exprs :+ Typed(expr, Ident(wildcardStar))
269270
ArgumentExprs ::= ParArgumentExprs
270271
| BlockExpr
271272
BlockExpr ::= <<< CaseClauses | Block >>>
@@ -309,44 +310,44 @@ SimplePattern1 ::= SimpleRef
309310
PatVar ::= varid
310311
| ‘_’
311312
Patterns ::= Pattern {‘,’ Pattern}
312-
ArgumentPatterns ::= ‘(’ [Patterns] ‘)’ Apply(fn, pats)
313-
| ‘(’ [Patterns ‘,’] PatVar ‘*’ ‘)’
313+
ArgumentPatterns ::= ‘(’ [Patterns] <,> ‘)’ Apply(fn, pats)
314+
| ‘(’ [Patterns ‘,’] PatVar ‘*’ <,> ‘)’
314315
```
315316

316317
### Type and Value Parameters
317318
```ebnf
318-
ClsTypeParamClause::= ‘[’ ClsTypeParam {‘,’ ClsTypeParam} ‘]’
319+
ClsTypeParamClause::= ‘[’ ClsTypeParam {‘,’ ClsTypeParam} <,> ‘]’
319320
ClsTypeParam ::= {Annotation} [‘+’ | ‘-’] TypeDef(Modifiers, name, tparams, bounds)
320321
id [HkTypeParamClause] TypeParamBounds Bound(below, above, context)
321322
322-
DefTypeParamClause::= ‘[’ DefTypeParam {‘,’ DefTypeParam} ‘]’
323+
DefTypeParamClause::= ‘[’ DefTypeParam {‘,’ DefTypeParam} <,> ‘]’
323324
DefTypeParam ::= {Annotation} id [HkTypeParamClause] TypeParamBounds
324325
325-
TypTypeParamClause::= ‘[’ TypTypeParam {‘,’ TypTypeParam} ‘]’
326+
TypTypeParamClause::= ‘[’ TypTypeParam {‘,’ TypTypeParam} <,> ‘]’
326327
TypTypeParam ::= {Annotation} id [HkTypeParamClause] TypeBounds
327328
328-
HkTypeParamClause ::= ‘[’ HkTypeParam {‘,’ HkTypeParam} ‘]’
329+
HkTypeParamClause ::= ‘[’ HkTypeParam {‘,’ HkTypeParam} <,> ‘]’
329330
HkTypeParam ::= {Annotation} [‘+’ | ‘-’] (id [HkTypeParamClause] | ‘_’)
330331
TypeBounds
331332
332-
ClsParamClauses ::= {ClsParamClause} [[nl] ‘(’ [‘implicit’] ClsParams ‘)’]
333-
ClsParamClause ::= [nl] ‘(’ ClsParams ‘)’
334-
| [nl] ‘(’ ‘using’ (ClsParams | FunArgTypes) ‘)’
333+
ClsParamClauses ::= {ClsParamClause} [[nl] ‘(’ [‘implicit’] ClsParams <,> ‘)’]
334+
ClsParamClause ::= [nl] ‘(’ ClsParams <,> ‘)’
335+
| [nl] ‘(’ ‘using’ (ClsParams | FunArgTypes) <,> ‘)’
335336
ClsParams ::= ClsParam {‘,’ ClsParam}
336337
ClsParam ::= {Annotation} ValDef(mods, id, tpe, expr) -- point of mods on val/var
337338
[{Modifier} (‘val’ | ‘var’) | ‘inline’] Param
338339
Param ::= id ‘:’ ParamType [‘=’ Expr]
339340
340-
DefParamClauses ::= {DefParamClause} [[nl] ‘(’ [‘implicit’] DefParams ‘)’]
341-
DefParamClause ::= [nl] ‘(’ DefParams ‘)’ | UsingParamClause
342-
UsingParamClause ::= [nl] ‘(’ ‘using’ (DefParams | FunArgTypes) ‘)’
341+
DefParamClauses ::= {DefParamClause} [[nl] ‘(’ [‘implicit’] DefParams <,> ‘)’]
342+
DefParamClause ::= [nl] ‘(’ DefParams <,> ‘)’ | UsingParamClause
343+
UsingParamClause ::= [nl] ‘(’ ‘using’ (DefParams | FunArgTypes) <,> ‘)’
343344
DefParams ::= DefParam {‘,’ DefParam}
344345
DefParam ::= {Annotation} [‘inline’] Param ValDef(mods, id, tpe, expr) -- point of mods at id.
345346
```
346347

347348
### Bindings and Imports
348349
```ebnf
349-
Bindings ::= ‘(’ [Binding {‘,’ Binding}] ‘)’
350+
Bindings ::= ‘(’ [Binding {‘,’ Binding} <,> ‘)’
350351
Binding ::= (id | ‘_’) [‘:’ Type] ValDef(_, id, tpe, EmptyTree)
351352
352353
Modifier ::= LocalModifier
@@ -371,7 +372,7 @@ ImportExpr ::= SimpleRef {‘.’ id} ‘.’ ImportSpec
371372
| SimpleRef ‘as’ id Import(EmptyTree, ImportSelector(ref, id))
372373
ImportSpec ::= NamedSelector
373374
| WildcardSelector
374-
| ‘{’ ImportSelectors) ‘}’
375+
| ‘{’ ImportSelectors <,> ‘}’
375376
NamedSelector ::= id [‘as’ (id | ‘_’)]
376377
WildCardSelector ::= ‘*' | ‘given’ [InfixType]
377378
ImportSelectors ::= NamedSelector [‘,’ ImportSelectors]

docs/_docs/reference/syntax.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ comment ::= ‘/*’ “any sequence of characters; nested comments ar
8585
| ‘//’ “any sequence of characters up to end of line”
8686
8787
nl ::= “new line character”
88+
<,> ::= [ ‘,’ nl ]
8889
semi ::= ‘;’ | nl {nl}
8990
```
9091

@@ -168,7 +169,7 @@ FunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type
168169
FunTypeArgs ::= InfixType
169170
| ‘(’ [ FunArgTypes ] ‘)’
170171
| FunParamClause
171-
FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } ‘)’
172+
FunParamClause ::= ‘(’ TypedFunParam {‘,’ TypedFunParam } <,> ‘)’
172173
TypedFunParam ::= id ‘:’ Type
173174
MatchType ::= InfixType `match` <<< TypeCaseClauses >>>
174175
InfixType ::= RefinedType {id [nl] RefinedType}
@@ -180,7 +181,7 @@ SimpleType ::= SimpleLiteral
180181
| id
181182
| Singleton ‘.’ id
182183
| Singleton ‘.’ ‘type’
183-
| ‘(’ Types ‘)’
184+
| ‘(’ Types <,> ‘)’
184185
| Refinement
185186
| ‘$’ ‘{’ Block ‘}’ -- unless inside quoted pattern
186187
| ‘$’ ‘{’ Pattern ‘}’ -- only inside quoted pattern
@@ -195,7 +196,7 @@ FunArgType ::= Type
195196
FunArgTypes ::= FunArgType { ‘,’ FunArgType }
196197
ParamType ::= [‘=>’] ParamValueType
197198
ParamValueType ::= Type [‘*’]
198-
TypeArgs ::= ‘[’ Types ‘]’
199+
TypeArgs ::= ‘[’ Types <,> ‘]’
199200
Refinement ::= ‘{’ [RefineDcl] {semi [RefineDcl]} ‘}’
200201
TypeBounds ::= [‘>:’ Type] [‘<:’ Type]
201202
TypeParamBounds ::= TypeBounds {‘:’ Type}
@@ -247,7 +248,7 @@ SimpleExpr ::= SimpleRef
247248
| quoteId -- only inside splices
248249
| ‘new’ ConstrApp {‘with’ ConstrApp} [TemplateBody]
249250
| ‘new’ TemplateBody
250-
| ‘(’ ExprsInParens ‘)’
251+
| ‘(’ ExprsInParens <,> ‘)’
251252
| SimpleExpr ‘.’ id
252253
| SimpleExpr ‘.’ MatchClause
253254
| SimpleExpr TypeArgs
@@ -257,8 +258,8 @@ Quoted ::= ‘'’ ‘{’ Block ‘}’
257258
ExprsInParens ::= ExprInParens {‘,’ ExprInParens}
258259
ExprInParens ::= PostfixExpr ‘:’ Type
259260
| Expr
260-
ParArgumentExprs ::= ‘(’ [‘using’] ExprsInParens ‘)’
261-
| ‘(’ [ExprsInParens ‘,’] PostfixExpr ‘*’ ‘)’
261+
ParArgumentExprs ::= ‘(’ [‘using’] ExprsInParens <,> ‘)’
262+
| ‘(’ [ExprsInParens ‘,’] PostfixExpr ‘*’ <,> ‘)’
262263
ArgumentExprs ::= ParArgumentExprs
263264
| BlockExpr
264265
BlockExpr ::= <<< (CaseClauses | Block) >>>
@@ -301,41 +302,41 @@ SimplePattern1 ::= SimpleRef
301302
PatVar ::= varid
302303
| ‘_’
303304
Patterns ::= Pattern {‘,’ Pattern}
304-
ArgumentPatterns ::= ‘(’ [Patterns] ‘)’
305-
| ‘(’ [Patterns ‘,’] PatVar ‘*’ ‘)’
305+
ArgumentPatterns ::= ‘(’ [Patterns] <,> ‘)’
306+
| ‘(’ [Patterns ‘,’] PatVar ‘*’ <,> ‘)’
306307
```
307308

308309
### Type and Value Parameters
309310
```
310-
ClsTypeParamClause::= ‘[’ ClsTypeParam {‘,’ ClsTypeParam} ‘]’
311+
ClsTypeParamClause::= ‘[’ ClsTypeParam {‘,’ ClsTypeParam} <,> ‘]’
311312
ClsTypeParam ::= {Annotation} [‘+’ | ‘-’] id [HkTypeParamClause] TypeParamBounds
312313
313-
DefTypeParamClause::= ‘[’ DefTypeParam {‘,’ DefTypeParam} ‘]’
314+
DefTypeParamClause::= ‘[’ DefTypeParam {‘,’ DefTypeParam} <,> ‘]’
314315
DefTypeParam ::= {Annotation} id [HkTypeParamClause] TypeParamBounds
315316
316-
TypTypeParamClause::= ‘[’ TypTypeParam {‘,’ TypTypeParam} ‘]’
317+
TypTypeParamClause::= ‘[’ TypTypeParam {‘,’ TypTypeParam} <,> ‘]’
317318
TypTypeParam ::= {Annotation} id [HkTypeParamClause] TypeBounds
318319
319-
HkTypeParamClause ::= ‘[’ HkTypeParam {‘,’ HkTypeParam} ‘]’
320+
HkTypeParamClause ::= ‘[’ HkTypeParam {‘,’ HkTypeParam} <,> ‘]’
320321
HkTypeParam ::= {Annotation} [‘+’ | ‘-’] (id [HkTypeParamClause] | ‘_’) TypeBounds
321322
322-
ClsParamClauses ::= {ClsParamClause} [[nl] ‘(’ [‘implicit’] ClsParams ‘)’]
323-
ClsParamClause ::= [nl] ‘(’ ClsParams ‘)’
324-
| [nl] ‘(’ ‘using’ (ClsParams | FunArgTypes) ‘)’
323+
ClsParamClauses ::= {ClsParamClause} [[nl] ‘(’ [‘implicit’] ClsParams <,> ‘)’]
324+
ClsParamClause ::= [nl] ‘(’ ClsParams <,> ‘)’
325+
| [nl] ‘(’ ‘using’ (ClsParams | FunArgTypes) <,> ‘)’
325326
ClsParams ::= ClsParam {‘,’ ClsParam}
326327
ClsParam ::= {Annotation} [{Modifier} (‘val’ | ‘var’) | ‘inline’] Param
327328
Param ::= id ‘:’ ParamType [‘=’ Expr]
328329
329-
DefParamClauses ::= {DefParamClause} [[nl] ‘(’ [‘implicit’] DefParams ‘)’]
330-
DefParamClause ::= [nl] ‘(’ DefParams ‘)’ | UsingParamClause
331-
UsingParamClause ::= [nl] ‘(’ ‘using’ (DefParams | FunArgTypes) ‘)’
330+
DefParamClauses ::= {DefParamClause} [[nl] ‘(’ [‘implicit’] DefParams <,> ‘)’]
331+
DefParamClause ::= [nl] ‘(’ DefParams <,> ‘)’ | UsingParamClause
332+
UsingParamClause ::= [nl] ‘(’ ‘using’ (DefParams | FunArgTypes) <,> ‘)’
332333
DefParams ::= DefParam {‘,’ DefParam}
333334
DefParam ::= {Annotation} [‘inline’] Param
334335
```
335336

336337
### Bindings and Imports
337338
```
338-
Bindings ::= ‘(’ [Binding {‘,’ Binding}] ‘)’
339+
Bindings ::= ‘(’ [Binding {‘,’ Binding}] <,> ‘)’
339340
Binding ::= (id | ‘_’) [‘:’ Type]
340341
341342
Modifier ::= LocalModifier
@@ -360,7 +361,7 @@ ImportExpr ::= SimpleRef {‘.’ id} ‘.’ ImportSpec
360361
| SimpleRef ‘as’ id
361362
ImportSpec ::= NamedSelector
362363
| WildcardSelector
363-
| ‘{’ ImportSelectors) ‘}’
364+
| ‘{’ ImportSelectors <,> ‘}’
364365
NamedSelector ::= id [‘as’ (id | ‘_’)]
365366
WildCardSelector ::= ‘*' | ‘given’ [InfixType]
366367
ImportSelectors ::= NamedSelector [‘,’ ImportSelectors]

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+
}

0 commit comments

Comments
 (0)