annotate python/ppci/common.py @ 334:6f4753202b9a

Added more recipes
author Windel Bouwman
date Thu, 13 Feb 2014 22:02:08 +0100
parents 2c9768114877
children b8ad45b3a573
rev   line source
153
e05b2b216bfc Added common
Windel Bouwman
parents:
diff changeset
1 from collections import namedtuple
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
2 import logging
153
e05b2b216bfc Added common
Windel Bouwman
parents:
diff changeset
3
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
4 """
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
5 Error handling routines
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
6 Diagnostic utils
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
7 Source location structures
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
8 """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 249
diff changeset
9
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 163
diff changeset
10 # Token is used in the lexical analyzer:
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
11 class Token:
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
12 def __init__(self, typ, val, loc=None):
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
13 self.typ = typ
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
14 self.val = val
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
15 if loc is None:
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 249
diff changeset
16 loc = SourceLocation('', 0, 0, 0)
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
17 assert type(loc) is SourceLocation
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
18 self.loc = loc
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
19
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
20 def __repr__(self):
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
21 return 'Token({0}, {1})'.format(self.typ, self.val)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 163
diff changeset
22
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 249
diff changeset
23
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 153
diff changeset
24 class SourceLocation:
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 249
diff changeset
25 def __init__(self, filename, row, col, ln):
1c7c1e619be8 File movage
Windel Bouwman
parents: 249
diff changeset
26 self.filename = filename
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 194
diff changeset
27 self.row = row
e41e4109addd Added current position arrow
Windel Bouwman
parents: 194
diff changeset
28 self.col = col
e41e4109addd Added current position arrow
Windel Bouwman
parents: 194
diff changeset
29 self.length = ln
e41e4109addd Added current position arrow
Windel Bouwman
parents: 194
diff changeset
30
e41e4109addd Added current position arrow
Windel Bouwman
parents: 194
diff changeset
31 def __repr__(self):
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
32 return '{}, {}, {}'.format(self.filename, self.row, self.col)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 153
diff changeset
33
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 249
diff changeset
34
153
e05b2b216bfc Added common
Windel Bouwman
parents:
diff changeset
35 SourceRange = namedtuple('SourceRange', ['p1', 'p2'])
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
36
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
37
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
38 class CompilerError(Exception):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
39 def __init__(self, msg, loc=None):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
40 self.msg = msg
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
41 self.loc = loc
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
42 if loc:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
43 assert type(loc) is SourceLocation, \
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
44 '{0} must be SourceLocation'.format(type(loc))
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
45 self.row = loc.row
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
46 self.col = loc.col
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
47 else:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
48 self.row = self.col = 0
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
49
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
50 def __repr__(self):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
51 return '"{}"'.format(self.msg)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
52
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
53
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
54 class DiagnosticsManager:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
55 def __init__(self):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
56 self.diags = []
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
57 self.sources = {}
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
58 self.logger = logging.getLogger('diagnostics')
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
59
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
60 def addSource(self, name, src):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 312
diff changeset
61 self.logger.debug('Adding source {}'.format(name))
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
62 self.sources[name] = src
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
63
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
64 def addDiag(self, d):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
65 #self.logger.warning(str(d.msg))
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
66 self.diags.append(d)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
67
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
68 def error(self, msg, loc):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
69 self.addDiag(CompilerError(msg, loc))
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
70
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
71 def clear(self):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
72 del self.diags[:]
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
73 self.sources.clear()
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
74
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
75 def printErrors(self):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
76 if len(self.diags) > 0:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
77 print('{0} Errors'.format(len(self.diags)))
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
78 for d in self.diags:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
79 self.printError(d)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
80
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
81 def printError(self, e):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
82 def printLine(row, txt):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
83 print(str(row)+':'+txt)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
84 print('==============')
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
85 if not e.loc:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
86 print('Error: {0}'.format(e))
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
87 else:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
88 if e.loc.filename not in self.sources:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
89 print('Error: {0}'.format(e))
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
90 return
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
91 print("File: {}".format(e.loc.filename))
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
92 source = self.sources[e.loc.filename]
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
93 lines = source.split('\n')
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
94 ro, co = e.row, e.col
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
95 prerow = ro - 2
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
96 if prerow < 1:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
97 prerow = 1
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
98 afterrow = ro + 3
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
99 if afterrow > len(lines):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
100 afterrow = len(lines)
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
101
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
102 # print preceding source lines:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
103 for r in range(prerow, ro):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
104 printLine(r, lines[r-1])
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
105 # print source line containing error:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
106 printLine(ro, lines[ro-1])
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
107 print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg))
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
108 # print trailing source line:
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
109 for r in range(ro+1, afterrow+1):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
110 printLine(r, lines[r-1])
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 293
diff changeset
111 print('==============')