Mercurial > lcfOS
annotate python/ppci/errors.py @ 290:7b38782ed496
File moves
author | Windel Bouwman |
---|---|
date | Sun, 24 Nov 2013 11:24:15 +0100 |
parents | bd2593de3ff8 |
children | 6aa721e7b10b |
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): |
215 | 22 if self.row: |
289 | 23 return '"{0}" at row {1}'.format(self.msg, self.row) |
200 | 24 else: |
289 | 25 return '"{0}"'.format(self.msg) |
1 | 26 |
27 def printError(source, e): | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
248
diff
changeset
|
28 def printLine(row, txt): |
1 | 29 print(str(row)+':'+txt) |
288 | 30 if e.row == 0 or True: |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
248
diff
changeset
|
31 print('Error: {0}'.format(e.msg)) |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
248
diff
changeset
|
32 else: |
1 | 33 lines = source.split('\n') |
215 | 34 ro, co = e.row, e.col |
148 | 35 prerow = ro - 2 |
1 | 36 if prerow < 1: |
37 prerow = 1 | |
148 | 38 afterrow = ro + 3 |
1 | 39 if afterrow > len(lines): |
40 afterrow = len(lines) | |
41 | |
42 # print preceding source lines: | |
148 | 43 for r in range(prerow, ro): |
1 | 44 printLine(r, lines[r-1]) |
45 # print source line containing error: | |
148 | 46 printLine(ro, lines[ro-1]) |
47 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg)) | |
1 | 48 # print trailing source line: |
148 | 49 for r in range(ro+1, afterrow+1): |
1 | 50 printLine(r, lines[r-1]) |
148 | 51 |
152 | 52 class DiagnosticsManager: |
225 | 53 def __init__(self): |
248 | 54 self.diags = [] |
255 | 55 self.logger = logging.getLogger('diagnostics') |
215 | 56 |
225 | 57 def addDiag(self, d): |
281 | 58 self.logger.info(str(d.msg)) |
248 | 59 self.diags.append(d) |
215 | 60 |
225 | 61 def error(self, msg, loc): |
248 | 62 self.addDiag(CompilerError(msg, loc)) |
215 | 63 |
225 | 64 def clear(self): |
248 | 65 del self.diags[:] |
215 | 66 |
225 | 67 def printErrors(self, src): |
68 if len(self.diags) > 0: | |
69 print('==============') | |
70 print('{0} Errors'.format(len(self.diags))) | |
71 for d in self.diags: | |
72 print('==============') | |
73 printError(src, d) | |
74 print('==============') | |
148 | 75 |