-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetToken.java
More file actions
178 lines (151 loc) · 5.42 KB
/
getToken.java
File metadata and controls
178 lines (151 loc) · 5.42 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.InputStreamReader;
class getToken {
public static void main(String[] args) {
boolean stream = true; // By default
boolean interactive = false; // REPL style
boolean fromfile = false; // direct stream to file
String filepath = null; // which file to read from
boolean json = false; // output format
boolean help = false; // display help or not
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "-j":
case "--json":
json = true;
break;
case "-i":
case "--interactive":
interactive = true;
break;
case "-f":
case "--file":
fromfile = true;
stream = false;
int next = ++i;
if (args.length <= next) help = true;
else filepath = args[next];
break;
case "-h":
case "--help":
help = true;
default:
break;
}
}
BufferedReader reader = null;
if (help) {
printHelp();
return;
} else if (fromfile) {
try {
reader = new BufferedReader( new FileReader(filepath) );
} catch(Exception err) {
System.err.println("File does not exist");
printHelp(true);
System.exit(1);
}
} else {
try {
reader = new BufferedReader( new InputStreamReader(System.in) );
} catch(Exception err) {
System.err.println("FATAL. Something bad happened.");
printHelp(true);
System.exit(2);
}
}
TokenParser parser = new TokenParser();
Token t = null;
if (interactive) { // repl
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
System.out.println("getToken -- interactive mode");
System.out.println("To exit type ':quit'");
while(true) {
System.out.print("> ");
String s = null;
try {
s = buffer.readLine();
} catch(IOException err) {
System.err.println("Bad read");
System.exit(1);
}
if (s.equals(":quit")) break;
t = parser.getTokenObject(s);
System.out.println(t.toJSON());
}
} else { // stream it up
String data = getBuffer(reader);
while ( data.length() > 0 ) {
t = parser.getTokenObject(data);
if (json)
System.out.println(t.toJSON());
else {
StringBuilder s = new StringBuilder();
s.append(t.getEscapedValue()).append(" ").append(t.getCode());
System.out.println(s.toString());
}
data = data.substring(t.getValue().length());
}
}
}
private static String getBuffer(BufferedReader reader) {
String line = null;
String data = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
try {
while(reader.ready() && ( line = reader.readLine() ) != null ) {
stringBuilder.append( line );
stringBuilder.append( ls );
}
} catch(IOException err) {
System.err.println("Something bad happened");
System.exit(1);
}
return stringBuilder.toString();
}
private static void printHelp() {
printHelp(false);
}
private static void printHelp(boolean error) {
StringBuilder help = new StringBuilder();
String ls = System.getProperty("line.separator");
help.append(ls)
.append(" getToken -- pseudocode-compiler")
.append(ls)
.append(" Retrieve token values and codes")
.append(ls)
.append(ls)
.append(" Usage: getToken [options]")
.append(ls)
.append(ls)
.append(" Options:")
.append(ls)
.append(ls)
.append(" -h, --help\t\tDisplay this help\n")
.append(" -f, --file <file>\tRead tokens from <file>\n")
.append(" -i, --interactive\tREPL for trying tokens\n")
.append(" -j, --json\t\tJSON output\n")
.append(ls)
.append(" Examples:")
.append(ls)
.append(ls)
.append(" getToken < token.dat")
.append(ls)
.append(" -- Read from stdin")
.append(ls)
.append(" getToken -f token.dat")
.append(ls)
.append(" -- Read from file")
.append(ls)
.append(" echo 29.45 | getToken --json")
.append(ls)
.append(" -- Output JSON")
.append(ls)
.append(ls);
if (error) System.err.print(help.toString());
else System.out.print(help.toString());
}
}