lexer.txt 10.5 KB
Newer Older
Jens Bendisposto's avatar
Jens Bendisposto committed
1
2
3
4
5
6
7
8
9
10
11
12
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of SableCC.                             *
 * See the file "LICENSE" for copyright information and the  *
 * terms and conditions for copying, distribution and        *
 * modification of SableCC.                                  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

Macro:LexerException
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

Jens Bendisposto's avatar
Jens Bendisposto committed
13
@SuppressWarnings({"serial"})
Jens Bendisposto's avatar
Jens Bendisposto committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class LexerException extends Exception
{
    public LexerException(String message)
    {
        super(message);
    }
}

$

Macro:LexerHeader
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

import java.io.*;
import java.util.*;
import $1$.*;
import de.hhu.stups.sablecc.patch.*;
bendisposto's avatar
bendisposto committed
33
import java.util.concurrent.LinkedBlockingQueue;
Jens Bendisposto's avatar
Jens Bendisposto committed
34

Jens Bendisposto's avatar
Jens Bendisposto committed
35
@SuppressWarnings({"unused"}) 
Jens Bendisposto's avatar
Jens Bendisposto committed
36
37
38
39
40
41
public class Lexer implements ITokenListContainer
{
    protected Token token;
    protected State state = State.$2$;

    private PushbackReader in;
42
43
    protected int line;
    protected int pos;
Jens Bendisposto's avatar
Jens Bendisposto committed
44
45
46
47
48
    private boolean cr;
    private boolean eof;
    private final StringBuffer text = new StringBuffer();
    
	private List<IToken> tokenList;
49
    private final Queue<IToken> nextList = new LinkedBlockingQueue<IToken>();
Jens Bendisposto's avatar
Jens Bendisposto committed
50
51
52

	private IToken tok;

53
54
55
56
    public Queue<IToken> getNextList() {
        return nextList;
    }

Jens Bendisposto's avatar
Jens Bendisposto committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
	public List<IToken> getTokenList() {
		return tokenList;
	}
	
	private void setToken(Token t) {
	  tok = t;
   	  token = t;	
	}
	
	
	public void setTokenList(final List<IToken> list) {
		tokenList = list;
	}

71
     
Jens Bendisposto's avatar
Jens Bendisposto committed
72
73
74
75
76
77
78
79
80
81
    protected void filter() throws LexerException, IOException
    {
        // Do nothing
    }

    protected void filterWrap() throws LexerException, IOException
    {
       filter();
       if (token != null) {
	          getTokenList().add(token); 
82
              nextList.add(token);
Jens Bendisposto's avatar
Jens Bendisposto committed
83
84
85
86
	   }
    }


87
    public Lexer(  PushbackReader in)
Jens Bendisposto's avatar
Jens Bendisposto committed
88
89
90
91
92
93
94
95
96
97
98
99
100
    {
        this.in = in;
    	setTokenList(new ArrayList<IToken>());
    }
    
    public Token peek() throws LexerException, IOException
    {
        while(this.token == null)
        {
            this.setToken(getToken());
            filterWrap();
        }

101
        return (Token) nextList.peek();
Jens Bendisposto's avatar
Jens Bendisposto committed
102
103
104
105
106
107
108
109
110
111
    }

    public Token next() throws LexerException, IOException
    {
        while(this.token == null)
        {
            this.setToken(getToken());
            filterWrap();
        }

112
        Token result = (Token) nextList.poll();
Jens Bendisposto's avatar
Jens Bendisposto committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
        this.setToken(null);
        return result;
    }

    protected Token getToken() throws IOException, LexerException
    {
        int dfa_state = 0;

        int start_pos = this.pos;
        int start_line = this.line;

        int accept_state = -1;
        int accept_token = -1;
        int accept_length = -1;
        int accept_pos = -1;
        int accept_line = -1;

130
131
          int[][][] gotoTable = Lexer.gotoTable[this.state.id()];
          int[] accept = Lexer.accept[this.state.id()];
Jens Bendisposto's avatar
Jens Bendisposto committed
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
        this.text.setLength(0);

        while(true)
        {
            int c = getChar();

            if(c != -1)
            {
                switch(c)
                {
                case 10:
                    if(this.cr)
                    {
                        this.cr = false;
                    }
                    else
                    {
                        this.line++;
                        this.pos = 0;
                    }
                    break;
                case 13:
                    this.line++;
                    this.pos = 0;
                    this.cr = true;
                    break;
                default:
                    this.pos++;
                    this.cr = false;
                    break;
                }

                this.text.append((char) c);

                do
                {
                    int oldState = (dfa_state < -1) ? (-2 -dfa_state) : dfa_state;

                    dfa_state = -1;

                    int[][] tmp1 =  gotoTable[oldState];
                    int low = 0;
                    int high = tmp1.length - 1;

                    while(low <= high)
                    {
                        int middle = (low + high) / 2;
                        int[] tmp2 = tmp1[middle];

                        if(c < tmp2[0])
                        {
                            high = middle - 1;
                        }
                        else if(c > tmp2[1])
                        {
                            low = middle + 1;
                        }
                        else
                        {
                            dfa_state = tmp2[2];
                            break;
                        }
                    }
                }while(dfa_state < -1);
            }
            else
            {
                dfa_state = -1;
            }

            if(dfa_state >= 0)
            {
                if(accept[dfa_state] != -1)
                {
                    accept_state = dfa_state;
                    accept_token = accept[dfa_state];
                    accept_length = this.text.length();
                    accept_pos = this.pos;
                    accept_line = this.line;
                }
            }
            else
            {
                if(accept_state != -1)
                {
                    switch(accept_token)
                    {

$

Macro:LexerVariableToken
                    case $0$:
                        {
225
                              Token token = new$0$(
Jens Bendisposto's avatar
Jens Bendisposto committed
226
227
228
229
230
231
232
233
234
235
236
237
                                getText(accept_length),
                                start_line + 1,
                                start_pos + 1);
                            pushBack(accept_length);
                            this.pos = accept_pos;
                            this.line = accept_line;

$

Macro:LexerFixedToken
                    case $0$:
                        {
238
                              Token token = new$0$(
Jens Bendisposto's avatar
Jens Bendisposto committed
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
                                start_line + 1,
                                start_pos + 1);
                            pushBack(accept_length);
                            this.pos = accept_pos;
                            this.line = accept_line;

$

Macro:TokenSwitchHeader
                            switch(state.id())
                            {

$

Macro:TokenCase
                                case $0$: state = State.$1$; break;

$

Macro:TokenSwitchTail
                            }

$

Macro:LexerTokenTail
                            return token;
                        }

$

Macro:LexerBody1
                    }
                }
                else
                {
                    if(this.text.length() > 0)
                    {
                        throw new LexerException(
                            "[" + (start_line + 1) + "," + (start_pos + 1) + "]" +
                            " Unknown token: " + this.text);
                    }

281
                      EOF token = new EOF(
Jens Bendisposto's avatar
Jens Bendisposto committed
282
283
284
285
286
287
288
289
290
291
292
293
                        start_line + 1,
                        start_pos + 1);
                    return token;
                }
            }
        }
    }


$

Macro:LexerNewVariableToken
294
    Token new$0$(  String text,   int line,   int pos) { return new $1$(text, line, pos); }
Jens Bendisposto's avatar
Jens Bendisposto committed
295
296
297
298

$

Macro:LexerNewFixedToken
299
    Token new$0$(  int line,   int pos) { return new $1$(line, pos); }
Jens Bendisposto's avatar
Jens Bendisposto committed
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

$

Macro:LexerBody2

    private int getChar() throws IOException
    {
        if(this.eof)
        {
            return -1;
        }

        int result = this.in.read();

        if(result == -1)
        {
            this.eof = true;
        }

        return result;
    }

    private void pushBack(int acceptLength) throws IOException
    {
        int length = this.text.length();
        for(int i = length - 1; i >= acceptLength; i--)
        {
            this.eof = false;

            this.in.unread(this.text.charAt(i));
        }
    }

333
    protected void unread(  Token token) throws IOException
Jens Bendisposto's avatar
Jens Bendisposto committed
334
    {
335
          String text = token.getText();
Jens Bendisposto's avatar
Jens Bendisposto committed
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
        int length = text.length();

        for(int i = length - 1; i >= 0; i--)
        {
            this.eof = false;

            this.in.unread(text.charAt(i));
        }

        this.pos = token.getPos() - 1;
        this.line = token.getLine() - 1;
    }

    private String getText(int acceptLength)
    {
        StringBuffer s = new StringBuffer(acceptLength);
        for(int i = 0; i < acceptLength; i++)
        {
            s.append(this.text.charAt(i));
        }

        return s.toString();
    }

    private static int[][][][] gotoTable;
/*  {

$

Macro:LexerAcceptHeader
    };*/

    private static int[][] accept;
/*  {

$

Macro:LexerAcceptTail
    };*/


$

Macro:LexerStateHeader
    public static class State
    {

$

Macro:LexerStateBody
        public final static State $0$ = new State($1$);

$

Macro:LexerStateTail

        private int id;

394
        private State(  int id)
Jens Bendisposto's avatar
Jens Bendisposto committed
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
        {
            this.id = id;
        }

        public int id()
        {
            return this.id;
        }
    }

$

Macro:LexerTail

    static 
    {
        try
        {
            DataInputStream s = new DataInputStream(
                new BufferedInputStream(
                Lexer.class.getResourceAsStream("lexer.dat")));

            // read gotoTable
            int length = s.readInt();
            gotoTable = new int[length][][][];
            for(int i = 0; i < gotoTable.length; i++)
            {
                length = s.readInt();
                gotoTable[i] = new int[length][][];
                for(int j = 0; j < gotoTable[i].length; j++)
                {
                    length = s.readInt();
                    gotoTable[i][j] = new int[length][3];
                    for(int k = 0; k < gotoTable[i][j].length; k++)
                    {
                        for(int l = 0; l < 3; l++)
                        {
                            gotoTable[i][j][k][l] = s.readInt();
                        }
                    }
                }
            }

            // read accept
            length = s.readInt();
            accept = new int[length][];
            for(int i = 0; i < accept.length; i++)
            {
                length = s.readInt();
                accept[i] = new int[length];
                for(int j = 0; j < accept[i].length; j++)
                {
                    accept[i][j] = s.readInt();
                }
            }

            s.close();
        }
        catch(Exception e)
        {
            throw new RuntimeException("The file \"lexer.dat\" is either missing or corrupted.");
        }
    }
}

$