comparison python/ppci/common.py @ 396:fb3c1f029b30

Added baselexer into c3 lexer
author Windel Bouwman
date Tue, 27 May 2014 22:19:32 +0200
parents b8ad45b3a573
children
comparison
equal deleted inserted replaced
395:3b0c495e3008 396:fb3c1f029b30
5 Error handling routines 5 Error handling routines
6 Diagnostic utils 6 Diagnostic utils
7 Source location structures 7 Source location structures
8 """ 8 """
9 9
10 # Token is used in the lexical analyzer:
11 class Token: 10 class Token:
12 def __init__(self, typ, val, loc=None): 11 """
12 Token is used in the lexical analyzer. The lexical analyzer takes
13 a text and splits it into tokens.
14 """
15 def __init__(self, typ, val, loc):
13 self.typ = typ 16 self.typ = typ
14 self.val = val 17 self.val = val
15 if loc is None:
16 loc = SourceLocation('', 0, 0, 0)
17 assert type(loc) is SourceLocation 18 assert type(loc) is SourceLocation
18 self.loc = loc 19 self.loc = loc
19 20
20 def __repr__(self): 21 def __repr__(self):
21 return 'Token({0}, {1})'.format(self.typ, self.val) 22 return 'Token({0}, {1})'.format(self.typ, self.val)
56 self.diags = [] 57 self.diags = []
57 self.sources = {} 58 self.sources = {}
58 self.logger = logging.getLogger('diagnostics') 59 self.logger = logging.getLogger('diagnostics')
59 60
60 def addSource(self, name, src): 61 def addSource(self, name, src):
61 self.logger.debug('Adding source {}'.format(name)) 62 self.logger.debug('Adding source, filename="{}"'.format(name))
62 self.sources[name] = src 63 self.sources[name] = src
63 64
64 def addDiag(self, d): 65 def addDiag(self, d):
65 self.logger.error(str(d.msg)) 66 self.logger.error(str(d.msg))
66 self.diags.append(d) 67 self.diags.append(d)
78 for d in self.diags: 79 for d in self.diags:
79 self.printError(d) 80 self.printError(d)
80 81
81 def printError(self, e): 82 def printError(self, e):
82 def printLine(row, txt): 83 def printLine(row, txt):
83 print(str(row)+':'+txt) 84 print(str(row) + ':' + txt)
84 print('==============') 85 print('==============')
85 if not e.loc: 86 if not e.loc:
86 print('Error: {0}'.format(e)) 87 print('Error: {0}'.format(e))
87 else: 88 else:
88 if e.loc.filename not in self.sources: 89 if e.loc.filename not in self.sources: