@@ -26,7 +26,7 @@ import Decorators._
26
26
import scala .internal .Chars
27
27
import scala .annotation .{tailrec , switch }
28
28
import rewrites .Rewrites .{patch , overlapsPatch }
29
- import config .Config .silentTemplateIdent
29
+ import config .Config .silentTemplateIndent
30
30
31
31
object Parsers {
32
32
@@ -338,6 +338,9 @@ object Parsers {
338
338
offset
339
339
}
340
340
341
+ def reportMissing (expected : Token ): Unit =
342
+ syntaxError(ExpectedTokenButFound (expected, in.token))
343
+
341
344
/** semi = nl {nl} | `;'
342
345
* nl = `\n' // where allowed
343
346
*/
@@ -357,10 +360,6 @@ object Parsers {
357
360
accept(SEMI )
358
361
}
359
362
360
- def acceptNested (): Unit =
361
- if in.token != LBRACE && in.token != INDENT then
362
- syntaxError(i " indented definitions or `{' expected " )
363
-
364
363
/** Under -language:Scala2 or -old-syntax, flag
365
364
*
366
365
* extends p1 with new p1 with t1 with
@@ -1266,13 +1265,14 @@ object Parsers {
1266
1265
newLineOptWhenFollowedBy(LBRACE )
1267
1266
}
1268
1267
1269
- def possibleTemplateStart (): Unit =
1268
+ def possibleTemplateStart (isNew : Boolean = false ): Unit =
1270
1269
if in.token == WITH then
1271
1270
in.nextToken()
1272
- acceptNested()
1273
- else if silentTemplateIdent then
1274
- in.observeIndented()
1275
- newLineOptWhenFollowedBy(LBRACE )
1271
+ if in.token != LBRACE && in.token != INDENT then
1272
+ syntaxError(i " indented definitions or `{' expected " )
1273
+ else
1274
+ if silentTemplateIndent && ! isNew then in.observeIndented()
1275
+ newLineOptWhenFollowedBy(LBRACE )
1276
1276
1277
1277
def indentRegion [T ](tag : EndMarkerTag )(op : => T ): T = {
1278
1278
val iw = in.currentRegion.indentWidth
@@ -1700,7 +1700,7 @@ object Parsers {
1700
1700
else
1701
1701
if (altToken == THEN || enclosedInParens) && in.isNewLine then
1702
1702
in.observeIndented()
1703
- if ! enclosedInParens && in.token != INDENT then accept (altToken)
1703
+ if ! enclosedInParens && in.token != INDENT then reportMissing (altToken)
1704
1704
if (rewriteToNewSyntax(t.span))
1705
1705
dropParensOrBraces(t.span.start, s " ${tokenString(altToken)}" )
1706
1706
t
@@ -2158,12 +2158,10 @@ object Parsers {
2158
2158
def reposition (t : Tree ) = t.withSpan(Span (start, in.lastOffset))
2159
2159
possibleBracesStart()
2160
2160
val parents =
2161
- if in.token == WITH then
2162
- possibleTemplateStart()
2163
- Nil
2164
- else if in.token == LBRACE then Nil
2161
+ if in.token == LBRACE || in.token == WITH then Nil
2165
2162
else constrApps(commaOK = false , templateCanFollow = true )
2166
- possibleBracesStart()
2163
+ colonAtEOLOpt()
2164
+ possibleTemplateStart(isNew = true )
2167
2165
parents match {
2168
2166
case parent :: Nil if ! in.isNestedStart =>
2169
2167
reposition(if (parent.isType) ensureApplied(wrapNew(parent)) else parent)
@@ -3470,9 +3468,12 @@ object Parsers {
3470
3468
val t = constrApp()
3471
3469
val ts =
3472
3470
if in.token == WITH then
3473
- in.nextToken()
3474
- if templateCanFollow && (in.token == LBRACE || in.token == INDENT ) then Nil
3471
+ val lookahead = in.LookaheadScanner (indent = true )
3472
+ lookahead.nextToken()
3473
+ if templateCanFollow && (lookahead.token == LBRACE || lookahead.token == INDENT ) then
3474
+ Nil
3475
3475
else
3476
+ in.nextToken()
3476
3477
checkNotWithAtEOL()
3477
3478
constrApps(commaOK, templateCanFollow)
3478
3479
else if commaOK && in.token == COMMA then
@@ -3481,10 +3482,11 @@ object Parsers {
3481
3482
else Nil
3482
3483
t :: ts
3483
3484
3484
- /** InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]
3485
+ /** Template ::= InheritClauses [TemplateBody]
3486
+ * InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]
3485
3487
*/
3486
- def inheritClauses () : ( List [ Tree ], List [ Tree ]) = {
3487
- val extended =
3488
+ def template ( constr : DefDef , isEnum : Boolean = false ) : Template = {
3489
+ val parents =
3488
3490
if (in.token == EXTENDS ) {
3489
3491
in.nextToken()
3490
3492
if (in.token == LBRACE || in.token == COLONEOL ) {
@@ -3494,20 +3496,13 @@ object Parsers {
3494
3496
else constrApps(commaOK = true , templateCanFollow = true )
3495
3497
}
3496
3498
else Nil
3499
+ newLinesOptWhenFollowedBy(nme.derives )
3497
3500
val derived =
3498
3501
if (isIdent(nme.derives )) {
3499
3502
in.nextToken()
3500
3503
tokenSeparated(COMMA , () => convertToTypeId(qualId()))
3501
3504
}
3502
3505
else Nil
3503
- (extended, derived)
3504
- }
3505
-
3506
- /** Template ::= InheritClauses [TemplateBody]
3507
- */
3508
- def template (constr : DefDef , isEnum : Boolean = false ): Template = {
3509
- newLinesOptWhenFollowedBy(nme.derives )
3510
- val (parents, derived) = inheritClauses()
3511
3506
possibleTemplateStart()
3512
3507
if (isEnum) {
3513
3508
val (self, stats) = withinEnum(templateBody())
0 commit comments