comparison ide/compiler/errors.py @ 1:92df07bc2081

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