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
|
|
13 def __repr__(self):
|
|
14 return 'Token({0}, {1})'.format(self.typ, self.val)
|
191
|
15
|
287
|
16
|
163
|
17 class SourceLocation:
|
287
|
18 def __init__(self, filename, row, col, ln):
|
|
19 self.filename = filename
|
249
|
20 self.row = row
|
|
21 self.col = col
|
|
22 self.length = ln
|
|
23
|
|
24 def __repr__(self):
|
|
25 return '{}, {}'.format(self.row, self.col)
|
163
|
26
|
287
|
27
|
153
|
28 SourceRange = namedtuple('SourceRange', ['p1', 'p2'])
|
|
29
|