Repository navigation
Expand file tree
/
Copy pathScanner.java
More file actions
435 lines (381 loc) · 10.5 KB
/
Copy pathScanner.java
File metadata and controls
435 lines (381 loc) · 10.5 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
Modifications Copyright (C) 1999 Raja Vallee-Rai ([email protected])
All rights reserved.
Changes:
- Added \\ to the list of possible escape characters for Strings.
- March 15, 1999: $ does no longer significant substitution
*/
/* --- Copyright Jonathan Meyer 1996. All rights reserved. -----------------
> File: jasmin/src/jasmin/Scanner.java
> Purpose: Tokenizer for Jasmin
> Author: Jonathan Meyer, 10 July 1996
*/
/* Scanner.java - class for tokenizing Jasmin files. This is rather
* cheap and cheerful.
*/
package jasmin;
import jas.jasError;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java_cup.runtime.Symbol;
class Scanner implements java_cup.runtime.Scanner {
InputStreamReader inp;
// single lookahead character
int next_char;
// temporary buffer
char chars[];
char secondChars[];
char[] unicodeBuffer;
// true if we have not yet emitted a SEP ('\n') token. This is a bit
// of a hack so to strip out multiple newlines at the start of the file
// and replace them with a single SEP token. (for some reason I can't
// write the CUP grammar to accept multiple newlines at the start of the
// file)
boolean is_first_sep;
// Whitespace characters
static final String WHITESPACE = " \n\t\r";
// Separator characters
static final String SEPARATORS = WHITESPACE + ":=";
// used for error reporting to print out where an error is on the line
public int line_num, char_num, token_line_num;
public StringBuffer line;
// used by the .set directive to define new variables.
public Hashtable<String, Object> dict = new Hashtable<String, Object>();
//
// returns true if a character code is a whitespace character
//
protected static boolean whitespace(int c) {
return (WHITESPACE.indexOf(c) != -1);
}
//
// returns true if a character code is a separator character
//
protected static boolean separator(int c) {
return (SEPARATORS.indexOf(c) != -1);
}
//
// Advanced the input by one character
//
protected void advance() throws java.io.IOException {
next_char = inp.read();
if (next_char == '\n') {
// a new line
line_num++;
char_num = 0;
line.setLength(0);
} else {
line.append((char) next_char);
char_num++;
}
}
//
// initialize the scanner
//
final static int BIGNUM = 65000;
public Scanner(InputStream i) throws java.io.IOException {
inp = new InputStreamReader(i);
line_num = 1;
char_num = 0;
line = new StringBuffer();
chars = new char[BIGNUM];
secondChars = new char[BIGNUM];
unicodeBuffer = new char[4];
is_first_sep = true;
advance();
}
int readOctal(int firstChar) throws java.io.IOException {
int d1, d2, d3;
d1 = firstChar;
advance();
d2 = next_char;
advance();
d3 = next_char;
return ((d1 - '0') & 7) * 64 + ((d2 - '0') & 7) * 8 + ((d3 - '0') & 7);
}
//
// recognize and return the next complete symbol
//
public Symbol next_token() throws java.io.IOException, jasError {
token_line_num = line_num;
for (;;) {
switch (next_char) {
case ';':
// a comment
do {
advance();
} while (next_char != '\n');
case '\n':
// return single SEP token (skip multiple newlines
// interspersed with whitespace or comments)
for (;;) {
do {
advance();
} while (whitespace(next_char));
if (next_char == ';') {
do {
advance();
} while (next_char != '\n');
} else {
break;
}
}
if (is_first_sep) {
return next_token();
}
token_line_num = line_num;
return new Symbol(sym.SEP);
case '"': // quoted strings
{
int pos = 0;
is_first_sep = false;
for (;;) {
advance();
if (next_char == '\\') {
advance();
switch (next_char) {
case 'n':
next_char = '\n';
break;
case 'r':
next_char = '\r';
break;
case 't':
next_char = '\t';
break;
case 'f':
next_char = '\f';
break;
case 'b':
next_char = '\b';
break;
case 'u': {
advance();
unicodeBuffer[0] = (char) next_char;
advance();
unicodeBuffer[1] = (char) next_char;
advance();
unicodeBuffer[2] = (char) next_char;
advance();
unicodeBuffer[3] = (char) next_char;
// System.out.println(unicodeBuffer[0] + ":" +
// unicodeBuffer[1] + ":" + unicodeBuffer[2] + ":" +
// unicodeBuffer[3] + ":");
next_char = (char) Integer.parseInt(new String(unicodeBuffer, 0, 4), 16);
// System.out.println("value: " + next_char);
break;
}
case '"':
next_char = '"';
break;
case '\'':
next_char = '\'';
break;
case '\\':
next_char = '\\';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
next_char = readOctal(next_char);
break;
default:
throw new jasError("Bad backslash escape sequence");
}
} else if (next_char == '"' || next_char < 0) {
break;
}
try {
if (chars.length == pos) {
char[] newChars = new char[chars.length * 2];
System.arraycopy(chars, 0, newChars, 0, chars.length);
chars = newChars;
}
chars[pos] = (char) next_char;
} catch (ArrayIndexOutOfBoundsException abe) {
char[] tmparray = new char[chars.length * 2];
System.arraycopy(chars, 0, tmparray, 0, chars.length);
chars = tmparray;
chars[pos] = (char) next_char;
}
pos++;
}
advance(); // skip close quote
return new Symbol(sym.Str, new String(chars, 0, pos));
}
case ' ':
case '\t':
case '\r': // whitespace
advance();
break;
case '=': // EQUALS token
advance();
is_first_sep = false;
return new Symbol(sym.EQ);
case ':': // COLON token
advance();
is_first_sep = false;
return new Symbol(sym.COLON);
case -1: // EOF token
is_first_sep = false;
char_num = -1;
line.setLength(0);
return new Symbol(sym.EOF);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
case '+':
case '.': // a number
default: {
// read up until a separatorcharacter
int pos = 0;
chars[0] = (char) next_char;
is_first_sep = false;
pos++;
for (;;) {
advance();
if (separator(next_char)) {
break;
}
try {
chars[pos] = (char) next_char;
} catch (ArrayIndexOutOfBoundsException abe) {
char[] tmparray = new char[chars.length * 2];
System.arraycopy(chars, 0, tmparray, 0, chars.length);
chars = tmparray;
chars[pos] = (char) next_char;
}
pos++;
}
// convert the byte array into a String
int secondPos = translateUnicodeCharacters(pos);
String str = new String(secondChars, 0, secondPos);
// Is this a number?
Symbol tok;
if ((tok = tryParseAsNumber(str)) != null) {
return tok;
}
if ((tok = ReservedWords.get(str)) != null || (tok = ReservedInstructions.get(str)) != null) {
// Jasmin keyword or directive
// or Jasmin instruction
return tok;
} else {
// Unrecognized string token (e.g. a classname)
return new Symbol(sym.Word, str);
}
} /* default */
} /* switch */
} /* for */
}
private Symbol tryParseAsNumber(String str) throws jasError {
if (str.isEmpty())
return null;
// Check for special numbers
if (str.equals("+DoubleInfinity"))
return new Symbol(sym.Num, new Double(1.0 / 0.0));
if (str.equals("+DoubleNaN"))
return new Symbol(sym.Num, new Double(0.0d / 0.0));
if (str.equals("+FloatNaN"))
return new Symbol(sym.Num, new Float(0.0f / 0.0));
if (str.equals("-DoubleInfinity"))
return new Symbol(sym.Num, new Double(-1.0 / 0.0));
if (str.equals("+FloatInfinity"))
return new Symbol(sym.Num, new Float(1.0f / 0.0f));
if (str.equals("-FloatInfinity"))
return new Symbol(sym.Num, new Float(-1.0f / 0.0f));
// This catches directives like ".method"
Symbol tok;
if ((tok = ReservedWords.get(str)) != null) {
return tok;
}
// Is this a method signature?
int idxOpen = str.indexOf("(");
int idxClose = str.indexOf(")");
if (idxOpen > 0 && idxClose > idxOpen)
return null;
// Parse regular numbers
char c = str.charAt(0);
if (c == '0'
|| c == '1'
|| c == '2'
|| c == '3'
|| c == '4'
|| c == '5'
|| c == '6'
|| c == '7'
|| c == '8'
|| c == '9'
|| c == '-'
|| c == '+'
|| c == '.') {
Number num;
try {
num = ScannerUtils.convertNumber(str);
} catch (NumberFormatException e) {
if (chars[0] == '.') {
throw new jasError("Unknown directive or badly formed number.");
} else {
throw new jasError("Badly formatted number: " + str);
}
}
if (num instanceof Integer) {
return new Symbol(sym.Int, new Integer(num.intValue()));
} else {
return new Symbol(sym.Num, num);
}
}
return null;
}
private int translateUnicodeCharacters(int pos) {
// Parse all the unicode escape sequences
int secondPos = 0;
for (int i = 0; i < pos; i++) {
if (chars[i] == '\\' && (i + 5) < pos && chars[i + 1] == 'u') {
int intValue = Integer.parseInt(new String(chars, i + 2, 4), 16);
try {
secondChars[secondPos] = (char) intValue;
} catch (ArrayIndexOutOfBoundsException abe) {
char[] tmparray = new char[secondChars.length * 2];
System.arraycopy(secondChars, 0, tmparray, 0, secondChars.length);
secondChars = tmparray;
secondChars[secondPos] = (char) intValue;
}
secondPos++;
i += 5;
} else {
try {
secondChars[secondPos] = chars[i];
} catch (ArrayIndexOutOfBoundsException abe) {
char[] tmparray = new char[secondChars.length * 2];
System.arraycopy(secondChars, 0, tmparray, 0, secondChars.length);
secondChars = tmparray;
secondChars[secondPos] = chars[i];
}
secondPos++;
}
}
return secondPos;
}
};
/*
* --- Revision History --------------------------------------------------- ---
* Jonathan Meyer, Feb 8 1997 Converted to be non-static --- Jonathan Meyer, Oct
* 30 1996 Added support for more \ escapes in quoted strings (including
* octals). --- Jonathan Meyer, Oct 1 1996 Added .interface and .implements ---
* Jonathan Meyer, July 25 1996 changed IN to IS. Added token_line_num, which is
* the line number of the last token returned by next_token(). --- Jonathan
* Meyer, July 24 1996 added mods to recognize '\r' as whitespace.
*/