Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable highlighting of Some Escape Codes for Python bytes Literals #2204

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 20 additions & 7 deletions pygments/lexers/python.py
Expand Up @@ -142,7 +142,7 @@ def fstring_rules(ttype):
combined('fstringescape', 'dqf')),
("([fF])(')", bygroups(String.Affix, String.Single),
combined('fstringescape', 'sqf')),
# raw strings
# raw bytes and strings
('(?i)(rb|br|r)(""")',
bygroups(String.Affix, String.Double), 'tdqs'),
("(?i)(rb|br|r)(''')",
Expand All @@ -152,14 +152,24 @@ def fstring_rules(ttype):
("(?i)(rb|br|r)(')",
bygroups(String.Affix, String.Single), 'sqs'),
# non-raw strings
('([uUbB]?)(""")', bygroups(String.Affix, String.Double),
('([uU]?)(""")', bygroups(String.Affix, String.Double),
combined('stringescape', 'tdqs')),
("([uUbB]?)(''')", bygroups(String.Affix, String.Single),
("([uU]?)(''')", bygroups(String.Affix, String.Single),
combined('stringescape', 'tsqs')),
('([uUbB]?)(")', bygroups(String.Affix, String.Double),
('([uU]?)(")', bygroups(String.Affix, String.Double),
combined('stringescape', 'dqs')),
("([uUbB]?)(')", bygroups(String.Affix, String.Single),
("([uU]?)(')", bygroups(String.Affix, String.Single),
combined('stringescape', 'sqs')),
# non-raw bytes
('([bB])(""")', bygroups(String.Affix, String.Double),
combined('bytesescape', 'tdqs')),
("([bB])(''')", bygroups(String.Affix, String.Single),
combined('bytesescape', 'tsqs')),
('([bB])(")', bygroups(String.Affix, String.Double),
combined('bytesescape', 'dqs')),
("([bB])(')", bygroups(String.Affix, String.Single),
combined('bytesescape', 'sqs')),

(r'[^\S\n]+', Text),
include('numbers'),
(r'!=|==|<<|>>|:=|[-~+/*%=<>&^|.]', Operator),
Expand Down Expand Up @@ -343,9 +353,12 @@ def fstring_rules(ttype):
include('rfstringescape'),
include('stringescape'),
],
'bytesescape': [
(r'\\([\\abfnrtv"\']|\n|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
],
'stringescape': [
(r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
(r'\\(N\{.*?\}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8})', String.Escape),
include('bytesescape')
],
'fstrings-single': fstring_rules(String.Single),
'fstrings-double': fstring_rules(String.Double),
Expand Down
24 changes: 24 additions & 0 deletions tests/snippets/python/test_bytes_escape_codes.txt
@@ -0,0 +1,24 @@
---input---
b'\\ \n \x12 \777 \u1234 \U00010348 \N{Plus-Minus Sign}'

---tokens---
'b' Literal.String.Affix
"'" Literal.String.Single
'\\\\' Literal.String.Escape
' ' Literal.String.Single
'\\n' Literal.String.Escape
' ' Literal.String.Single
'\\x12' Literal.String.Escape
' ' Literal.String.Single
'\\777' Literal.String.Escape
' ' Literal.String.Single
'\\' Literal.String.Single
'u1234 ' Literal.String.Single
'\\' Literal.String.Single
'U00010348 ' Literal.String.Single
'\\' Literal.String.Single
'N' Literal.String.Single
'{' Literal.String.Single
'Plus-Minus Sign}' Literal.String.Single
"'" Literal.String.Single
'\n' Text
20 changes: 20 additions & 0 deletions tests/snippets/python/test_string_escape_codes.txt
@@ -0,0 +1,20 @@
---input---
'\\ \n \x12 \777 \u1234 \U00010348 \N{Plus-Minus Sign}'

---tokens---
"'" Literal.String.Single
'\\\\' Literal.String.Escape
' ' Literal.String.Single
'\\n' Literal.String.Escape
' ' Literal.String.Single
'\\x12' Literal.String.Escape
' ' Literal.String.Single
'\\777' Literal.String.Escape
' ' Literal.String.Single
'\\u1234' Literal.String.Escape
' ' Literal.String.Single
'\\U00010348' Literal.String.Escape
' ' Literal.String.Single
'\\N{Plus-Minus Sign}' Literal.String.Escape
"'" Literal.String.Single
'\n' Text