diff options
Diffstat (limited to 'lib/ffi_yajl/ffi/parser.rb')
-rw-r--r-- | lib/ffi_yajl/ffi/parser.rb | 57 |
1 files changed, 26 insertions, 31 deletions
diff --git a/lib/ffi_yajl/ffi/parser.rb b/lib/ffi_yajl/ffi/parser.rb index 77d2389..e3226c0 100644 --- a/lib/ffi_yajl/ffi/parser.rb +++ b/lib/ffi_yajl/ffi/parser.rb @@ -23,36 +23,32 @@ module FFI_Yajl module FFI module Parser - - def set_value(val) - case stack.last - when Hash - raise FFI_Yajl::ParseError.new("internal error: missing key in parse") if key.nil? - if @opts[:unique_key_checking] && stack.last.has_key?(key) - raise FFI_Yajl::ParseError.new("repeated key: #{key}") - end - stack.last[key] = val - when Array - stack.last.push(val) - else - stack.push(val) - end - end - - def stack_pop - if stack.length > 1 - set_value( stack.pop ) + def set_value(val) + case stack.last + when Hash + raise FFI_Yajl::ParseError.new("internal error: missing key in parse") if key.nil? + if @opts[:unique_key_checking] && stack.last.key?(key) + raise FFI_Yajl::ParseError.new("repeated key: #{key}") end + stack.last[key] = val + when Array + stack.last.push(val) + else + stack.push(val) end + end - def key_push - key_stack.push(key) - end + def stack_pop + set_value( stack.pop ) if stack.length > 1 + end - def key_pop - @key = key_stack.pop() - end + def key_push + key_stack.push(key) + end + def key_pop + @key = key_stack.pop + end def setup_callbacks @null_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx| @@ -68,7 +64,7 @@ module FFI_Yajl 1 end @number_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t ]) do |ctx, stringval, stringlen| - s = stringval.slice(0,stringlen) + s = stringval.slice(0, stringlen) s.force_encoding('UTF-8') if defined? Encoding # XXX: I can't think of a better way to do this right now. need to call to_f if and only if its a float. v = ( s =~ /[\.eE]/ ) ? s.to_f : s.to_i @@ -80,18 +76,18 @@ module FFI_Yajl 1 end @string_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t]) do |ctx, stringval, stringlen| - s = stringval.slice(0,stringlen) + s = stringval.slice(0, stringlen) s.force_encoding('UTF-8') if defined? Encoding set_value(s) 1 end @start_map_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx| key_push # for key => { } case, save the key - stack.push(Hash.new) + stack.push({}) 1 end @map_key_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t]) do |ctx, key, keylen| - s = key.slice(0,keylen) + s = key.slice(0, keylen) s.force_encoding('UTF-8') if defined? Encoding self.key = @opts[:symbolize_keys] ? s.to_sym : s 1 @@ -103,7 +99,7 @@ module FFI_Yajl end @start_array_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx| key_push # for key => [ ] case, save the key - stack.push(Array.new) + stack.push([]) 1 end @end_array_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx| @@ -113,7 +109,6 @@ module FFI_Yajl end end - def do_yajl_parse(str, yajl_opts = {}) setup_callbacks callback_ptr = ::FFI::MemoryPointer.new(::FFI_Yajl::YajlCallbacks) |