comparison python/ppci/common.py @ 287:1c7c1e619be8

File movage
author Windel Bouwman
date Thu, 21 Nov 2013 11:57:27 +0100
parents e41e4109addd
children 534b94b40aa8
comparison
equal deleted inserted replaced
286:d9df72971cbf 287:1c7c1e619be8
1 from collections import namedtuple 1 from collections import namedtuple
2
2 3
3 # Token is used in the lexical analyzer: 4 # Token is used in the lexical analyzer:
4 class Token: 5 class Token:
5 def __init__(self, typ, val, loc=None): 6 def __init__(self, typ, val, loc=None):
6 self.typ = typ 7 self.typ = typ
7 self.val = val 8 self.val = val
8 if loc is None: 9 if loc is None:
9 loc = SourceLocation(0, 0, 0) 10 loc = SourceLocation('', 0, 0, 0)
10 assert type(loc) is SourceLocation 11 assert type(loc) is SourceLocation
11 self.loc = loc 12 self.loc = loc
12 def __repr__(self): 13 def __repr__(self):
13 return 'Token({0}, {1})'.format(self.typ, self.val) 14 return 'Token({0}, {1})'.format(self.typ, self.val)
14 15
16
15 class SourceLocation: 17 class SourceLocation:
16 def __init__(self, row, col, ln): 18 def __init__(self, filename, row, col, ln):
19 self.filename = filename
17 self.row = row 20 self.row = row
18 self.col = col 21 self.col = col
19 self.length = ln 22 self.length = ln
20 self.filename = ''
21 23
22 def __repr__(self): 24 def __repr__(self):
23 return '{}, {}'.format(self.row, self.col) 25 return '{}, {}'.format(self.row, self.col)
24 26
27
25 SourceRange = namedtuple('SourceRange', ['p1', 'p2']) 28 SourceRange = namedtuple('SourceRange', ['p1', 'p2'])
26 29