Skip to content

Commit 7342196

Browse files
committed
Fix parsing Java annotation values
1 parent 2e05d9e commit 7342196

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,6 @@ object JavaParsers {
359359
* but instead we skip entire annotation silently.
360360
*/
361361
def annotation(): Option[Tree] = {
362-
object LiteralT:
363-
def unapply(token: Token) = Option(token match {
364-
case TRUE => true
365-
case FALSE => false
366-
case CHARLIT => in.name(0)
367-
case INTLIT => in.intVal(false).toInt
368-
case LONGLIT => in.intVal(false)
369-
case FLOATLIT => in.floatVal(false).toFloat
370-
case DOUBLELIT => in.floatVal(false)
371-
case STRINGLIT => in.name.toString
372-
case _ => null
373-
}).map(Constant(_))
374-
375362
def classOrId(): Tree =
376363
val id = qualId()
377364
if in.lookaheadToken == CLASS then
@@ -398,17 +385,17 @@ object JavaParsers {
398385
}
399386

400387
def argValue(): Option[Tree] =
401-
val tree = in.token match {
402-
case LiteralT(c) =>
403-
val tree = atSpan(in.offset)(Literal(c))
404-
in.nextToken()
405-
Some(tree)
406-
case AT =>
407-
in.nextToken()
408-
annotation()
409-
case IDENTIFIER => Some(classOrId())
410-
case LBRACE => array()
411-
case _ => None
388+
val tree = tryConstant match {
389+
case Some(c) =>
390+
Some(atSpan(in.offset)(Literal(c)))
391+
case _ => in.token match {
392+
case AT =>
393+
in.nextToken()
394+
annotation()
395+
case IDENTIFIER => Some(classOrId())
396+
case LBRACE => array()
397+
case _ => None
398+
}
412399
}
413400
if in.token == COMMA || in.token == RBRACE || in.token == RPAREN then
414401
tree
@@ -716,11 +703,7 @@ object JavaParsers {
716703

717704
in.nextToken() // EQUALS
718705
if (mods.is(Flags.JavaStatic) && mods.is(Flags.Final)) {
719-
val neg = in.token match {
720-
case MINUS | BANG => in.nextToken(); true
721-
case _ => false
722-
}
723-
tryLiteral(neg).map(forConst).getOrElse(tpt1)
706+
tryConstant.map(forConst).getOrElse(tpt1)
724707
}
725708
else tpt1
726709
}
@@ -1012,7 +995,11 @@ object JavaParsers {
1012995
case _ => in.nextToken(); syntaxError("illegal start of type declaration", skipIt = true); List(errorTypeTree)
1013996
}
1014997

1015-
def tryLiteral(negate: Boolean = false): Option[Constant] = {
998+
def tryConstant: Option[Constant] = {
999+
val negate = in.token match {
1000+
case MINUS | BANG => in.nextToken(); true
1001+
case _ => false
1002+
}
10161003
val l = in.token match {
10171004
case TRUE => !negate
10181005
case FALSE => negate

0 commit comments

Comments
 (0)