Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
povder committed Nov 4, 2023
1 parent e9b364a commit 6d1de70
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
12 changes: 5 additions & 7 deletions compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -1117,7 +1116,6 @@ object JavaParsers {
}
}
}

val unit = atSpan(start) { PackageDef(pkg, (buf ++ typesBuf).toList) }
accept(EOF)
unit match
Expand Down
32 changes: 9 additions & 23 deletions compiler/test/dotty/tools/dotc/parsing/JavaJep445ParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand All @@ -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 =
Expand Down

0 comments on commit 6d1de70

Please sign in to comment.