Mercurial > lcfOS
annotate python/ppci/errors.py @ 295:917eab04b8b7
Added disasm
author | Windel Bouwman |
---|---|
date | Thu, 28 Nov 2013 21:10:32 +0100 |
parents | 6aa721e7b10b |
children | 9417caea2eb3 |
rev | line source |
---|---|
148 | 1 """ |
2 Error handling routines | |
3 Diagnostic utils | |
4 """ | |
5 | |
255 | 6 import logging |
194 | 7 from . import SourceLocation |
8 | |
152 | 9 class CompilerError(Exception): |
200 | 10 def __init__(self, msg, loc=None): |
191 | 11 self.msg = msg |
12 self.loc = loc | |
200 | 13 if loc: |
289 | 14 assert type(loc) is SourceLocation, \ |
15 '{0} must be SourceLocation'.format(type(loc)) | |
215 | 16 self.row = loc.row |
17 self.col = loc.col | |
18 else: | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
248
diff
changeset
|
19 self.row = self.col = 0 |
215 | 20 |
191 | 21 def __repr__(self): |
293 | 22 return '"{}"'.format(self.msg) |
1 | 23 |
148 | 24 |
152 | 25 class DiagnosticsManager: |
225 | 26 def __init__(self): |
248 | 27 self.diags = [] |
293 | 28 self.sources = {} |
255 | 29 self.logger = logging.getLogger('diagnostics') |
215 | 30 |
293 | 31 def addSource(self, name, src): |
32 self.logger.info('Adding source {}'.format(name)) | |
33 self.sources[name] = src | |
34 | |
225 | 35 def addDiag(self, d): |
293 | 36 self.logger.warning(str(d.msg)) |
248 | 37 self.diags.append(d) |
215 | 38 |
225 | 39 def error(self, msg, loc): |
248 | 40 self.addDiag(CompilerError(msg, loc)) |
215 | 41 |
225 | 42 def clear(self): |
248 | 43 del self.diags[:] |
293 | 44 self.sources.clear() |
215 | 45 |
293 | 46 def printErrors(self): |
225 | 47 if len(self.diags) > 0: |
48 print('{0} Errors'.format(len(self.diags))) | |
49 for d in self.diags: | |
293 | 50 self.printError(d) |
148 | 51 |
293 | 52 def printError(self, e): |
53 def printLine(row, txt): | |
54 print(str(row)+':'+txt) | |
295 | 55 print('==============') |
293 | 56 if not e.loc: |
57 print('Error: {0}'.format(e)) | |
58 else: | |
59 if e.loc.filename not in self.sources: | |
60 print('Error: {0}'.format(e)) | |
61 return | |
295 | 62 print("File: {}".format(e.loc.filename)) |
293 | 63 source = self.sources[e.loc.filename] |
64 lines = source.split('\n') | |
65 ro, co = e.row, e.col | |
66 prerow = ro - 2 | |
67 if prerow < 1: | |
68 prerow = 1 | |
69 afterrow = ro + 3 | |
70 if afterrow > len(lines): | |
71 afterrow = len(lines) | |
72 | |
73 # print preceding source lines: | |
74 for r in range(prerow, ro): | |
75 printLine(r, lines[r-1]) | |
76 # print source line containing error: | |
77 printLine(ro, lines[ro-1]) | |
78 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg)) | |
79 # print trailing source line: | |
80 for r in range(ro+1, afterrow+1): | |
81 printLine(r, lines[r-1]) | |
295 | 82 print('==============') |