Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 9e4ca66

Browse files
committed
*: Add support for comments: native, annotations and samples
1 parent f8934ed commit 9e4ca66

11 files changed

Lines changed: 224 additions & 2 deletions

ANNOTATION.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@
160160
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='SingleMemberAnnotation'\] | Incomplete |
161161
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='TagElement'\] | Incomplete |
162162
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='TextElement'\] | Incomplete |
163+
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='BlockComment'\] | Comment |
164+
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='Javadoc'\] | Documentation, Comment |
165+
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='LineComment'\] | Comment |
163166
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='ArrayAccess'\] | Expression, Incomplete |
164167
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='ArrayCreation'\] | Expression, Incomplete |
165168
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='CastExpression'\] | Expression, Incomplete |
@@ -177,5 +180,4 @@
177180
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='SynchronizedStatement'\] | Statement, Incomplete |
178181
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='ArrayInitializer'\] | Incomplete |
179182
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='Dimension'\] | Incomplete |
180-
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='Javadoc'\] | Documentation, Comment |
181183
| /self::\*\[@InternalType='CompilationUnit'\]//\*\[@InternalType='TypeParameter'\] | Incomplete |

driver/normalizer/annotation.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ var AnnotationRules = On(jdt.CompilationUnit).Roles(File).Descendants(
260260
On(jdt.TagElement).Roles(Incomplete),
261261
On(jdt.TextElement).Roles(Incomplete),
262262

263+
// Comments
264+
On(jdt.BlockComment).Roles(Comment),
265+
On(jdt.Javadoc).Roles(Documentation, Comment),
266+
On(jdt.LineComment).Roles(Comment),
267+
263268
// Other expressions
264269
On(jdt.ArrayAccess).Roles(Expression, Incomplete),
265270
On(jdt.ArrayCreation).Roles(Expression, Incomplete),
@@ -282,6 +287,5 @@ var AnnotationRules = On(jdt.CompilationUnit).Roles(File).Descendants(
282287
// Others
283288
On(jdt.ArrayInitializer).Roles(Incomplete),
284289
On(jdt.Dimension).Roles(Incomplete),
285-
On(jdt.Javadoc).Roles(Documentation, Comment),
286290
On(jdt.TypeParameter).Roles(Incomplete),
287291
)

native/src/main/java/bblfsh/CompilationUnitSerializer.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.databind.SerializerProvider;
55
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
66
import org.eclipse.jdt.core.dom.ASTNode;
7+
import org.eclipse.jdt.core.dom.Comment;
78
import org.eclipse.jdt.core.dom.CompilationUnit;
89
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
910

@@ -58,6 +59,30 @@ private void serializeAll(CompilationUnit cu, ASTNode node, JsonGenerator jG, Se
5859
jG.writeNumber(cu.getLineNumber(position));
5960
}
6061
}
62+
63+
if (node == cu) {
64+
List<Comment> cl = cu.getCommentList();
65+
if (!cl.isEmpty()) {
66+
jG.writeFieldName("comments");
67+
jG.writeStartArray();
68+
for (Comment c: (List<Comment>) cu.getCommentList()) {
69+
if (c.getParent() != null) continue;
70+
jG.writeStartObject();
71+
final int type = c.getNodeType();
72+
String name = c.nodeClassForType(type).getName().substring(25);
73+
jG.writeFieldName("internalClass");
74+
jG.writeString(name);
75+
jG.writeFieldName("startPosition");
76+
final int position = c.getStartPosition();
77+
jG.writeNumber(position);
78+
jG.writeFieldName("line");
79+
jG.writeNumber(cu.getLineNumber(position));
80+
jG.writeEndObject();
81+
}
82+
jG.writeEndArray();
83+
}
84+
}
85+
6186
jG.writeEndObject();
6287
}
6388

tests/block_comment.native

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"status": "ok",
3+
"errors": [],
4+
"ast": {
5+
"CompilationUnit": {
6+
"comments": [
7+
{
8+
"internalClass": "BlockComment",
9+
"line": 1,
10+
"startPosition": 0
11+
},
12+
{
13+
"internalClass": "BlockComment",
14+
"line": 2,
15+
"startPosition": 18
16+
}
17+
],
18+
"internalClass": "CompilationUnit",
19+
"types": [
20+
{
21+
"interface": "false",
22+
"internalClass": "TypeDeclaration",
23+
"line": 2,
24+
"name": {
25+
"identifier": "A",
26+
"internalClass": "SimpleName",
27+
"line": 2,
28+
"startPosition": 14
29+
},
30+
"startPosition": 8
31+
}
32+
]
33+
}
34+
}
35+
}

tests/block_comment.source

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* a */
2+
class A { /* b */ }

tests/block_comment.uast

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Status: ok
2+
Errors:
3+
UAST:
4+
CompilationUnit {
5+
. Roles: File
6+
. Children: {
7+
. . 0: BlockComment {
8+
. . . Roles: Comment
9+
. . . StartPosition: {
10+
. . . . Offset: 0
11+
. . . . Line: 1
12+
. . . . Col: 1
13+
. . . }
14+
. . . Properties: {
15+
. . . . internalRole: comments
16+
. . . }
17+
. . }
18+
. . 1: TypeDeclaration {
19+
. . . Roles: VisibleFromPackage,TypeDeclaration
20+
. . . StartPosition: {
21+
. . . . Offset: 8
22+
. . . . Line: 2
23+
. . . . Col: 1
24+
. . . }
25+
. . . Properties: {
26+
. . . . interface: false
27+
. . . . internalRole: types
28+
. . . }
29+
. . . Children: {
30+
. . . . 0: SimpleName {
31+
. . . . . Roles: SimpleIdentifier,Expression
32+
. . . . . TOKEN "A"
33+
. . . . . StartPosition: {
34+
. . . . . . Offset: 14
35+
. . . . . . Line: 2
36+
. . . . . . Col: 7
37+
. . . . . }
38+
. . . . . Properties: {
39+
. . . . . . internalRole: name
40+
. . . . . }
41+
. . . . }
42+
. . . }
43+
. . }
44+
. . 2: BlockComment {
45+
. . . Roles: Comment
46+
. . . StartPosition: {
47+
. . . . Offset: 18
48+
. . . . Line: 2
49+
. . . . Col: 11
50+
. . . }
51+
. . . Properties: {
52+
. . . . internalRole: comments
53+
. . . }
54+
. . }
55+
. }
56+
}
57+

tests/line_comment.native

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"status": "ok",
3+
"errors": [],
4+
"ast": {
5+
"CompilationUnit": {
6+
"comments": [
7+
{
8+
"internalClass": "LineComment",
9+
"line": 1,
10+
"startPosition": 0
11+
},
12+
{
13+
"internalClass": "LineComment",
14+
"line": 2,
15+
"startPosition": 15
16+
}
17+
],
18+
"internalClass": "CompilationUnit",
19+
"types": [
20+
{
21+
"interface": "false",
22+
"internalClass": "TypeDeclaration",
23+
"line": 2,
24+
"name": {
25+
"identifier": "A",
26+
"internalClass": "SimpleName",
27+
"line": 2,
28+
"startPosition": 11
29+
},
30+
"startPosition": 5
31+
}
32+
]
33+
}
34+
}
35+
}

tests/line_comment.source

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// a
2+
class A { // b
3+
}

tests/line_comment.uast

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Status: ok
2+
Errors:
3+
UAST:
4+
CompilationUnit {
5+
. Roles: File
6+
. Children: {
7+
. . 0: LineComment {
8+
. . . Roles: Comment
9+
. . . StartPosition: {
10+
. . . . Offset: 0
11+
. . . . Line: 1
12+
. . . . Col: 1
13+
. . . }
14+
. . . Properties: {
15+
. . . . internalRole: comments
16+
. . . }
17+
. . }
18+
. . 1: TypeDeclaration {
19+
. . . Roles: VisibleFromPackage,TypeDeclaration
20+
. . . StartPosition: {
21+
. . . . Offset: 5
22+
. . . . Line: 2
23+
. . . . Col: 1
24+
. . . }
25+
. . . Properties: {
26+
. . . . interface: false
27+
. . . . internalRole: types
28+
. . . }
29+
. . . Children: {
30+
. . . . 0: SimpleName {
31+
. . . . . Roles: SimpleIdentifier,Expression
32+
. . . . . TOKEN "A"
33+
. . . . . StartPosition: {
34+
. . . . . . Offset: 11
35+
. . . . . . Line: 2
36+
. . . . . . Col: 7
37+
. . . . . }
38+
. . . . . Properties: {
39+
. . . . . . internalRole: name
40+
. . . . . }
41+
. . . . }
42+
. . . }
43+
. . }
44+
. . 2: LineComment {
45+
. . . Roles: Comment
46+
. . . StartPosition: {
47+
. . . . Offset: 15
48+
. . . . Line: 2
49+
. . . . Col: 11
50+
. . . }
51+
. . . Properties: {
52+
. . . . internalRole: comments
53+
. . . }
54+
. . }
55+
. }
56+
}
57+

tests/member_ref.native

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"errors": [],
44
"ast": {
55
"CompilationUnit": {
6+
"comments": [],
67
"internalClass": "CompilationUnit",
78
"types": [
89
{

0 commit comments

Comments
 (0)