annotate python/ppci/core/errors.py @ 133:a816c683c1c0

Fixes in hexedit
author Windel Bouwman
date Sun, 20 Jan 2013 21:48:41 +0100
parents 4a37d6992bd3
children
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):
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 6
diff changeset
4 def __init__(self, msg, row=0, col=0, filename=None):
1
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
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 6
diff changeset
8 self.filename = filename
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
9 def __repr__(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
10 return self.msg
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
11 def __str__(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
12 return self.msg
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13
6
1784af239df4 Added error list
windel
parents: 1
diff changeset
14 class ErrorNode:
1784af239df4 Added error list
windel
parents: 1
diff changeset
15 def __init__(self, row, col, msg):
1784af239df4 Added error list
windel
parents: 1
diff changeset
16 self.row, self.col = row,col
1784af239df4 Added error list
windel
parents: 1
diff changeset
17 self.msg = msg
1784af239df4 Added error list
windel
parents: 1
diff changeset
18
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
19 def Error(msg, node=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
20 if node is None:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
21 raise CompilerException(msg)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
22 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
23 raise CompilerException(msg, node.row, node.col)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
24
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 def printError(source, e):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 def printLine(row, txt):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
27 print(str(row)+':'+txt)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
28 if e.row == 0:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
29 print('Error: {0}'.format(e.msg))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
30 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
31 lines = source.split('\n')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32 prerow = e.row - 3
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
33 if prerow < 1:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34 prerow = 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 afterrow = e.row + 3
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 if afterrow > len(lines):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 afterrow = len(lines)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39 # print preceding source lines:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40 for r in range(prerow, e.row):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 printLine(r, lines[r-1])
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
42 # print source line containing error:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
43 printLine(e.row, lines[e.row-1])
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
44 print(' '*(len(str(e.row)+':')+e.col-1) + '^ Error: {0}'.format(e.msg))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
45 # print trailing source line:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
46 for r in range(e.row+1, afterrow+1):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
47 printLine(r, lines[r-1])