annotate python/ppci/errors.py @ 311:ff665880a6b0

Added testcase for kernel and userspace
author Windel Bouwman
date Mon, 16 Dec 2013 12:49:24 +0100
parents 0615b5308710
children
rev   line source
305
0615b5308710 Updated docs
Windel Bouwman
parents: 296
diff changeset
1 """
0615b5308710 Updated docs
Windel Bouwman
parents: 296
diff changeset
2 Error handling routines
148
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
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 295
diff changeset
9
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
10 class CompilerError(Exception):
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
11 def __init__(self, msg, loc=None):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 182
diff changeset
12 self.msg = msg
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 182
diff changeset
13 self.loc = loc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
14 if loc:
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
15 assert type(loc) is SourceLocation, \
305
0615b5308710 Updated docs
Windel Bouwman
parents: 296
diff changeset
16 '{0} must be SourceLocation'.format(type(loc))
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
17 self.row = loc.row
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
18 self.col = loc.col
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
19 else:
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 248
diff changeset
20 self.row = self.col = 0
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
21
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 182
diff changeset
22 def __repr__(self):
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
23 return '"{}"'.format(self.msg)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
24
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
25
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
26 class DiagnosticsManager:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
27 def __init__(self):
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 225
diff changeset
28 self.diags = []
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
29 self.sources = {}
255
7416c923a02a Added more logging
Windel Bouwman
parents: 251
diff changeset
30 self.logger = logging.getLogger('diagnostics')
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
31
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
32 def addSource(self, name, src):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
33 self.logger.info('Adding source {}'.format(name))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
34 self.sources[name] = src
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
35
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
36 def addDiag(self, d):
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 295
diff changeset
37 #self.logger.warning(str(d.msg))
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 225
diff changeset
38 self.diags.append(d)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
39
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
40 def error(self, msg, loc):
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 225
diff changeset
41 self.addDiag(CompilerError(msg, loc))
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
42
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
43 def clear(self):
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 225
diff changeset
44 del self.diags[:]
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
45 self.sources.clear()
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 200
diff changeset
46
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
47 def printErrors(self):
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
48 if len(self.diags) > 0:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
49 print('{0} Errors'.format(len(self.diags)))
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 215
diff changeset
50 for d in self.diags:
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
51 self.printError(d)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
52
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
53 def printError(self, e):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
54 def printLine(row, txt):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
55 print(str(row)+':'+txt)
295
917eab04b8b7 Added disasm
Windel Bouwman
parents: 293
diff changeset
56 print('==============')
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
57 if not e.loc:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
58 print('Error: {0}'.format(e))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
59 else:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
60 if e.loc.filename not in self.sources:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
61 print('Error: {0}'.format(e))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
62 return
295
917eab04b8b7 Added disasm
Windel Bouwman
parents: 293
diff changeset
63 print("File: {}".format(e.loc.filename))
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
64 source = self.sources[e.loc.filename]
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
65 lines = source.split('\n')
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
66 ro, co = e.row, e.col
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
67 prerow = ro - 2
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
68 if prerow < 1:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
69 prerow = 1
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
70 afterrow = ro + 3
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
71 if afterrow > len(lines):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
72 afterrow = len(lines)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
73
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
74 # print preceding source lines:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
75 for r in range(prerow, ro):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
76 printLine(r, lines[r-1])
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
77 # print source line containing error:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
78 printLine(ro, lines[ro-1])
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
79 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
80 # print trailing source line:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
81 for r in range(ro+1, afterrow+1):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
82 printLine(r, lines[r-1])
295
917eab04b8b7 Added disasm
Windel Bouwman
parents: 293
diff changeset
83 print('==============')