comparison python/ppci/errors.py @ 152:b73bc14a3aa3

Light coupling ide and c3 frontend
author Windel Bouwman
date Sat, 02 Mar 2013 09:56:12 +0100
parents e5263f74b287
children 0b5b2ee6b435
comparison
equal deleted inserted replaced
151:afc8c0207984 152:b73bc14a3aa3
1 """ 1 """
2 Error handling routines 2 Error handling routines
3 Diagnostic utils 3 Diagnostic utils
4 """ 4 """
5 5
6 from collections import namedtuple 6 class CompilerError(Exception):
7
8 SourceLocation = namedtuple('SourceLocation', ['row', 'col'])
9 SourceRange = namedtuple('SourceRange', ['p1', 'p2'])
10
11 class CompilerException(Exception):
12 def __init__(self, msg, loc): 7 def __init__(self, msg, loc):
13 self.msg = msg 8 self.msg = msg
14 self.loc = loc 9 self.loc = loc
15 def __repr__(self): 10 def __repr__(self):
16 return 'error {0} at {1}'.format(self.msg, self.loc) 11 return 'Compilererror {0} at {1}'.format(self.msg, self.loc)
17 def __str__(self):
18 return 'error {0} at {1}'.format(self.msg, self.loc)
19
20 class ErrorNode:
21 def __init__(self, row, col, msg):
22 self.row, self.col = row,col
23 self.msg = msg
24
25 def Error(msg, node=None):
26 if node is None:
27 raise CompilerException(msg)
28 else:
29 raise CompilerException(msg, node.row, node.col)
30 12
31 def printError(source, e): 13 def printError(source, e):
32 def printLine(row, txt): 14 def printLine(row, txt):
33 print(str(row)+':'+txt) 15 print(str(row)+':'+txt)
34 if e.loc.row == 0: 16 if e.loc.row == 0:
51 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg)) 33 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg))
52 # print trailing source line: 34 # print trailing source line:
53 for r in range(ro+1, afterrow+1): 35 for r in range(ro+1, afterrow+1):
54 printLine(r, lines[r-1]) 36 printLine(r, lines[r-1])
55 37
56 class Diagnostics: 38 class DiagnosticsManager:
57 def __init__(self): 39 def __init__(self):
58 self.diags = [] 40 self.diags = []
59 def diag(self, d): 41 def addDiag(self, d):
60 self.diags.append(d) 42 self.diags.append(d)
43 def error(self, msg, loc):
44 self.addDiag(CompilerError(msg, loc))
61 45