Debian Codegraph @

pypy/interpreter/pyparser/future.py

raw symbols SCIP
1 from pypy.tool import stdlib___future__ as future
2
3 class FutureFlags(object):
4
5 def __init__(self, version):
6 compiler_flags = 0
7 self.compiler_features = {}
8 self.mandatory_flags = 0
9 for fname in future.all_feature_names:
10 feature = getattr(future, fname)
11 if version >= feature.getOptionalRelease():
12 flag = feature.compiler_flag
13 compiler_flags |= flag
14 self.compiler_features[fname] = flag
15 mandatory_release = feature.getMandatoryRelease()
16 if version >= mandatory_release and mandatory_release is not None:
17 self.mandatory_flags |= feature.compiler_flag
18 self.allowed_flags = compiler_flags
19
20 def get_flag_names(self, space, flags):
21 flag_names = []
22 for name, value in self.compiler_features.items():
23 if flags & value:
24 flag_names.append(name)
25 return flag_names
26
27 def get_compiler_feature(self, name):
28 return self.compiler_features.get(name, 0)
29
30 futureFlags_2_4 = FutureFlags((2, 4, 4, 'final', 0))
31 futureFlags_2_5 = FutureFlags((2, 5, 0, 'final', 0))
32 futureFlags_2_7 = FutureFlags((2, 7, 0, 'final', 0))
33 futureFlags_3_2 = FutureFlags((3, 2, 0, 'final', 0))
34 futureFlags_3_5 = FutureFlags((3, 5, 0, 'final', 0))
35 futureFlags_3_7 = FutureFlags((3, 7, 0, 'final', 0))
36 futureFlags_3_8 = FutureFlags((3, 8, 0, 'final', 0))
37 futureFlags_3_9 = FutureFlags((3, 9, 0, 'final', 0))
38 futureFlags_3_11 = FutureFlags((3, 11, 0, 'final', 0))
39
40
41 class TokenIterator:
42 def __init__(self, tokens):
43 self.tokens = tokens
44 self.index = 0
45 self.next()
46
47 def next(self):
48 index = self.index
49 self.index = index + 1
50 self.tok = self.tokens[index]
51
52 def skip(self, n):
53 if self.tok.token_type == n:
54 self.next()
55 return True
56 else:
57 return False
58
59 def skip_name(self, name):
60 from pypy.interpreter.pyparser import pygram
61 if self.tok.token_type == pygram.tokens.NAME and self.tok.value == name:
62 self.next()
63 return True
64 else:
65 return False
66
67 def next_feature_name(self):
68 from pypy.interpreter.pyparser import pygram
69 if self.tok.token_type == pygram.tokens.NAME:
70 name = self.tok.value
71 self.next()
72 if self.skip_name("as"):
73 self.skip(pygram.tokens.NAME)
74 return name
75 else:
76 return ''
77
78 def skip_newlines(self):
79 from pypy.interpreter.pyparser import pygram
80 while self.skip(pygram.tokens.NEWLINE):
81 pass
82
83
84 def add_future_flags(future_flags, tokens):
85 from pypy.interpreter.pyparser import pygram
86 it = TokenIterator(tokens)
87 result = 0
88 last_position = (0, 0)
89 #
90 # The only things that can precede a future statement are another
91 # future statement and a doc string (only one). This is a very
92 # permissive parsing of the given list of tokens; it relies on
93 # the real parsing done afterwards to give errors.
94 it.skip_newlines()
95 it.skip_name("r") or it.skip_name("u") or it.skip_name("ru")
96 if it.skip(pygram.tokens.STRING):
97 while it.skip(pygram.tokens.STRING): # implicit concatenation
98 pass
99 it.skip_newlines()
100
101 while (it.skip_name("from") and
102 it.skip_name("__future__") and
103 it.skip_name("import")):
104 it.skip(pygram.tokens.LPAR) # optionally
105 # return in 'last_position' any line-column pair that points
106 # somewhere inside the last __future__ import statement
107 # (at the start would be fine too, but it's easier to grab a
108 # random position inside)
109 last_position = (it.tok.lineno, it.tok.column)
110 result |= future_flags.get_compiler_feature(it.next_feature_name())
111 while it.skip(pygram.tokens.COMMA):
112 result |= future_flags.get_compiler_feature(it.next_feature_name())
113 it.skip(pygram.tokens.RPAR) # optionally
114 it.skip(pygram.tokens.SEMI) # optionally
115 it.skip_newlines()
116
117 # remove the flags that were specified but are anyway mandatory
118 result &= ~future_flags.mandatory_flags
119
120 return result, last_position