-
Notifications
You must be signed in to change notification settings - Fork 7
/
Scanner.py
582 lines (539 loc) · 17.7 KB
/
Scanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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
333
334
335
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
394
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#-------------------------------------
#Scanner.py - The ATG file scanner.
#Compiler Generator Coco/R,
#Copyright (c) 1990, 2004 Hanspeter Moessenboeck, University of Linz
#extended by M. Loeberbauer & A. Woess, Univ. of Linz
#ported from Java to Python by Ronald Longo
#
#This program is free software; you can redistribute it and/or modify it
#under the terms of the GNU General Public License as published by the
#Free Software Foundation; either version 2, or (at your option) any
#later version.
#
#This program is distributed in the hope that it will be useful, but
#WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
#for more details.
#
#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#As an exception, it is allowed to write an extension of Coco/R that is
#used as a plugin in non-free software.
#
#If not otherwise stated, any source code generated by Coco/R (other than
#Coco/R itself) does not fall under the GNU General Public License.
#-------------------------------------
import sys
class Token( object ):
def __init__( self ):
self.kind = 0 # token kind
self.pos = 0 # token position in the source text (starting at 0)
self.col = 0 # token column (starting at 0)
self.line = 0 # token line (starting at 1)
self.val = u'' # token value
self.next = None # AW 2003-03-07 Tokens are kept in linked list
class Position( object ): # position of source code stretch (e.g. semantic action, resolver expressions)
def __init__( self, buf, beg, len, col ):
assert isinstance( buf, Buffer )
assert isinstance( beg, int )
assert isinstance( len, int )
assert isinstance( col, int )
self.buf = buf
self.beg = beg # start relative to the beginning of the file
self.len = len # length of stretch
self.col = col # column number of start position
def getSubstring( self ):
return self.buf.readPosition( self )
class Buffer( object ):
EOF = u'\u0100' # 256
def __init__( self, s ):
self.buf = s
self.bufLen = len(s)
self.pos = 0
self.lines = s.splitlines( True )
def Read( self ):
if self.pos < self.bufLen:
result = unichr(ord(self.buf[self.pos]) & 0xff) # mask out sign bits
self.pos += 1
return result
else:
return Buffer.EOF
def ReadChars( self, numBytes=1 ):
result = self.buf[ self.pos : self.pos + numBytes ]
self.pos += numBytes
return result
def Peek( self ):
if self.pos < self.bufLen:
return unichr(ord(self.buf[self.pos]) & 0xff) # mask out sign bits
else:
return Scanner.buffer.EOF
def getString( self, beg, end ):
s = ''
oldPos = self.getPos( )
self.setPos( beg )
while beg < end:
s += self.Read( )
beg += 1
self.setPos( oldPos )
return s
def getPos( self ):
return self.pos
def setPos( self, value ):
if value < 0:
self.pos = 0
elif value >= self.bufLen:
self.pos = self.bufLen
else:
self.pos = value
def readPosition( self, pos ):
assert isinstance( pos, Position )
self.setPos( pos.beg )
return self.ReadChars( pos.len )
def __iter__( self ):
return iter(self.lines)
class Scanner(object):
EOL = u'\n'
eofSym = 0
charSetSize = 256
maxT = 48
noSym = 48
# terminals
EOF_SYM = 0
ident_Sym = 1
number_Sym = 2
string_Sym = 3
badString_Sym = 4
COMPILER_Sym = 5
IGNORECASE_Sym = 6
CHARACTERS_Sym = 7
TOKENS_Sym = 8
NAMES_Sym = 9
PRAGMAS_Sym = 10
COMMENTS_Sym = 11
FROM_Sym = 12
TO_Sym = 13
NESTED_Sym = 14
IGNORE_Sym = 15
PRODUCTIONS_Sym = 16
equal_Sym = 17
point_Sym = 18
END_Sym = 19
plus_Sym = 20
minus_Sym = 21
pointpoint_Sym = 22
ANY_Sym = 23
CHR_Sym = 24
lparen_Sym = 25
rparen_Sym = 26
less_Sym = 27
uparrow_Sym = 28
out_Sym = 29
greater_Sym = 30
comma_Sym = 31
lesspoint_Sym = 32
pointgreater_Sym = 33
bar_Sym = 34
WEAK_Sym = 35
lbrack_Sym = 36
rbrack_Sym = 37
lbrace_Sym = 38
rbrace_Sym = 39
SYNC_Sym = 40
IF_Sym = 41
CONTEXT_Sym = 42
lparenpoint_Sym = 43
pointrparen_Sym = 44
from_Sym = 45
import_Sym = 46
star_Sym = 47
NOT_SYM = 48
# pragmas
ddtSym_Sym = 49
start = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 6, 0, 5, 0, 0, 7, 29, 14, 27, 11, 17, 12, 28, 0,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 30, 10, 16, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 21, 0, 22, 15, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 23, 20, 24, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-1]
def __init__( self, s ):
self.buffer = Buffer( unicode(s) ) # the buffer instance
self.ch = u'\0' # current input character
self.pos = -1 # column number of current character
self.line = 1 # line number of current character
self.lineStart = 0 # start position of current line
self.oldEols = 0 # EOLs that appeared in a comment;
self.NextCh( )
self.ignore = set( ) # set of characters to be ignored by the scanner
self.ignore.add( ord(' ') ) # blanks are always white space
self.ignore.add(9)
self.ignore.add(10)
self.ignore.add(13)
# fill token list
self.tokens = Token( ) # the complete input token stream
node = self.tokens
node.next = self.NextToken( )
node = node.next
while node.kind != Scanner.eofSym:
node.next = self.NextToken( )
node = node.next
node.next = node
node.val = u'EOF'
self.t = self.tokens # current token
self.pt = self.tokens # current peek token
def NextCh( self ):
if self.oldEols > 0:
self.ch = Scanner.EOL
self.oldEols -= 1
else:
self.ch = self.buffer.Read( )
self.pos += 1
# replace isolated '\r' by '\n' in order to make
# eol handling uniform across Windows, Unix and Mac
if (self.ch == u'\r') and (self.buffer.Peek() != u'\n'):
self.ch = Scanner.EOL
if self.ch == Scanner.EOL:
self.line += 1
self.lineStart = self.pos + 1
def Comment0( self ):
level = 1
line0 = self.line
lineStart0 = self.lineStart
self.NextCh()
if self.ch == '/':
self.NextCh()
while True:
if ord(self.ch) == 10:
level -= 1
if level == 0:
self.oldEols = self.line - line0
self.NextCh()
return True
self.NextCh()
elif self.ch == Buffer.EOF:
return False
else:
self.NextCh()
else:
if self.ch == Scanner.EOL:
self.line -= 1
self.lineStart = lineStart0
self.pos = self.pos - 2
self.buffer.setPos(self.pos+1)
self.NextCh()
return False
def Comment1( self ):
level = 1
line0 = self.line
lineStart0 = self.lineStart
self.NextCh()
if self.ch == '*':
self.NextCh()
while True:
if self.ch == '*':
self.NextCh()
if self.ch == '/':
level -= 1
if level == 0:
self.oldEols = self.line - line0
self.NextCh()
return True
self.NextCh()
elif self.ch == '/':
self.NextCh()
if self.ch == '*':
level += 1
self.NextCh()
elif self.ch == Buffer.EOF:
return False
else:
self.NextCh()
else:
if self.ch == Scanner.EOL:
self.line -= 1
self.lineStart = lineStart0
self.pos = self.pos - 2
self.buffer.setPos(self.pos+1)
self.NextCh()
return False
def CheckLiteral( self ):
lit = self.t.val
if lit == "COMPILER":
self.t.kind = Scanner.COMPILER_Sym
elif lit == "IGNORECASE":
self.t.kind = Scanner.IGNORECASE_Sym
elif lit == "CHARACTERS":
self.t.kind = Scanner.CHARACTERS_Sym
elif lit == "TOKENS":
self.t.kind = Scanner.TOKENS_Sym
elif lit == "NAMES":
self.t.kind = Scanner.NAMES_Sym
elif lit == "PRAGMAS":
self.t.kind = Scanner.PRAGMAS_Sym
elif lit == "COMMENTS":
self.t.kind = Scanner.COMMENTS_Sym
elif lit == "FROM":
self.t.kind = Scanner.FROM_Sym
elif lit == "TO":
self.t.kind = Scanner.TO_Sym
elif lit == "NESTED":
self.t.kind = Scanner.NESTED_Sym
elif lit == "IGNORE":
self.t.kind = Scanner.IGNORE_Sym
elif lit == "PRODUCTIONS":
self.t.kind = Scanner.PRODUCTIONS_Sym
elif lit == "END":
self.t.kind = Scanner.END_Sym
elif lit == "ANY":
self.t.kind = Scanner.ANY_Sym
elif lit == "CHR":
self.t.kind = Scanner.CHR_Sym
elif lit == "out":
self.t.kind = Scanner.out_Sym
elif lit == "WEAK":
self.t.kind = Scanner.WEAK_Sym
elif lit == "SYNC":
self.t.kind = Scanner.SYNC_Sym
elif lit == "IF":
self.t.kind = Scanner.IF_Sym
elif lit == "CONTEXT":
self.t.kind = Scanner.CONTEXT_Sym
elif lit == "from":
self.t.kind = Scanner.from_Sym
elif lit == "import":
self.t.kind = Scanner.import_Sym
def NextToken( self ):
while ord(self.ch) in self.ignore:
self.NextCh( )
if (self.ch == '/' and self.Comment0() or self.ch == '/' and self.Comment1()):
return self.NextToken()
self.t = Token( )
self.t.pos = self.pos
self.t.col = self.pos - self.lineStart + 1
self.t.line = self.line
state = self.start[ord(self.ch)]
buf = u''
buf += unicode(self.ch)
self.NextCh()
done = False
while not done:
if state == -1:
self.t.kind = Scanner.eofSym # NextCh already done
done = True
elif state == 0:
self.t.kind = Scanner.noSym # NextCh already done
done = True
elif state == 1:
if (self.ch >= '0' and self.ch <= '9'
or self.ch >= 'A' and self.ch <= 'Z'
or self.ch >= 'a' and self.ch <= 'z'):
buf += unicode(self.ch)
self.NextCh()
state = 1
else:
self.t.kind = Scanner.ident_Sym
self.t.val = buf
self.CheckLiteral()
return self.t
elif state == 2:
if (self.ch >= '0' and self.ch <= '9'):
buf += unicode(self.ch)
self.NextCh()
state = 2
else:
self.t.kind = Scanner.number_Sym
done = True
elif state == 3:
self.t.kind = Scanner.string_Sym
done = True
elif state == 4:
self.t.kind = Scanner.badString_Sym
done = True
elif state == 5:
if (self.ch >= '0' and self.ch <= '9'
or self.ch >= 'A' and self.ch <= 'Z'
or self.ch >= 'a' and self.ch <= 'z'):
buf += unicode(self.ch)
self.NextCh()
state = 5
else:
self.t.kind = Scanner.ddtSym_Sym
done = True
elif state == 6:
if (ord(self.ch) <= 9
or ord(self.ch) >= 11 and ord(self.ch) <= 12
or ord(self.ch) >= 14 and self.ch <= '!'
or self.ch >= '#' and self.ch <= '['
or self.ch >= ']' and ord(self.ch) <= 255):
buf += unicode(self.ch)
self.NextCh()
state = 6
elif (ord(self.ch) == 10
or ord(self.ch) == 13):
buf += unicode(self.ch)
self.NextCh()
state = 4
elif self.ch == '"':
buf += unicode(self.ch)
self.NextCh()
state = 3
elif ord(self.ch) == 92:
buf += unicode(self.ch)
self.NextCh()
state = 8
else:
self.t.kind = Scanner.noSym
done = True
elif state == 7:
if (ord(self.ch) <= 9
or ord(self.ch) >= 11 and ord(self.ch) <= 12
or ord(self.ch) >= 14 and self.ch <= '&'
or self.ch >= '(' and self.ch <= '['
or self.ch >= ']' and ord(self.ch) <= 255):
buf += unicode(self.ch)
self.NextCh()
state = 7
elif (ord(self.ch) == 10
or ord(self.ch) == 13):
buf += unicode(self.ch)
self.NextCh()
state = 4
elif ord(self.ch) == 39:
buf += unicode(self.ch)
self.NextCh()
state = 3
elif ord(self.ch) == 92:
buf += unicode(self.ch)
self.NextCh()
state = 9
else:
self.t.kind = Scanner.noSym
done = True
elif state == 8:
if (self.ch >= ' ' and self.ch <= '~'):
buf += unicode(self.ch)
self.NextCh()
state = 6
else:
self.t.kind = Scanner.noSym
done = True
elif state == 9:
if (self.ch >= ' ' and self.ch <= '~'):
buf += unicode(self.ch)
self.NextCh()
state = 7
else:
self.t.kind = Scanner.noSym
done = True
elif state == 10:
self.t.kind = Scanner.equal_Sym
done = True
elif state == 11:
self.t.kind = Scanner.plus_Sym
done = True
elif state == 12:
self.t.kind = Scanner.minus_Sym
done = True
elif state == 13:
self.t.kind = Scanner.pointpoint_Sym
done = True
elif state == 14:
self.t.kind = Scanner.rparen_Sym
done = True
elif state == 15:
self.t.kind = Scanner.uparrow_Sym
done = True
elif state == 16:
self.t.kind = Scanner.greater_Sym
done = True
elif state == 17:
self.t.kind = Scanner.comma_Sym
done = True
elif state == 18:
self.t.kind = Scanner.lesspoint_Sym
done = True
elif state == 19:
self.t.kind = Scanner.pointgreater_Sym
done = True
elif state == 20:
self.t.kind = Scanner.bar_Sym
done = True
elif state == 21:
self.t.kind = Scanner.lbrack_Sym
done = True
elif state == 22:
self.t.kind = Scanner.rbrack_Sym
done = True
elif state == 23:
self.t.kind = Scanner.lbrace_Sym
done = True
elif state == 24:
self.t.kind = Scanner.rbrace_Sym
done = True
elif state == 25:
self.t.kind = Scanner.lparenpoint_Sym
done = True
elif state == 26:
self.t.kind = Scanner.pointrparen_Sym
done = True
elif state == 27:
self.t.kind = Scanner.star_Sym
done = True
elif state == 28:
if self.ch == '.':
buf += unicode(self.ch)
self.NextCh()
state = 13
elif self.ch == '>':
buf += unicode(self.ch)
self.NextCh()
state = 19
elif self.ch == ')':
buf += unicode(self.ch)
self.NextCh()
state = 26
else:
self.t.kind = Scanner.point_Sym
done = True
elif state == 29:
if self.ch == '.':
buf += unicode(self.ch)
self.NextCh()
state = 25
else:
self.t.kind = Scanner.lparen_Sym
done = True
elif state == 30:
if self.ch == '.':
buf += unicode(self.ch)
self.NextCh()
state = 18
else:
self.t.kind = Scanner.less_Sym
done = True
self.t.val = buf
return self.t
def Scan( self ):
self.t = self.t.next
self.pt = self.t.next
return self.t
def Peek( self ):
self.pt = self.pt.next
while self.pt.kind > self.maxT:
self.pt = self.pt.next
return self.pt
def ResetPeek( self ):
self.pt = self.t