summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2016-02-13 13:17:23 +0100
committerKornelius Kalnbach <murphy@rubychan.de>2016-02-13 13:17:23 +0100
commitc33f3f5c43064f7b468a59e086dc4a9a4f949ff7 (patch)
tree60c9172e5cfaa20f4ea89145f0e8c3dfc6164eee
parent415498eaf9417cf30656c4a745eef0409b214afc (diff)
downloadcoderay-c33f3f5c43064f7b468a59e086dc4a9a4f949ff7.tar.gz
check for keys with escape sequences, too
-rw-r--r--lib/coderay/scanners/ruby.rb5
-rw-r--r--lib/coderay/scanners/ruby/string_state.rb8
2 files changed, 11 insertions, 2 deletions
diff --git a/lib/coderay/scanners/ruby.rb b/lib/coderay/scanners/ruby.rb
index f7feb46..5b8de42 100644
--- a/lib/coderay/scanners/ruby.rb
+++ b/lib/coderay/scanners/ruby.rb
@@ -165,9 +165,10 @@ module Scanners
elsif match = scan(/ ' (?:(?>[^'\\]*) ')? | " (?:(?>[^"\\\#]*) ")? /mx)
if match.size == 1
- encoder.begin_group :string
+ kind = check(self.class::StringState.simple_key_pattern(match)) ? :key : :string
+ encoder.begin_group kind
encoder.text_token match, :delimiter
- state = self.class::StringState.new :string, match == '"', match # important for streaming
+ state = self.class::StringState.new kind, match == '"', match # important for streaming
else
kind = value_expected == true && scan(/:/) ? :key : :string
encoder.begin_group kind
diff --git a/lib/coderay/scanners/ruby/string_state.rb b/lib/coderay/scanners/ruby/string_state.rb
index 28ddd6c..93e7208 100644
--- a/lib/coderay/scanners/ruby/string_state.rb
+++ b/lib/coderay/scanners/ruby/string_state.rb
@@ -37,6 +37,14 @@ module Scanners
end
end
+ def self.simple_key_pattern delim
+ if delim == "'"
+ / (?> (?: [^\\']+ | \\. )* ) ' : /mx
+ else
+ / (?> (?: [^\\"\#]+ | \\. | \#\$[\\"] | \#(?!\{) )* ) " : /mx
+ end
+ end
+
def initialize kind, interpreted, delim, heredoc = false
if heredoc
pattern = heredoc_pattern delim, interpreted, heredoc == :indented