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