diff options
author | Kornelius Kalnbach <murphy@rubychan.de> | 2013-10-28 03:49:51 +0100 |
---|---|---|
committer | Kornelius Kalnbach <murphy@rubychan.de> | 2013-10-28 03:49:51 +0100 |
commit | 5e3df7f4d78ddc1fc01abe866f3c8e283c06315a (patch) | |
tree | 86ba8fce24e0f0a90bead886f62aaff729af0629 /lib/coderay/scanners/json.rb | |
parent | e93aae88985667189bb5b24ad0d5f54cb5fdba70 (diff) | |
download | coderay-5e3df7f4d78ddc1fc01abe866f3c8e283c06315a.tar.gz |
experiment with JSON scanner
Diffstat (limited to 'lib/coderay/scanners/json.rb')
-rw-r--r-- | lib/coderay/scanners/json.rb | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/lib/coderay/scanners/json.rb b/lib/coderay/scanners/json.rb index b09970c..0d514cf 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* : /x + KEY = / (?> (?: [^\\"]+ | \\. )* ) " \s* : /mx protected @@ -37,40 +37,41 @@ module Scanners when :initial if match = scan(/ \s+ /x) encoder.text_token match, :space - elsif match = scan(/"/) - state = check(/#{KEY}/o) ? :key : :string - encoder.begin_group state + 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 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) - if scan(/ \.\d+ (?:[eE][-+]?\d+)? | [eE][-+]? \d+ /x) - match << matched - encoder.text_token match, :float - else - encoder.text_token match, :integer - end + encoder.text_token match, :integer else encoder.text_token getch, :error end when :string, :key - if match = scan(/[^\\"]+/) + if match = scan(/ [^\\"]+ /x) encoder.text_token match, :content - elsif match = scan(/"/) + elsif match = scan(/ " /x) encoder.text_token match, :delimiter encoder.end_group state state = :initial - elsif match = scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox) + elsif match = scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /ox) encoder.text_token match, :char - elsif match = scan(/\\./m) + elsif match = scan(/ \\. /mx) 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 |