Skip to content

Commit cafaf6a

Browse files
committed
Added nonlocal as a python3 specific keyword. Added print as a python 2 specific keyword.
1 parent 708b6f8 commit cafaf6a

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

mypy/lex.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,29 @@ def __init__(self, string: str, type: int) -> None:
148148
COMMENT_CONTEXT = 2
149149

150150

151-
def lex(string: str, first_line: int = 1) -> List[Token]:
151+
def lex(string: str, first_line: int = 1, pyversion: int = 3) -> List[Token]:
152152
"""Analyze string and return an array of token objects.
153153
154154
The last token is always Eof.
155155
"""
156-
l = Lexer()
156+
l = Lexer(pyversion)
157157
l.lex(string, first_line)
158158
return l.tok
159159

160160

161161
# Reserved words (not including operators)
162-
keywords = set([
162+
keywords_common = set([
163163
'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif',
164164
'else', 'except', 'finally', 'from', 'for', 'global', 'if', 'import',
165165
'lambda', 'pass', 'raise', 'return', 'try', 'while', 'with',
166166
'yield'])
167167

168+
# Reserved words specific for Python version 2
169+
keywords2 = set(['print'])
170+
171+
# Reserved words specific for Python version 3
172+
keywords3 = set(['nonlocal'])
173+
168174
# Alphabetical operators (reserved words)
169175
alpha_operators = set(['in', 'is', 'not', 'and', 'or'])
170176

@@ -279,7 +285,7 @@ class Lexer:
279285
# newlines within parentheses/brackets.
280286
open_brackets = Undefined(List[str])
281287

282-
def __init__(self) -> None:
288+
def __init__(self, pyversion: int = 3) -> None:
283289
self.map = [self.unknown_character] * 256
284290
self.tok = []
285291
self.indents = [0]
@@ -302,6 +308,10 @@ def __init__(self) -> None:
302308
('-+*/<>%&|^~=!,@', self.lex_misc)]:
303309
for c in seq:
304310
self.map[ord(c)] = method
311+
if pyversion == 2:
312+
self.keywords = keywords_common | keywords2
313+
if pyversion == 3:
314+
self.keywords = keywords_common | keywords3
305315

306316
def lex(self, s: str, first_line: int) -> None:
307317
"""Lexically analyze a string, storing the tokens at the tok list."""
@@ -401,7 +411,7 @@ def lex_name(self) -> None:
401411
Also deal with prefixed string literals such as r'...'.
402412
"""
403413
s = self.match(self.name_exp)
404-
if s in keywords:
414+
if s in self.keywords:
405415
self.add_token(Keyword(s))
406416
elif s in alpha_operators:
407417
self.add_token(Op(s))

mypy/parse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def __init__(self, fnam: str, errors: Errors, pyversion: int,
108108
self.errors.set_file('<input>')
109109

110110
def parse(self, s: str) -> MypyFile:
111-
self.tok = lex.lex(s)
111+
self.tok = lex.lex(s, pyversion = self.pyversion)
112112
self.ind = 0
113113
self.imports = []
114114
self.future_options = []
@@ -678,7 +678,7 @@ def parse_statement(self) -> Node:
678678
stmt = self.parse_class_def()
679679
elif ts == 'global':
680680
stmt = self.parse_global_decl()
681-
elif ts == 'nonlocal':
681+
elif ts == 'nonlocal' and self.pyversion >= 3:
682682
stmt = self.parse_nonlocal_decl()
683683
elif ts == 'assert':
684684
stmt = self.parse_assert_stmt()

0 commit comments

Comments
 (0)