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