-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXRef.java
More file actions
116 lines (110 loc) · 4.36 KB
/
XRef.java
File metadata and controls
116 lines (110 loc) · 4.36 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.*;
import java.io.*;
public class XRef {
private static final String
DELIMITER = "\"(?:\\\\\"|[^\"])*?\"|[\\s.,;:+*/|!=><@?#%&(){}\\-\\^\\[\\]\\&&]+";
private static LinkedList identifiers = new LinkedList(new StringComparator());
public static String[] tokenizer(String javaStmt) {
String[] tokens = javaStmt.split(DELIMITER);
return tokens;
}
public static void InitIdentifiers() {
identifiers.addToTail("abstract");
identifiers.addToTail("continue");
identifiers.addToTail("for");
identifiers.addToTail("new");
identifiers.addToTail("switch");
identifiers.addToTail("assert");
identifiers.addToTail("default");
identifiers.addToTail("goto");
identifiers.addToTail("package");
identifiers.addToTail("synchronized");
identifiers.addToTail("boolean");
identifiers.addToTail("do");
identifiers.addToTail("if");
identifiers.addToTail("private");
identifiers.addToTail("this");
identifiers.addToTail("break");
identifiers.addToTail("double");
identifiers.addToTail("implements");
identifiers.addToTail("protected");
identifiers.addToTail("throw");
identifiers.addToTail("byte");
identifiers.addToTail("else");
identifiers.addToTail("import");
identifiers.addToTail("public");
identifiers.addToTail("throws");
identifiers.addToTail("case");
identifiers.addToTail("enum");
identifiers.addToTail("instanceof");
identifiers.addToTail("return");
identifiers.addToTail("transient");
identifiers.addToTail("catch");
identifiers.addToTail("extends");
identifiers.addToTail("int");
identifiers.addToTail("short");
identifiers.addToTail("try");
identifiers.addToTail("char");
identifiers.addToTail("final");
identifiers.addToTail("interface");
identifiers.addToTail("static");
identifiers.addToTail("void");
identifiers.addToTail("class");
identifiers.addToTail("finally");
identifiers.addToTail("long");
identifiers.addToTail("strictfp");
identifiers.addToTail("volatile");
identifiers.addToTail("const");
identifiers.addToTail("float");
identifiers.addToTail("native");
identifiers.addToTail("super");
identifiers.addToTail("while");
}
public static boolean checkWordCanUse(String word) {
if(word.isEmpty() || word.isBlank())
return false;
if(identifiers.exist(word))
return false;
char firstChar = word.charAt(0);
if( (firstChar < 'A' || firstChar > 'Z') && (firstChar < 'a' || firstChar > 'z') && firstChar != '$' && firstChar != '_' ) //Check the first character is violated the rule or not
return false;
return true;
}
public static void main(String[] args) {
System.out.println("X R e f v 1");
System.out.println("============================================");
System.out.println();
String path = null;
try {
path = args[0];
Scanner scanner = new Scanner(new File(path));
LinkedList list = new LinkedList(new TokenDataComparator());
InitIdentifiers();
System.out.println("SOURCE FILE: " + path);
int lineNum = 1;
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(String.format("%04d | %s", lineNum, line));
for(String t : tokenizer(line)) {
if(checkWordCanUse(t))
list.insertToken(new TokenData(t, lineNum));
}
lineNum++;
}
System.out.println();
System.out.println("CROSS REFERENCE:");
list.println();
System.out.println("XRef v1 normally terminated.");
}
catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("Error: No path provided!");
}
catch (FileNotFoundException ex) {
System.out.println("Error: Can't find file " + path);
}
catch (Exception ex) {
System.out.println("Error: Unexpected error");
System.out.println(ex.toString());
}
}
}