10000 Merge pull request #189 from rubychan/ruby-2.3 · rubychan/coderay@16d8313 · GitHub
[go: up one dir, main page]

Skip to content

Commit 16d8313

Browse files
committed
Merge pull request #189 from rubychan/ruby-2.3
New Ruby 2.1, 2.2, 2.3 Syntax
2 parents ae5d868 + 4f325ea commit 16d8313

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

Changes.textile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ p=. _This files lists all changes in the CodeRay library since the 0.9.8 release
55
h2. Changes in 1.1.1
66

77
* SQL scanner: fix open strings [#163, thanks to Adam]
8+
* Ruby scanner: Accept number literal suffixes @r@ and @i@ (Ruby 2.1)
9+
* Ruby scanner: Accept quoted hash keys like @{ "a": boss }@ (Ruby 2.2)
10+
* Ruby scanner: Accept save navigation operator @&.@ (Ruby 2.3)
11+
* Ruby scanner: Accept squiggly heredoc @<<~@ (Ruby 2.3)
812

913
h2. Changes in 1.1
1014

lib/coderay/scanners/ruby.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,19 @@ def scan_tokens encoder, options
164164
end
165165

166166
elsif match = scan(/ ' (?:(?>[^'\\]*) ')? | " (?:(?>[^"\\\#]*) ")? /mx)
167-
encoder.begin_group :string
168167
if match.size == 1
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
173+
kind = value_expected == true && scan(/:/) ? :key : :string
174+
encoder.begin_group kind
172175
encoder.text_token match[0,1], :delimiter
173176
encoder.text_token match[1..-2], :content if match.size > 2
174177
encoder.text_token match[-1,1], :delimiter
175-
encoder.end_group :string
178+
encoder.end_group kind
179+
encoder.text_token ':', :operator if kind == :key
176180
value_expected = false
177181
end
178182

@@ -191,11 +195,14 @@ def scan_tokens encoder, options
191195
encoder.text_token match, :error
192196
method_call_expected = false
193197
else
194-
encoder.text_token match, self[1] ? :float : :integer # TODO: send :hex/:octal/:binary
198+
kind = self[1] ? :float : :integer # TODO: send :hex/:octal/:binary
199+
match << 'r' if match !~ /e/i && scan(/r/)
200+
match << 'i' if scan(/i/)
201+
encoder.text_token match, kind
195202
end
196203
value_expected = false
197204

198-
elsif match = scan(/ [-+!~^\/]=? | [:;] | [*|&]{1,2}=? | >>? /x)
205+
elsif match = scan(/ [-+!~^\/]=? | [:;] | &\. | [*|&]{1,2}=? | >>? /x)
199206
value_expected = true
200207
encoder.text_token match, :operator
201208

@@ -208,7 +215,7 @@ def scan_tokens encoder, options
208215
encoder.end_group kind
209216
heredocs ||= [] # create heredocs if empty
210217
heredocs << self.class::StringState.new(kind, quote != "'", delim,
211-
self[1] == '-' ? :indented : :linestart)
218+
self[1] ? :indented : :linestart)
212219
value_expected = false
213220

214221
elsif value_expected && match = scan(/#{patterns::FANCY_STRING_START}/o)

lib/coderay/scanners/ruby/patterns.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ module Ruby::Patterns # :nodoc: all
114114
# NOTE: This is not completely correct, but
115115
# nobody needs heredoc delimiters ending with \n.
116116
HEREDOC_OPEN = /
117-
<< (-)? # $1 = float
117+
<< ([-~])? # $1 = float
118118
(?:
119119
( [A-Za-z_0-9]+ ) # $2 = delim
120120
|

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)
0