comparison python/ppci/errors.py @ 147:4e79484a9d47

Moved core to ir folder
author Windel Bouwman
date Fri, 22 Feb 2013 10:33:48 +0100
parents python/ppci/core/errors.py@4a37d6992bd3
children e5263f74b287
comparison
equal deleted inserted replaced
146:91af0e40f868 147:4e79484a9d47
1 """ Error handling routines """
2
3 class CompilerException(Exception):
4 def __init__(self, msg, row=0, col=0, filename=None):
5 self.msg = msg
6 self.row = row
7 self.col = col
8 self.filename = filename
9 def __repr__(self):
10 return self.msg
11 def __str__(self):
12 return self.msg
13
14 class ErrorNode:
15 def __init__(self, row, col, msg):
16 self.row, self.col = row,col
17 self.msg = msg
18
19 def Error(msg, node=None):
20 if node is None:
21 raise CompilerException(msg)
22 else:
23 raise CompilerException(msg, node.row, node.col)
24
25 def printError(source, e):
26 def printLine(row, txt):
27 print(str(row)+':'+txt)
28 if e.row == 0:
29 print('Error: {0}'.format(e.msg))
30 else:
31 lines = source.split('\n')
32 prerow = e.row - 3
33 if prerow < 1:
34 prerow = 1
35 afterrow = e.row + 3
36 if afterrow > len(lines):
37 afterrow = len(lines)
38
39 # print preceding source lines:
40 for r in range(prerow, e.row):
41 printLine(r, lines[r-1])
42 # print source line containing error:
43 printLine(e.row, lines[e.row-1])
44 print(' '*(len(str(e.row)+':')+e.col-1) + '^ Error: {0}'.format(e.msg))
45 # print trailing source line:
46 for r in range(e.row+1, afterrow+1):
47 printLine(r, lines[r-1])