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