1 | """Parser for future statements
|
2 |
|
3 | """
|
4 |
|
5 | from . import ast
|
6 | from .visitor import walk
|
7 |
|
8 |
|
9 | def is_future(stmt):
|
10 | """Return true if statement is a well-formed future statement"""
|
11 | if not isinstance(stmt, ast.From):
|
12 | return 0
|
13 | if stmt.modname == "__future__":
|
14 | return 1
|
15 | else:
|
16 | return 0
|
17 |
|
18 | class FutureParser:
|
19 |
|
20 | features = ("nested_scopes", "generators", "division",
|
21 | "absolute_import", "with_statement", "print_function",
|
22 | "unicode_literals")
|
23 |
|
24 | def __init__(self):
|
25 | self.found = {} # set
|
26 |
|
27 | def visitModule(self, node):
|
28 | stmt = node.node
|
29 | for s in stmt.nodes:
|
30 | if not self.check_stmt(s):
|
31 | break
|
32 |
|
33 | def check_stmt(self, stmt):
|
34 | if is_future(stmt):
|
35 | for name, asname in stmt.names:
|
36 | if name in self.features:
|
37 | self.found[name] = 1
|
38 | else:
|
39 | raise SyntaxError, \
|
40 | "future feature %s is not defined" % name
|
41 | stmt.valid_future = 1
|
42 | return 1
|
43 | return 0
|
44 |
|
45 | def get_features(self):
|
46 | """Return list of features enabled by future statements"""
|
47 | return self.found.keys()
|
48 |
|
49 | class BadFutureParser:
|
50 | """Check for invalid future statements"""
|
51 |
|
52 | def visitFrom(self, node):
|
53 | if hasattr(node, 'valid_future'):
|
54 | return
|
55 | if node.modname != "__future__":
|
56 | return
|
57 | raise SyntaxError, "invalid future statement " + repr(node)
|
58 |
|
59 | def find_futures(node):
|
60 | p1 = FutureParser()
|
61 | p2 = BadFutureParser()
|
62 | walk(node, p1)
|
63 | walk(node, p2)
|
64 | return p1.get_features()
|
65 |
|
66 | if __name__ == "__main__":
|
67 | import sys
|
68 | from compiler import parseFile, walk
|
69 |
|
70 | for file in sys.argv[1:]:
|
71 | print(file)
|
72 | tree = parseFile(file)
|
73 | v = FutureParser()
|
74 | walk(tree, v)
|
75 | print(v.found)
|
76 | print()
|