PyXR

c:\python24\lib \ compiler \ syntax.py



0001 """Check for errs in the AST.
0002 
0003 The Python parser does not catch all syntax errors.  Others, like
0004 assignments with invalid targets, are caught in the code generation
0005 phase.
0006 
0007 The compiler package catches some errors in the transformer module.
0008 But it seems clearer to write checkers that use the AST to detect
0009 errors.
0010 """
0011 
0012 from compiler import ast, walk
0013 
0014 def check(tree, multi=None):
0015     v = SyntaxErrorChecker(multi)
0016     walk(tree, v)
0017     return v.errors
0018 
0019 class SyntaxErrorChecker:
0020     """A visitor to find syntax errors in the AST."""
0021 
0022     def __init__(self, multi=None):
0023         """Create new visitor object.
0024 
0025         If optional argument multi is not None, then print messages
0026         for each error rather than raising a SyntaxError for the
0027         first.
0028         """
0029         self.multi = multi
0030         self.errors = 0
0031 
0032     def error(self, node, msg):
0033         self.errors = self.errors + 1
0034         if self.multi is not None:
0035             print "%s:%s: %s" % (node.filename, node.lineno, msg)
0036         else:
0037             raise SyntaxError, "%s (%s:%s)" % (msg, node.filename, node.lineno)
0038 
0039     def visitAssign(self, node):
0040         # the transformer module handles many of these
0041         for target in node.nodes:
0042             pass
0043 ##            if isinstance(target, ast.AssList):
0044 ##                if target.lineno is None:
0045 ##                    target.lineno = node.lineno
0046 ##                self.error(target, "can't assign to list comprehension")
0047 

Generated by PyXR 0.9.4
SourceForge.net Logo