[macruby-changes] [2222] MacRuby/branches/experimental

source_changes at macosforge.org source_changes at macosforge.org
Wed Aug 5 13:20:49 PDT 2009


Revision: 2222
          http://trac.macosforge.org/projects/ruby/changeset/2222
Author:   pthomson at apple.com
Date:     2009-08-05 13:20:48 -0700 (Wed, 05 Aug 2009)
Log Message:
-----------
Tightened up some sections of the API; fixed quoting of strings.

Modified Paths:
--------------
    MacRuby/branches/experimental/ext/libyaml/rubyext.c
    MacRuby/branches/experimental/lib/yaml/rubytypes.rb
    MacRuby/branches/experimental/lib/yaml.rb

Modified: MacRuby/branches/experimental/ext/libyaml/rubyext.c
===================================================================
--- MacRuby/branches/experimental/ext/libyaml/rubyext.c	2009-08-05 20:20:47 UTC (rev 2221)
+++ MacRuby/branches/experimental/ext/libyaml/rubyext.c	2009-08-05 20:20:48 UTC (rev 2222)
@@ -53,6 +53,8 @@
 static VALUE rb_cResolver;
 
 static ID id_tags_ivar;
+static ID id_plain;
+static ID id_quote2;
 
 static VALUE rb_oDefaultResolver;
 
@@ -178,15 +180,29 @@
 		((void(*)(void *, SEL))rb_yaml_parser_finalize_super)(rcv, sel);
 	}
 }
-#if 0
+
 static yaml_scalar_style_t
 rb_symbol_to_scalar_style(VALUE sym)
 {
-	rb_notimplement();
+	yaml_scalar_style_t style = YAML_ANY_SCALAR_STYLE;
+	if (NIL_P(sym))
+	{
+		return style;
+	}
+	else if (rb_to_id(sym) == id_plain)
+	{
+		style = YAML_PLAIN_SCALAR_STYLE;
+	}
+	else if (rb_to_id(sym) == id_quote2)
+	{
+		style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
+	}
+	return style;
 }
 
+
 static yaml_char_t*
-rb_yaml_tag_or_null(VALUE tagstr)
+rb_yaml_tag_or_null(VALUE tagstr, int *can_omit_tag)
 {
 	// todo: make this part of the resolver chain; this is the wrong place for it
 	const char *tag = RSTRING_PTR(tagstr);
@@ -194,13 +210,17 @@
 		(strcmp(tag, "tag:yaml.org,2002:float") == 0) ||
 		(strcmp(tag, "tag:ruby.yaml.org,2002:symbol") == 0) ||
 		(strcmp(tag, "tag:yaml.org,2002:bool") == 0) ||
-		(strcmp(tag, "tag:yaml.org,2002:null") == 0))
+		(strcmp(tag, "tag:yaml.org,2002:null") == 0) ||
+		(strcmp(tag, "tag:yaml.org,2002:str") == 0))
 	{
+		*can_omit_tag = 1;
 		return NULL;	
 	}
 	return (yaml_char_t*)tag;
 }
-#endif
+
+
+#if 0
 static void
 rb_yaml_guess_type_of_plain_node(yaml_node_t *node)
 {
@@ -225,6 +245,8 @@
 	} 
 }
 
+#endif
+
 static VALUE
 rb_yaml_resolver_initialize(VALUE self, SEL sel)
 {
@@ -246,7 +268,6 @@
 static int
 rb_yaml_bytestring_output_handler(void *bs, unsigned char *buffer, size_t size)
 {
-	printf("okay, in bytestring output_hanlder\n");
 	CFMutableDataRef data = rb_bytestring_wrapped_data((VALUE)bs);
 	CFDataAppendBytes(data, (const UInt8*)buffer, (CFIndex)size);
 	return 1;
@@ -314,7 +335,7 @@
 	yaml_emitter_emit(emitter, &ev);
 	yaml_emitter_flush(emitter);
 	yaml_emitter_delete(emitter);
-	// XXX: cleanup here...
+	// XXX: more cleanup here...
 	return RYAMLEmitter(self)->output;
 }
 
@@ -361,7 +382,7 @@
 }
 
 static VALUE
-rb_yaml_emitter_mapping(VALUE self, SEL sel, VALUE style, VALUE taguri)
+rb_yaml_emitter_mapping(VALUE self, SEL sel, VALUE taguri, VALUE style)
 {
 	yaml_event_t ev;
 	yaml_emitter_t *emitter = RYAMLEmitter(self)->emitter;
@@ -384,10 +405,10 @@
 {
 	yaml_event_t ev;
 	yaml_emitter_t *emitter = RYAMLEmitter(self)->emitter;
-	yaml_char_t *tag = (yaml_char_t*)RSTRING_PTR(taguri);
 	yaml_char_t *output = (yaml_char_t*)RSTRING_PTR(val);
-	
-	yaml_scalar_event_initialize(&ev, NULL, tag, output, RSTRING_LEN(val), 1, 1, YAML_ANY_SCALAR_STYLE);
+	int can_omit_tag = 0;
+	yaml_char_t *tag = rb_yaml_tag_or_null(taguri, &can_omit_tag);
+	yaml_scalar_event_initialize(&ev, NULL, tag, output, RSTRING_LEN(val), can_omit_tag, 0, rb_symbol_to_scalar_style(style));
 	yaml_emitter_emit(emitter, &ev);
 	yaml_emitter_flush(emitter);
 	
@@ -425,6 +446,8 @@
 void
 Init_libyaml()
 {
+	id_plain = rb_intern("plain");
+	id_quote2 = rb_intern("quote2");
 	id_tags_ivar = rb_intern("@tags");
 	
 	rb_mYAML = rb_define_module("YAML");

Modified: MacRuby/branches/experimental/lib/yaml/rubytypes.rb
===================================================================
--- MacRuby/branches/experimental/lib/yaml/rubytypes.rb	2009-08-05 20:20:47 UTC (rev 2221)
+++ MacRuby/branches/experimental/lib/yaml/rubytypes.rb	2009-08-05 20:20:48 UTC (rev 2222)
@@ -28,18 +28,14 @@
     self.instance_variables.sort
   end
   
-  def to_yaml(out = YAML::LibYAML::QuickEmitter.new)
-    if out.respond_to? :map
+  def to_yaml(output)
+    YAML::quick_emit(output) do |out|
       out.map(taguri, to_yaml_style) do |map|
         to_yaml_properties.each do |m|
           # The [1..-1] strips the @ from the variable name.
           out.add(m[1..-1], instance_variable_get(m))
         end
       end
-    elsif out.respond_to? :add
-      to_yaml_properties.each do |m|
-        out.add(m[1..-1], instance_variable_get(m))
-      end
     end
   end
 end
@@ -47,8 +43,8 @@
 class String
   yaml_as "tag:yaml.org,2002:str"
   
-  def to_yaml(doc = nil)
-    YAML::quick_emit(self, doc) do |out|
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
       out.scalar(taguri, self, self =~ /^:/ ? :quote2 : nil)
     end
   end
@@ -56,8 +52,8 @@
 
 class Exception
   yaml_as "tag:ruby.yaml.org,2002:exception"
-  def to_yaml(doc = nil)
-    YAML::quick_emit(self, doc) do |out|
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
       out.map(taguri, to_yaml_style) do |map|
         map.add('message', message)
       end
@@ -68,8 +64,8 @@
 class Array
   yaml_as "tag:yaml.org,2002:seq"
   
-  def to_yaml(doc = nil)
-    YAML::quick_emit(self, doc) do |out|
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
       out.seq(taguri, to_yaml_style) do |seq|
         each { |i| seq.add(i) }
       end
@@ -80,8 +76,8 @@
 class Hash
   yaml_as "tag:yaml.org,2002:map"
   
-  def to_yaml(doc = nil)
-    YAML::quick_emit(self, doc) do |out|
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
       out.map(taguri, to_yaml_style) do |map| 
         each { |k,v| map.add(k,v) }
       end
@@ -96,8 +92,8 @@
     val.to_i
   end
   
-	def to_yaml(doc = nil)
-	  YAML::quick_emit(self, doc) do |out|
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
       out.scalar( "tag:yaml.org,2002:int", self.to_s, :plain )
     end
 	end
@@ -110,7 +106,7 @@
     val.to_f
   end
   
-  def to_yaml(doc = nil)
+  def to_yaml(output = nil)
     str = self.to_s
     if str == "Infinity"
       str = ".Inf"
@@ -119,7 +115,7 @@
     elsif str == "NaN"
       str = ".NaN"
     end
-    YAML::quick_emit(self, doc) do |out|
+    YAML::quick_emit(output) do |out|
       out.scalar("tag:yaml.org,2002:float", str, :plain)
     end
   end
@@ -130,8 +126,8 @@
   
   def self.yaml_new(val); val[1..-1].to_sym; end
   
-  def to_yaml(doc = nil)
-    YAML::quick_emit(self, doc) do |out|
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
       out.scalar(taguri, self.inspect, :plain)
     end
   end
@@ -144,8 +140,8 @@
     Range.new(attrs['begin'], attrs['end'], attrs['excl'])
   end
   
-  def to_yaml(doc=nil)
-    YAML::quick_emit(self, doc) do |out|
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
       out.map(taguri, to_yaml_style) do |map|
         map.add('begin', self.begin)
         map.add('end', self.end)
@@ -157,8 +153,8 @@
 
 class Regexp
   yaml_as "tag:ruby.yaml.org,2002:regexp"
-  def to_yaml(doc = nil)
-    YAML::quick_emit(self, doc) do |out|
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
       out.scalar(taguri, self.inspect, :plain)
     end
 	end
@@ -169,8 +165,8 @@
   
   def self.yaml_new(val); nil; end
   
-	def to_yaml(doc = nil)
-	  YAML::quick_emit(self, doc) do |out|
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
       out.scalar(taguri, "", :plain)
     end
 	end
@@ -181,8 +177,8 @@
   
   def self.yaml_new(val); true; end
   
-  def to_yaml(doc = nil)
-    YAML::quick_emit(self, doc) do |out|
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
       out.scalar(taguri, "true", :plain)
     end
   end
@@ -193,8 +189,8 @@
   
   def self.yaml_new(val); false; end
   
-  def to_yaml(doc = nil)
-    YAML::quick_emit(self, doc) do |out|
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
       out.scalar(taguri, "false", :plain)
     end
   end

Modified: MacRuby/branches/experimental/lib/yaml.rb
===================================================================
--- MacRuby/branches/experimental/lib/yaml.rb	2009-08-05 20:20:47 UTC (rev 2221)
+++ MacRuby/branches/experimental/lib/yaml.rb	2009-08-05 20:20:48 UTC (rev 2222)
@@ -20,7 +20,7 @@
   end
   
   def YAML.dump(obj, io=nil)
-    obj.to_yaml(nil)
+    obj.to_yaml(io)
   end
   
   def YAML.load(io)
@@ -40,15 +40,14 @@
     File.open(path) { |f| parse(f) }
   end
   
-  def YAML.quick_emit(obj, out=nil, &block)
-    if(out)
+  def YAML.quick_emit(out, &block)
+    if out.is_a? LibYAML::Emitter
       yield(out)
     else
-      LibYAML::Emitter.new.stream do |stream|
+      LibYAML::Emitter.new(out).stream do |stream|
         stream.document { |doc| yield(doc) }
       end
     end
-    
   end
 end
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090805/99432340/attachment-0001.html>


More information about the macruby-changes mailing list