1 | /* This file was generated by SableCC (http://www.sablecc.org/). */ |
2 | |
3 | package uk.co.zonetora.fj.ast.lexer; |
4 | |
5 | import java.io.*; |
6 | import java.util.*; |
7 | import uk.co.zonetora.fj.ast.node.*; |
8 | |
9 | public class Lexer |
10 | { |
11 | protected Token token; |
12 | protected State state = State.INITIAL; |
13 | |
14 | private PushbackReader in; |
15 | private int line; |
16 | private int pos; |
17 | private boolean cr; |
18 | private boolean eof; |
19 | private final StringBuffer text = new StringBuffer(); |
20 | |
21 | protected void filter() throws LexerException, IOException |
22 | { |
23 | } |
24 | |
25 | public Lexer(PushbackReader in) |
26 | { |
27 | this.in = in; |
28 | |
29 | if(gotoTable == null) |
30 | { |
31 | try |
32 | { |
33 | DataInputStream s = new DataInputStream( |
34 | new BufferedInputStream( |
35 | Lexer.class.getResourceAsStream("lexer.dat"))); |
36 | |
37 | // read gotoTable |
38 | int length = s.readInt(); |
39 | gotoTable = new int[length][][][]; |
40 | for(int i = 0; i < gotoTable.length; i++) |
41 | { |
42 | length = s.readInt(); |
43 | gotoTable[i] = new int[length][][]; |
44 | for(int j = 0; j < gotoTable[i].length; j++) |
45 | { |
46 | length = s.readInt(); |
47 | gotoTable[i][j] = new int[length][3]; |
48 | for(int k = 0; k < gotoTable[i][j].length; k++) |
49 | { |
50 | for(int l = 0; l < 3; l++) |
51 | { |
52 | gotoTable[i][j][k][l] = s.readInt(); |
53 | } |
54 | } |
55 | } |
56 | } |
57 | |
58 | // read accept |
59 | length = s.readInt(); |
60 | accept = new int[length][]; |
61 | for(int i = 0; i < accept.length; i++) |
62 | { |
63 | length = s.readInt(); |
64 | accept[i] = new int[length]; |
65 | for(int j = 0; j < accept[i].length; j++) |
66 | { |
67 | accept[i][j] = s.readInt(); |
68 | } |
69 | } |
70 | |
71 | s.close(); |
72 | } |
73 | catch(Exception e) |
74 | { |
75 | throw new RuntimeException("The file \"lexer.dat\" is either missing or corrupted."); |
76 | } |
77 | } |
78 | } |
79 | |
80 | public Token peek() throws LexerException, IOException |
81 | { |
82 | while(token == null) |
83 | { |
84 | token = getToken(); |
85 | filter(); |
86 | } |
87 | |
88 | return token; |
89 | } |
90 | |
91 | public Token next() throws LexerException, IOException |
92 | { |
93 | while(token == null) |
94 | { |
95 | token = getToken(); |
96 | filter(); |
97 | } |
98 | |
99 | Token result = token; |
100 | token = null; |
101 | return result; |
102 | } |
103 | |
104 | protected Token getToken() throws IOException, LexerException |
105 | { |
106 | int dfa_state = 0; |
107 | |
108 | int start_pos = pos; |
109 | int start_line = line; |
110 | |
111 | int accept_state = -1; |
112 | int accept_token = -1; |
113 | int accept_length = -1; |
114 | int accept_pos = -1; |
115 | int accept_line = -1; |
116 | |
117 | int[][][] gotoTable = this.gotoTable[state.id()]; |
118 | int[] accept = this.accept[state.id()]; |
119 | text.setLength(0); |
120 | |
121 | while(true) |
122 | { |
123 | int c = getChar(); |
124 | |
125 | if(c != -1) |
126 | { |
127 | switch(c) |
128 | { |
129 | case 10: |
130 | if(cr) |
131 | { |
132 | cr = false; |
133 | } |
134 | else |
135 | { |
136 | line++; |
137 | pos = 0; |
138 | } |
139 | break; |
140 | case 13: |
141 | line++; |
142 | pos = 0; |
143 | cr = true; |
144 | break; |
145 | default: |
146 | pos++; |
147 | cr = false; |
148 | break; |
149 | }; |
150 | |
151 | text.append((char) c); |
152 | |
153 | do |
154 | { |
155 | int oldState = (dfa_state < -1) ? (-2 -dfa_state) : dfa_state; |
156 | |
157 | dfa_state = -1; |
158 | |
159 | int[][] tmp1 = gotoTable[oldState]; |
160 | int low = 0; |
161 | int high = tmp1.length - 1; |
162 | |
163 | while(low <= high) |
164 | { |
165 | int middle = (low + high) / 2; |
166 | int[] tmp2 = tmp1[middle]; |
167 | |
168 | if(c < tmp2[0]) |
169 | { |
170 | high = middle - 1; |
171 | } |
172 | else if(c > tmp2[1]) |
173 | { |
174 | low = middle + 1; |
175 | } |
176 | else |
177 | { |
178 | dfa_state = tmp2[2]; |
179 | break; |
180 | } |
181 | } |
182 | }while(dfa_state < -1); |
183 | } |
184 | else |
185 | { |
186 | dfa_state = -1; |
187 | } |
188 | |
189 | if(dfa_state >= 0) |
190 | { |
191 | if(accept[dfa_state] != -1) |
192 | { |
193 | accept_state = dfa_state; |
194 | accept_token = accept[dfa_state]; |
195 | accept_length = text.length(); |
196 | accept_pos = pos; |
197 | accept_line = line; |
198 | } |
199 | } |
200 | else |
201 | { |
202 | if(accept_state != -1) |
203 | { |
204 | switch(accept_token) |
205 | { |
206 | case 0: |
207 | { |
208 | Token token = new0( |
209 | start_line + 1, |
210 | start_pos + 1); |
211 | pushBack(accept_length); |
212 | pos = accept_pos; |
213 | line = accept_line; |
214 | return token; |
215 | } |
216 | case 1: |
217 | { |
218 | Token token = new1( |
219 | start_line + 1, |
220 | start_pos + 1); |
221 | pushBack(accept_length); |
222 | pos = accept_pos; |
223 | line = accept_line; |
224 | return token; |
225 | } |
226 | case 2: |
227 | { |
228 | Token token = new2( |
229 | start_line + 1, |
230 | start_pos + 1); |
231 | pushBack(accept_length); |
232 | pos = accept_pos; |
233 | line = accept_line; |
234 | return token; |
235 | } |
236 | case 3: |
237 | { |
238 | Token token = new3( |
239 | start_line + 1, |
240 | start_pos + 1); |
241 | pushBack(accept_length); |
242 | pos = accept_pos; |
243 | line = accept_line; |
244 | return token; |
245 | } |
246 | case 4: |
247 | { |
248 | Token token = new4( |
249 | start_line + 1, |
250 | start_pos + 1); |
251 | pushBack(accept_length); |
252 | pos = accept_pos; |
253 | line = accept_line; |
254 | return token; |
255 | } |
256 | case 5: |
257 | { |
258 | Token token = new5( |
259 | start_line + 1, |
260 | start_pos + 1); |
261 | pushBack(accept_length); |
262 | pos = accept_pos; |
263 | line = accept_line; |
264 | return token; |
265 | } |
266 | case 6: |
267 | { |
268 | Token token = new6( |
269 | start_line + 1, |
270 | start_pos + 1); |
271 | pushBack(accept_length); |
272 | pos = accept_pos; |
273 | line = accept_line; |
274 | return token; |
275 | } |
276 | case 7: |
277 | { |
278 | Token token = new7( |
279 | start_line + 1, |
280 | start_pos + 1); |
281 | pushBack(accept_length); |
282 | pos = accept_pos; |
283 | line = accept_line; |
284 | return token; |
285 | } |
286 | case 8: |
287 | { |
288 | Token token = new8( |
289 | start_line + 1, |
290 | start_pos + 1); |
291 | pushBack(accept_length); |
292 | pos = accept_pos; |
293 | line = accept_line; |
294 | return token; |
295 | } |
296 | case 9: |
297 | { |
298 | Token token = new9( |
299 | start_line + 1, |
300 | start_pos + 1); |
301 | pushBack(accept_length); |
302 | pos = accept_pos; |
303 | line = accept_line; |
304 | return token; |
305 | } |
306 | case 10: |
307 | { |
308 | Token token = new10( |
309 | start_line + 1, |
310 | start_pos + 1); |
311 | pushBack(accept_length); |
312 | pos = accept_pos; |
313 | line = accept_line; |
314 | return token; |
315 | } |
316 | case 11: |
317 | { |
318 | Token token = new11( |
319 | start_line + 1, |
320 | start_pos + 1); |
321 | pushBack(accept_length); |
322 | pos = accept_pos; |
323 | line = accept_line; |
324 | return token; |
325 | } |
326 | case 12: |
327 | { |
328 | Token token = new12( |
329 | start_line + 1, |
330 | start_pos + 1); |
331 | pushBack(accept_length); |
332 | pos = accept_pos; |
333 | line = accept_line; |
334 | return token; |
335 | } |
336 | case 13: |
337 | { |
338 | Token token = new13( |
339 | start_line + 1, |
340 | start_pos + 1); |
341 | pushBack(accept_length); |
342 | pos = accept_pos; |
343 | line = accept_line; |
344 | return token; |
345 | } |
346 | case 14: |
347 | { |
348 | Token token = new14( |
349 | start_line + 1, |
350 | start_pos + 1); |
351 | pushBack(accept_length); |
352 | pos = accept_pos; |
353 | line = accept_line; |
354 | return token; |
355 | } |
356 | case 15: |
357 | { |
358 | Token token = new15( |
359 | start_line + 1, |
360 | start_pos + 1); |
361 | pushBack(accept_length); |
362 | pos = accept_pos; |
363 | line = accept_line; |
364 | return token; |
365 | } |
366 | case 16: |
367 | { |
368 | Token token = new16( |
369 | getText(accept_length), |
370 | start_line + 1, |
371 | start_pos + 1); |
372 | pushBack(accept_length); |
373 | pos = accept_pos; |
374 | line = accept_line; |
375 | return token; |
376 | } |
377 | case 17: |
378 | { |
379 | Token token = new17( |
380 | getText(accept_length), |
381 | start_line + 1, |
382 | start_pos + 1); |
383 | pushBack(accept_length); |
384 | pos = accept_pos; |
385 | line = accept_line; |
386 | return token; |
387 | } |
388 | case 18: |
389 | { |
390 | Token token = new18( |
391 | getText(accept_length), |
392 | start_line + 1, |
393 | start_pos + 1); |
394 | pushBack(accept_length); |
395 | pos = accept_pos; |
396 | line = accept_line; |
397 | return token; |
398 | } |
399 | } |
400 | } |
401 | else |
402 | { |
403 | if(text.length() > 0) |
404 | { |
405 | throw new LexerException( |
406 | "[" + (start_line + 1) + "," + (start_pos + 1) + "]" + |
407 | " Unknown token: " + text); |
408 | } |
409 | else |
410 | { |
411 | EOF token = new EOF( |
412 | start_line + 1, |
413 | start_pos + 1); |
414 | return token; |
415 | } |
416 | } |
417 | } |
418 | } |
419 | } |
420 | |
421 | Token new0(int line, int pos) { return new TTclass(line, pos); } |
422 | Token new1(int line, int pos) { return new TExtends(line, pos); } |
423 | Token new2(int line, int pos) { return new TSuper(line, pos); } |
424 | Token new3(int line, int pos) { return new TThis(line, pos); } |
425 | Token new4(int line, int pos) { return new TNew(line, pos); } |
426 | Token new5(int line, int pos) { return new TReturn(line, pos); } |
427 | Token new6(int line, int pos) { return new TLBrace(line, pos); } |
428 | Token new7(int line, int pos) { return new TRBrace(line, pos); } |
429 | Token new8(int line, int pos) { return new TSemicolon(line, pos); } |
430 | Token new9(int line, int pos) { return new TComma(line, pos); } |
431 | Token new10(int line, int pos) { return new TLPar(line, pos); } |
432 | Token new11(int line, int pos) { return new TRPar(line, pos); } |
433 | Token new12(int line, int pos) { return new TLSq(line, pos); } |
434 | Token new13(int line, int pos) { return new TRSq(line, pos); } |
435 | Token new14(int line, int pos) { return new TPeriod(line, pos); } |
436 | Token new15(int line, int pos) { return new TEq(line, pos); } |
437 | Token new16(String text, int line, int pos) { return new TNewLine(text, line, pos); } |
438 | Token new17(String text, int line, int pos) { return new TBlank(text, line, pos); } |
439 | Token new18(String text, int line, int pos) { return new TIdentifier(text, line, pos); } |
440 | |
441 | private int getChar() throws IOException |
442 | { |
443 | if(eof) |
444 | { |
445 | return -1; |
446 | } |
447 | |
448 | int result = in.read(); |
449 | |
450 | if(result == -1) |
451 | { |
452 | eof = true; |
453 | } |
454 | |
455 | return result; |
456 | } |
457 | |
458 | private void pushBack(int acceptLength) throws IOException |
459 | { |
460 | int length = text.length(); |
461 | for(int i = length - 1; i >= acceptLength; i--) |
462 | { |
463 | eof = false; |
464 | |
465 | in.unread(text.charAt(i)); |
466 | } |
467 | } |
468 | |
469 | protected void unread(Token token) throws IOException |
470 | { |
471 | String text = token.getText(); |
472 | int length = text.length(); |
473 | |
474 | for(int i = length - 1; i >= 0; i--) |
475 | { |
476 | eof = false; |
477 | |
478 | in.unread(text.charAt(i)); |
479 | } |
480 | |
481 | pos = token.getPos() - 1; |
482 | line = token.getLine() - 1; |
483 | } |
484 | |
485 | private String getText(int acceptLength) |
486 | { |
487 | StringBuffer s = new StringBuffer(acceptLength); |
488 | for(int i = 0; i < acceptLength; i++) |
489 | { |
490 | s.append(text.charAt(i)); |
491 | } |
492 | |
493 | return s.toString(); |
494 | } |
495 | |
496 | private static int[][][][] gotoTable; |
497 | /* { |
498 | { // INITIAL |
499 | {{9, 9, 1}, {10, 10, 2}, {13, 13, 3}, {32, 32, 4}, {40, 40, 5}, {41, 41, 6}, {44, 44, 7}, {46, 46, 8}, {59, 59, 9}, {61, 61, 10}, {65, 90, 11}, {91, 91, 12}, {92, 92, 13}, {93, 93, 14}, {95, 95, 15}, {97, 98, 16}, {99, 99, 17}, {100, 100, 16}, {101, 101, 18}, {102, 109, 16}, {110, 110, 19}, {111, 113, 16}, {114, 114, 20}, {115, 115, 21}, {116, 116, 22}, {117, 122, 16}, {123, 123, 23}, {125, 125, 24}, }, |
500 | {{9, 9, 1}, {32, 32, 4}, {92, 92, 13}, }, |
501 | {}, |
502 | {{10, 10, 25}, }, |
503 | {{9, 92, -3}, }, |
504 | {}, |
505 | {}, |
506 | {}, |
507 | {}, |
508 | {}, |
509 | {}, |
510 | {{48, 57, 26}, {65, 90, 27}, {95, 95, 28}, {97, 122, 29}, }, |
511 | {}, |
512 | {{110, 110, 30}, }, |
513 | {}, |
514 | {{48, 122, -13}, }, |
515 | {{48, 122, -13}, }, |
516 | {{48, 95, -13}, {97, 107, 29}, {108, 108, 31}, {109, 122, 29}, }, |
517 | {{48, 95, -13}, {97, 119, 29}, {120, 120, 32}, {121, 122, 29}, }, |
518 | {{48, 95, -13}, {97, 100, 29}, {101, 101, 33}, {102, 122, 29}, }, |
519 | {{48, 100, -21}, {101, 101, 34}, {102, 122, 29}, }, |
520 | {{48, 95, -13}, {97, 116, 29}, {117, 117, 35}, {118, 122, 29}, }, |
521 | {{48, 95, -13}, {97, 103, 29}, {104, 104, 36}, {105, 122, 29}, }, |
522 | {}, |
523 | {}, |
524 | {}, |
525 | {{48, 122, -13}, }, |
526 | {{48, 122, -13}, }, |
527 | {{48, 122, -13}, }, |
528 | {{48, 122, -13}, }, |
529 | {{9, 92, -3}, }, |
530 | {{48, 95, -13}, {97, 97, 37}, {98, 122, 29}, }, |
531 | {{48, 95, -13}, {97, 115, 29}, {116, 116, 38}, {117, 122, 29}, }, |
532 | {{48, 95, -13}, {97, 118, 29}, {119, 119, 39}, {120, 122, 29}, }, |
533 | {{48, 115, -34}, {116, 116, 40}, {117, 122, 29}, }, |
534 | {{48, 95, -13}, {97, 111, 29}, {112, 112, 41}, {113, 122, 29}, }, |
535 | {{48, 95, -13}, {97, 104, 29}, {105, 105, 42}, {106, 122, 29}, }, |
536 | {{48, 95, -13}, {97, 114, 29}, {115, 115, 43}, {116, 122, 29}, }, |
537 | {{48, 100, -21}, {101, 101, 44}, {102, 122, 29}, }, |
538 | {{48, 122, -13}, }, |
539 | {{48, 116, -23}, {117, 117, 45}, {118, 122, 29}, }, |
540 | {{48, 100, -21}, {101, 101, 46}, {102, 122, 29}, }, |
541 | {{48, 114, -39}, {115, 115, 47}, {116, 122, 29}, }, |
542 | {{48, 114, -39}, {115, 115, 48}, {116, 122, 29}, }, |
543 | {{48, 95, -13}, {97, 109, 29}, {110, 110, 49}, {111, 122, 29}, }, |
544 | {{48, 95, -13}, {97, 113, 29}, {114, 114, 50}, {115, 122, 29}, }, |
545 | {{48, 113, -47}, {114, 114, 51}, {115, 122, 29}, }, |
546 | {{48, 122, -13}, }, |
547 | {{48, 122, -13}, }, |
548 | {{48, 95, -13}, {97, 99, 29}, {100, 100, 52}, {101, 122, 29}, }, |
549 | {{48, 109, -46}, {110, 110, 53}, {111, 122, 29}, }, |
550 | {{48, 122, -13}, }, |
551 | {{48, 114, -39}, {115, 115, 54}, {116, 122, 29}, }, |
552 | {{48, 122, -13}, }, |
553 | {{48, 122, -13}, }, |
554 | } |
555 | };*/ |
556 | |
557 | private static int[][] accept; |
558 | /* { |
559 | // INITIAL |
560 | {-1, 17, 16, 16, 17, 10, 11, 9, 14, 8, 15, 18, 12, -1, 13, 18, 18, 18, 18, 18, 18, 18, 18, 6, 7, 16, 18, 18, 18, 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 4, 18, 18, 18, 18, 18, 18, 18, 3, 0, 18, 18, 2, 18, 5, 1, }, |
561 | |
562 | };*/ |
563 | |
564 | public static class State |
565 | { |
566 | public final static State INITIAL = new State(0); |
567 | |
568 | private int id; |
569 | |
570 | private State(int id) |
571 | { |
572 | this.id = id; |
573 | } |
574 | |
575 | public int id() |
576 | { |
577 | return id; |
578 | } |
579 | } |
580 | } |