/
TokenConv.java
88 lines (83 loc) · 2.52 KB
/
TokenConv.java
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
// COMS22201: Token converter to allow Java programs to use output from ANTLR
import java.lang.reflect.Array;
import java.io.*;
import java.util.*;
/**
* Token converter.
*
*/
public class TokenConv
{
public static void main(String[] args)
{
String tokensFile = args[0];
String progFile = args[1];
String tempFile = "CAMLE_TEMP.txt";
List<String> tokenName = new ArrayList<String>();
List<Integer> tokenType = new ArrayList<Integer>();
List<String> nameOf = new ArrayList<String>();
String line;
boolean skipping = false;
try {
int nTokens = readTokensFile(tokensFile, tokenName, tokenType, nameOf);
BufferedReader in = new BufferedReader(new FileReader(progFile));
PrintStream o = new PrintStream(new FileOutputStream(tempFile));
while ((line = in.readLine()) != null) {
if (line.equals("// CAMLE TOKENS END")) {
skipping = false;
}
if (!skipping) {
o.println(line);
}
if (line.equals("// CAMLE TOKENS BEGIN")) {
skipping = true;
writeTokensCode(o, nTokens, tokenName, tokenType, nameOf);
}
}
in.close();
o.close();
new File(tempFile).renameTo(new File(progFile));
}
catch (Exception e) {
System.out.println(e.toString());
System.exit(1);
}
}
private static void writeTokensCode(PrintStream o, int nTokens, List<String> tokenName,
List<Integer> tokenType, List<String> nameOf) throws Exception
{
int i;
o.println(" public static final String[] tokenNames = new String[] {");
o.print("\""+nameOf.get(0)+"\"");
for (i=1; i<nameOf.size(); i++) {
o.print(", \""+nameOf.get(i)+"\"");
}
o.println("};");
for (i=0; i<nTokens; i++) {
o.println(" public static final int "+tokenName.get(i)+"="+tokenType.get(i)+";");
}
}
private static int readTokensFile(String tokensFile, List<String> tokenName,
List<Integer> tokenType, List<String> nameOf) throws Exception
{
String line, name;
String[] part;
int nTokens = 0;
int type, i;
BufferedReader in = new BufferedReader(new FileReader(tokensFile));
while ((line = in.readLine()) != null) {
part = line.split("=");
name = part[0];
type = Integer.parseInt(part[1]);
tokenName.add(name);
tokenType.add(type);
while (nameOf.size() <= type) {
nameOf.add("NONE");
}
nameOf.set(type, name);
nTokens++;
}
in.close();
return nTokens;
}
}