Mercurial > lcfOS
annotate python/ppci/errors.py @ 280:02385f62f250
Rework from str interface to Instruction interface
author | Windel Bouwman |
---|---|
date | Sat, 02 Nov 2013 10:03:26 +0100 |
parents | 7416c923a02a |
children | 4496cae24d7f |
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: |
14 assert type(loc) is SourceLocation, '{0} must be SourceLocation'.format(type(loc)) | |
215 | 15 self.row = loc.row |
16 self.col = loc.col | |
17 else: | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
248
diff
changeset
|
18 self.row = self.col = 0 |
215 | 19 |
191 | 20 def __repr__(self): |
215 | 21 if self.row: |
22 return 'Compilererror: "{0}" at row {1}'.format(self.msg, self.row) | |
200 | 23 else: |
24 return 'Compilererror: "{0}"'.format(self.msg) | |
1 | 25 |
26 def printError(source, e): | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
248
diff
changeset
|
27 def printLine(row, txt): |
1 | 28 print(str(row)+':'+txt) |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
248
diff
changeset
|
29 if e.row == 0: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
248
diff
changeset
|
30 print('Error: {0}'.format(e.msg)) |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
248
diff
changeset
|
31 else: |
1 | 32 lines = source.split('\n') |
215 | 33 ro, co = e.row, e.col |
148 | 34 prerow = ro - 2 |
1 | 35 if prerow < 1: |
36 prerow = 1 | |
148 | 37 afterrow = ro + 3 |
1 | 38 if afterrow > len(lines): |
39 afterrow = len(lines) | |
40 | |
41 # print preceding source lines: | |
148 | 42 for r in range(prerow, ro): |
1 | 43 printLine(r, lines[r-1]) |
44 # print source line containing error: | |
148 | 45 printLine(ro, lines[ro-1]) |
46 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg)) | |
1 | 47 # print trailing source line: |
148 | 48 for r in range(ro+1, afterrow+1): |
1 | 49 printLine(r, lines[r-1]) |
148 | 50 |
152 | 51 class DiagnosticsManager: |
225 | 52 def __init__(self): |
248 | 53 self.diags = [] |
255 | 54 self.logger = logging.getLogger('diagnostics') |
215 | 55 |
225 | 56 def addDiag(self, d): |
255 | 57 self.logger.info(str(d)) |
248 | 58 self.diags.append(d) |
215 | 59 |
225 | 60 def error(self, msg, loc): |
248 | 61 self.addDiag(CompilerError(msg, loc)) |
215 | 62 |
225 | 63 def clear(self): |
248 | 64 del self.diags[:] |
215 | 65 |
225 | 66 def printErrors(self, src): |
67 if len(self.diags) > 0: | |
68 print('==============') | |
69 print('{0} Errors'.format(len(self.diags))) | |
70 for d in self.diags: | |
71 print('==============') | |
72 printError(src, d) | |
73 print('==============') | |
148 | 74 |