1.1 --- a/micropython/common.py Sat Jun 12 03:04:08 2010 +0200
1.2 +++ b/micropython/common.py Sat Jun 12 21:33:36 2010 +0200
1.3 @@ -19,11 +19,27 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 +import compiler.visitor
1.8 +
1.9 try:
1.10 set
1.11 except NameError:
1.12 from sets import Set as set
1.13
1.14 +# Visitors.
1.15 +
1.16 +class ASTVisitor(compiler.visitor.ASTVisitor):
1.17 +
1.18 + "A base class for visitors."
1.19 +
1.20 + def dispatch(self, node, *args):
1.21 + try:
1.22 + return compiler.visitor.ASTVisitor.dispatch(self, node, *args)
1.23 + except NodeProcessingError, exc:
1.24 + exc.astnode = node
1.25 + exc.unit_name = self.full_name()
1.26 + raise
1.27 +
1.28 # Errors.
1.29
1.30 class ProcessingError(Exception):
1.31 @@ -48,13 +64,24 @@
1.32
1.33 "A processing error associated with a particular program node."
1.34
1.35 - def __init__(self, unit_name, node, message):
1.36 - self.unit_name = unit_name
1.37 - self.astnode = node
1.38 + def __init__(self, message):
1.39 self.message = message
1.40 + self.unit_name = None
1.41 + self.astnode = None
1.42 +
1.43 + def get_lineno(self, node):
1.44 + lineno = node.lineno
1.45 + if lineno is not None:
1.46 + return lineno
1.47 + else:
1.48 + for child in node.getChildNodes():
1.49 + lineno = self.get_lineno(child)
1.50 + if lineno is not None:
1.51 + return lineno
1.52 + return None
1.53
1.54 def __repr__(self):
1.55 - return "Error in %r at line %r: %s" % (self.unit_name, self.astnode.lineno, self.message)
1.56 + return "Error in %r at line %r: %s" % (self.unit_name, self.get_lineno(self.astnode), self.message)
1.57
1.58 def __str__(self):
1.59 return repr(self)