OILS / opy / _regtest / src / opy / compiler2 / syntax.py View on Github | oils.pub

48 lines, 18 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
12from . import ast
13from .visitor import walk
14
15
16def check(tree, multi=None):
17 v = SyntaxErrorChecker(multi)
18 walk(tree, v)
19 return v.errors
20
21class SyntaxErrorChecker:
22 """A visitor to find syntax errors in the AST."""
23
24 def __init__(self, multi=None):
25 """Create new visitor object.
26
27 If optional argument multi is not None, then print messages
28 for each error rather than raising a SyntaxError for the
29 first.
30 """
31 self.multi = multi
32 self.errors = 0
33
34 def error(self, node, msg):
35 self.errors = self.errors + 1
36 if self.multi is not None:
37 print("%s:%s: %s" % (node.filename, node.lineno, msg))
38 else:
39 raise SyntaxError, "%s (%s:%s)" % (msg, node.filename, node.lineno)
40
41 def visitAssign(self, node):
42 # the transformer module handles many of these
43 pass
44## for target in node.nodes:
45## if isinstance(target, ast.AssList):
46## if target.lineno is None:
47## target.lineno = node.lineno
48## self.error(target, "can't assign to list comprehension")