ยปCore Development>Code coverage>Lib/compiler/syntax.py

Python code coverage for Lib/compiler/syntax.py

#countcontent
1n/a"""Check for errs in the AST.
2n/a
3n/aThe Python parser does not catch all syntax errors. Others, like
4n/aassignments with invalid targets, are caught in the code generation
5n/aphase.
6n/a
7n/aThe compiler package catches some errors in the transformer module.
8n/aBut it seems clearer to write checkers that use the AST to detect
9n/aerrors.
102"""
11n/a
122from compiler import ast, walk
13n/a
142def check(tree, multi=None):
1531 v = SyntaxErrorChecker(multi)
1631 walk(tree, v)
1731 return v.errors
18n/a
194class SyntaxErrorChecker:
202 """A visitor to find syntax errors in the AST."""
21n/a
222 def __init__(self, multi=None):
23n/a """Create new visitor object.
24n/a
25n/a If optional argument multi is not None, then print messages
26n/a for each error rather than raising a SyntaxError for the
27n/a first.
28n/a """
2931 self.multi = multi
3031 self.errors = 0
31n/a
322 def error(self, node, msg):
330 self.errors = self.errors + 1
340 if self.multi is not None:
350 print "%s:%s: %s" % (node.filename, node.lineno, msg)
36n/a else:
370 raise SyntaxError, "%s (%s:%s)" % (msg, node.filename, node.lineno)
38n/a
392 def visitAssign(self, node):
40n/a # the transformer module handles many of these
41971 pass
42n/a## for target in node.nodes:
43n/a## if isinstance(target, ast.AssList):
44n/a## if target.lineno is None:
45n/a## target.lineno = node.lineno
46n/a## self.error(target, "can't assign to list comprehension")