summaryrefslogtreecommitdiff
path: root/pygments
diff options
context:
space:
mode:
authorLaurenceWarne <17688577+LaurenceWarne@users.noreply.github.com>2022-08-14 23:40:46 +0100
committerGitHub <noreply@github.com>2022-08-15 00:40:46 +0200
commit78efbf0bd992871e560b3997645f5b2fe29d0f3c (patch)
tree64ec7305856254820d96bb2258dc4ebea0a3964a /pygments
parent581f7ee2567d756c0fbfce5e99ba10d461d08acb (diff)
downloadpygments-git-78efbf0bd992871e560b3997645f5b2fe29d0f3c.tar.gz
Disable highlighting of some escape codes for python bytes literals (#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.
Diffstat (limited to 'pygments')
-rw-r--r--pygments/lexers/python.py27
1 files changed, 20 insertions, 7 deletions
diff --git a/pygments/lexers/python.py b/pygments/lexers/python.py
index 83c5ef4e..0a7e8f83 100644
--- a/pygments/lexers/python.py
+++ b/pygments/lexers/python.py
@@ -142,7 +142,7 @@ class PythonLexer(RegexLexer):
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)(''')",
@@ -152,14 +152,24 @@ class PythonLexer(RegexLexer):
("(?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),
@@ -343,9 +353,12 @@ class PythonLexer(RegexLexer):
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),