summaryrefslogtreecommitdiff
path: root/lib/coderay/scanners/json.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/coderay/scanners/json.rb')
-rw-r--r--lib/coderay/scanners/json.rb34
1 files changed, 16 insertions, 18 deletions
diff --git a/lib/coderay/scanners/json.rb b/lib/coderay/scanners/json.rb
index cb61960..b09970c 100644
--- a/lib/coderay/scanners/json.rb
+++ b/lib/coderay/scanners/json.rb
@@ -14,7 +14,7 @@ module Scanners
ESCAPE = / [bfnrt\\"\/] /x # :nodoc:
UNICODE_ESCAPE = / u[a-fA-F0-9]{4} /x # :nodoc:
- KEY = / (?> (?: [^\\"]+ | \\. )* ) " \s* : /mx
+ KEY = / (?> (?: [^\\"]+ | \\. )* ) " \s* : /x
protected
@@ -37,41 +37,40 @@ module Scanners
when :initial
if match = scan(/ \s+ /x)
encoder.text_token match, :space
- elsif match = scan(/ " (?=#{KEY}) /ox)
- state = :key
- encoder.begin_group :key
- encoder.text_token match, :delimiter
- elsif match = scan(/ " /x)
- state = :string
- encoder.begin_group :string
+ elsif match = scan(/"/)
+ state = check(/#{KEY}/o) ? :key : :string
+ encoder.begin_group state
encoder.text_token match, :delimiter
elsif match = scan(/ [:,\[{\]}] /x)
encoder.text_token match, :operator
elsif match = scan(/ true | false | null /x)
encoder.text_token match, :value
- elsif match = scan(/ -? (?: 0 | [1-9]\d* ) (?: \.\d+ (?: [eE][-+]? \d+ )? | [eE][-+]? \d+ ) /x)
- encoder.text_token match, :float
elsif match = scan(/ -? (?: 0 | [1-9]\d* ) /x)
- encoder.text_token match, :integer
+ if scan(/ \.\d+ (?:[eE][-+]?\d+)? | [eE][-+]? \d+ /x)
+ match << matched
+ encoder.text_token match, :float
+ else
+ encoder.text_token match, :integer
+ end
else
encoder.text_token getch, :error
end
when :string, :key
- if match = scan(/ [^\\"]+ /x)
+ if match = scan(/[^\\"]+/)
encoder.text_token match, :content
- elsif match = scan(/ " /x)
+ elsif match = scan(/"/)
encoder.text_token match, :delimiter
encoder.end_group state
state = :initial
- elsif match = scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /ox)
+ elsif match = scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
encoder.text_token match, :char
- elsif match = scan(/ \\. /mx)
+ elsif match = scan(/\\./m)
encoder.text_token match, :content
- elsif match = scan(/ \\ /x)
+ elsif match = scan(/ \\ | $ /x)
encoder.end_group state
+ encoder.text_token match, :error unless match.empty?
state = :initial
- encoder.text_token match, :error
else
raise_inspect "else case \" reached; %p not handled." % peek(1), encoder
end
@@ -80,7 +79,6 @@ module Scanners
raise_inspect 'Unknown state: %p' % [state], encoder
end
-
end
if options[:keep_state]