summaryrefslogtreecommitdiff
path: root/lib/coderay/scanners/json.rb
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2013-06-23 16:06:02 +0200
committerKornelius Kalnbach <murphy@rubychan.de>2013-06-23 16:06:02 +0200
commit0013b649f714f23eef0859921fa7804ca7caef76 (patch)
tree7c278ee7c420729b4738fe2a195e529ffd2bb6da /lib/coderay/scanners/json.rb
parentaddcbd446066d0da1627112814e3ce1b8d404da0 (diff)
parent64ca2ae8ad5130bdcf652aa7aa08298de00f20f4 (diff)
downloadcoderay-0013b649f714f23eef0859921fa7804ca7caef76.tar.gz
Merge branch 'master' into go-scanner
Conflicts: lib/coderay/helpers/file_type.rb
Diffstat (limited to 'lib/coderay/scanners/json.rb')
-rw-r--r--lib/coderay/scanners/json.rb25
1 files changed, 14 insertions, 11 deletions
diff --git a/lib/coderay/scanners/json.rb b/lib/coderay/scanners/json.rb
index 4e0f462..b09970c 100644
--- a/lib/coderay/scanners/json.rb
+++ b/lib/coderay/scanners/json.rb
@@ -14,15 +14,21 @@ module Scanners
ESCAPE = / [bfnrt\\"\/] /x # :nodoc:
UNICODE_ESCAPE = / u[a-fA-F0-9]{4} /x # :nodoc:
+ KEY = / (?> (?: [^\\"]+ | \\. )* ) " \s* : /x
protected
+ def setup
+ @state = :initial
+ end
+
# See http://json.org/ for a definition of the JSON lexic/grammar.
def scan_tokens encoder, options
+ state = options[:state] || @state
- state = :initial
- stack = []
- key_expected = false
+ if [:string, :key].include? state
+ encoder.begin_group state
+ end
until eos?
@@ -32,18 +38,11 @@ module Scanners
if match = scan(/ \s+ /x)
encoder.text_token match, :space
elsif match = scan(/"/)
- state = key_expected ? :key : :string
+ state = check(/#{KEY}/o) ? :key : :string
encoder.begin_group state
encoder.text_token match, :delimiter
elsif match = scan(/ [:,\[{\]}] /x)
encoder.text_token match, :operator
- case match
- when ':' then key_expected = false
- when ',' then key_expected = true if stack.last == :object
- when '{' then stack << :object; key_expected = true
- when '[' then stack << :array
- when '}', ']' then stack.pop # no error recovery, but works for valid JSON
- end
elsif match = scan(/ true | false | null /x)
encoder.text_token match, :value
elsif match = scan(/ -? (?: 0 | [1-9]\d* ) /x)
@@ -82,6 +81,10 @@ module Scanners
end
end
+ if options[:keep_state]
+ @state = state
+ end
+
if [:string, :key].include? state
encoder.end_group state
end