annotate python/ppci/errors.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents b73bc14a3aa3
children 0b5b2ee6b435
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
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
6 class CompilerError(Exception):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
7 def __init__(self, msg, loc):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
8 self.msg = msg
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
9 self.loc = loc
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
10 def __repr__(self):
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
11 return 'Compilererror {0} at {1}'.format(self.msg, self.loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
12
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13 def printError(source, e):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
14 def printLine(row, txt):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
15 print(str(row)+':'+txt)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
16 if e.loc.row == 0:
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
17 print('Error: {0}'.format(e.msg))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
18 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
19 lines = source.split('\n')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
20 ro, co = e.loc.row, e.loc.col
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
21 prerow = ro - 2
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
22 if prerow < 1:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
23 prerow = 1
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
24 afterrow = ro + 3
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 if afterrow > len(lines):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 afterrow = len(lines)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
27
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
28 # print preceding source lines:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
29 for r in range(prerow, ro):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
30 printLine(r, lines[r-1])
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
31 # print source line containing error:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
32 printLine(ro, lines[ro-1])
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
33 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg))
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34 # print trailing source line:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
35 for r in range(ro+1, afterrow+1):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 printLine(r, lines[r-1])
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
37
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
38 class DiagnosticsManager:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
39 def __init__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
40 self.diags = []
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
41 def addDiag(self, d):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
42 self.diags.append(d)
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
43 def error(self, msg, loc):
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 148
diff changeset
44 self.addDiag(CompilerError(msg, loc))
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
45