Mercurial > lcfOS
comparison python/ppci/errors.py @ 293:6aa721e7b10b
Try to improve build sequence
author | Windel Bouwman |
---|---|
date | Thu, 28 Nov 2013 20:39:37 +0100 |
parents | bd2593de3ff8 |
children | 917eab04b8b7 |
comparison
equal
deleted
inserted
replaced
292:534b94b40aa8 | 293:6aa721e7b10b |
---|---|
17 self.col = loc.col | 17 self.col = loc.col |
18 else: | 18 else: |
19 self.row = self.col = 0 | 19 self.row = self.col = 0 |
20 | 20 |
21 def __repr__(self): | 21 def __repr__(self): |
22 if self.row: | 22 return '"{}"'.format(self.msg) |
23 return '"{0}" at row {1}'.format(self.msg, self.row) | |
24 else: | |
25 return '"{0}"'.format(self.msg) | |
26 | 23 |
27 def printError(source, e): | |
28 def printLine(row, txt): | |
29 print(str(row)+':'+txt) | |
30 if e.row == 0 or True: | |
31 print('Error: {0}'.format(e.msg)) | |
32 else: | |
33 lines = source.split('\n') | |
34 ro, co = e.row, e.col | |
35 prerow = ro - 2 | |
36 if prerow < 1: | |
37 prerow = 1 | |
38 afterrow = ro + 3 | |
39 if afterrow > len(lines): | |
40 afterrow = len(lines) | |
41 | |
42 # print preceding source lines: | |
43 for r in range(prerow, ro): | |
44 printLine(r, lines[r-1]) | |
45 # print source line containing error: | |
46 printLine(ro, lines[ro-1]) | |
47 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg)) | |
48 # print trailing source line: | |
49 for r in range(ro+1, afterrow+1): | |
50 printLine(r, lines[r-1]) | |
51 | 24 |
52 class DiagnosticsManager: | 25 class DiagnosticsManager: |
53 def __init__(self): | 26 def __init__(self): |
54 self.diags = [] | 27 self.diags = [] |
28 self.sources = {} | |
55 self.logger = logging.getLogger('diagnostics') | 29 self.logger = logging.getLogger('diagnostics') |
56 | 30 |
31 def addSource(self, name, src): | |
32 self.logger.info('Adding source {}'.format(name)) | |
33 self.sources[name] = src | |
34 | |
57 def addDiag(self, d): | 35 def addDiag(self, d): |
58 self.logger.info(str(d.msg)) | 36 self.logger.warning(str(d.msg)) |
59 self.diags.append(d) | 37 self.diags.append(d) |
60 | 38 |
61 def error(self, msg, loc): | 39 def error(self, msg, loc): |
62 self.addDiag(CompilerError(msg, loc)) | 40 self.addDiag(CompilerError(msg, loc)) |
63 | 41 |
64 def clear(self): | 42 def clear(self): |
65 del self.diags[:] | 43 del self.diags[:] |
44 self.sources.clear() | |
66 | 45 |
67 def printErrors(self, src): | 46 def printErrors(self): |
68 if len(self.diags) > 0: | 47 if len(self.diags) > 0: |
69 print('==============') | 48 print('==============') |
70 print('{0} Errors'.format(len(self.diags))) | 49 print('{0} Errors'.format(len(self.diags))) |
71 for d in self.diags: | 50 for d in self.diags: |
72 print('==============') | 51 print('==============') |
73 printError(src, d) | 52 self.printError(d) |
74 print('==============') | 53 print('==============') |
75 | 54 |
55 def printError(self, e): | |
56 def printLine(row, txt): | |
57 print(str(row)+':'+txt) | |
58 print(type(e), e, e.msg) | |
59 if not e.loc: | |
60 print('Error: {0}'.format(e)) | |
61 else: | |
62 if e.loc.filename not in self.sources: | |
63 print('Error: {0}'.format(e)) | |
64 return | |
65 source = self.sources[e.loc.filename] | |
66 lines = source.split('\n') | |
67 ro, co = e.row, e.col | |
68 prerow = ro - 2 | |
69 if prerow < 1: | |
70 prerow = 1 | |
71 afterrow = ro + 3 | |
72 if afterrow > len(lines): | |
73 afterrow = len(lines) | |
74 | |
75 # print preceding source lines: | |
76 for r in range(prerow, ro): | |
77 printLine(r, lines[r-1]) | |
78 # print source line containing error: | |
79 printLine(ro, lines[ro-1]) | |
80 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg)) | |
81 # print trailing source line: | |
82 for r in range(ro+1, afterrow+1): | |
83 printLine(r, lines[r-1]) |