Skip to content

Commit

Permalink
Disable highlighting of some escape codes for python bytes literals (#…
Browse files Browse the repository at this point in the history
…2204)

Disable highlighting of unicode escape codes in python bytes literals,
as described in
https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals.
So escape codes of the form "\N{name}", "\uxxxx" and "\Uxxxxxxxx" will
no longer be highlighted within bytes literals.  Add tests for escape
code highlighting in string and bytes literals.
  • Loading branch information
LaurenceWarne committed Aug 14, 2022
1 parent 581f7ee commit 78efbf0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
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

0 comments on commit 78efbf0

Please sign in to comment.