diff --git a/parsers/ruby/lib/parse_server.rb b/parsers/ruby/lib/parse_server.rb index bcc605f..0dcaebb 100644 --- a/parsers/ruby/lib/parse_server.rb +++ b/parsers/ruby/lib/parse_server.rb @@ -1,6 +1,17 @@ require 'socket' require File.expand_path('../parser.rb', __FILE__) +def pack_int(i) + bytes = []; mask = 255 + + while bytes.length < 4 + bytes.unshift (i & mask) + i = i >> 8 + end + + return bytes.pack('cccc') +end + server = TCPServer.new 5003 loop do @@ -11,8 +22,8 @@ loop do size = (client.readline).to_i p = Bitshift::Parser.new client.read(size) # Get the parsed result - symbols = p.parse.to_s - client.puts [symbols.length].pack('c') + symbols = p.parse + client.puts pack_int(symbols.length) client.puts symbols ensure # Close the socket diff --git a/parsers/ruby/lib/parser.rb b/parsers/ruby/lib/parser.rb index 5751ce0..150e940 100644 --- a/parsers/ruby/lib/parser.rb +++ b/parsers/ruby/lib/parser.rb @@ -12,9 +12,9 @@ module Bitshift parser = RubyParser.new tree = parser.parse(@source) offset = tree.line - 1 - processor = NodeVisitor.new offset - processor.process tree - return processor.symbols + processor = NodeVisitor.new offset, tree + processor.process(tree) + return processor.to_s end end @@ -22,16 +22,16 @@ module Bitshift attr_accessor :symbols attr_accessor :offset - def initialize(offset) + def initialize(offset, tree) super() - @require_empty = false - @offset = offset module_hash = Hash.new {|hash, key| hash[key] = Hash.new} class_hash = module_hash.clone function_hash = Hash.new {|hash, key| hash[key] = { calls: [] } } var_hash = Hash.new {|hash, key| hash[key] = [] } + @require_empty = false + @offset = offset @symbols = { modules: module_hash, classes: class_hash, @@ -68,7 +68,7 @@ module Bitshift end def process_module(exp) - pos = block_position exp + pos = block_position(exp) exp.shift name = exp.shift symbols[:modules][name] = pos @@ -77,7 +77,7 @@ module Bitshift end def process_class(exp) - pos = block_position exp + pos = block_position(exp) exp.shift name = exp.shift symbols[:classes][name] = pos @@ -86,7 +86,7 @@ module Bitshift end def process_defn(exp) - pos = block_position exp + pos = block_position(exp) exp.shift name = exp.shift symbols[:functions][name][:declaration] = pos @@ -95,7 +95,7 @@ module Bitshift end def process_call(exp) - pos = statement_position exp + pos = statement_position(exp) exp.shift exp.shift name = exp.shift @@ -105,7 +105,7 @@ module Bitshift end def process_iasgn(exp) - pos = statement_position exp + pos = statement_position(exp) exp.shift name = exp.shift symbols[:vars][name] << pos @@ -114,12 +114,18 @@ module Bitshift end def process_lasgn(exp) - pos = statement_position exp + pos = statement_position(exp) exp.shift name = exp.shift symbols[:vars][name] << pos exp.each_sexp {|s| process(s)} return exp.clear end + + def to_s + str = symbols.to_s + str = str.gsub(/:(\w*)=>/, '"\1":') + return str + end end end