Skip to content

Commit c33f3f5

Browse files
committed
check for keys with escape sequences, too
1 parent 415498e commit c33f3f5

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/coderay/scanners/ruby.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ def scan_tokens encoder, options
165165

166166
elsif match = scan(/ ' (?:(?>[^'\\]*) ')? | " (?:(?>[^"\\\#]*) ")? /mx)
167167
if match.size == 1
168-
encoder.begin_group :string
168+
kind = check(self.class::StringState.simple_key_pattern(match)) ? :key : :string
169+
encoder.begin_group kind
169170
encoder.text_token match, :delimiter
170-
state = self.class::StringState.new :string, match == '"', match # important for streaming
171+
state = self.class::StringState.new kind, match == '"', match # important for streaming
171172
else
172173
kind = value_expected == true && scan(/:/) ? :key : :string
173174
encoder.begin_group kind

lib/coderay/scanners/ruby/string_state.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ class StringState < Struct.new :type, :interpreted, :delim, :heredoc,
3737
end
3838
end
3939

40+
def self.simple_key_pattern delim
41+
if delim == "'"
42+
/ (?> (?: [^\\']+ | \\. )* ) ' : /mx
43+
else
44+
/ (?> (?: [^\\"\#]+ | \\. | \#\$[\\"] | \#(?!\{) )* ) " : /mx
45+
end
46+
end
47+
4048
def initialize kind, interpreted, delim, heredoc = false
4149
if heredoc
4250
pattern = heredoc_pattern delim, interpreted, heredoc == :indented

0 commit comments

Comments
 (0)