Mercurial > lcfOS
comparison python/ppci/core/errors.py @ 95:4a37d6992bd3
movage
author | windel |
---|---|
date | Mon, 24 Dec 2012 13:24:59 +0100 |
parents | python/libs/compiler/core/errors.py@5351594349b0 |
children |
comparison
equal
deleted
inserted
replaced
94:1be00bcfaabb | 95:4a37d6992bd3 |
---|---|
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]) |