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
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
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
6 from . import SourceLocation
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
7
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
8 class CompilerError(Exception):
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
9 def __init__(self, msg, loc=None):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 182
diff changeset
10 self.msg = msg
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 182
diff changeset
11 self.loc = loc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
12 if loc:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
13 assert type(loc) is SourceLocation, '{0} must be SourceLocation'.format(type(loc))
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 182
diff changeset
14 def __repr__(self):
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
15 if self.loc:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
16 return 'Compilererror: "{0}" at row {1}'.format(self.msg, self.loc.row)
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
17 else:
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 194
diff changeset
18 return 'Compilererror: "{0}"'.format(self.msg)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
19
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
20 def printError(source, e):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
21 def printLine(row, txt):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
22 print(str(row)+':'+txt)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
23 if e.loc.row == 0:
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
24 print('Error: {0}'.format(e.msg))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 lines = source.split('\n')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
27 ro, co = e.loc.row, e.loc.col
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
28 prerow = ro - 2
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
29 if prerow < 1:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
30 prerow = 1
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
31 afterrow = ro + 3
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32 if afterrow > len(lines):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
33 afterrow = len(lines)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 # print preceding source lines:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
36 for r in range(prerow, ro):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 printLine(r, lines[r-1])
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 # print source line containing error:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
39 printLine(ro, lines[ro-1])
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
40 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg))
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 # print trailing source line:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
42 for r in range(ro+1, afterrow+1):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
43 printLine(r, lines[r-1])
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
44
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
45 class DiagnosticsManager:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
46 def __init__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
47 self.diags = []
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
48 def addDiag(self, d):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
49 self.diags.append(d)
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
50 def error(self, msg, loc):
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
51 self.addDiag(CompilerError(msg, loc))
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 152
diff changeset
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
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 167
diff changeset
54 def printErrors(self, src):
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 167
diff changeset
55 if len(self.diags) > 0:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 167
diff changeset
56 print('{0} Errors'.format(len(self.diags)))
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 167
diff changeset
57 for d in self.diags:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 167
diff changeset
58 printError(src, d)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
59