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
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
1 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
2 Error handling routines
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
3 Diagnostic utils
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
4 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
5
255
7416c923a02a Added more logging
Windel Bouwman
parents: 251
diff changeset
6 import logging
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
7 from . import SourceLocation
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
8
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
9 class CompilerError(Exception):
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
10 def __init__(self, msg, loc=None):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 182
diff changeset
11 self.msg = msg
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 182
diff changeset
12 self.loc = loc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
13 if loc:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
14 assert type(loc) is SourceLocation, '{0} must be SourceLocation'.format(type(loc))
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
15 self.row = loc.row
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
16 self.col = loc.col
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
17 else:
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 248
diff changeset
18 self.row = self.col = 0
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
19
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 182
diff changeset
20 def __repr__(self):
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
21 if self.row:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
22 return 'Compilererror: "{0}" at row {1}'.format(self.msg, self.row)
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
23 else:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
24 return 'Compilererror: "{0}"'.format(self.msg)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
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
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
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
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32 lines = source.split('\n')
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
33 ro, co = e.row, e.col
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
34 prerow = ro - 2
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 if prerow < 1:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 prerow = 1
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
37 afterrow = ro + 3
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 if afterrow > len(lines):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39 afterrow = len(lines)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 # print preceding source lines:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
42 for r in range(prerow, ro):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
43 printLine(r, lines[r-1])
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
44 # print source line containing error:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
45 printLine(ro, lines[ro-1])
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
46 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg))
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
47 # print trailing source line:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
48 for r in range(ro+1, afterrow+1):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
49 printLine(r, lines[r-1])
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
50
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
51 class DiagnosticsManager:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
52 def __init__(self):
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 225
diff changeset
53 self.diags = []
255
7416c923a02a Added more logging
Windel Bouwman
parents: 251
diff changeset
54 self.logger = logging.getLogger('diagnostics')
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
55
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
56 def addDiag(self, d):
255
7416c923a02a Added more logging
Windel Bouwman
parents: 251
diff changeset
57 self.logger.info(str(d))
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 225
diff changeset
58 self.diags.append(d)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
59
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
60 def error(self, msg, loc):
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 225
diff changeset
61 self.addDiag(CompilerError(msg, loc))
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
62
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
63 def clear(self):
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 225
diff changeset
64 del self.diags[:]
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
65
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
66 def printErrors(self, src):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
67 if len(self.diags) > 0:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
68 print('==============')
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
69 print('{0} Errors'.format(len(self.diags)))
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
70 for d in self.diags:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
71 print('==============')
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
72 printError(src, d)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
73 print('==============')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
74