annotate python/ppci/errors.py @ 149:74241ca312cc

Fixes on parser and semantics
author Windel Bouwman
date Fri, 01 Mar 2013 11:43:52 +0100
parents e5263f74b287
children b73bc14a3aa3
rev   line source
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
1 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
2 Error handling routines
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
3 Diagnostic utils
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
4 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
5
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
6 from collections import namedtuple
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
7
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
8 SourceLocation = namedtuple('SourceLocation', ['row', 'col'])
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
9 SourceRange = namedtuple('SourceRange', ['p1', 'p2'])
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
10
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
11 class CompilerException(Exception):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
12 def __init__(self, msg, loc):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13 self.msg = msg
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
14 self.loc = loc
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
15 def __repr__(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
16 return 'error {0} at {1}'.format(self.msg, self.loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
17 def __str__(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
18 return 'error {0} at {1}'.format(self.msg, self.loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
19
6
1784af239df4 Added error list
windel
parents: 1
diff changeset
20 class ErrorNode:
1784af239df4 Added error list
windel
parents: 1
diff changeset
21 def __init__(self, row, col, msg):
1784af239df4 Added error list
windel
parents: 1
diff changeset
22 self.row, self.col = row,col
1784af239df4 Added error list
windel
parents: 1
diff changeset
23 self.msg = msg
1784af239df4 Added error list
windel
parents: 1
diff changeset
24
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 def Error(msg, node=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 if node is None:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
27 raise CompilerException(msg)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
28 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
29 raise CompilerException(msg, node.row, node.col)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
30
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
31 def printError(source, e):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32 def printLine(row, txt):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
33 print(str(row)+':'+txt)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
34 if e.loc.row == 0:
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 print('Error: {0}'.format(e.msg))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 lines = source.split('\n')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
38 ro, co = e.loc.row, e.loc.col
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
39 prerow = ro - 2
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40 if prerow < 1:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 prerow = 1
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
42 afterrow = ro + 3
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
43 if afterrow > len(lines):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
44 afterrow = len(lines)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
45
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
46 # print preceding source lines:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
47 for r in range(prerow, ro):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
48 printLine(r, lines[r-1])
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
49 # print source line containing error:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
50 printLine(ro, lines[ro-1])
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
51 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg))
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
52 # print trailing source line:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
53 for r in range(ro+1, afterrow+1):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
54 printLine(r, lines[r-1])
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
55
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
56 class Diagnostics:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
57 def __init__(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
58 self.diags = []
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
59 def diag(self, d):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
60 self.diags.append(d)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents: 147
diff changeset
61