Skip to content

Commit e7feb0e

Browse files
committed
Prevent character literals spanning lines
The single-quote character literal regex was using `[^']*` which would match any character including newlines. This caused character literals to incorrectly span multiple lines when there were unmatched quotes in the input.
1 parent 92202ab commit e7feb0e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/rouge/lexers/crystal.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def self.detect?(text)
8080
rule %r/\b[\p{Ll}_]\p{Word}*?[?!]?:\s+/, Str::Symbol, :expr_start
8181
rule %r/"/, Str::Double, :simple_string
8282
rule %r/(?<!\.)`/, Str::Backtick, :simple_backtick
83-
rule %r/(')(\\u[a-fA-F0-9]{4}|\\u\{[a-fA-F0-9]{1,6}\}|\\[abefnrtv])?(\\\\|\\'|[^'])*(')/ do
83+
rule %r/(')(\\u[a-fA-F0-9]{4}|\\u\{[a-fA-F0-9]{1,6}\}|\\[abefnrtv])?(\\\\|\\'|[^'\n])*(')/ do
8484
groups Str::Single, Str::Escape, Str::Single, Str::Single
8585
end
8686
end

spec/lexers/crystal_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,17 @@
2020
assert_guess :source => '#!/usr/local/bin/crystal'
2121
end
2222
end
23+
24+
describe 'lexing' do
25+
include Support::Lexing
26+
27+
it 'rejects string literal than spans multiple lines' do
28+
assert_tokens_equal "'a\nb'",
29+
['Error', "'"],
30+
["Name", "a"],
31+
["Text", "\n"],
32+
["Name", "b"],
33+
["Error", "'"]
34+
end
35+
end
2336
end

0 commit comments

Comments
 (0)