| 1 | """Check for errs in the AST. | 
| 2 |  | 
| 3 | The Python parser does not catch all syntax errors.  Others, like | 
| 4 | assignments with invalid targets, are caught in the code generation | 
| 5 | phase. | 
| 6 |  | 
| 7 | The compiler package catches some errors in the transformer module. | 
| 8 | But it seems clearer to write checkers that use the AST to detect | 
| 9 | errors. | 
| 10 | """ | 
| 11 |  | 
| 12 |  | 
| 13 | from .visitor import ASTVisitor | 
| 14 |  | 
| 15 |  | 
| 16 | class SyntaxErrorChecker(ASTVisitor): | 
| 17 | """A visitor to find syntax errors in the AST.""" | 
| 18 |  | 
| 19 | def __init__(self, multi=None): | 
| 20 | """Create new visitor object. | 
| 21 |  | 
| 22 | If optional argument multi is not None, then print messages | 
| 23 | for each error rather than raising a SyntaxError for the | 
| 24 | first. | 
| 25 | """ | 
| 26 | ASTVisitor.__init__(self) | 
| 27 | self.multi = multi | 
| 28 | self.errors = 0 | 
| 29 |  | 
| 30 | def error(self, node, msg): | 
| 31 | self.errors = self.errors + 1 | 
| 32 | if self.multi is not None: | 
| 33 | print("%s:%s: %s" % (node.filename, node.lineno, msg)) | 
| 34 | else: | 
| 35 | raise SyntaxError, "%s (%s:%s)" % (msg, node.filename, node.lineno) | 
| 36 |  | 
| 37 | def visitAssign(self, node): | 
| 38 | # the transformer module handles many of these | 
| 39 | pass | 
| 40 | ##        for target in node.nodes: | 
| 41 | ##            if isinstance(target, ast.AssList): | 
| 42 | ##                if target.lineno is None: | 
| 43 | ##                    target.lineno = node.lineno | 
| 44 | ##                self.error(target, "can't assign to list comprehension") |