~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/interpreter/stablecompiler/future.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Parser for future statements
 
2
 
 
3
"""
 
4
 
 
5
from pypy.interpreter.stablecompiler 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
 
 
18
    features = ("nested_scopes", "generators", "division")
 
19
 
 
20
    def __init__(self):
 
21
        self.found = {} # set
 
22
 
 
23
    def visitModule(self, node):
 
24
        stmt = node.node
 
25
        for s in stmt.nodes:
 
26
            if not self.check_stmt(s):
 
27
                break
 
28
 
 
29
    def check_stmt(self, stmt):
 
30
        if is_future(stmt):
 
31
            for name, asname in stmt.names:
 
32
                if name in self.features:
 
33
                    self.found[name] = 1
 
34
                elif name=="*":
 
35
                    raise SyntaxError(
 
36
                        "future statement does not support import *",
 
37
                        ( stmt.filename, stmt.lineno, 0, "" ) )
 
38
                else:
 
39
                    raise SyntaxError(
 
40
                        "future feature %s is not defined" % name,
 
41
                        ( stmt.filename, stmt.lineno, 0, "" ) )
 
42
            stmt.valid_future = 1
 
43
            return 1
 
44
        return 0
 
45
 
 
46
    def get_features(self):
 
47
        """Return list of features enabled by future statements"""
 
48
        return self.found.keys()
 
49
 
 
50
class BadFutureParser:
 
51
    """Check for invalid future statements
 
52
    Those not marked valid are appearing after other statements
 
53
    """
 
54
 
 
55
    def visitFrom(self, node):
 
56
        if hasattr(node, 'valid_future'):
 
57
            return
 
58
        if node.modname != "__future__":
 
59
            return
 
60
        raise SyntaxError( "from __future__ imports must occur at the beginning of the file",
 
61
                           ( node.filename, node.lineno, 0, "" ) )
 
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 pypy.interpreter.stablecompiler 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