Skip to content

Commit 9b60382

Browse files
committed
Add t-string support to syntax highlighting
1 parent 388e494 commit 9b60382

File tree

7 files changed

+22
-17
lines changed

7 files changed

+22
-17
lines changed

Lib/_pyrepl/_module_completer.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pkgutil
44
import sys
5+
import token
56
import tokenize
67
from io import StringIO
78
from contextlib import contextmanager
@@ -180,8 +181,8 @@ class ImportParser:
180181
when parsing multiple statements.
181182
"""
182183
_ignored_tokens = {
183-
tokenize.INDENT, tokenize.DEDENT, tokenize.COMMENT,
184-
tokenize.NL, tokenize.NEWLINE, tokenize.ENDMARKER
184+
token.INDENT, token.DEDENT, token.COMMENT,
185+
token.NL, token.NEWLINE, token.ENDMARKER
185186
}
186187
_keywords = {'import', 'from', 'as'}
187188

@@ -350,11 +351,11 @@ def peek(self) -> TokenInfo | None:
350351
def peek_name(self) -> bool:
351352
if not (tok := self.peek()):
352353
return False
353-
return tok.type == tokenize.NAME
354+
return tok.type == token.NAME
354355

355356
def pop_name(self) -> str:
356357
tok = self.pop()
357-
if tok.type != tokenize.NAME:
358+
if tok.type != token.NAME:
358359
raise ParseError('pop_name')
359360
return tok.string
360361

Lib/_pyrepl/mypy.ini

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,3 @@ check_untyped_defs = False
2323
# Various internal modules that typeshed deliberately doesn't have stubs for:
2424
[mypy-_abc.*,_opcode.*,_overlapped.*,_testcapi.*,_testinternalcapi.*,test.*]
2525
ignore_missing_imports = True
26-
27-
# Other untyped parts of the stdlib
28-
[mypy-idlelib.*]
29-
ignore_missing_imports = True

Lib/_pyrepl/utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ def recover_unterminated_string(
113113
if (
114114
msg.startswith("unterminated string literal")
115115
or msg.startswith("unterminated f-string literal")
116+
or msg.startswith("unterminated t-string literal")
116117
or msg.startswith("EOF in multi-line string")
117118
or msg.startswith("unterminated triple-quoted f-string literal")
119+
or msg.startswith("unterminated triple-quoted t-string literal")
118120
):
119121
start = line_lengths[loc[0] - 1] + loc[1] - 1
120122
end = line_lengths[-1] - 1
@@ -149,7 +151,11 @@ def gen_colors_from_token_stream(
149151
continue
150152

151153
match token.type:
152-
case T.STRING | T.FSTRING_START | T.FSTRING_MIDDLE | T.FSTRING_END:
154+
case (
155+
T.STRING
156+
| T.FSTRING_START | T.FSTRING_MIDDLE | T.FSTRING_END
157+
| T.TSTRING_START | T.TSTRING_MIDDLE | T.TSTRING_END
158+
):
153159
span = Span.from_token(token, line_lengths)
154160
yield ColorSpan(span, "STRING")
155161
case T.COMMENT:
@@ -202,7 +208,7 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool:
202208
case (
203209
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(string=":"),
204210
TI(string="match"),
205-
TI(T.NUMBER | T.STRING | T.FSTRING_START)
211+
TI(T.NUMBER | T.STRING | T.FSTRING_START | T.TSTRING_START)
206212
| TI(T.OP, string="(" | "*" | "-" | "+" | "[" | "{" | "~" | "...")
207213
):
208214
return True
@@ -217,7 +223,7 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool:
217223
case (
218224
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(string=":"),
219225
TI(string="case"),
220-
TI(T.NUMBER | T.STRING | T.FSTRING_START)
226+
TI(T.NUMBER | T.STRING | T.FSTRING_START | T.TSTRING_START)
221227
| TI(T.OP, string="(" | "*" | "-" | "[" | "{")
222228
):
223229
return True

Lib/token.py

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Misc/mypy/token.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Lib/token.py

Misc/mypy/typed-stdlib.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
_colorize.py
44
_pyrepl
5+
token.py
56
tomllib

Tools/build/generate_token.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,13 @@ def make_rst(infile, outfile='Doc/library/token-list.inc',
278278
%s
279279
}
280280
281-
def ISTERMINAL(x):
281+
def ISTERMINAL(x: int) -> bool:
282282
return x < NT_OFFSET
283283
284-
def ISNONTERMINAL(x):
284+
def ISNONTERMINAL(x: int) -> bool:
285285
return x >= NT_OFFSET
286286
287-
def ISEOF(x):
287+
def ISEOF(x: int) -> bool:
288288
return x == ENDMARKER
289289
'''
290290

0 commit comments

Comments
 (0)