@@ -440,11 +440,11 @@ object JavaParsers {
440
440
}
441
441
}
442
442
443
- def modifiers (inInterface : Boolean ): Modifiers = {
443
+ def modifiers (inInterface : Boolean , annots0 : List [ Tree ] = Nil ): Modifiers = {
444
444
var flags : FlagSet = Flags .JavaDefined
445
445
// assumed true unless we see public/private/protected
446
446
var isPackageAccess = true
447
- var annots = new ListBuffer [Tree ]
447
+ var annots = ListBuffer .from [Tree ](annots0)
448
448
def addAnnot (tpt : Tree ) =
449
449
annots += atSpan(in.offset) {
450
450
in.nextToken()
@@ -453,7 +453,7 @@ object JavaParsers {
453
453
454
454
while (true )
455
455
in.token match {
456
- case AT if ( in.lookaheadToken != INTERFACE ) =>
456
+ case AT if in.lookaheadToken != INTERFACE =>
457
457
in.nextToken()
458
458
annotation() match {
459
459
case Some (anno) => annots += anno
@@ -1080,24 +1080,30 @@ object JavaParsers {
1080
1080
/** CompilationUnit ::= [package QualId semi] TopStatSeq
1081
1081
*/
1082
1082
def compilationUnit (): Tree = {
1083
- val start = in.offset
1083
+ val buf = ListBuffer .empty[Tree ]
1084
+ var start = in.offset
1085
+ val leadingAnnots = if (in.token == AT ) annotations() else Nil
1084
1086
val pkg : RefTree =
1085
- if (in.token == AT || in.token == PACKAGE ) {
1086
- annotations()
1087
+ if in.token == PACKAGE then
1088
+ if ! leadingAnnots.isEmpty then
1089
+ // if (unit.source.file.name != "package-info.java")
1090
+ // syntaxError(pos, "package annotations must be in file package-info.java")
1091
+ start = in.offset
1087
1092
accept(PACKAGE )
1088
1093
val pkg = qualId()
1089
1094
accept(SEMI )
1090
1095
pkg
1091
- }
1092
1096
else
1097
+ if ! leadingAnnots.isEmpty then
1098
+ buf ++= typeDecl(start, modifiers(inInterface = false , annots0 = leadingAnnots))
1093
1099
Ident (nme.EMPTY_PACKAGE )
1094
1100
thisPackageName = convertToTypeName(pkg) match {
1095
1101
case Some (t) => t.name.toTypeName
1096
1102
case _ => tpnme.EMPTY
1097
1103
}
1098
- val buf = new ListBuffer [ Tree ]
1099
- while ( in.token == IMPORT )
1100
- buf ++= importDecl()
1104
+ if ( buf.isEmpty)
1105
+ while in.token == IMPORT do
1106
+ buf ++= importDecl()
1101
1107
while (in.token != EOF && in.token != RBRACE ) {
1102
1108
while (in.token == SEMI ) in.nextToken()
1103
1109
if (in.token != EOF ) {
0 commit comments