Jeremy Hylton | 80e29bd | 2001-04-09 04:28:48 +0000 | [diff] [blame] | 1 | """Parser for future statements |
| 2 | |
| 3 | """ |
| 4 | |
| 5 | from compiler import ast, walk |
| 6 | |
| 7 | def is_future(stmt): |
| 8 | """Return true if statement is a well-formed future statement""" |
| 9 | if not isinstance(stmt, ast.From): |
| 10 | return 0 |
| 11 | if stmt.modname == "__future__": |
| 12 | return 1 |
| 13 | else: |
| 14 | return 0 |
| 15 | |
| 16 | class FutureParser: |
| 17 | |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 18 | features = ("nested_scopes", "generators", "division", |
| 19 | "absolute_import", "with_statement") |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 20 | |
Jeremy Hylton | 80e29bd | 2001-04-09 04:28:48 +0000 | [diff] [blame] | 21 | def __init__(self): |
| 22 | self.found = {} # set |
| 23 | |
| 24 | def visitModule(self, node): |
Jeremy Hylton | 80e29bd | 2001-04-09 04:28:48 +0000 | [diff] [blame] | 25 | stmt = node.node |
Martin v. Löwis | 415ed93 | 2006-02-27 19:56:30 +0000 | [diff] [blame] | 26 | found_docstring = False |
Jeremy Hylton | ec5bfd1 | 2001-08-18 00:07:46 +0000 | [diff] [blame] | 27 | for s in stmt.nodes: |
Martin v. Löwis | 415ed93 | 2006-02-27 19:56:30 +0000 | [diff] [blame] | 28 | # Skip over docstrings |
| 29 | if not found_docstring and isinstance(s, ast.Discard) \ |
| 30 | and isinstance(s.expr, ast.Const) \ |
| 31 | and isinstance(s.expr.value, str): |
| 32 | found_docstring = True |
| 33 | continue |
Jeremy Hylton | 80e29bd | 2001-04-09 04:28:48 +0000 | [diff] [blame] | 34 | if not self.check_stmt(s): |
| 35 | break |
| 36 | |
| 37 | def check_stmt(self, stmt): |
| 38 | if is_future(stmt): |
| 39 | for name, asname in stmt.names: |
| 40 | if name in self.features: |
| 41 | self.found[name] = 1 |
| 42 | else: |
| 43 | raise SyntaxError, \ |
| 44 | "future feature %s is not defined" % name |
| 45 | stmt.valid_future = 1 |
| 46 | return 1 |
| 47 | return 0 |
| 48 | |
| 49 | def get_features(self): |
| 50 | """Return list of features enabled by future statements""" |
| 51 | return self.found.keys() |
| 52 | |
| 53 | class BadFutureParser: |
| 54 | """Check for invalid future statements""" |
| 55 | |
| 56 | def visitFrom(self, node): |
| 57 | if hasattr(node, 'valid_future'): |
| 58 | return |
| 59 | if node.modname != "__future__": |
| 60 | return |
Martin v. Löwis | 415ed93 | 2006-02-27 19:56:30 +0000 | [diff] [blame] | 61 | raise SyntaxError, "invalid future statement " + repr(node) |
Jeremy Hylton | 80e29bd | 2001-04-09 04:28:48 +0000 | [diff] [blame] | 62 | |
| 63 | def find_futures(node): |
| 64 | p1 = FutureParser() |
| 65 | p2 = BadFutureParser() |
| 66 | walk(node, p1) |
| 67 | walk(node, p2) |
| 68 | return p1.get_features() |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | import sys |
| 72 | from compiler import parseFile, walk |
| 73 | |
| 74 | for file in sys.argv[1:]: |
| 75 | print file |
| 76 | tree = parseFile(file) |
| 77 | v = FutureParser() |
| 78 | walk(tree, v) |
| 79 | print v.found |
| 80 | print |