Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support for Java YAML.

git-svn-id: http://svn.codehaus.org/jruby/branches/yaml@2322 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information...
commit 97c465bdf37cad94148801450a32238f2a4011f5 1 parent d540583
@olabini olabini authored
Showing with 1,040 additions and 5,460 deletions.
  1. BIN  lib/jvyaml.jar
  2. +0 −383 lib/ruby/site_ruby/1.8/rbyaml.rb
  3. +0 −112 lib/ruby/site_ruby/1.8/rbyaml/composer.rb
  4. +0 −417 lib/ruby/site_ruby/1.8/rbyaml/constructor.rb
  5. +0 −36 lib/ruby/site_ruby/1.8/rbyaml/dumper.rb
  6. +0 −1,101 lib/ruby/site_ruby/1.8/rbyaml/emitter.rb
  7. +0 −75 lib/ruby/site_ruby/1.8/rbyaml/error.rb
  8. +0 −117 lib/ruby/site_ruby/1.8/rbyaml/events.rb
  9. +0 −40 lib/ruby/site_ruby/1.8/rbyaml/loader.rb
  10. +0 −56 lib/ruby/site_ruby/1.8/rbyaml/nodes.rb
  11. +0 −632 lib/ruby/site_ruby/1.8/rbyaml/parser.rb
  12. +0 −127 lib/ruby/site_ruby/1.8/rbyaml/reader.rb
  13. +0 −273 lib/ruby/site_ruby/1.8/rbyaml/representer.rb
  14. +0 −163 lib/ruby/site_ruby/1.8/rbyaml/resolver.rb
  15. +0 −1,343 lib/ruby/site_ruby/1.8/rbyaml/scanner.rb
  16. +0 −117 lib/ruby/site_ruby/1.8/rbyaml/serializer.rb
  17. +0 −56 lib/ruby/site_ruby/1.8/rbyaml/test.rb
  18. +0 −204 lib/ruby/site_ruby/1.8/rbyaml/tokens.rb
  19. +0 −38 lib/ruby/site_ruby/1.8/rbyaml/util.rb
  20. +0 −136 lib/ruby/site_ruby/1.8/rbyaml/yaml.rb
  21. +28 −28 src/builtin/yaml.rb
  22. +2 −0  src/org/jruby/Ruby.java
  23. +621 −0 src/org/jruby/RubyYAML.java
  24. +43 −0 src/org/jruby/libraries/YamlLibrary.java
  25. +60 −0 src/org/jruby/util/IOWriter.java
  26. +37 −6 src/org/jruby/yaml/JRubyConstructor.java
  27. +120 −0 src/org/jruby/yaml/JRubyRepresenter.java
  28. +50 −0 src/org/jruby/yaml/JRubySerializer.java
  29. +78 −0 test/testYAML.rb
  30. +1 −0  test/test_index
View
BIN  lib/jvyaml.jar
Binary file not shown
View
383 lib/ruby/site_ruby/1.8/rbyaml.rb
@@ -1,383 +0,0 @@
-
-require 'rbyaml/yaml'
-
-module RbYAML
- def self.dump(obj, io = nil)
- _dump(obj,io)
- end
-
- def self.load( io )
- _load(io)
- end
-
- def self.load_file( filepath )
- File.open( filepath ) do |f|
- load( f )
- end
- end
-
- # this operation does not make sense in RbYAML (right now)
- def self.parse( io )
- # yp = @@parser.new( :Model => :Generic ).load( io )
- end
-
- # this operation does not make sense in RbYAML (right now)
- def self.parse_file( filepath )
- # File.open( filepath ) do |f|
- # parse( f )
- # end
- end
-
- def self.each_document( io, &block )
- _load_all(io,&block)
- end
-
- def self.load_documents( io, &doc_proc )
- each_document( io, &doc_proc )
- end
-
- # this operation does not make sense in RbYAML (right now)
- def self.each_node( io, &doc_proc )
- # yp = @@parser.new( :Model => :Generic ).load_documents( io, &doc_proc )
- end
-
- # this operation does not make sense in RbYAML (right now)
- def self.parse_documents( io, &doc_proc )
- # YAML.each_node( io, &doc_proc )
- end
-
- def self.load_stream( io )
- d = nil
- load_documents(io) { |doc|
- d = Stream.new( nil ) if not d
- d.add( doc )
- }
- d
- end
-
- def self.dump_stream( *objs )
- d = RbYAML::Stream.new
- objs.each do |doc|
- d.add( doc )
- end
- d.emit
- end
-
-
- def self.add_builtin_ctor(type_tag, &transfer_proc)
- BaseConstructor::add_constructor("tag:yaml.org,2002:#{ type_tag }",transfer_proc)
- end
-
- # this operation does not make sense in RbYAML (right now)
- def self.add_domain_type( domain, type_re, &transfer_proc )
- # @@loader.add_domain_type( domain, type_re, &transfer_proc )
- end
-
- # this operation does not make sense in RbYAML (right now)
- def self.add_builtin_type( type_re, &transfer_proc )
- # @@loader.add_builtin_type( type_re, &transfer_proc )
- end
-
- # this operation does not make sense in RbYAML (right now)
- def self.add_ruby_type( type_tag, &transfer_proc )
- # @@loader.add_ruby_type( type, &transfer_proc )
- end
-
- # this operation does not make sense in RbYAML (right now)
- def self.add_private_type( type_re, &transfer_proc )
- # @@loader.add_private_type( type_re, &transfer_proc )
- end
-
- def self.detect_implicit( val )
- SimpleDetector.detect(val)
- end
-
- # this operation does not make sense in RbYAML (right now)
- def self.transfer( type_id, obj )
- # @@loader.transfer( type_id, obj )
- end
-
- # this operation does not make sense in RbYAML (right now)
- def self.try_implicit( obj )
- # YAML.transfer( YAML.detect_implicit( obj ), obj )
- end
-
- def self.read_type_class( type, obj_class )
- scheme, domain, type, tclass = type.split( ':', 4 )
- tclass.split( "::" ).each { |c| obj_class = obj_class.const_get( c ) } if tclass
- return [ type, obj_class ]
- end
-
- def self.object_maker( obj_class, val )
- if Hash === val
- o = obj_class.allocate
- val.each_pair { |k,v|
- o.instance_variable_set("@#{k}", v)
- }
- o
- else
- raise YAMLError, "Invalid object explicitly tagged !ruby/Object: " + val.inspect
- end
- end
-
- # this operation does not make sense in RbYAML (right now)
- def self.quick_emit( oid, opts = {}, &e )
- end
-
- # A dictionary of taguris which map to
- # Ruby classes.
- @@tagged_classes = {}
-
- #
- # Associates a taguri _tag_ with a Ruby class _cls_. The taguri is used to give types
- # to classes when loading YAML. Taguris are of the form:
- #
- # tag:authorityName,date:specific
- #
- # The +authorityName+ is a domain name or email address. The +date+ is the date the type
- # was issued in YYYY or YYYY-MM or YYYY-MM-DD format. The +specific+ is a name for
- # the type being added.
- #
- # For example, built-in YAML types have 'yaml.org' as the +authorityName+ and '2002' as the
- # +date+. The +specific+ is simply the name of the type:
- #
- # tag:yaml.org,2002:int
- # tag:yaml.org,2002:float
- # tag:yaml.org,2002:timestamp
- #
- # The domain must be owned by you on the +date+ declared. If you don't own any domains on the
- # date you declare the type, you can simply use an e-mail address.
- #
- # tag:why@ruby-lang.org,2004:notes/personal
- #
- def self.tag_class( tag, cls )
- if @@tagged_classes.has_key? tag
- warn "class #{ @@tagged_classes[tag] } held ownership of the #{ tag } tag"
- end
- @@tagged_classes[tag] = cls
- end
-
- # Returns the complete dictionary of taguris, paired with classes. The key for
- # the dictionary is the full taguri. The value for each key is the class constant
- # associated to that taguri.
- #
- # YAML.tagged_classes["tag:yaml.org,2002:int"] => Integer
- #
- def self.tagged_classes
- @@tagged_classes
- end
-
- #
- # RbYAML::Stream -- for emitting many documents
- #
- class Stream
- include Enumerable
-
- attr_accessor :documents, :options
- def initialize(opts = {})
- @options = opts
- @documents = []
- end
-
- def [](i)
- @documents[ i ]
- end
-
- def add(doc)
- @documents << doc
- end
-
- def edit(doc_num,doc)
- @documents[ doc_num ] = doc
- end
-
- def each(&block)
- @documents.each(&block)
- end
-
- def emit
-# TODO: implement
-
- opts = @options.dup
- opts[:UseHeader] = true if @documents.length > 1
- ct = 0
- out = Emitter.new( opts )
- @documents.each { |v|
- if ct > 0
- out << "\n--- "
- end
- v.to_yaml( :Emitter => out )
- ct += 1
- }
- out.end_object
- end
- end
-end
-
-if !Object.method_defined?(:to_yaml)
- class Module # :nodoc: all
- def yaml_as( tag, sc = true )
- class_eval <<-"end;", __FILE__, __LINE__+1
- attr_writer :taguri
- def taguri
- return @taguri if defined?(@taguri) and @taguri
- tag = #{ tag.dump }
- if self.class.yaml_tag_subclasses? and self.class != RbYAML::tagged_classes[tag]
- tag = "\#{ tag }:\#{ self.class.yaml_tag_class_name }"
- end
- tag
- end
- def self.yaml_tag_subclasses?; #{ sc ? 'true' : 'false' }; end
- end;
- RbYAML::tag_class tag, self
- end
- # Transforms the subclass name into a name suitable for display
- # in a subclassed tag.
- def yaml_tag_class_name
- self.name
- end
- # Transforms the subclass name found in the tag into a Ruby
- # constant name.
- def yaml_tag_read_class( name )
- name
- end
- end
-
- require 'date'
-
- class Class
- def to_yaml( opts = {} )
- raise RbYAML::TypeError, "can't dump anonymous class %s" % self.class
- end
- end
-
- class Object
- yaml_as "tag:ruby.yaml.org,2002:object"
- def is_complex_yaml?; true; end
- def to_yaml_style; end
- def to_yaml_properties; instance_variables.sort; end
- def to_yaml( opts = {} )
- RbYAML::_dump_ruby_object(self)
- end
- end
-
- class Hash
- yaml_as "tag:ruby.yaml.org,2002:hash"
- yaml_as "tag:yaml.org,2002:map"
- def is_complex_yaml?; true; end
- def yaml_initialize( tag, val )
- if Array === val
- update Hash.[]( *val ) # Convert the map to a sequence
- elsif Hash === val
- update val
- else
- raise RbYAML::TypeError, "Invalid map explicitly tagged #{ tag }: " + val.inspect
- end
- end
- end
-
- class Array
- yaml_as "tag:ruby.yaml.org,2002:array"
- yaml_as "tag:yaml.org,2002:seq"
- def is_complex_yaml?; true; end
- def yaml_initialize( tag, val ); concat( val.to_a ); end
- end
-
- class Exception
- yaml_as "tag:ruby.yaml.org,2002:exception"
- def Exception.yaml_new( klass, tag, val )
- o = RbYAML.object_maker( klass, { 'mesg' => val.delete( 'message' ) } )
- val.each_pair do |k,v|
- o.instance_variable_set("@#{k}", v)
- end
- o
- end
- end
-
- class String
- yaml_as "tag:ruby.yaml.org,2002:string"
- yaml_as "tag:yaml.org,2002:binary"
- yaml_as "tag:yaml.org,2002:str"
- def is_complex_yaml?
- to_yaml_style or not to_yaml_properties.empty? or self =~ /\n.+/
- end
- def is_binary_data?
- ( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 ) unless empty?
- end
- def String.yaml_new( klass, tag, val )
- val = val.unpack("m")[0] if tag == "tag:yaml.org,2002:binary"
- val = { 'str' => val } if String === val
- if Hash === val
- s = klass.allocate
- # Thank you, NaHi
- String.instance_method(:initialize).
- bind(s).
- call( val.delete( 'str' ) )
- val.each { |k,v| s.instance_variable_set( k, v ) }
- s
- else
- raise RbYAML::TypeError, "Invalid String: " + val.inspect
- end
- end
- end
-
- class Symbol
- yaml_as "tag:ruby.yaml.org,2002:symbol"
- yaml_as "tag:ruby.yaml.org,2002:sym"
- def is_complex_yaml?; false; end
- def Symbol.yaml_new( klass, tag, val )
- if String === val
- val.intern
- else
- raise RbYAML::TypeError, "Invalid Symbol: " + val.inspect
- end
- end
- end
-
- class Time
- yaml_as "tag:ruby.yaml.org,2002:time"
- yaml_as "tag:yaml.org,2002:timestamp"
- def is_complex_yaml?; false; end
- def Time.yaml_new( klass, tag, val )
- if Hash === val
- t = val.delete( 'at' )
- val.each { |k,v| t.instance_variable_set( k, v ) }
- t
- else
- raise RbYAML::TypeError, "Invalid Time: " + val.inspect
- end
- end
- end
-
- class Date
- yaml_as "tag:yaml.org,2002:timestamp#ymd"
- def is_complex_yaml?; false; end
- end
-
- class Numeric
- def is_complex_yaml?; false; end
- end
-
- class Fixnum
- yaml_as "tag:yaml.org,2002:int"
- end
-
- class Float
- yaml_as "tag:yaml.org,2002:float"
- end
-
- class TrueClass
- yaml_as "tag:yaml.org,2002:bool#yes"
- def is_complex_yaml?; false; end
- end
-
- class FalseClass
- yaml_as "tag:yaml.org,2002:bool#no"
- def is_complex_yaml?; false; end
- end
-
- class NilClass
- yaml_as "tag:yaml.org,2002:null"
- def is_complex_yaml?; false; end
- end
-end
View
112 lib/ruby/site_ruby/1.8/rbyaml/composer.rb
@@ -1,112 +0,0 @@
-require 'rbyaml/error'
-require 'rbyaml/events'
-require 'rbyaml/nodes'
-
-module RbYAML
- class ComposerError < MarkedYAMLError
- end
-
- class Composer
- def initialize(parser,resolver)
- @parser = parser
- @resolver = resolver
- @anchors = {}
- end
-
- def check_node
- !@parser.peek_event.__is_stream_end
- end
-
- def get_node
- compose_document if check_node
- end
-
- def each_node
- yield compose_document while check_node
- end
-
- def compose_document
- # Drop the STREAM-START event.
- @parser.get_event if @parser.peek_event.__is_stream_start
- # Drop the DOCUMENT-START event.
- @parser.get_event
- # Compose the root node.
- node = compose_node(nil,nil)
- # Drop the DOCUMENT-END event.
- @parser.get_event
- @anchors = {}
- node
- end
-
- def compose_node(parent,index)
- if @parser.peek_event.__is_alias
- event = @parser.get_event
- anchor = event.anchor
- raise ComposerError.new(nil, nil, "found undefined alias #{anchor}", event.start_mark) if !@anchors.include?(anchor)
- return @anchors[anchor]
- end
- event = @parser.peek_event
- anchor = event.anchor
- if !anchor.nil?
- if @anchors.include?(anchor)
- raise ComposerError.new("found duplicate anchor #{anchor}; first occurence", @anchors[anchor].start_mark,"second occurence", event.start_mark)
- end
- end
- @resolver.descend_resolver(parent,index)
- if @parser.peek_event.__is_scalar
- node = compose_scalar_node(anchor)
- elsif @parser.peek_event.__is_sequence_start
- node = compose_sequence_node(anchor)
- elsif @parser.peek_event.__is_mapping_start
- node = compose_mapping_node(anchor)
- end
- @resolver.ascend_resolver
- node
- end
-
- def compose_scalar_node(anchor)
- event = @parser.get_event
- tag = event.tag
- tag = @resolver.resolve(ScalarNode,event.value,event.implicit) if tag.nil? || tag == "!"
- node = ScalarNode.new(tag, event.value,event.start_mark, event.end_mark, event.style)
- @anchors[anchor] = node if !anchor.nil?
- node
- end
-
- def compose_sequence_node(anchor)
- start_event = @parser.get_event
- tag = start_event.tag
- tag = @resolver.resolve(SequenceNode,nil,start_event.implicit) if tag.nil? || tag == "!"
- node = SequenceNode.new(tag,[],start_event.start_mark,nil,start_event.flow_style)
- @anchors[anchor] = node if !anchor.nil?
- index = 0
- while !@parser.peek_event.__is_sequence_end
- node.value << compose_node(node,index)
- index += 1
- end
- end_event = @parser.get_event
- node.end_mark = end_event.end_mark
- node
- end
-
- def compose_mapping_node(anchor)
- start_event = @parser.get_event
- tag = start_event.tag
- tag = @resolver.resolve(MappingNode,nil,start_event.implicit) if tag.nil? || tag == "!"
- node = MappingNode.new(tag, {},start_event.start_mark,nil,start_event.flow_style)
- @anchors[anchor] = node if !anchor.nil?
- while !@parser.peek_event.__is_mapping_end
- key_event = @parser.peek_event
- item_key = compose_node(node,nil)
- if node.value.include?(item_key)
- raise ComposerError.new("while composing a mapping", start_event.start_mark,"found duplicate key", key_event.start_mark)
- end
- item_value = compose_node(node,item_key)
- node.value[item_key] = item_value
- end
- end_event = @parser.get_event
- node.end_mark = end_event.end_mark
- node
- end
- end
-end
View
417 lib/ruby/site_ruby/1.8/rbyaml/constructor.rb
@@ -1,417 +0,0 @@
-require 'base64'
-require 'set'
-
-require 'rbyaml/util'
-
-require 'rbyaml/error'
-require 'rbyaml/nodes'
-require 'rbyaml/composer'
-
-class Symbol
- def __call(obj,*args)
- obj.send(self,*args)
- end
-end
-
-class Proc
- def __call(obj,*args)
- call(obj,*args)
- end
-end
-
-class Method
- def __call(obj,*args)
- call(*args)
- end
-end
-
-
-module RbYAML
- class ConstructorError < MarkedYAMLError
- end
-
- class BaseConstructor
- @@yaml_constructors = {}
- @@yaml_multi_constructors = {}
- @@yaml_multi_regexps = {}
-
- def initialize(composer)
- @composer = composer
- @constructed_objects = {}
- @recursive_objects = {}
- end
-
- def check_data
- # If there are more documents available?
- @composer.check_node
- end
-
- def get_data
- # Construct and return the next document.
- construct_document(@composer.get_node) if @composer.check_node
- end
-
- def each_document
- # Iterator protocol.
- while @composer.check_node
- yield construct_document(@composer.get_node)
- end
- end
-
- def construct_document(node)
- data = construct_object(node)
- @constructed_objects = {}
- @recursive_objects = {}
- data
- end
-
- class RecursiveProxy
- attr_writer :value
- def method_missing(*args)
- @value.send(*args)
- end
- def class
- @value.class
- end
- def to_s
- @value.to_s
- end
- end
-
- def construct_object(node)
- return @constructed_objects[node] if @constructed_objects.include?(node)
- @constructed_objects[node] = RecursiveProxy.new
- constructor = @@yaml_constructors[node.tag]
- if !constructor
- ruby_cls = RbYAML::tagged_classes[node.tag]
- if ruby_cls && (ruby_cls.method_defined?(:yaml_initialize) || ruby_cls.respond_to?(:yaml_new))
- constructor = lambda { |obj,node| send(:construct_ruby_object,ruby_cls,node) }
- else
- through = true
- for tag_prefix,reg in @@yaml_multi_regexps
- if reg =~ node.tag
- tag_suffix = node.tag[tag_prefix.length..-1]
- constructor = lambda { |obj, node| @@yaml_multi_constructors[tag_prefix].__call(self,tag_suffix, node) }
- through = false
- break
- end
- end
- if through
- ctor = @@yaml_multi_constructors[nil]
- if ctor
- constructor = lambda { |obj, node| ctor.__call(self,node.tag,node) }
- else
- ctor = @@yaml_constructors[nil]
- if ctor
- constructor = lambda { |obj, node| ctor.__call(self,node)}
- else
- constructor = lambda { |obj, node| construct_primitive(node) }
- end
- end
- end
- end
- end
- data = constructor.__call(self,node)
- @constructed_objects[node].value = data
- @constructed_objects[node] = data
- data
- end
-
- def construct_primitive(node)
- if node.__is_scalar
- construct_scalar(node)
- elsif node.__is_sequence
- construct_sequence(node)
- elsif node.__is_mapping
- construct_mapping(node)
- else
- puts node.tag
- end
- end
-
- def construct_scalar(node)
- if !node.__is_scalar
- if node.__is_mapping
- for key_node in node.value.keys
- if key_node.tag == "tag:yaml.org,2002:value"
- return construct_scalar(node.value[key_node])
- end
- end
- end
- raise ConstructorError.new(nil, nil,"expected a scalar node, but found #{node.tid}",node.start_mark)
- end
- node.value
- end
-
- def construct_private_type(node)
-# construct_scalar(node)
- PrivateType.new(node.tag,node.value)
- end
-
- def construct_sequence(node)
- raise ConstructorError.new(nil,nil,"expected a sequence node, but found #{node.tid}",node.start_mark) if !node.__is_sequence
- node.value.map {|child| construct_object(child) }
- end
-
- def construct_mapping(node)
- raise ConstructorError.new(nil,nil,"expected a mapping node, but found #{node.tid}",node.start_mark) if !node.__is_mapping
- mapping = {}
- merge = nil
- for key_node,value_node in node.value
- if key_node.tag == "tag:yaml.org,2002:merge"
- raise ConstructorError.new("while constructing a mapping", node.start_mark,"found duplicate merge key", key_node.start_mark) if !merge.nil?
- if value_node.__is_mapping
- merge = [construct_mapping(value_node)]
- elsif value_node.__is_sequence
- merge = []
- for subnode in value_node.value
- if !subnode.__is_mapping
- raise ConstructorError.new("while constructing a mapping",node.start_mark,"expected a mapping for merging, but found #{subnode.tid}", subnode.start_mark)
- end
- merge.unshift(construct_mapping(subnode))
- end
- else
- raise ConstructorError.new("while constructing a mapping", node.start_mark,"expected a mapping or list of mappings for merging, but found #{value_node.tid}", value_node.start_mark)
- end
- elsif key_node.tag == "tag:yaml.org,2002:value"
- raise ConstructorError.new("while construction a mapping", node.start_mark,"found duplicate value key", key_node.start_mark) if mapping.include?("=")
- value = construct_object(value_node)
- mapping["="] = value
- else
- key = construct_object(key_node)
- value = construct_object(value_node)
- mapping[key] = value
-# raise ConstructorError.new("while constructing a mapping", node.start_mark,"found duplicate key", key_node.start_mark) if mapping.include?(key)
- end
- end
- if !merge.nil?
- merge << mapping
- mapping = { }
- for submapping in merge
- mapping.merge!(submapping)
- end
- end
- mapping
- end
-
- def construct_pairs(node)
- raise ConstructorError.new(nil,nil,"expected a mapping node, but found #{node.tid}",node.start_mark) if !node.__is_mapping
- node.value.collect {|key_node,value_node| [construct_object(key_node), construct_object(value_node)] }
- end
-
- def self.add_constructor(tag, constructor)
- @@yaml_constructors[tag] = constructor
- end
-
- def self.add_multi_constructor(tag_prefix, multi_constructor)
- @@yaml_multi_constructors[tag_prefix] = multi_constructor
- @@yaml_multi_regexps[tag_prefix] = Regexp.new("^"+Regexp.escape(tag_prefix))
- end
- end
-
- class SafeConstructor < BaseConstructor
- def construct_yaml_null(node)
- construct_scalar(node)
- nil
- end
-
- BOOL_VALUES = {
- "y" => true,
- "n" => false,
- "yes" => true,
- "no" => false,
- "true" => true,
- "false" => false,
- "on" => true,
- "off" => false
- }
-
- def construct_yaml_bool(node)
- value = construct_scalar(node)
- BOOL_VALUES[value.downcase]
- end
-
- def construct_yaml_int(node)
- value = construct_scalar(node).to_s
- value = value.gsub(/_/, '')
- sign = +1
- first = value[0]
- if first == ?-
- sign = -1
- value.slice!(0)
- elsif first == ?+
- value.slice!(0)
- end
- base = 10
- if value == "0"
- return 0
- elsif value[0..1] == "0b"
- value.slice!(0..1)
- base = 2
- elsif value[0..1] == "0x"
- value.slice!(0..1)
- base = 16
- elsif value[0] == ?0
- value.slice!(0)
- base = 8
- elsif value.include?(?:)
- digits = (value.split(/:/).map {|val| val.to_i}).reverse
- base = 1
- value = 0
- for digit in digits
- value += digit*base
- base *= 60
- end
- return sign*value
- else
- return sign*value.to_i
- end
- return sign*value.to_i(base)
- end
-
- INF_VALUE = +1.0/0.0
- NAN_VALUE = 0.0/0.0
-
- def construct_yaml_float(node)
- value = construct_scalar(node).to_s
- value = value.gsub(/_/, '')
- sign = +1
- first = value[0]
- if first == ?-
- sign = -1
- value.slice!(0)
- elsif first == ?+
- value.slice!(0)
- end
- if value.downcase == ".inf"
- return sign*INF_VALUE
- elsif value.downcase == ".nan"
- return NAN_VALUE
- elsif value.include?(?:)
- digits = (value.split(/:/).map {|val| val.to_f}).reverse
- base = 1
- value = 0.0
- for digit in digits
- value += digit*base
- base *= 60
- end
- return sign*value
- else
- return value.to_f
- end
- end
-
- def construct_yaml_binary(node)
- value = construct_scalar(node)
- Base64.decode64(value.split(/[\n\x85]|(?:\r[^\n])/).to_s)
- end
-
- TIMESTAMP_REGEXP = /^([0-9][0-9][0-9][0-9])-([0-9][0-9]?)-([0-9][0-9]?)(?:(?:[Tt]|[ \t]+)([0-9][0-9]?):([0-9][0-9]):([0-9][0-9])(?:\.([0-9]*))?(?:[ \t]*(?:Z|([-+][0-9][0-9]?)(?::([0-9][0-9])?)?))?)?$/
-
- def construct_yaml_timestamp(node)
- unless (match = TIMESTAMP_REGEXP.match(node.value))
- return construct_private_type(node)
- end
- values = match.captures.map {|val| val.to_i}
- fraction = values[6]
- if fraction != 0
- fraction *= 10 while 10*fraction < 1000
- values[6] = fraction
- end
- stamp = Time.gm(values[0],values[1],values[2],values[3],values[4],values[5],values[6])
-
- diff = values[7] * 3600 + values[8] * 60
- return stamp-diff
- end
-
- def construct_yaml_omap(node)
- # Note: we do not check for duplicate keys, because its too
- # CPU-expensive.
- raise ConstructorError.new("while constructing an ordered map", node.start_mark,
- "expected a sequence, but found #{node.tid}", node.start_mark) if !node.__is_sequence
- omap = []
- for subnode in node.value
- raise ConstructorError.new("while constructing an ordered map", node.start_mark,
- "expected a mapping of length 1, but found #{subnode.tid}",subnode.start_mark) if !subnode.__is_mapping
- raise ConstructorError.new("while constructing an ordered map", node.start_mark,
- "expected a single mapping item, but found #{subnode.value.length} items",subnode.start_mark) if subnode.value.length != 1
- key_node = subnode.value.keys[0]
- key = construct_object(key_node)
- value = construct_object(subnode.value[key_node])
- omap << [key, value]
- end
- omap
- end
-
- def construct_yaml_pairs(node)
- construct_yaml_omap(node)
- end
-
- def construct_yaml_set(node)
- Set.new(construct_mapping(node).keys)
- end
-
- def construct_yaml_str(node)
- val = construct_scalar(node).to_s
- val.empty? ? nil : val
- end
-
- def construct_yaml_seq(node)
- construct_sequence(node)
- end
-
- def construct_yaml_map(node)
- construct_mapping(node)
- end
-
- def construct_yaml_object(node, cls)
- mapping = construct_mapping(node)
- data = cls.new
- mapping.each {|key,val| data.instance_variable_set("@#{key}",val)}
- data
- end
-
- def construct_undefined(node)
- raise ConstructorError.new(nil,nil,"could not determine a constructor for the tag #{node.tag}",node.start_mark)
- end
-
- def construct_ruby_object(cls,node)
- val = construct_primitive(node)
- if cls.respond_to?(:yaml_new)
- obj = cls.yaml_new(cls,node.tag,val)
- else
- obj = cls.allocate
- obj.yaml_initialize(node.tag,val)
- end
- obj
- end
-
- def construct_ruby(tag,node)
- obj_class = Object
- tag.split( "::" ).each { |c| obj_class = obj_class.const_get( c ) } if tag
- o = obj_class.allocate
- mapping = construct_mapping(node)
- mapping.each {|key,val| o.instance_variable_set("@#{key}",val)}
- o
- end
- end
-
- SafeConstructor::add_constructor('tag:yaml.org,2002:null',:construct_yaml_null)
- BaseConstructor::add_constructor('tag:yaml.org,2002:bool',:construct_yaml_bool)
- BaseConstructor::add_constructor('tag:yaml.org,2002:int',:construct_yaml_int)
- BaseConstructor::add_constructor('tag:yaml.org,2002:float',:construct_yaml_float)
- BaseConstructor::add_constructor('tag:yaml.org,2002:binary',:construct_yaml_binary)
- BaseConstructor::add_constructor('tag:yaml.org,2002:timestamp',:construct_yaml_timestamp)
- BaseConstructor::add_constructor('tag:yaml.org,2002:omap',:construct_yaml_omap)
- BaseConstructor::add_constructor('tag:yaml.org,2002:pairs',:construct_yaml_pairs)
- BaseConstructor::add_constructor('tag:yaml.org,2002:set',:construct_yaml_set)
- BaseConstructor::add_constructor('tag:yaml.org,2002:str',:construct_yaml_str)
- BaseConstructor::add_constructor('tag:yaml.org,2002:seq',:construct_yaml_seq)
- BaseConstructor::add_constructor('tag:yaml.org,2002:map',:construct_yaml_map)
- BaseConstructor::add_constructor(nil,:construct_private_type)
-
- BaseConstructor::add_multi_constructor("!ruby/object:",:construct_ruby)
-
- class Constructor < SafeConstructor
- end
-end
View
36 lib/ruby/site_ruby/1.8/rbyaml/dumper.rb
@@ -1,36 +0,0 @@
-require 'rbyaml/emitter'
-require 'rbyaml/serializer'
-require 'rbyaml/representer'
-require 'rbyaml/resolver'
-
-module RbYAML
- class CommonDumper
- attr_accessor :emitter, :serializer, :representer, :resolver
- def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil,emitter=Emitter,serializer=Serializer,representer=Representer,resolver=Resolver)
- super()
- @emitter = emitter.new(stream,canonical,indent,width,line_break)
- @resolver = resolver.new
- @serializer = serializer.new(@emitter,@resolver,explicit_start,explicit_end,version,tags)
- @representer = representer.new(@serializer,default_style,default_flow_style)
- end
- end
-
- class BaseDumper < CommonDumper
- attr_accessor
- def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil,emitter=Emitter,serializer=Serializer,representer=BaseRepresenter,resolver=BaseResolver)
- super
- end
- end
-
- class SafeDumper < CommonDumper
- def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil,emitter=Emitter,serializer=Serializer,representer=SafeRepresenter,resolver=Resolver)
- super
- end
- end
-
- class Dumper < CommonDumper
- def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil,emitter=Emitter,serializer=Serializer,representer=Representer,resolver=Resolver)
- super
- end
- end
-end
View
1,101 lib/ruby/site_ruby/1.8/rbyaml/emitter.rb
@@ -1,1101 +0,0 @@
-# Emitter expects events obeying the following grammar:
-# stream ::= STREAM-START document* STREAM-END
-# document ::= DOCUMENT-START node DOCUMENT-END
-# node ::= SCALAR | sequence | mapping
-# sequence ::= SEQUENCE-START node* SEQUENCE-END
-# mapping ::= MAPPING-START (node node)* MAPPING-END
-
-require 'rbyaml/error'
-require 'rbyaml/events'
-
-module RbYAML
- class EmitterError < YAMLError
- end
-
- ScalarAnalysis = Struct.new(:scalar,:empty,:multiline,:allow_flow_plain,:allow_block_plain,:allow_single_quoted,:allow_double_quoted,:allow_block)
-
- class Emitter
- DEFAULT_TAG_PREFIXES = {
- "!" => "!",
- "tag:yaml.org,2002:" => "!!"
- }
-
- def initialize(stream, canonical=nil, indent=nil, width=nil,line_break=nil)
- # The stream should have the methods `write` and possibly `flush`.
- @stream = stream
-
- # Emitter is a state machine with a stack of states to handle nested
- # structures.
- @states = []
- @state = :expect_stream_start
-
- # Current event and the event queue.
- @events = []
- @event = nil
-
- # The current indentation level and the stack of previous indents.
- @indents = []
- @indent = nil
-
- # Flow level.
- @flow_level = 0
-
- # Contexts.
- @root_context = false
- @sequence_context = false
- @mapping_context = false
- @simple_key_context = false
-
- # Characteristics of the last emitted character:
- # - current position.
- # - is it a whitespace?
- # - is it an indention character
- # (indentation space, '-', '?', or ':')?
- @line = 0
- @column = 0
- @whitespace = true
- @indention = true
-
- # Formatting details.
- @canonical = canonical
- @best_indent = 2
- @best_indent = indent if indent && indent!=0 && 1 < indent && indent < 10
-
- @best_width = 80
- @best_width = width if width && width != 0 && width > @best_indent*2
-
- @best_line_break = "\n"
- @best_line_break = line_break if ["\r", "\n", "\r\n"].include?(line_break)
-
- # Tag prefixes.
- @tag_prefixes = nil
-
- # Prepared anchor and tag.
- @prepared_anchor = nil
- @prepared_tag = nil
-
- # Scalar analysis and style.
- @analysis = nil
- @style = nil
- end
-
- def emit(event)
- @events << event
- while !need_more_events
- @event = @events.shift
- send(@state)
- @event = nil
- end
- end
-
- # In some cases, we wait for a few next events before emitting.
-
- def need_more_events
- return true if @events.empty?
- event = @events.first
- if DocumentStartEvent === event
- need_events(1)
- elsif SequenceStartEvent === event
- need_events(2)
- elsif MappingStartEvent === event
- need_events(3)
- else
- false
- end
- end
-
- def need_events(count)
- level = 0
- for event in @events[1..-1]
- if DocumentStartEvent === event || CollectionStartEvent === event
- level += 1
- elsif DocumentEndEvent === event || CollectionEndEvent === event
- level -= 1
- elsif StreamEndEvent === event
- level = -1
- end
- if level < 0
- return false
- end
- end
- @events.length < count+1
- end
-
- def increase_indent(flow=false, indentless=false)
- @indents << @indent
- if @indent.nil?
- if flow
- @indent = @best_indent
- else
- @indent = 0
- end
- elsif !indentless
- @indent += @best_indent
- end
- end
-
- # States.
-
- # Stream handlers.
-
- def expect_stream_start
- if StreamStartEvent === @event
- write_stream_start
- @state = :expect_first_document_start
- else
- raise EmitterError.new("expected StreamStartEvent, but got #{@event}")
- end
- end
-
- def expect_nothing
- raise EmitterError.new("expected nothing, but got #{@event}")
- end
-
- # Document handlers.
-
- def expect_first_document_start
- expect_document_start(true)
- end
-
- def expect_document_start(first=false)
- if DocumentStartEvent === @event
- if @event.version
- version_text = prepare_version(@event.version)
- write_version_directive(version_text)
- end
- @tag_prefixes = Emitter::DEFAULT_TAG_PREFIXES.dup
- if @event.tags
- handles = @event.tags.keys
- handles.sort!
- for handle in handles
- prefix = @event.tags[handle]
- @tag_prefixes[prefix] = handle
- handle_text = prepare_tag_handle(handle)
- prefix_text = prepare_tag_prefix(prefix)
- write_tag_directive(handle_text, prefix_text)
- end
- end
- implicit = first && !@event.explicit && !@canonical && !@event.version && !@event.tags && !check_empty_document
- if !implicit
- write_indent
- write_indicator("--- ",true,true)
- if @canonical
- write_indent
- end
- end
- @state = :expect_document_root
- elsif StreamEndEvent === @event
- write_stream_end
- @state = :expect_nothing
- else
- raise EmitterError.new("expected DocumentStartEvent, but got #{@event}")
- end
- end
-
- def expect_document_end
- if DocumentEndEvent === @event
- write_indent
- if @event.explicit
- write_indicator("...", true)
- write_indent
- end
- flush_stream
- @state = :expect_document_start
- else
- raise EmitterError.new("expected DocumentEndEvent, but got #{@event}")
- end
- end
-
- def expect_document_root
- @states << :expect_document_end
- expect_node(true)
- end
-
- # Node handlers.
-
- def expect_node(root=false, sequence=false, mapping=false, simple_key=false)
- @root_context = root
- @sequence_context = sequence
- @mapping_context = mapping
- @simple_key_context = simple_key
- if AliasEvent === @event
- expect_alias
- elsif ScalarEvent === @event || CollectionStartEvent === @event
- process_anchor("&")
- process_tag
- if ScalarEvent === @event
- expect_scalar
- elsif SequenceStartEvent === @event
- if @flow_level!=0 || @canonical || @event.flow_style || check_empty_sequence
- expect_flow_sequence
- else
- expect_block_sequence
- end
- elsif MappingStartEvent === @event
- if @flow_level!=0 || @canonical || @event.flow_style || check_empty_mapping
- expect_flow_mapping
- else
- expect_block_mapping
- end
- end
- else
- raise EmitterError.new("expected NodeEvent, but got #{@event}")
- end
- end
-
- def expect_alias
- raise EmitterError.new("anchor is not specified for alias") if @event.anchor.nil?
- process_anchor("*")
- @state = @states.pop
- end
-
- def expect_scalar
- increase_indent(true)
- process_scalar
- @indent = @indents.pop
- @state = @states.pop
- end
-
- # Flow sequence handlers.
-
- def expect_flow_sequence
- write_indicator("[", true, true)
- @flow_level += 1
- increase_indent(true)
- @state = :expect_first_flow_sequence_item
- end
-
- def expect_first_flow_sequence_item
- if SequenceEndEvent === @event
- @indent = @indents.pop
- @flow_level -= 1
- write_indicator("]", false)
- @state = @states.pop
- else
- write_indent if @canonical || @column > @best_width
- @states << :expect_flow_sequence_item
- expect_node(false,true)
- end
- end
-
- def expect_flow_sequence_item
- if SequenceEndEvent === @event
- @indent = @indents.pop
- @flow_level -= 1
- if @canonical
- write_indicator(",",false)
- write_indent
- end
- write_indicator("]",false)
- @state = @states.pop
- else
- write_indicator(",", false)
- write_indent if @canonical or @column > @best_width
- @states << :expect_flow_sequence_item
- expect_node(false,true)
- end
- end
-
- # Flow mapping handlers.
-
- def expect_flow_mapping
- write_indicator("{", true, true)
- @flow_level += 1
- increase_indent(true)
- @state = :expect_first_flow_mapping_key
- end
-
- def expect_first_flow_mapping_key
- if MappingEndEvent === @event
- @indent = @indents.pop
- @flow_level -= 1
- write_indicator("}", false)
- @state = @states.pop
- else
- write_indent if @canonical || @column > @best_width
- if !@canonical && check_simple_key
- @states << :expect_flow_mapping_simple_value
- expect_node(false,false,true,true)
- else
- write_indicator("?", true)
- @states << :expect_flow_mapping_value
- expect_node(false,false,true)
- end
- end
- end
-
- def expect_flow_mapping_key
- if MappingEndEvent === @event
- @indent = @indents.pop
- @flow_level -= 1
- if @canonical
- write_indicator(",", false)
- write_indent
- end
- write_indicator("}", false)
- @state = @states.pop
- else
- write_indicator(",", false)
- write_indent if @canonical || @column > @best_width
- if !@canonical && check_simple_key
- @states << :expect_flow_mapping_simple_value
- expect_node(false,false,true,true)
- else
- write_indicator("?", true)
- @states << :expect_flow_mapping_value
- expect_node(false,false,true)
- end
- end
- end
-
- def expect_flow_mapping_simple_value
- write_indicator(": ", false,true)
- @states << :expect_flow_mapping_key
- expect_node(false,false,true)
- end
-
- def expect_flow_mapping_value
- write_indent if @canonical || @column > @best_width
- write_indicator(": ", false,true)
- @states << :expect_flow_mapping_key
- expect_node(false,false,true)
- end
-
- # Block sequence handlers.
-
- def expect_block_sequence
- indentless = @mapping_context && !@indention
- increase_indent(false,indentless)
- @state = :expect_first_block_sequence_item
- end
-
- def expect_first_block_sequence_item
- expect_block_sequence_item(true)
- end
-
- def expect_block_sequence_item(first=false)
- if !first && SequenceEndEvent === @event
- @indent = @indents.pop
- @state = @states.pop
- else
- write_indent
- write_indicator("-", true, false, true)
- @states << :expect_block_sequence_item
- expect_node(false,true)
- end
- end
-
- # Block mapping handlers.
-
- def expect_block_mapping
- increase_indent(false)
- @state = :expect_first_block_mapping_key
- end
-
- def expect_first_block_mapping_key
- expect_block_mapping_key(true)
- end
-
- def expect_block_mapping_key(first=false)
- if !first && MappingEndEvent === @event
- @indent = @indents.pop
- @state = @states.pop
- else
- write_indent
- if check_simple_key
- @states << :expect_block_mapping_simple_value
- expect_node(false,false,true,true)
- else
- write_indicator("?", true, false, true)
- @states << :expect_block_mapping_value
- expect_node(false,false,true)
- end
- end
- end
-
- def expect_block_mapping_simple_value
- write_indicator(": ", false,true)
- @states << :expect_block_mapping_key
- expect_node(false,false,true)
- end
-
- def expect_block_mapping_value
- write_indent
- write_indicator(": ",true,true,true)
- @states << :expect_block_mapping_key
- expect_node(false,false,true)
- end
-
- # Checkers.
-
- def check_empty_sequence
- @event.__is_sequence_start && !@events.empty? && @events.first.__is_sequence_end
- end
-
- def check_empty_mapping
- @event.__is_mapping_start && !@events.empty? && @events.first.__is_mapping_end
- end
-
- def check_empty_document
- return false if !@event.__is_document_start || @events.empty?
- event = @events.first
- event.__is_scalar && event.anchor.nil? && event.tag.nil? && event.implicit && event.value == ""
- end
-
- def check_simple_key
- length = 0
- if @event.__is_node && !@event.anchor.nil?
- @prepared_anchor = prepare_anchor(@event.anchor) if @prepared_anchor.nil?
- length += @prepared_anchor.length
- end
- if (@event.__is_scalar || @event.__is_collection_start) && !@event.tag.nil?
- @prepared_tag = prepare_tag(@event.tag) if @prepared_tag.nil?
- length += @prepared_tag.length
- end
- if @event.__is_scalar
- @analysis = analyze_scalar(@event.value) if @analysis.nil?
- length += @analysis.scalar.length
- end
-
- (length < 128 && (@event.__is_alias || (@event.__is_scalar && !@analysis.empty && !@analysis.multiline) ||
- check_empty_sequence || check_empty_mapping))
- end
-
-
- # Anchor, Tag, and Scalar processors.
-
- def process_anchor(indicator)
- if @event.anchor.nil?
- @prepared_anchor = nil
- return nil
- end
- @prepared_anchor = prepare_anchor(@event.anchor) if @prepared_anchor.nil?
- write_indicator(indicator+@prepared_anchor, true) if @prepared_anchor && !@prepared_anchor.empty?
- @prepared_anchor = nil
- end
-
- def process_tag
- tag = @event.tag
- if ScalarEvent === @event
- @style = choose_scalar_style if @style.nil?
- if ((!@canonical || tag.nil?) && ((@style == "" && @event.implicit[0]) || (@style != "" && @event.implicit[1])))
- @prepared_tag = nil
- return
- end
- if @event.implicit[0] && tag.nil?
- tag = "!"
- @prepared_tag = nil
- end
- else
- if (!@canonical || tag.nil?) && @event.implicit
- @prepared_tag = nil
- return
- end
- end
- raise EmitterError.new("tag is not specified") if tag.nil?
- @prepared_tag = prepare_tag(tag) if @prepared_tag.nil?
- write_indicator(@prepared_tag, true) if @prepared_tag && !@prepared_tag.empty?
- @prepared_tag = nil
- end
-
- def choose_scalar_style
- @analysis = analyze_scalar(@event.value) if @analysis.nil?
- return '"' if @event.style == '"' || @canonical
- if !@event.style && @event.implicit[0]
- if !(@simple_key_context && (@analysis.empty || @analysis.multiline)) && ((@flow_level!=0 && @analysis.allow_flow_plain) || (@flow_level == 0 && @analysis.allow_block_plain))
- return ""
- end
- end
- if !@event.style && @event.implicit && (!(@simple_key_context && (@analysis.empty || @analysis.multiline)) &&
- (@flow_level!=0 && @analysis.allow_flow_plain || (@flow_level==0 && @analysis.allow_block_plain)))
- return ""
- end
- return @event.style if @event.style && /^[|>]$/ =~ @event.style && @flow_level==0 && @analysis.allow_block
- return "'" if (!@event.style || @event.style == "'") && (@analysis.allow_single_quoted && !(@simple_key_context && @analysis.multiline))
- return '"'
- end
-
- def process_scalar
- @analysis = analyze_scalar(@event.value) if @analysis.nil?
- @style = choose_scalar_style if @style.nil?
- split = !@simple_key_context
- if @style == '"'
- write_double_quoted(@analysis.scalar, split)
- elsif @style == "'"
- write_single_quoted(@analysis.scalar, split)
- elsif @style == ">"
- write_folded(@analysis.scalar)
- elsif @style == "|"
- write_literal(@analysis.scalar)
- else
- write_plain(@analysis.scalar, split)
- end
- @analysis = nil
- @style = nil
- end
-
- # Analyzers.
-
- def prepare_version(version)
- major, minor = version
- raise EmitterError.new("unsupported YAML version: #{major}.#{minor}") if major != 1
- "#{major}.#{minor}"
- end
-
- def prepare_tag_handle(handle)
- raise EmitterError.new("tag handle must not be empty") if handle.nil? || handle.empty?
- raise EmitterError("tag handle must start and end with '!': #{handle}") if handle[0] != ?! || handle[-1] != ?!
- raise EmitterError.new("invalid character #{$&} in the tag handle: #{handle}") if /[^-\w]/ =~ handle[1...-1]
- handle
- end
-
- def prepare_tag_prefix(prefix)
- raise EmitterError.new("tag prefix must not be empty") if prefix.nil? || prefix.empty?
- chunks = []
- start = ending = 0
- ending = 1 if prefix[0] == ?!
- ending += 1 while ending < prefix.length
- chunks << prefix[start...ending] if start < ending
- chunks.to_s
- end
-
- def prepare_tag(tag)
- raise EmitterError.new("tag must not be empty") if tag.nil? || tag.empty?
- return tag if tag == "!"
- handle = nil
- suffix = tag
- for prefix in @tag_prefixes.keys
- if Regexp.new("^"+Regexp.escape(prefix)) =~ tag && (prefix == "!" || prefix.length < tag.length)
- handle = @tag_prefixes[prefix]
- suffix = tag[prefix.length..-1]
- end
- end
- chunks = []
- start = ending = 0
- ending += 1 while ending < suffix.length
- chunks << suffix[start...ending] if start < ending
- suffix_text = chunks.to_s
- if handle
- "#{handle}#{suffix_text}"
- else
- "!<#{suffix_text}>"
- end
- end
-
- def prepare_anchor(anchor)
- raise EmitterError.new("anchor must not be empty") if anchor.nil? || anchor.empty?
- raise EmitterError.new("invalid character #{$&} in the anchor: #{anchor}") if /[^-\w]/ =~ anchor
- anchor
- end
-
- def analyze_scalar(scalar)
- # Empty scalar is a special case.
- return ScalarAnalysis.new(scalar,true,false,false,true,true,true,false) if scalar.nil? || scalar.empty?
- # Indicators and special characters.
- block_indicators = false
- flow_indicators = false
- line_breaks = false
- special_characters = false
-
- # Whitespaces.
- inline_spaces = false # non-space space+ non-space
- inline_breaks = false # non-space break+ non-space
- leading_spaces = false # ^ space+ (non-space | $)
- leading_breaks = false # ^ break+ (non-space | $)
- trailing_spaces = false # (^ | non-space) space+ $
- trailing_breaks = false # (^ | non-space) break+ $
- inline_breaks_spaces = false # non-space break+ space+ non-space
- mixed_breaks_spaces = false # anything else
-
- # Check document indicators.
- if /^(---|\.\.\.)/ =~ scalar
- block_indicators = true
- flow_indicators = true
- end
-
- # First character or preceded by a whitespace.
- preceeded_by_space = true
-
- # Last character or followed by a whitespace.
- followed_by_space = scalar.length == 1 || "\0 \t\r\n\x85".include?(scalar[1])
-
- # The current series of whitespaces contain plain spaces.
- spaces = false
-
- # The current series of whitespaces contain line breaks.
- breaks = false
-
- # The current series of whitespaces contain a space followed by a
- # break.
- mixed = false
-
- # The current series of whitespaces start at the beginning of the
- # scalar.
- leading = false
-
- index = 0
- while index < scalar.length
- ch = scalar[index]
-
- # Check for indicators.
-
- if index == 0
- # Leading indicators are special characters.
- if "#,[]{}#&*!|>'\"%@`".include?(ch)
- flow_indicators = true
- block_indicators = true
- end
- if "?:".include?(ch)
- flow_indicators = true
- if followed_by_space
- block_indicators = true
- end
- end
- if ch == ?- && followed_by_space
- flow_indicators = true
- block_indicators = true
- end
- else
- # Some indicators cannot appear within a scalar as well.
- flow_indicators = true if ",?[]{}".include?(ch)
- if ch == ?:
- flow_indicators = true
- block_indicators = true if followed_by_space
- end
- if ch == ?# && preceeded_by_space
- flow_indicators = true
- block_indicators = true
- end
- end
- # Check for line breaks, special, and unicode characters.
- line_breaks = true if "\n\x85".include?(ch)
- if !(ch == ?\n || (?\x20 <= ch && ch <= ?\x7E))
- special_characters = true
- end
- # Spaces, line breaks, and how they are mixed. State machine.
-
- # Start or continue series of whitespaces.
- if " \n\x85".include?(ch)
- if spaces && breaks
- mixed = true if ch != 32 # break+ (space+ break+) => mixed
- elsif spaces
- if ch != 32 # (space+ break+) => mixed
- breaks = true
- mixed = true
- end
- elsif breaks
- spaces = true if ch == 32 # break+ space+
- else
- leading = (index == 0)
- if ch == 32 # space+
- spaces = true
- else # break+
- breaks = true
- end
- end
- # Series of whitespaces ended with a non-space.
- elsif spaces || breaks
- if leading
- if spaces && breaks
- mixed_breaks_spaces = true
- elsif spaces
- leading_spaces = true
- elsif breaks
- leading_breaks = true
- end
- else
- if mixed
- mixed_breaks_spaces = true
- elsif spaces && breaks
- inline_breaks_spaces = true
- elsif spaces
- inline_spaces = true
- elsif breaks
- inline_breaks = true
- end
- end
- spaces = breaks = mixed = leading = false
- end
-
- # Series of whitespaces reach the end.
- if (spaces || breaks) && (index == scalar.length-1)
- if spaces && breaks
- mixed_breaks_spaces = true
- elsif spaces
- trailing_spaces = true
- leading_spaces = true if leading
- elsif breaks
- trailing_breaks = true
- leading_breaks = true if leading
- end
- spaces = breaks = mixed = leading = false
- end
- # Prepare for the next character.
- index += 1
- preceeded_by_space = "\0 \t\r\n\x85".include?(ch)
- followed_by_space = index+1 >= scalar.length || "\0 \t\r\n\x85".include?(scalar[index+1])
- end
- # Let's decide what styles are allowed.
- allow_flow_plain = true
- allow_block_plain = true
- allow_single_quoted = true
- allow_double_quoted = true
- allow_block = true
- # Leading and trailing whitespace are bad for plain scalars. We also
- # do not want to mess with leading whitespaces for block scalars.
- allow_flow_plain = allow_block_plain = allow_block = false if leading_spaces || leading_breaks || trailing_spaces
-
- # Trailing breaks are fine for block scalars, but unacceptable for
- # plain scalars.
- allow_flow_plain = allow_block_plain = false if trailing_breaks
-
- # The combination of (space+ break+) is only acceptable for block
- # scalars.
- allow_flow_plain = allow_block_plain = allow_single_quoted = false if inline_breaks_spaces
-
- # Mixed spaces and breaks, as well as special character are only
- # allowed for double quoted scalars.
- allow_flow_plain = allow_block_plain = allow_single_quoted = allow_block = false if mixed_breaks_spaces || special_characters
-
- # We don't emit multiline plain scalars.
- allow_flow_plain = allow_block_plain = false if line_breaks
-
- # Flow indicators are forbidden for flow plain scalars.
- allow_flow_plain = false if flow_indicators
-
- # Block indicators are forbidden for block plain scalars.
- allow_block_plain = false if block_indicators
-
- ScalarAnalysis.new(scalar,false,line_breaks,allow_flow_plain,allow_block_plain,allow_single_quoted,allow_double_quoted,allow_block)
- end
-
- # Writers.
-
- def flush_stream
- @stream.flush if @stream.respond_to?(:flush)
- end
-
- def write_stream_start
- end
-
- def write_stream_end
- flush_stream
- end
-
- def write_indicator(indicator, need_whitespace,whitespace=false,indention=false)
- if @whitespace || !need_whitespace
- data = indicator
- else
- data = " "+indicator
- end
-
- @whitespace = whitespace
- @indention = @indention && indention
- @column += data.length
- @stream.write(data)
- end
-
- def write_indent
- indent = @indent || 0
- write_line_break if !@indention || @column > indent || (@column == indent && !@whitespace)
- if @column < indent
- @whitespace = true
- data = " "*(indent-@column)
- @column = indent
- @stream.write(data)
- end
- end
-
- def write_line_break(data=nil)
- data = @best_line_break if data.nil?
- @whitespace = true
- @indention = true
- @line += 1
- @column = 0
- @stream.write(data)
- end
-
-
- def write_version_directive(version_text)
- data = "%YAML #{version_text}"
- @stream.write(data)
- write_line_break
- end
-
- def write_tag_directive(handle_text, prefix_text)
- data = "%TAG #{handle_text} #{prefix_text}"
- @stream.write(data)
- write_line_break
- end
-
- # Scalar streams.
-
- def write_single_quoted(text, split=true)
- write_indicator("'",true)
- spaces = false
- breaks = false
- start = ending = 0
- while ending <= text.length
- ch = nil
- ch = text[ending] if ending < text.length
- if spaces
- if ch.nil? || ch != 32
- if start+1 == ending && @column > @best_width && split && start != 0 && ending != text.length
- write_indent
- else
- data = text[start...ending]
- @column += data.length
- @stream.write(data)
- end
- start = ending
- end
- elsif breaks
- if ch.nil? or !"\n\x85".include?(ch)
- (text[start...ending]).each_byte { |br|
- if br == ?\n
- write_line_break
- else
- write_line_break(br)
- end
- }
- write_indent
- start = ending
- end
- else
- if ch.nil? || "' \n\x85".include?(ch)
- if start < ending
- data = text[start...ending]
- @column += data.length
- @stream.write(data)
- start = ending
- end
- if ch == ?'
- data = "''"
- @column += 2
- @stream.write(data)
- start = ending + 1
- end
- end
- end
-
- if !ch.nil?
- spaces = ch == 32
- breaks = "\n\x85".include?(ch)
- end
-
- ending += 1
- end
- write_indicator("'", false)
- end
-
- ESCAPE_REPLACEMENTS = {
- ?\0 => "0",
- ?\x07 => "a",
- ?\x08 => "b",
- ?\x09 => "t",
- ?\x0A => "n",
- ?\x0B => "v",
- ?\x0C => "f",
- ?\x0D => "r",
- ?\x1B => "e",
- ?" => "\"",
- ?\\ => "\\",
- ?\x85 => "N",
- ?\xA0 => "_"
- }
-
- def write_double_quoted(text, split=true)
- write_indicator('"', true)
- start = ending = 0
- while ending <= text.length
- ch = nil
- ch = text[ending] if ending < text.length
- if ch.nil? || "\"\\\x85".include?(ch) || !(?\x20 <= ch && ch <= ?\x7E)
- if start < ending
- data = text[start...ending]
- @column += data.length
- @stream.write(data)
- start = ending
- end
- if !ch.nil?
- if ESCAPE_REPLACEMENTS.include?(ch)
- data = "\\"+ESCAPE_REPLACEMENTS[ch]
- elsif ch <= ?\xFF
- data = "\\x%02X" % ch
- end
- @column += data.length
- @stream.write(data)
- start = ending+1
- end
- end
- if (0 < ending && ending < text.length-1) && (ch == 32 || start >= ending) && @column+(ending-start) > @best_width && split
- data = text[start...ending]+"\\"
- start = ending if start < ending
- @column += data.length
- @stream.write(data)
- write_indent
- @whitespace = false
- @indention = false
- if text[start] == 32
- data = "\\"
- @column += data.length
- @stream.write(data)
- end
- end
- ending += 1
- end
- write_indicator('"', false)
- end
-
- def determine_chomp(text)
- tail = text[-2..-1]
- tail = " "+tail while tail.length < 2
- "\n\x85".include?(tail[-1])? ("\n\x85".include?(tail[-2])? "+" : "" ) : "-"
- end
-
- def write_folded(text)
- chomp = determine_chomp(text)
- write_indicator(">"+chomp, true)
- write_indent
- leading_space = false
- spaces = false
- breaks = false
- start = ending = 0
- while ending <= text.length
- ch = nil
- ch = text[ending] if ending < text.length
- if breaks
- if ch.nil? || !"\n\x85".include?(ch)
- write_line_break if !leading_space && !ch.nil? && ch != 32 && text[start] == ?\n
- leading_space = ch == 32
- (text[start...ending]).each_byte { |br|
- if br == ?\n
- write_line_break
- else
- write_line_break(br)
- end
- }
- write_indent if !ch.nil?
- start = ending
- end
- elsif spaces
- if ch != 32
- if start+1 == ending && @column > @best_width
- write_indent
- else
- data = text[start...ending]
- @column += data.length
- @stream.write(data)
- end
- start = ending
- end
- else
- if ch.nil? || " \n\x85".include?(ch)
- data = text[start...ending]
- @stream.write(data)
- write_line_break if ch.nil?
- start = ending
- end
- end
- if !ch.nil?
- breaks = "\n\x85".include?(ch)
- spaces = ch == 32
- end
- ending += 1
- end
- end
-
- def write_literal(text)
- chomp = determine_chomp(text)
- write_indicator("|"+chomp, true)
- write_indent
- breaks = false
- start = ending = 0
- while ending <= text.length
- ch = nil
- ch = text[ending] if ending < text.length
- if breaks
- if ch.nil? || !"\n\x85".include?(ch)
- (text[start...ending]).each_byte { |br|
- if br == ?\n
- write_line_break
- else
- write_line_break(br)
- end
- }
- write_indent if !ch.nil?
- start = ending
- end
- else
- if ch.nil? || "\n\x85".include?(ch)
- data = text[start...ending]
- @stream.write(data)
- write_line_break if ch.nil?
- start = ending
- end
- end
- breaks = "\n\x85".include?(ch) if !ch.nil?
- ending += 1
- end
- end
-
- def write_plain(text, split=true)
- return nil if text.nil? || text.empty?
- if !@whitespace
- data = " "
- @column += data.length
- @stream.write(data)
- end
- @writespace = false
- @indention = false
- spaces = false
- breaks = false
- start = ending = 0
- while ending <= text.length
- ch = nil
- ch = text[ending] if ending < text.length
- if spaces
- if ch != 32
- if start+1 == ending && @column > @best_width && split
- write_indent
- @writespace = false
- @indention = false
- else
- data = text[start...ending]
- @column += data.length
- @stream.write(data)
- end
- start = ending
- end
- elsif breaks
- if !"\n\x85".include?(ch)
- write_line_break if text[start] == ?\n
- (text[start...ending]).each_byte { |br|
- if br == ?\n
- write_line_break
- else
- write_line_break(br)
- end
- }
- write_indent
- @whitespace = false
- @indention = false
- start = ending
- end
- else
- if ch.nil? || " \n\x85".include?(ch)
- data = text[start...ending]
- @column += data.length
- @stream.write(data)
- start = ending
- end
- end
- if !ch.nil?
- spaces = ch == 32
- breaks = "\n\x85".include?(ch)
- end
- ending += 1
- end
- end
- end
-end
View
75 lib/ruby/site_ruby/1.8/rbyaml/error.rb
@@ -1,75 +0,0 @@
-
-module RbYAML
- Mark = Struct.new(:name,:column,:buffer,:pointer)
- class Mark
- def get_snippet(indent=4, max_length=75)
- return nil if buffer.nil?
- head = ""
- start = pointer
- while start > 0 && !"\0\r\n\x85".include?(buffer[start-1])
- start -= 1
- if pointer-start > max_length/2-1
- head = " ... "
- start += 5
- break
- end
- end
- tail = ""
- tend = pointer
- while tend < buffer.length && !"\0\r\n\x85".include?(buffer[tend])
- tend += 1
- if tend-pointer > max_length/2-1
- tail = " ... "
- tend -= 5
- break
- end
- end
- snippet = buffer[start..tend]
- ' ' * indent + "#{head}#{snippet}#{tail}\n" + ' '*(indent+pointer-start+head.length) + ' '
- end
-
- def to_s
- snippet = get_snippet()
- where = " in \"#{name}\", line ?, column #{column+1}"
- if snippet
- where << ":\n" << snippet
- end
- end
-
- def hash
- object_id
- end
- end
-
- class YAMLError < StandardError
- end
-
- class TypeError < YAMLError
- end
-
- class MarkedYAMLError < YAMLError
- def initialize(context=nil, context_mark=nil, problem=nil, problem_mark=nil, note=nil)
- super()
- @context = context
- @context_mark = context_mark
- @problem = problem
- @problem_mark = problem_mark
- @note = note
- end
-
- def to_s
- lines = []
-
- lines << @context if @context
- if @context_mark && (@problem.nil? || @problem_mark.nil? ||
- @context_mark.name != @problem_mark.name ||
- @context_mark.column != @problem_mark.column)
- lines << @context_mark.to_s
- end
- lines << @problem if @problem
- lines << @problem_mark.to_s if @problem_mark
- lines << @note if @note
- lines.join("\n")
- end
- end
-end
View
117 lib/ruby/site_ruby/1.8/rbyaml/events.rb
@@ -1,117 +0,0 @@
-
-module RbYAML
- Event = Struct.new(:start_mark,:end_mark)
- class Event
- def hash
- object_id
- end
- def to_s
- attributes = ["@anchor","@tag","@implicit","@value"] & self.instance_variables
- args = attributes.collect {|val| "#{val[1..-1]}=" + eval("#{val}").to_s}.join(", ")
- "#{self.class.name}(#{args})"
- end
- def __is_node; false; end
- def __is_collection_start; false; end
- def __is_collection_end; false; end
- def __is_stream_start; false; end
- def __is_stream_end; false; end
- def __is_document_start; false; end
- def __is_document_end; false; end
- def __is_alias; false; end
- def __is_scalar; false; end
- def __is_sequence_start; false; end
- def __is_sequence_end; false; end
- def __is_mapping_start; false; end
- def __is_mapping_end; false; end
- end
-
- class NodeEvent < Event
- attr_reader :anchor
- def initialize(anchor, start_mark=nil, end_mark=nil)
- super(start_mark,end_mark)
- @anchor = anchor
- end
- def __is_node; true; end
- end
-
- class CollectionStartEvent < NodeEvent
- attr_reader :tag, :implicit, :flow_style
- def initialize(anchor,tag,implicit,start_mark=nil, end_mark=nil,flow_style=nil)
- super(anchor,start_mark,end_mark)
- @tag = tag
- @implicit = implicit
- @flow_style = flow_style
- end
- def __is_collection_start; true; end
- end
-
- class CollectionEndEvent < Event
- def __is_collection_end; true; end
- end
-
- class StreamStartEvent < Event
- attr_reader :encoding
- def initialize(start_mark=nil,end_mark=nil,encoding=nil)
- super(start_mark,end_mark)
- @encoding = encoding
- end
- def __is_stream_start; true; end
- end
-
- class StreamEndEvent < Event
- def __is_stream_end; true; end
- end
-
- class DocumentStartEvent < Event
- attr_reader :explicit, :version, :tags
- def initialize(start_mark=nil,end_mark=nil,explicit=nil,version=nil,tags=nil)
- super(start_mark,end_mark)
- @explicit = explicit
- @version = version
- @tags = tags
- end
- def __is_document_start; true; end
- end
-
- class DocumentEndEvent < Event
- attr_reader :explicit
- def initialize(start_mark=nil,end_mark=nil,explicit=nil)
- super(start_mark,end_mark)
- @explicit = explicit
- end
- def __is_document_end; true; end
- end
-
- class AliasEvent < NodeEvent
- def __is_alias; true; end
- end
-
- class ScalarEvent < NodeEvent
- attr_reader :tag, :style, :value, :implicit
- def initialize(anchor,tag,implicit,value,start_mark=nil, end_mark=nil,style=nil)
- super(anchor,start_mark,end_mark)
- @tag = tag
- @style = style
- @value = value
- @implicit = implicit
- end
- def __is_scalar; true; end
- end
-
- class SequenceStartEvent < CollectionStartEvent
- def __is_sequence_start; true; end
- end
-
- class SequenceEndEvent < CollectionEndEvent
- def __is_sequence_end; true; end
- end
-
- class MappingStartEvent < CollectionStartEvent
- def __is_mapping_start; true; end
- end
-
- class MappingEndEvent < CollectionEndEvent
- def __is_mapping_end; true; end
- end
-end
-
View
40 lib/ruby/site_ruby/1.8/rbyaml/loader.rb
@@ -1,40 +0,0 @@
-# This is a more or less straight translation of PyYAML3000 to Ruby
-
-require 'rbyaml/scanner'
-require 'rbyaml/parser'
-require 'rbyaml/composer'
-require 'rbyaml/constructor'
-require 'rbyaml/resolver'
-
-module RbYAML
- class CommonLoader
- attr_accessor :scanner, :parser, :composer, :constructor, :resolver
-
- def initialize(stream,scanner=Scanner,parser=Parser,composer=Composer,constructor=BaseConstructor,resolver=BaseResolver)
- @scanner = scanner.new(stream)
- @parser = parser.new(@scanner)
- @resolver = resolver.new
- @composer = composer.new(@parser,@resolver)
- @constructor = constructor.new(@composer)
- end
- end
-
- class BaseLoader < CommonLoader
- def initialize(stream)
- super(stream,Scanner,Parser,Composer,BaseConstructor,BaseResolver)
- end
- end
-
- class SafeLoader < CommonLoader
- def initialize(stream)
- super(stream,Scanner,Parser,Composer,SafeConstructor,Resolver)
- end
- end
-
- class Loader < CommonLoader
- def initialize(stream)
- super(stream,Scanner,Parser,Composer,Constructor,Resolver)
- end
- end
-end
-
View
56 lib/ruby/site_ruby/1.8/rbyaml/nodes.rb
@@ -1,56 +0,0 @@
-
-module RbYAML
- Node = Struct.new(:tag, :value, :start_mark, :end_mark)
- class Node
- def hash
- object_id
- end
- def to_s
- "#{self.class.name}(tag=#{tag}, value=#{value})"
- end
-
- def __is_scalar; false; end
- def __is_collection; false; end
- def __is_sequence; false; end
- def __is_mapping; false; end
- end
-
- class ScalarNode < Node
- def tid
- "scalar"
- end
-
- attr_accessor :style
-
- def initialize(tag, value,start_mark=nil,end_mark=nil,style=nil)
- super(tag,value,start_mark,end_mark)
- @style = style
- end
- def __is_scalar; true; end
- end
-
- class CollectionNode < Node
- attr_accessor :flow_style
-
- def initialize(tag, value,start_mark=nil,end_mark