annotate ide/compiler/errors.py @ 4:0d5ef85b8698

Improved link between ast viewer and code edit
author windel-eee
date Wed, 21 Sep 2011 19:05:18 +0200
parents 92df07bc2081
children 1784af239df4
rev   line source
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
1 """ Error handling routines """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
2
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
3 class CompilerException(Exception):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
4 def __init__(self, msg, row=0, col=0):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
5 self.msg = msg
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
6 self.row = row
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
7 self.col = col
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
8 def __repr__(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
9 return self.msg
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
10 def __str__(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
11 return self.msg
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
12
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13 def Error(msg, node=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
14 if node is None:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
15 raise CompilerException(msg)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
16 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
17 raise CompilerException(msg, node.row, node.col)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
18
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
19 def printError(source, e):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
20 def printLine(row, txt):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
21 print(str(row)+':'+txt)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
22 if e.row == 0:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
23 print('Error: {0}'.format(e.msg))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
24 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 lines = source.split('\n')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 prerow = e.row - 3
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
27 if prerow < 1:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
28 prerow = 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
29 afterrow = e.row + 3
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
30 if afterrow > len(lines):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
31 afterrow = len(lines)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
33 # print preceding source lines:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34 for r in range(prerow, e.row):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 printLine(r, lines[r-1])
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 # print source line containing error:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 printLine(e.row, lines[e.row-1])
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 print(' '*(len(str(e.row)+':')+e.col-1) + '^ Error: {0}'.format(e.msg))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39 # print trailing source line:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40 for r in range(e.row+1, afterrow+1):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 printLine(r, lines[r-1])