Skip to content

Commit 291007f

Browse files
committed
Syntax refactoring
- Break out optional `with` from TemplateBody production - Disallow `new with { ...}`.
1 parent 65a404f commit 291007f

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,7 @@ object Parsers {
20462046
}
20472047
else simpleExpr()
20482048

2049-
/** SimpleExpr ::= ‘new’ (ConstrApp [TemplateBody] | TemplateBody)
2049+
/** SimpleExpr ::= ‘new’ (ConstrApp [[‘with’] TemplateBody] | TemplateBody)
20502050
* | BlockExpr
20512051
* | ‘$’ ‘{’ Block ‘}’
20522052
* | Quoted
@@ -2141,7 +2141,7 @@ object Parsers {
21412141
}
21422142
}
21432143

2144-
/** SimpleExpr ::= ‘new’ ConstrApp {`with` ConstrApp} [TemplateBody]
2144+
/** SimpleExpr ::= ‘new’ ConstrApp {`with` ConstrApp} [[‘with’] TemplateBody]
21452145
* | ‘new’ TemplateBody
21462146
*/
21472147
def newExpr(): Tree =
@@ -3312,7 +3312,7 @@ object Parsers {
33123312
}
33133313
}
33143314

3315-
/** EnumDef ::= id ClassConstr InheritClauses EnumBody
3315+
/** EnumDef ::= id ClassConstr InheritClauses [‘with’] EnumBody
33163316
*/
33173317
def enumDef(start: Offset, mods: Modifiers): TypeDef = atSpan(start, nameStart) {
33183318
val modulName = ident()
@@ -3381,7 +3381,7 @@ object Parsers {
33813381
}
33823382

33833383
/** GivenDef ::= [GivenSig (‘:’ | <:)] Type ‘=’ Expr
3384-
* | [GivenSig ‘:’] [ConstrApp {‘,’ ConstrApp }] [TemplateBody]
3384+
* | [GivenSig ‘:’] [ConstrApp {‘,’ ConstrApp }] [[‘with’] TemplateBody]
33853385
* | [id ‘:’] ‘extension’ ExtParamClause {GivenParamClause} ExtMethods
33863386
* GivenSig ::= [id] [DefTypeParamClause] {GivenParamClause}
33873387
* ExtParamClause ::= [DefTypeParamClause] DefParamClause {GivenParamClause}
@@ -3505,7 +3505,7 @@ object Parsers {
35053505
else Nil
35063506
t :: ts
35073507

3508-
/** Template ::= InheritClauses [TemplateBody]
3508+
/** Template ::= InheritClauses [[‘with’] TemplateBody]
35093509
* InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]
35103510
*/
35113511
def template(constr: DefDef, isEnum: Boolean = false): Template = {

docs/docs/internals/syntax.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ SimpleExpr ::= Path
214214
| ‘$’ ‘{’ Block ‘}’
215215
| Quoted
216216
| quoteId // only inside splices
217-
| ‘new’ ConstrApp {`with` ConstrApp} [TemplateBody] New(constr | templ)
217+
| ‘new’ ConstrApp {with ConstrApp} [[‘with’] TemplateBody] New(constr | templ)
218218
| ‘new’ TemplateBody
219219
| ‘(’ ExprsInParens ‘)’ Parens(exprs)
220220
| SimpleExpr ‘.’ id Select(expr, id)
@@ -383,24 +383,23 @@ ClassDef ::= id ClassConstr [Template]
383383
ClassConstr ::= [ClsTypeParamClause] [ConstrMods] ClsParamClauses with DefDef(_, <init>, Nil, vparamss, EmptyTree, EmptyTree) as first stat
384384
ConstrMods ::= {Annotation} [AccessModifier]
385385
ObjectDef ::= id [Template] ModuleDef(mods, name, template) // no constructor
386-
EnumDef ::= id ClassConstr InheritClauses EnumBody EnumDef(mods, name, tparams, template)
386+
EnumDef ::= id ClassConstr InheritClauses [‘with’] EnumBody EnumDef(mods, name, tparams, template)
387387
GivenDef ::= [GivenSig (‘:’ | <:)] Type ‘=’ Expr
388-
| [GivenSig ‘:’] [ConstrApp {‘,’ ConstrApp }] [TemplateBody]
388+
| [GivenSig ‘:’] [ConstrApp {‘,’ ConstrApp }] [[‘with’] TemplateBody]
389389
| [[id ‘:’] ‘extension’ ExtParamClause {GivenParamClause}
390390
ExtMethods
391391
GivenSig ::= [id] [DefTypeParamClause] {GivenParamClause}
392392
ExtParamClause ::= [DefTypeParamClause] ‘(’ DefParam ‘)’
393393
ExtMethods ::= [nl] ‘{’ ‘def’ DefDef {semi ‘def’ DefDef} ‘}’
394-
Template ::= InheritClauses [TemplateBody] Template(constr, parents, self, stats)
394+
Template ::= InheritClauses [[‘with’] TemplateBody] Template(constr, parents, self, stats)
395395
InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]
396396
ConstrApps ::= ConstrApp {(‘,’ | ‘with’) ConstrApp}
397397
ConstrApp ::= AnnotType {ParArgumentExprs} Apply(tp, args)
398398
ConstrExpr ::= SelfInvocation
399399
| ‘{’ SelfInvocation {semi BlockStat} ‘}’
400400
SelfInvocation ::= ‘this’ ArgumentExprs {ArgumentExprs}
401401
402-
TemplateBody ::= [nl | ‘with’]
403-
‘{’ [SelfType] TemplateStat {semi TemplateStat} ‘}’ (self, stats)
402+
TemplateBody ::= [nl] ‘{’ [SelfType] TemplateStat {semi TemplateStat} ‘}’ (self, stats)
404403
TemplateStat ::= Import
405404
| Export
406405
| {Annotation [nl]} {Modifier} Def

tests/neg/new-with.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val x = new with { } // error

0 commit comments

Comments
 (0)