Mercurial > lcfOS
annotate python/ppci/errors.py @ 196:ec2b423cdbea
Merge asm and asmlib files
author | Windel Bouwman |
---|---|
date | Sat, 01 Jun 2013 11:55:49 +0200 |
parents | b01429a5d695 |
children | 5e391d9a3381 |
rev | line source |
---|---|
148 | 1 """ |
2 Error handling routines | |
3 Diagnostic utils | |
4 """ | |
5 | |
194 | 6 from . import SourceLocation |
7 | |
152 | 8 class CompilerError(Exception): |
191 | 9 def __init__(self, msg, loc): |
10 self.msg = msg | |
11 self.loc = loc | |
194 | 12 assert type(loc) is SourceLocation, '{0} must be SourceLocation'.format(type(loc)) |
191 | 13 def __repr__(self): |
14 return 'Compilererror {0} at row {1}'.format(self.msg, self.loc.row) | |
1 | 15 |
16 def printError(source, e): | |
17 def printLine(row, txt): | |
18 print(str(row)+':'+txt) | |
148 | 19 if e.loc.row == 0: |
1 | 20 print('Error: {0}'.format(e.msg)) |
21 else: | |
22 lines = source.split('\n') | |
148 | 23 ro, co = e.loc.row, e.loc.col |
24 prerow = ro - 2 | |
1 | 25 if prerow < 1: |
26 prerow = 1 | |
148 | 27 afterrow = ro + 3 |
1 | 28 if afterrow > len(lines): |
29 afterrow = len(lines) | |
30 | |
31 # print preceding source lines: | |
148 | 32 for r in range(prerow, ro): |
1 | 33 printLine(r, lines[r-1]) |
34 # print source line containing error: | |
148 | 35 printLine(ro, lines[ro-1]) |
36 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg)) | |
1 | 37 # print trailing source line: |
148 | 38 for r in range(ro+1, afterrow+1): |
1 | 39 printLine(r, lines[r-1]) |
148 | 40 |
152 | 41 class DiagnosticsManager: |
148 | 42 def __init__(self): |
43 self.diags = [] | |
152 | 44 def addDiag(self, d): |
148 | 45 self.diags.append(d) |
152 | 46 def error(self, msg, loc): |
47 self.addDiag(CompilerError(msg, loc)) | |
167 | 48 def clear(self): |
182
e9b27f7193e3
Replace clear function by function also supported in python 3.2
Windel Bouwman
parents:
172
diff
changeset
|
49 del self.diags[:] |
172 | 50 def printErrors(self, src): |
51 if len(self.diags) > 0: | |
52 print('{0} Errors'.format(len(self.diags))) | |
53 for d in self.diags: | |
54 printError(src, d) | |
148 | 55 |