#require 'treetop' #require File.expand_path("../simple_marshal_parser.rb",__FILE__) require File.expand_path("../tools/parser/parser_test.rb",__FILE__) require 'time' module Simple module Marshal INDENT=" " def self.dump(data) case data when String "\""+data.gsub("\\"){"\\\\"}.gsub("\"","\\\"").gsub("\n","\\n")+"\"" when Numeric data.to_s when Array "[\n"+data.map{|d|INDENT+dump(d)}.join(",\n")+"\n]" when Hash "{\n"+INDENT+data.map{|k,v|dump(k)+" => "+dump(v)}.join(",\n").gsub("\n","\n"+INDENT)+"\n}" when Symbol ":"+data.to_s when NilClass "nil" when FalseClass "false" when TrueClass "true" else o=data hash=nil n=o.class.to_s case o when Time hash={:data=>o.to_s} end if o.respond_to?(:members) and o.respond_to?(:values) hash={} o.members.each{|m| hash[m.to_sym]=o.send(m) } end if hash "("+n+"\n"+hash.map{|k,v|INDENT+dump(k)+" => "+dump(v)}.join(",\n")+"\n)" else raise "Unknown Type to marshal #{data} #{data.class}" end end end Buf=Struct.new(:str,:pos) class Buf def buf str[pos..-1] end end def self.load(str) if false @@parser||=nil unless @@parser Treetop. load File.expand_path("../simple_marshal_parser.tt",__FILE__) @@parser=true end kcode=$KCODE parser = SimpleMarshalParser.new result = parser.parse(str) #pp result $KCODE=kcode if result pp "GOT RESULT" return result.sload else nil end else @@parser||=nil unless @@parser #Treetop.load File.expand_path("../simple_marshal_parser.tt",__FILE__) @@parser=true end parser = SimpleMarshalParser #.new result = parser.parse(str) if result #pp "GOT RESULT" return result.convert else nil end end end end end