From 6d1de7083ffc104be15b3d73368c9336030f2396 Mon Sep 17 00:00:00 2001 From: "Chris (Krzysztof) Pado" Date: Sat, 4 Nov 2023 14:26:14 -0700 Subject: [PATCH] wip --- .../tools/dotc/parsing/JavaParsers.scala | 12 +++---- .../dotc/parsing/JavaJep445ParserTest.scala | 32 ++++++------------- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala b/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala index bdcf190f590c..e96e339ad5f2 100644 --- a/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala @@ -826,9 +826,9 @@ object JavaParsers { addCompanionObject(statics, cls) } - def unnamedClassDecl(priorTypes: List[Tree], start: Offset): List[Tree] = { + def unnamedClassDecl(priorTypes: List[Tree], firstMemberMods: Modifiers, start: Offset): List[Tree] = { val name = source.name.replaceAll("\\.java$", "").nn.toTypeName - val (statics, body) = typeBodyDecls(CLASS, name, Nil) + val (statics, body) = typeBodyDecls(CLASS, name, parentTParams = Nil, firstMemberMods = Some(firstMemberMods)) val (priorStatics, priorBody) = priorTypes.partition { case t: TypeDef => t.mods.is(Flags.JavaStatic) @@ -920,13 +920,13 @@ object JavaParsers { defs } - def typeBodyDecls(parentToken: Int, parentName: Name, parentTParams: List[TypeDef]): (List[Tree], List[Tree]) = { + def typeBodyDecls(parentToken: Int, parentName: Name, parentTParams: List[TypeDef], firstMemberMods: Option[Modifiers] = None): (List[Tree], List[Tree]) = { val inInterface = definesInterface(parentToken) val statics = new ListBuffer[Tree] val members = new ListBuffer[Tree] while (in.token != RBRACE && in.token != EOF) { val start = in.offset - var mods = modifiers(inInterface) + var mods = (if (statics.isEmpty && members.isEmpty) firstMemberMods else None).getOrElse(modifiers(inInterface)) if (in.token == LBRACE) { skipAhead() // skip init block, we just assume we have seen only static accept(RBRACE) @@ -1105,8 +1105,7 @@ object JavaParsers { if (thisPackageName == tpnme.EMPTY_PACKAGE) { // upon encountering non-types directly at a compilation unit level in an unnamed package, // the entire compilation unit is treated as a JEP-445 unnamed class - //TODO support @annotated members of unnamed class - val cls = unnamedClassDecl(priorTypes = typesBuf.toList, start = afterImports) + val cls = unnamedClassDecl(priorTypes = typesBuf.toList, firstMemberMods = mods, start = afterImports) typesBuf.clear() typesBuf ++= cls } else { @@ -1117,7 +1116,6 @@ object JavaParsers { } } } - val unit = atSpan(start) { PackageDef(pkg, (buf ++ typesBuf).toList) } accept(EOF) unit match diff --git a/compiler/test/dotty/tools/dotc/parsing/JavaJep445ParserTest.scala b/compiler/test/dotty/tools/dotc/parsing/JavaJep445ParserTest.scala index c2bd1fa50532..bb81df914dd4 100644 --- a/compiler/test/dotty/tools/dotc/parsing/JavaJep445ParserTest.scala +++ b/compiler/test/dotty/tools/dotc/parsing/JavaJep445ParserTest.scala @@ -20,8 +20,9 @@ class JavaJep445ParserTest extends DottyTest { | |import some.pkg.*; | - |private volatile int x = 0; |@Magic + |private volatile int x = 0; + | |protected String s = "s"; | |void main() {} @@ -36,39 +37,24 @@ class JavaJep445ParserTest extends DottyTest { fail("TODO") } - @Test def `treats leading top-level types as nested types of unnamed class`: Unit = { + @Test def `treats leading top-level types as nested types of unnamed class` + : Unit = { val code = s""" |// hello | |import some.pkg.*; | - |interface Inner {} + |@interface InnerAnnotation {} | - |static class InnerStatic {} + |interface InnerInterface {} | - |void main() {} - |""".stripMargin - - val parser = - JavaParsers.JavaParser(SourceFile.virtual("MyUnnamed.java", code)) - val tree = parser.parse() - - println(tree.show) - - fail("TODO") - } - - @Test def `treats leading top-level annotated vars as members of unnamed class`: Unit = { - val code = - s""" + |static class InnerStaticClass {} | - |import some.pkg.*; + |void main() {} | - |@MyAnnotation - |int x = 0; + |interface SecondInnerInterface {} | - |void main() {} |""".stripMargin val parser =