OILS / opy / compiler2 / syntax.py View on Github | oilshell.org

44 lines, 14 significant
1"""Check for errs in the AST.
2
3The Python parser does not catch all syntax errors. Others, like
4assignments with invalid targets, are caught in the code generation
5phase.
6
7The compiler package catches some errors in the transformer module.
8But it seems clearer to write checkers that use the AST to detect
9errors.
10"""
11
12
13from .visitor import ASTVisitor
14
15
16class 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")