@@ -196,7 +196,6 @@ object Parsers {
196
196
def isTemplateIntro = templateIntroTokens contains in.token
197
197
def isDclIntro = dclIntroTokens contains in.token
198
198
def isStatSeqEnd = in.isNestedEnd || in.token == EOF || in.token == RPAREN
199
- def isTemplateBodyStart = in.token == WITH || in.isNestedStart
200
199
def mustStartStat = mustStartStatTokens contains in.token
201
200
202
201
/** Is current token a hard or soft modifier (in modifier position or not)? */
@@ -920,39 +919,6 @@ object Parsers {
920
919
val next = in.lookahead.token
921
920
next == LBRACKET || next == LPAREN
922
921
923
- /** Does a template start after `with`? This is the case if either
924
- * - the next token is `{`
925
- * - the `with` is at the end of a line
926
- * (except for source = 3.0-migration, when a warning is issued)
927
- * - the next tokens is `<ident>` or `this` and the one after it is `:` or `=>`
928
- * (i.e. we see the start of a self type)
929
- */
930
- def followingIsTemplateStart () =
931
- val lookahead = in.LookaheadScanner ()
932
- lookahead.nextToken()
933
- lookahead.token == LBRACE
934
- || lookahead.isAfterLineEnd
935
- && {
936
- if migrateTo3 then
937
- warning(
938
- em """ In Scala 3, `with` at the end of a line will start definitions,
939
- |so it cannot be used in front of a parent constructor anymore.
940
- |Place the `with` at the beginning of the next line instead. """ )
941
- false
942
- else
943
- true
944
- }
945
- || (lookahead.isIdent || lookahead.token == THIS )
946
- && {
947
- lookahead.nextToken()
948
- lookahead.token == COLON
949
- && { // needed only as long as we support significant colon at eol
950
- lookahead.nextToken()
951
- ! lookahead.isAfterLineEnd
952
- }
953
- || lookahead.token == ARROW
954
- }
955
-
956
922
/* --------- OPERAND/OPERATOR STACK --------------------------------------- */
957
923
958
924
var opStack : List [OpInfo ] = Nil
@@ -1315,14 +1281,14 @@ object Parsers {
1315
1281
in.sourcePos())
1316
1282
patch(source, Span (in.offset), " " )
1317
1283
1318
- def possibleTemplateStart (): Unit =
1284
+ def possibleTemplateStart (isNew : Boolean = false ): Unit =
1319
1285
in.observeColonEOL()
1320
- if in.token == COLONEOL then
1286
+ if in.token == COLONEOL || in.token == WITH then
1321
1287
if in.lookahead.isIdent(nme.end) then in.token = NEWLINE
1322
1288
else
1323
1289
in.nextToken()
1324
1290
if in.token != INDENT && in.token != LBRACE then
1325
- syntaxErrorOrIncomplete(ExpectedTokenButFound ( INDENT , in.token) )
1291
+ syntaxErrorOrIncomplete(i " indented definitions expected, ${in} found " )
1326
1292
else
1327
1293
newLineOptWhenFollowedBy(LBRACE )
1328
1294
@@ -2347,11 +2313,13 @@ object Parsers {
2347
2313
val start = in.skipToken()
2348
2314
def reposition (t : Tree ) = t.withSpan(Span (start, in.lastOffset))
2349
2315
possibleTemplateStart()
2350
- val parents = if isTemplateBodyStart then Nil else constrApp() :: withConstrApps()
2316
+ val parents =
2317
+ if in.isNestedStart then Nil
2318
+ else constrApp() :: withConstrApps()
2351
2319
colonAtEOLOpt()
2352
- possibleTemplateStart()
2320
+ possibleTemplateStart(isNew = true )
2353
2321
parents match {
2354
- case parent :: Nil if ! isTemplateBodyStart =>
2322
+ case parent :: Nil if ! in.isNestedStart =>
2355
2323
reposition(if (parent.isType) ensureApplied(wrapNew(parent)) else parent)
2356
2324
case _ =>
2357
2325
New (reposition(templateBodyOpt(emptyConstructor, parents, Nil )))
@@ -3675,7 +3643,21 @@ object Parsers {
3675
3643
/** `{`with` ConstrApp} but no EOL allowed after `with`.
3676
3644
*/
3677
3645
def withConstrApps (): List [Tree ] =
3678
- if in.token == WITH && ! followingIsTemplateStart() then
3646
+ def isTemplateStart =
3647
+ val la = in.lookahead
3648
+ la.token == LBRACE
3649
+ || la.isAfterLineEnd
3650
+ && {
3651
+ if migrateTo3 then
3652
+ warning(
3653
+ em """ In Scala 3, `with` at the end of a line will start definitions,
3654
+ |so it cannot be used in front of a parent constructor anymore.
3655
+ |Place the `with` at the beginning of the next line instead. """ )
3656
+ false
3657
+ else
3658
+ true
3659
+ }
3660
+ if in.token == WITH && ! isTemplateStart then
3679
3661
in.nextToken()
3680
3662
constrApp() :: withConstrApps()
3681
3663
else Nil
@@ -3719,66 +3701,32 @@ object Parsers {
3719
3701
template(constr)
3720
3702
else
3721
3703
possibleTemplateStart()
3722
- if isTemplateBodyStart then
3704
+ if in.isNestedStart then
3723
3705
template(constr)
3724
3706
else
3725
3707
checkNextNotIndented()
3726
3708
Template (constr, Nil , Nil , EmptyValDef , Nil )
3727
3709
3728
- /** TemplateBody ::= [nl | ‘with’] `{' TemplateStatSeq `}'
3729
- * | ‘with’ [SelfType] indent TemplateStats outdent
3730
- * EnumBody ::= [nl | ‘with’] ‘{’ [SelfType] EnumStats ‘}’
3731
- * | ‘with’ [SelfType] indent EnumStats outdent
3710
+ /** TemplateBody ::= [nl] `{' TemplateStatSeq `}'
3711
+ * EnumBody ::= [nl] ‘{’ [SelfType] EnumStat {semi EnumStat} ‘}’
3732
3712
*/
3733
3713
def templateBodyOpt (constr : DefDef , parents : List [Tree ], derived : List [Tree ]): Template =
3734
3714
val (self, stats) =
3735
- if isTemplateBodyStart then
3715
+ if in.isNestedStart then
3736
3716
templateBody()
3737
3717
else
3738
3718
checkNextNotIndented()
3739
3719
(EmptyValDef , Nil )
3740
3720
Template (constr, parents, derived, self, stats)
3741
3721
3742
3722
def templateBody (): (ValDef , List [Tree ]) =
3743
- val givenSelf =
3744
- if in.token == WITH then
3745
- in.nextToken()
3746
- selfDefOpt()
3747
- else EmptyValDef
3748
- val r = inDefScopeBraces(templateStatSeq(givenSelf), rewriteWithColon = true )
3723
+ val r = inDefScopeBraces(templateStatSeq(), rewriteWithColon = true )
3749
3724
if in.token == WITH then
3750
3725
syntaxError(EarlyDefinitionsNotSupported ())
3751
3726
in.nextToken()
3752
3727
template(emptyConstructor)
3753
3728
r
3754
3729
3755
- /** SelfType ::= id [‘:’ InfixType] ‘=>’
3756
- * | ‘this’ ‘:’ InfixType ‘=>’
3757
- * Only called immediately after a `with`, in which case it must in turn
3758
- * be followed by `INDENT`.
3759
- */
3760
- def selfDefOpt (): ValDef = atSpan(in.offset) {
3761
- val vd =
3762
- if in.isIdent then
3763
- val selfName = ident()
3764
- if in.token == COLON then
3765
- in.nextToken()
3766
- makeSelfDef(selfName, infixType())
3767
- else
3768
- makeSelfDef(selfName, TypeTree ())
3769
- else if in.token == THIS then
3770
- in.nextToken()
3771
- accept(COLON )
3772
- makeSelfDef(nme.WILDCARD , infixType())
3773
- else
3774
- EmptyValDef
3775
- if ! vd.isEmpty then
3776
- accept(ARROW )
3777
- if in.token != INDENT then
3778
- syntaxErrorOrIncomplete(ExpectedTokenButFound (INDENT , in.token))
3779
- vd
3780
- }
3781
-
3782
3730
/** with Template, with EOL <indent> interpreted */
3783
3731
def withTemplate (constr : DefDef , parents : List [Tree ]): Template =
3784
3732
if in.token != WITH then syntaxError(em " `with` expected " )
@@ -3852,10 +3800,10 @@ object Parsers {
3852
3800
* EnumStat ::= TemplateStat
3853
3801
* | Annotations Modifiers EnumCase
3854
3802
*/
3855
- def templateStatSeq (givenSelf : ValDef = EmptyValDef ): (ValDef , List [Tree ]) = checkNoEscapingPlaceholders {
3856
- var self = givenSelf
3803
+ def templateStatSeq (): (ValDef , List [Tree ]) = checkNoEscapingPlaceholders {
3804
+ var self : ValDef = EmptyValDef
3857
3805
val stats = new ListBuffer [Tree ]
3858
- if (self.isEmpty && isExprIntro && ! isDefIntro(modifierTokens)) {
3806
+ if (isExprIntro && ! isDefIntro(modifierTokens)) {
3859
3807
val first = expr1()
3860
3808
if (in.token == ARROW ) {
3861
3809
first match {
@@ -4008,7 +3956,7 @@ object Parsers {
4008
3956
possibleTemplateStart()
4009
3957
if in.token == EOF then
4010
3958
ts += makePackaging(start, pkg, List ())
4011
- else if isTemplateBodyStart then
3959
+ else if in.isNestedStart then
4012
3960
ts += inDefScopeBraces(makePackaging(start, pkg, topStatSeq()), rewriteWithColon = true )
4013
3961
continue = true
4014
3962
else
@@ -4046,7 +3994,6 @@ object Parsers {
4046
3994
}
4047
3995
4048
3996
override def templateBody (): (ValDef , List [Thicket ]) = {
4049
- if in.token == WITH then in.nextToken()
4050
3997
skipBraces()
4051
3998
(EmptyValDef , List (EmptyTree ))
4052
3999
}
0 commit comments