Skip to content

Commit 3a93566

Browse files
committed
Add Parsing Simple Annotations in JavaParser
1 parent 230e92d commit 3a93566

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ object JavaParsers {
138138
// ------------- general parsing ---------------------------
139139

140140
/** skip parent or brace enclosed sequence of things */
141-
def skipAhead(): Unit = {
142-
var nparens = 0
143-
var nbraces = 0
141+
def skipAhead(initnparens: Int, initnbraces: Int): Unit = {
142+
var nparens = initnparens
143+
var nbraces = initnbraces
144144
do {
145145
in.token match {
146146
case LPAREN =>
@@ -160,6 +160,8 @@ object JavaParsers {
160160
} while (in.token != EOF && (nparens > 0 || nbraces > 0))
161161
}
162162

163+
def skipAhead(): Unit = skipAhead(0,0)
164+
163165
def skipTo(tokens: Int*): Unit = {
164166
while (!(tokens contains in.token) && in.token != EOF) {
165167
if (in.token == LBRACE) { skipAhead(); accept(RBRACE) }
@@ -327,20 +329,42 @@ object JavaParsers {
327329
}
328330

329331
def annotations(): List[Tree] = {
330-
//var annots = new ListBuffer[Tree]
332+
var annots = new ListBuffer[Tree]
331333
while (in.token == AT) {
332334
in.nextToken()
333-
annotation()
335+
annotation() match {
336+
case Some(anno) => annots += anno
337+
case _ =>
338+
}
334339
}
335-
List() // don't pass on annotations for now
340+
annots.toList
336341
}
337342

338343
/** Annotation ::= TypeName [`(` AnnotationArgument {`,` AnnotationArgument} `)`]
339344
*/
340-
def annotation(): Unit = {
341-
qualId()
342-
if (in.token == LPAREN) { skipAhead(); accept(RPAREN) }
343-
else if (in.token == LBRACE) { skipAhead(); accept(RBRACE) }
345+
def annotation(): Option[Tree] = {
346+
val id = convertToTypeId(qualId())
347+
var skipAnno = false
348+
349+
// only parse annotations without arguments
350+
if (in.token == LPAREN) {
351+
in.nextToken()
352+
if (in.token != RPAREN) {
353+
skipAnno = true
354+
skipAhead(1, 0)
355+
}
356+
accept(RPAREN)
357+
} else if (in.token == LBRACE) {
358+
in.nextToken()
359+
if (in.token != RBRACE) {
360+
skipAnno = true
361+
skipAhead(0, 1)
362+
}
363+
accept(RBRACE)
364+
}
365+
366+
if (skipAnno) None
367+
else Some(ensureApplied(Select(New(id), nme.CONSTRUCTOR)))
344368
}
345369

346370
def modifiers(inInterface: Boolean): Modifiers = {
@@ -358,7 +382,10 @@ object JavaParsers {
358382
in.token match {
359383
case AT if (in.lookaheadToken != INTERFACE) =>
360384
in.nextToken()
361-
annotation()
385+
annotation() match {
386+
case Some(anno) => annots :+= anno
387+
case _ =>
388+
}
362389
case PUBLIC =>
363390
isPackageAccess = false
364391
in.nextToken()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class AnnoJavaParent {
2+
public String n(int i) {
3+
return "n: " + i;
4+
}
5+
}
6+
7+
public class AnnoJava extends AnnoJavaParent {
8+
9+
@MyAnno()
10+
public static String m(int i) {
11+
return "m: " + i;
12+
}
13+
14+
@Override
15+
public String n(int i) {
16+
return "n: " + i;
17+
}
18+
}
19+
20+
@interface MyAnno {
21+
int value() default 1;
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class CallJava {
2+
def mm(i: Int): String = AnnoJava.m(i)
3+
}

0 commit comments

Comments
 (0)