-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjavaTokens.js
57 lines (51 loc) · 1.68 KB
/
javaTokens.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import {parse} from "java-parser";
/**
* Visits Java source file and collect 'unannType' tokens.
* The `parse` method returns an array with the images of collected tokens
*/
export class JavaTokens {
constructor(source) {
this.source = source;
}
parse() {
const cstNode = parse(this.source);
return this.collect(cstNode);
}
collect(root) {
const inbox = [root];
const images = [];
for (let node; (node = inbox.shift());) {
for (const identifier in node.children) {
node.children[identifier]
.forEach(element => {
if (element.name === 'unannType') {
images.push(...this.referenceTypes(element));
} else {
inbox.push(element);
}
})
}
}
return images;
}
referenceTypes(element) {
const inbox = [element];
const images = [];
for (let node; (node = inbox.shift());) {
for (const identifier in node.children) {
node.children[identifier]
.forEach(element => {
if (element.name === 'referenceType') {
images.push(...this.referenceTypes(element));
} else {
inbox.push(element);
}
})
}
}
return images.length ? images : [this.image(element.location)];
}
image(location) {
return this.source.substring(location.startOffset, location.endOffset + 1);
}
}