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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix escape sequences in Python's raw strings #1508

Merged
merged 5 commits into from May 12, 2020
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
20 changes: 6 additions & 14 deletions lib/rouge/lexers/python.rb
Expand Up @@ -186,14 +186,7 @@ def current_string
end
end

rule %r/\\/ do |m|
if current_string.type? "r"
token Str
else
token Str::Interpol
end
push :generic_escape
end
rule %r/(?=\\)/, Str, :generic_escape

rule %r/{/ do |m|
if current_string.type? "f"
Expand All @@ -206,23 +199,22 @@ def current_string
end

state :generic_escape do
rule %r(
rule %r(\\
( [\\abfnrtv"']
| \n
| newline
| N{[a-zA-Z][a-zA-Z ]+[a-zA-Z]}
| u[a-fA-F0-9]{4}
| U[a-fA-F0-9]{8}
| x[a-fA-F0-9]{2}
| [0-7]{1,3}
)
)x do
if current_string.type? "r"
token Str
else
token Str::Escape
end
token (current_string.type?("r") ? Str : Str::Escape)
pop!
end

rule %r/\\./, Str, :pop!
end

state :generic_interpol do
Expand Down
2 changes: 2 additions & 0 deletions spec/visual/samples/python
Expand Up @@ -47,6 +47,7 @@ def baz():
'\UaaaaAF09'
'\xaf\xAF\x09'
'\007'
'.*\[p00t_(d\d{4})\].*' # There are no escape sequences in this string

# escaped characters in raw strings
def baz():
Expand All @@ -56,6 +57,7 @@ def baz():
r'\UaaaaAF09'
r'\xaf\xAF\x09'
r'\007'
r'.*\[p00t_(d\d{4})\].*'

# line continuations
apple.filter(x, y)
Expand Down