@@ -770,14 +770,9 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
770
770
}
771
771
}
772
772
773
- def memberDecl (mods : Modifiers , parentToken : Int ): List [Tree ] = {
774
- in.token match {
775
- case CLASS | ENUM | RECORD | INTERFACE | AT =>
776
- typeDecl(mods)
777
- case _ =>
778
- termDecl(mods, parentToken)
779
- }
780
- }
773
+ def memberDecl (mods : Modifiers , parentToken : Int ): List [Tree ] =
774
+ if (isTypeDeclStart()) typeDecl(mods)
775
+ else termDecl(mods, parentToken)
781
776
782
777
def makeCompanionObject (cdef : ClassDef , statics : List [Tree ]): Tree =
783
778
atPos(cdef.pos) {
@@ -1061,6 +1056,13 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
1061
1056
(res, hasClassBody)
1062
1057
}
1063
1058
1059
+ def isTypeDeclStart (): Boolean = {
1060
+ adaptRecordIdentifier()
1061
+ in.token match {
1062
+ case ENUM | INTERFACE | AT | CLASS | RECORD => true
1063
+ case _ => false
1064
+ }
1065
+ }
1064
1066
def typeDecl (mods : Modifiers ): List [Tree ] = {
1065
1067
adaptRecordIdentifier()
1066
1068
in.token match {
@@ -1092,6 +1094,14 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
1092
1094
/** CompilationUnit ::= [[Annotation] package QualId semi] {Import} {TypeDecl} //TopStatSeq
1093
1095
*/
1094
1096
def compilationUnit (): Tree = {
1097
+ var compact = false
1098
+ def typeDeclOrCompact (mods : Modifiers ): List [Tree ] =
1099
+ if (isTypeDeclStart()) typeDecl(mods)
1100
+ else {
1101
+ val ts = termDecl(mods, CLASS )
1102
+ if (ts.nonEmpty) compact = true
1103
+ Nil
1104
+ }
1095
1105
val buf = ListBuffer .empty[Tree ]
1096
1106
var pos = in.currentPos
1097
1107
val leadingAnnots = if (in.token == AT ) annotations() else Nil
@@ -1107,7 +1117,7 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
1107
1117
}
1108
1118
else {
1109
1119
if (! leadingAnnots.isEmpty)
1110
- buf ++= typeDecl (modifiers(inInterface = false , annots0 = leadingAnnots))
1120
+ buf ++= typeDeclOrCompact (modifiers(inInterface = false , annots0 = leadingAnnots))
1111
1121
Ident (nme.EMPTY_PACKAGE_NAME )
1112
1122
}
1113
1123
thisPackageName = gen.convertToTypeName(pkg) match {
@@ -1120,10 +1130,11 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
1120
1130
while (in.token != EOF && in.token != RBRACE ) {
1121
1131
while (in.token == SEMI ) in.nextToken()
1122
1132
if (in.token != EOF )
1123
- buf ++= typeDecl (modifiers(inInterface = false ))
1133
+ buf ++= typeDeclOrCompact (modifiers(inInterface = false ))
1124
1134
}
1125
1135
accept(EOF )
1126
- atPos(pos) {
1136
+ if (compact) EmptyTree
1137
+ else atPos(pos) {
1127
1138
makePackaging(pkg, buf.toList)
1128
1139
}
1129
1140
}
0 commit comments