Skip to content

Commit bddf379

Browse files
committed
[JEP-409] Add support for sealed and non-sealed templates in parser
1 parent 55c2002 commit bddf379

File tree

7 files changed

+40
-4
lines changed

7 files changed

+40
-4
lines changed

Diff for: compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala

+16
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,11 @@ object JavaParsers {
483483
addAnnot(scalaDot(jtpnme.VOLATILEkw))
484484
case SYNCHRONIZED | STRICTFP =>
485485
in.nextToken()
486+
case SEALED =>
487+
//flags |= Flags.Sealed
488+
in.nextToken()
489+
case NON_SEALED =>
490+
in.nextToken()
486491
case _ =>
487492
val privateWithin: TypeName =
488493
if (isPackageAccess && !inInterface) thisPackageName
@@ -806,6 +811,15 @@ object JavaParsers {
806811
else
807812
List()
808813

814+
815+
def permittedSubclassesOpt(isSealed: Boolean) : List[Tree] =
816+
if in.token == PERMITS then
817+
in.nextToken()
818+
repsep(() => typ(), COMMA)
819+
else
820+
if isSealed then syntaxError(em"sealed class must have subclasses", false)
821+
Nil
822+
809823
def classDecl(start: Offset, mods: Modifiers): List[Tree] = {
810824
accept(CLASS)
811825
val nameOffset = in.offset
@@ -819,6 +833,7 @@ object JavaParsers {
819833
else
820834
javaLangObject()
821835
val interfaces = interfacesOpt()
836+
val permittedSubclasses = permittedSubclassesOpt(mods.is(Flags.Sealed))
822837
val (statics, body) = typeBody(CLASS, name, tparams)
823838
val cls = atSpan(start, nameOffset) {
824839
TypeDef(name, makeTemplate(superclass :: interfaces, body, tparams, true)).withMods(mods)
@@ -883,6 +898,7 @@ object JavaParsers {
883898
}
884899
else
885900
List(javaLangObject())
901+
val permittedSubclasses = permittedSubclassesOpt(mods is Flags.Sealed)
886902
val (statics, body) = typeBody(INTERFACE, name, tparams)
887903
val iface = atSpan(start, nameOffset) {
888904
TypeDef(

Diff for: compiler/src/dotty/tools/dotc/parsing/JavaScanners.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,10 @@ object JavaScanners {
393393
'5' | '6' | '7' | '8' | '9' =>
394394
putChar(ch)
395395
nextChar()
396-
396+
case '-' =>
397+
// TODO HR : Add lookahead to only allow the 'non-sealed keyword'
398+
putChar(ch)
399+
nextChar()
397400
case '_' =>
398401
putChar(ch)
399402
nextChar()

Diff for: compiler/src/dotty/tools/dotc/parsing/JavaTokens.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object JavaTokens extends TokensCommon {
1010

1111
final val javaOnlyKeywords: TokenSet = tokenRange(INSTANCEOF, ASSERT)
1212
final val sharedKeywords: BitSet = BitSet( IF, FOR, ELSE, THIS, NULL, NEW, SUPER, ABSTRACT, FINAL, PRIVATE, PROTECTED,
13-
EXTENDS, TRUE, FALSE, CLASS, IMPORT, PACKAGE, DO, THROW, TRY, CATCH, FINALLY, WHILE, RETURN )
13+
EXTENDS, TRUE, FALSE, CLASS, IMPORT, PACKAGE, DO, THROW, TRY, CATCH, FINALLY, WHILE, RETURN, SEALED)
1414
final val primTypes: TokenSet = tokenRange(VOID, DOUBLE)
1515
final val keywords: BitSet = sharedKeywords | javaOnlyKeywords | primTypes
1616

@@ -22,6 +22,8 @@ object JavaTokens extends TokensCommon {
2222
inline val INTERFACE = 105; enter(INTERFACE, "interface")
2323
inline val ENUM = 106; enter(ENUM, "enum")
2424
inline val IMPLEMENTS = 107; enter(IMPLEMENTS, "implements")
25+
inline val PERMITS = 108; enter(PERMITS, "permits")
26+
inline val NON_SEALED = 109; enter(NON_SEALED, "non-sealed")
2527

2628
/** modifiers */
2729
inline val PUBLIC = 110; enter(PUBLIC, "public")

Diff for: compiler/src/dotty/tools/dotc/parsing/Tokens.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ abstract class TokensCommon {
7878
//inline val YIELD = 48; enter(YIELD, "yield")
7979
inline val DO = 49; enter(DO, "do")
8080
//inline val TRAIT = 50; enter(TRAIT, "trait")
81-
//inline val SEALED = 51; enter(SEALED, "sealed")
81+
inline val SEALED = 51; enter(SEALED, "sealed")
8282
inline val THROW = 52; enter(THROW, "throw")
8383
inline val TRY = 53; enter(TRY, "try")
8484
inline val CATCH = 54; enter(CATCH, "catch")
@@ -169,7 +169,7 @@ object Tokens extends TokensCommon {
169169
inline val OBJECT = 44; enter(OBJECT, "object")
170170
inline val YIELD = 48; enter(YIELD, "yield")
171171
inline val TRAIT = 50; enter(TRAIT, "trait")
172-
inline val SEALED = 51; enter(SEALED, "sealed")
172+
//inline val SEALED = 51; enter(SEALED, "sealed")
173173
inline val MATCH = 58; enter(MATCH, "match")
174174
inline val LAZY = 59; enter(LAZY, "lazy")
175175
inline val THEN = 60; enter(THEN, "then")

Diff for: tests/pos/i18533/Cat.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package i18533;
2+
3+
public final class Cat extends Pet {
4+
5+
}

Diff for: tests/pos/i18533/Dog.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package i18533;
2+
3+
public non-sealed class Dog extends Pet {
4+
5+
}

Diff for: tests/pos/i18533/Pet.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package i18533;
2+
3+
public sealed class Pet permits Cat, Dog {
4+
5+
}

0 commit comments

Comments
 (0)