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