Skip to content

Commit d05f9a5

Browse files
committed
Simplified lexing of ellipsis
1 parent 3ca0913 commit d05f9a5

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

mypy/lex.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ class Colon(Token):
117117
pass
118118

119119

120+
class EllipsisToken(Token):
121+
pass
122+
123+
120124
class Op(Token):
121125
"""Operator (e.g. '+' or 'in')"""
122126

@@ -146,7 +150,6 @@ def __init__(self, string: str, type: int) -> None:
146150
INVALID_UTF8_SEQUENCE = 5
147151
INVALID_BACKSLASH = 6
148152
INVALID_DEDENT = 7
149-
ELLIPSIS_ERROR = 8
150153

151154
# Encoding contexts
152155
STR_CONTEXT = 1
@@ -176,7 +179,7 @@ def lex(string: str, first_line: int = 1, pyversion: int = 3) -> List[Token]:
176179
keywords2 = set([]) # type: Set[str]
177180

178181
# Reserved words specific for Python version 3
179-
keywords3 = set(['nonlocal', '...'])
182+
keywords3 = set(['nonlocal'])
180183

181184
# Alphabetical operators (reserved words)
182185
alpha_operators = set(['in', 'is', 'not', 'and', 'or'])
@@ -429,11 +432,7 @@ def lex_number(self) -> None:
429432
self.add_token(ComplexLit(sc))
430433

431434
def lex_ellipsis(self) -> None:
432-
ellipsis = self.match(re.compile('\.\.\.[0-9a-zA-Z_]*'))
433-
if len(ellipsis) == 3:
434-
self.add_token(Keyword('...'))
435-
else:
436-
self.add_token(LexError(ellipsis, ELLIPSIS_ERROR))
435+
self.add_token(EllipsisToken('...'))
437436

438437
name_exp = re.compile('[a-zA-Z_][a-zA-Z0-9_]*')
439438

mypy/parse.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from mypy import lex
1212
from mypy.lex import (
1313
Token, Eof, Bom, Break, Name, Colon, Dedent, IntLit, StrLit, BytesLit,
14-
UnicodeLit, FloatLit, Op, Indent, Keyword, Punct, LexError, ComplexLit
14+
UnicodeLit, FloatLit, Op, Indent, Keyword, Punct, LexError, ComplexLit,
15+
EllipsisToken
1516
)
1617
import mypy.types
1718
from mypy.nodes import (
@@ -1128,7 +1129,7 @@ def parse_expression(self, prec: int = 0, star_expr_allowed: bool = False) -> No
11281129
expr = self.parse_complex_expr()
11291130
elif isinstance(t, Keyword) and s == "yield":
11301131
expr = self.parse_yield_from_expr() # The expression yield from and yield to assign
1131-
elif isinstance(t, Keyword) and s == '...':
1132+
elif isinstance(t, EllipsisToken):
11321133
expr = self.parse_ellipsis()
11331134
else:
11341135
# Invalid expression.
@@ -1856,8 +1857,6 @@ def token_repr(tok: Token) -> str:
18561857
return 'non-ASCII character in string'
18571858
elif t == lex.INVALID_DEDENT:
18581859
return 'inconsistent indentation'
1859-
elif t == lex.ELLIPSIS_ERROR:
1860-
return 'invalid ellipsis'
18611860
raise ValueError('Unknown token {}'.format(repr(tok)))
18621861

18631862

mypy/test/data/parse-errors.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,6 @@ file, line 1: Parse error before end of line
380380
..._
381381
...a
382382
[out]
383-
file, line 1: Invalid ellipsis
384-
file, line 2: Invalid ellipsis
385-
file, line 3: Invalid ellipsis
383+
file, line 1: Parse error before numeric literal
384+
file, line 2: Parse error before "_"
385+
file, line 3: Parse error before "a"

0 commit comments

Comments
 (0)