Skip to content

Commit a715762

Browse files
committed
Refactoring: don't consume final with in constrApps
1 parent 42409bf commit a715762

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

compiler/src/dotty/tools/dotc/config/Config.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ object Config {
161161
final val defaultIndent = true
162162

163163
/** Assume indentation is significant after a class, object, ... signature */
164-
final val silentTemplateIdent = true
164+
final val silentTemplateIndent = true
165165

166166
/** If set, prints a trace of all symbol completions */
167167
final val showCompletions = false

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

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Decorators._
2626
import scala.internal.Chars
2727
import scala.annotation.{tailrec, switch}
2828
import rewrites.Rewrites.{patch, overlapsPatch}
29-
import config.Config.silentTemplateIdent
29+
import config.Config.silentTemplateIndent
3030

3131
object Parsers {
3232

@@ -338,6 +338,9 @@ object Parsers {
338338
offset
339339
}
340340

341+
def reportMissing(expected: Token): Unit =
342+
syntaxError(ExpectedTokenButFound(expected, in.token))
343+
341344
/** semi = nl {nl} | `;'
342345
* nl = `\n' // where allowed
343346
*/
@@ -357,10 +360,6 @@ object Parsers {
357360
accept(SEMI)
358361
}
359362

360-
def acceptNested(): Unit =
361-
if in.token != LBRACE && in.token != INDENT then
362-
syntaxError(i"indented definitions or `{' expected")
363-
364363
/** Under -language:Scala2 or -old-syntax, flag
365364
*
366365
* extends p1 with new p1 with t1 with
@@ -1266,13 +1265,14 @@ object Parsers {
12661265
newLineOptWhenFollowedBy(LBRACE)
12671266
}
12681267

1269-
def possibleTemplateStart(): Unit =
1268+
def possibleTemplateStart(isNew: Boolean = false): Unit =
12701269
if in.token == WITH then
12711270
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)
12761276

12771277
def indentRegion[T](tag: EndMarkerTag)(op: => T): T = {
12781278
val iw = in.currentRegion.indentWidth
@@ -1700,7 +1700,7 @@ object Parsers {
17001700
else
17011701
if (altToken == THEN || enclosedInParens) && in.isNewLine then
17021702
in.observeIndented()
1703-
if !enclosedInParens && in.token != INDENT then accept(altToken)
1703+
if !enclosedInParens && in.token != INDENT then reportMissing(altToken)
17041704
if (rewriteToNewSyntax(t.span))
17051705
dropParensOrBraces(t.span.start, s"${tokenString(altToken)}")
17061706
t
@@ -2158,12 +2158,10 @@ object Parsers {
21582158
def reposition(t: Tree) = t.withSpan(Span(start, in.lastOffset))
21592159
possibleBracesStart()
21602160
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
21652162
else constrApps(commaOK = false, templateCanFollow = true)
2166-
possibleBracesStart()
2163+
colonAtEOLOpt()
2164+
possibleTemplateStart(isNew = true)
21672165
parents match {
21682166
case parent :: Nil if !in.isNestedStart =>
21692167
reposition(if (parent.isType) ensureApplied(wrapNew(parent)) else parent)
@@ -3470,9 +3468,12 @@ object Parsers {
34703468
val t = constrApp()
34713469
val ts =
34723470
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
34753475
else
3476+
in.nextToken()
34763477
checkNotWithAtEOL()
34773478
constrApps(commaOK, templateCanFollow)
34783479
else if commaOK && in.token == COMMA then
@@ -3481,10 +3482,11 @@ object Parsers {
34813482
else Nil
34823483
t :: ts
34833484

3484-
/** InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]
3485+
/** Template ::= InheritClauses [TemplateBody]
3486+
* InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]
34853487
*/
3486-
def inheritClauses(): (List[Tree], List[Tree]) = {
3487-
val extended =
3488+
def template(constr: DefDef, isEnum: Boolean = false): Template = {
3489+
val parents =
34883490
if (in.token == EXTENDS) {
34893491
in.nextToken()
34903492
if (in.token == LBRACE || in.token == COLONEOL) {
@@ -3494,20 +3496,13 @@ object Parsers {
34943496
else constrApps(commaOK = true, templateCanFollow = true)
34953497
}
34963498
else Nil
3499+
newLinesOptWhenFollowedBy(nme.derives)
34973500
val derived =
34983501
if (isIdent(nme.derives)) {
34993502
in.nextToken()
35003503
tokenSeparated(COMMA, () => convertToTypeId(qualId()))
35013504
}
35023505
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()
35113506
possibleTemplateStart()
35123507
if (isEnum) {
35133508
val (self, stats) = withinEnum(templateBody())

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,8 @@ object Scanners {
869869

870870
// Lookahead ---------------------------------------------------------------
871871

872-
class LookaheadScanner extends Scanner(source, offset) {
873-
override val indentSyntax = false
872+
class LookaheadScanner(indent: Boolean = false) extends Scanner(source, offset) {
873+
override val indentSyntax = indent
874874
override protected def printState() = {
875875
print("la:")
876876
super.printState()

0 commit comments

Comments
 (0)