153
|
1 from collections import namedtuple
|
|
2
|
287
|
3
|
191
|
4 # Token is used in the lexical analyzer:
|
194
|
5 class Token:
|
|
6 def __init__(self, typ, val, loc=None):
|
|
7 self.typ = typ
|
|
8 self.val = val
|
|
9 if loc is None:
|
287
|
10 loc = SourceLocation('', 0, 0, 0)
|
194
|
11 assert type(loc) is SourceLocation
|
|
12 self.loc = loc
|
292
|
13
|
194
|
14 def __repr__(self):
|
|
15 return 'Token({0}, {1})'.format(self.typ, self.val)
|
191
|
16
|
287
|
17
|
163
|
18 class SourceLocation:
|
287
|
19 def __init__(self, filename, row, col, ln):
|
|
20 self.filename = filename
|
249
|
21 self.row = row
|
|
22 self.col = col
|
|
23 self.length = ln
|
|
24
|
|
25 def __repr__(self):
|
293
|
26 return '{}, {}, {}'.format(self.filename, self.row, self.col)
|
163
|
27
|
287
|
28
|
153
|
29 SourceRange = namedtuple('SourceRange', ['p1', 'p2'])
|