annotate python/ppci/common.py @ 396:fb3c1f029b30

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