RbYAML 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/README +1 -1
  2. data/lib/rbyaml/composer.rb +28 -25
  3. data/lib/rbyaml/composer.rb.~1.2.~ +109 -0
  4. data/lib/rbyaml/constructor.rb +94 -84
  5. data/lib/rbyaml/constructor.rb.~1.2.~ +381 -0
  6. data/lib/rbyaml/dumper.rb +10 -17
  7. data/lib/rbyaml/dumper.rb.~1.2.~ +43 -0
  8. data/lib/rbyaml/emitter.rb +13 -26
  9. data/lib/rbyaml/emitter.rb.~1.2.~ +1116 -0
  10. data/lib/rbyaml/error.rb +15 -21
  11. data/lib/rbyaml/events.rb +29 -5
  12. data/lib/rbyaml/events.rb.~1.2.~ +93 -0
  13. data/lib/rbyaml/loader.rb +11 -23
  14. data/lib/rbyaml/loader.rb.~1.2.~ +52 -0
  15. data/lib/rbyaml/nodes.rb +13 -9
  16. data/lib/rbyaml/nodes.rb.~1.2.~ +52 -0
  17. data/lib/rbyaml/parser.rb +481 -343
  18. data/lib/rbyaml/parser.rb.old +531 -0
  19. data/lib/rbyaml/parser.rb.~1.2.~ +494 -0
  20. data/lib/rbyaml/reader.rb.~1.1.1.1.~ +127 -0
  21. data/lib/rbyaml/representer.rb +26 -17
  22. data/lib/rbyaml/representer.rb.~1.2.~ +239 -0
  23. data/lib/rbyaml/resolver.rb +15 -15
  24. data/lib/rbyaml/resolver.rb.~1.1.~ +163 -0
  25. data/lib/rbyaml/scanner.rb +457 -366
  26. data/lib/rbyaml/scanner.rb.~1.2.~ +1259 -0
  27. data/lib/rbyaml/serializer.rb +19 -17
  28. data/lib/rbyaml/serializer.rb.~1.2.~ +115 -0
  29. data/lib/rbyaml/tokens.rb +44 -4
  30. data/lib/rbyaml/tokens.rb.~1.2.~ +164 -0
  31. data/lib/rbyaml/util.rb +28 -0
  32. data/lib/rbyaml/yaml.rb +12 -12
  33. data/lib/rbyaml/yaml.rb.~1.2.~ +136 -0
  34. data/test/test_bm.rb +28 -0
  35. data/test/test_bm_syck.rb +28 -0
  36. data/test/test_invoke.rb +31 -0
  37. data/test/test_one.rb +5 -0
  38. data/test/test_profile.rb +32 -0
  39. data/test/test_rbyaml.rb +2 -1
  40. data/test/test_rbyaml.rb.~1.2.~ +31 -0
  41. data/test/test_time.rb +13 -8
  42. data/test/test_time.rb.~1.1.~ +29 -0
  43. data/test/yamlx.rb +3563 -0
  44. metadata +27 -2
@@ -0,0 +1,28 @@
1
+ class Object
2
+ def __is_str; false end
3
+ def __is_sym; false end
4
+ def __is_a; false end
5
+ def __is_int; false end
6
+ end
7
+
8
+ class String
9
+ def __is_str; true end
10
+ end
11
+
12
+ class Symbol
13
+ def __is_sym; true end
14
+ end
15
+
16
+ class Array
17
+ def __is_a; true end
18
+ end
19
+
20
+ class Integer
21
+ def __is_int; true end
22
+ end
23
+
24
+ class Fixnum
25
+ def __is_ascii_num
26
+ self <= ?9 && self >= ?0
27
+ end
28
+ end
@@ -12,32 +12,32 @@ require 'rbyaml/dumper'
12
12
  module RbYAML
13
13
  def self._scan(stream, loader=Loader)
14
14
  l = loader.new(stream)
15
- yield l.get_token while l.check_token
15
+ yield l.scanner.get_token while l.scanner.check_token
16
16
  end
17
17
 
18
18
  def self._parse(stream, loader=Loader)
19
19
  l = loader.new(stream)
20
- yield l.get_event while l.check_event
20
+ yield l.parser.get_event while l.parser.check_event
21
21
  end
22
22
 
23
23
  def self._compose(stream, loader=Loader)
24
24
  l = loader.new(stream)
25
- l.get_node if l.check_node
25
+ l.composer.get_node if l.composer.check_node
26
26
  end
27
27
 
28
28
  def self._compose_all(stream, loader=Loader)
29
29
  l = loader.new(stream)
30
- yield l.get_node while l.check_node
30
+ yield l.composer.get_node while l.composer.check_node
31
31
  end
32
32
 
33
33
  def self._load_all(stream, loader=Loader)
34
34
  l = loader.new(stream)
35
- yield l.get_data while l.check_data
35
+ yield l.constructor.get_data while l.constructor.check_data
36
36
  end
37
37
 
38
38
  def self._load(stream, loader=Loader)
39
39
  l = loader.new(stream)
40
- l.get_data if l.check_data
40
+ l.constructor.get_data if l.constructor.check_data
41
41
  end
42
42
 
43
43
  def self._safe_load_all(stream)
@@ -66,11 +66,11 @@ module RbYAML
66
66
  stream = StringIO.new
67
67
  end
68
68
  dumper = dumper.new(stream,default_style,default_flow_style,canonical,indent,width,line_break,version,tags,explicit_start,explicit_end)
69
- dumper.open
69
+ dumper.serializer.open
70
70
  for node in nodes
71
- dumper.serialize(node)
71
+ dumper.serializer.serialize(node)
72
72
  end
73
- dumper.close
73
+ dumper.serializer.close
74
74
  stream.string if StringIO === stream
75
75
  end
76
76
 
@@ -84,11 +84,11 @@ module RbYAML
84
84
  stream = StringIO.new
85
85
  end
86
86
  dumper = dumper.new(stream,default_style,default_flow_style,canonical,indent,width,line_break,version,tags,explicit_start,explicit_end)
87
- dumper.open
87
+ dumper.serializer.open
88
88
  for data in documents
89
- dumper.represent(data)
89
+ dumper.representer.represent(data)
90
90
  end
91
- dumper.close
91
+ dumper.serializer.close
92
92
  stream.string if StringIO === stream
93
93
  end
94
94
 
@@ -0,0 +1,136 @@
1
+ require 'stringio'
2
+
3
+ require 'rbyaml/error'
4
+
5
+ require 'rbyaml/tokens'
6
+ require 'rbyaml/events'
7
+ require 'rbyaml/nodes'
8
+
9
+ require 'rbyaml/loader'
10
+ require 'rbyaml/dumper'
11
+
12
+ module RbYAML
13
+ def self._scan(stream, loader=Loader)
14
+ l = loader.new(stream)
15
+ yield l.get_token while l.check_token
16
+ end
17
+
18
+ def self._parse(stream, loader=Loader)
19
+ l = loader.new(stream)
20
+ yield l.get_event while l.check_event
21
+ end
22
+
23
+ def self._compose(stream, loader=Loader)
24
+ l = loader.new(stream)
25
+ l.get_node if l.check_node
26
+ end
27
+
28
+ def self._compose_all(stream, loader=Loader)
29
+ l = loader.new(stream)
30
+ yield l.get_node while l.check_node
31
+ end
32
+
33
+ def self._load_all(stream, loader=Loader)
34
+ l = loader.new(stream)
35
+ yield l.get_data while l.check_data
36
+ end
37
+
38
+ def self._load(stream, loader=Loader)
39
+ l = loader.new(stream)
40
+ l.get_data if l.check_data
41
+ end
42
+
43
+ def self._safe_load_all(stream)
44
+ _load_all(stream, SafeLoader)
45
+ end
46
+
47
+ def self._safe_load(stream)
48
+ _load(stream, SafeLoader)
49
+ end
50
+
51
+ def self._emit(events, stream=nil, dumper=Dumper,default_style=nil,default_flow_style=nil,canonical=nil, indent=nil, width=nil,line_break=nil)
52
+ if stream.nil?
53
+ require 'stringio'
54
+ stream = StringIO.new
55
+ end
56
+ dumper = dumper.new(stream,default_style,default_flow_style,canonical,indent,width,line_break)
57
+ for event in events
58
+ dumper.emit(event)
59
+ end
60
+ stream.string if StringIO === stream
61
+ end
62
+
63
+ def self._serialize_all(nodes,stream=nil,dumper=Dumper,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)
64
+ if stream.nil?
65
+ require 'stringio'
66
+ stream = StringIO.new
67
+ end
68
+ dumper = dumper.new(stream,default_style,default_flow_style,canonical,indent,width,line_break,version,tags,explicit_start,explicit_end)
69
+ dumper.open
70
+ for node in nodes
71
+ dumper.serialize(node)
72
+ end
73
+ dumper.close
74
+ stream.string if StringIO === stream
75
+ end
76
+
77
+ def self._serialize(node, stream=nil, dumper=Dumper, *kwds)
78
+ _serialize_all([node], stream, dumper, *kwds)
79
+ end
80
+
81
+ def self._dump_all(documents,stream=nil,dumper=Dumper,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)
82
+ if stream.nil?
83
+ require 'stringio'
84
+ stream = StringIO.new
85
+ end
86
+ dumper = dumper.new(stream,default_style,default_flow_style,canonical,indent,width,line_break,version,tags,explicit_start,explicit_end)
87
+ dumper.open
88
+ for data in documents
89
+ dumper.represent(data)
90
+ end
91
+ dumper.close
92
+ stream.string if StringIO === stream
93
+ end
94
+
95
+ def self._dump(data, stream=nil, dumper=Dumper, *kwds)
96
+ _dump_all([data], stream, dumper, *kwds)
97
+ end
98
+
99
+ def self._safe_dump_all(documents, stream=nil, *kwds)
100
+ _dump_all(documents, stream, SafeDumper, *kwds)
101
+ end
102
+
103
+ def self._safe_dump(data, stream=nil, *kwds)
104
+ _dump_all([data], stream, SafeDumper, *kwds)
105
+ end
106
+
107
+ def self._add_implicit_resolver(tag, regexp, first=nil, loader=Loader, dumper=Dumper)
108
+ loader.add_implicit_resolver(tag, regexp, first)
109
+ dumper.add_implicit_resolver(tag, regexp, first)
110
+ end
111
+
112
+ def self._add_path_resolver(tag, path, kind=nil, loader=Loader, dumper=Dumper)
113
+ loader.add_path_resolver(tag, path, kind)
114
+ lumper.add_path_resolver(tag, path, kind)
115
+ end
116
+
117
+ def self._add_constructor(tag, constructor, loader=Loader)
118
+ loader.add_constructor(tag, constructor)
119
+ end
120
+
121
+ def self._add_multi_constructor(tag_prefix, multi_constructor, loader=Loader)
122
+ loader.add_multi_constructor(tag_prefix, multi_constructor)
123
+ end
124
+
125
+ def self._add_representer(data_type, representer, dumper=Dumper)
126
+ dumper.add_representer(data_type, representer)
127
+ end
128
+
129
+ def self._add_multi_representer(data_type, multi_representer, dumper=Dumper)
130
+ dumper.add_multi_representer(data_type, multi_representer)
131
+ end
132
+
133
+ def self._dump_ruby_object(data, dumper=Dumper)
134
+ _dump(data,nil,dumper)
135
+ end
136
+ end
@@ -0,0 +1,28 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'benchmark'
4
+ require 'rbyaml'
5
+ require 'yamlx'
6
+
7
+ FILES = %w{test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test12 test13 test14 test16 test18 test19 test20 gems2}
8
+ DO_TIMES = 100
9
+
10
+ Benchmark.bmbm do |bm|
11
+ bm.report("JRuby YAML") {
12
+ FILES.each {|fname|
13
+ x = File.read("yaml/#{fname}.yml")
14
+ for k in 1..DO_TIMES
15
+ YAML::load(x)
16
+ end
17
+ }
18
+ }
19
+
20
+ bm.report("RbYAML") {
21
+ FILES.each {|fname|
22
+ x = File.read("yaml/#{fname}.yml")
23
+ for k in 1..DO_TIMES
24
+ RbYAML::load(x)
25
+ end
26
+ }
27
+ }
28
+ end
@@ -0,0 +1,28 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'benchmark'
4
+ require 'rbyaml'
5
+ require 'yaml'
6
+
7
+ FILES = %w{test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test12 test13 test14 test16 test18 test19 test20 gems2}
8
+ DO_TIMES = 100
9
+
10
+ Benchmark.bmbm do |bm|
11
+ bm.report("SYCK") {
12
+ FILES.each {|fname|
13
+ x = File.read("yaml/#{fname}.yml")
14
+ for k in 1..DO_TIMES
15
+ YAML::load(x)
16
+ end
17
+ }
18
+ }
19
+
20
+ bm.report("RbYAML") {
21
+ FILES.each {|fname|
22
+ x = File.read("yaml/#{fname}.yml")
23
+ for k in 1..DO_TIMES
24
+ RbYAML::load(x)
25
+ end
26
+ }
27
+ }
28
+ end
@@ -0,0 +1,31 @@
1
+
2
+
3
+ class Time
4
+ def self.time(times = 1)
5
+ if block_given?
6
+ before = Time.now
7
+ times.times {
8
+ yield
9
+ }
10
+ after = Time.now
11
+ return after-before
12
+ end
13
+ end
14
+ end
15
+
16
+
17
+ def methoda
18
+ 10000+1
19
+ end
20
+
21
+ DO_TIMES = 1000000
22
+ ttimes = Time.time(DO_TIMES) {
23
+ methoda
24
+ }
25
+
26
+ ttimes2 = Time.time(DO_TIMES) {
27
+ send(:methoda)
28
+ }
29
+
30
+ puts "direct invoke: #{ttimes}"
31
+ puts "send invoke: #{ttimes2}"
@@ -0,0 +1,5 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'rbyaml'
4
+
5
+ RbYAML.dump(open(File.expand_path(ARGV[0]),"r") {|f| RbYAML.load(f)})
@@ -0,0 +1,32 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ class Module
4
+ def add_tracer(meth)
5
+ m_alias = case meth
6
+ when :+ : "old_plus"
7
+ when :=== : "old_triple"
8
+ when :== : "old_eq"
9
+ else ; "old_#{method}"
10
+ end
11
+ module_eval %Q{
12
+ alias :#{m_alias} :#{meth}
13
+ def #{meth}(*args,&block)
14
+ puts caller[0] + ": " + self.class.name + "##{meth} called"
15
+ #{m_alias}(*args,&block)
16
+ end
17
+ }
18
+ end
19
+ end
20
+
21
+ #Kernel.add_tracer(:===)
22
+ #Kernel.add_tracer(:==)
23
+ #Fixnum.add_tracer(:+)
24
+
25
+ require 'profile'
26
+ require 'rbyaml'
27
+
28
+ FILES = %w{test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test12 test13 test14 test16 test18 test19 test20 gems2}
29
+ FILES.each {|fname|
30
+ RbYAML::load(File.read("yaml/#{fname}.yml"))
31
+ }
32
+
@@ -2,6 +2,7 @@ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
2
 
3
3
  require 'rbyaml'
4
4
  require 'test/unit'
5
+ require 'rubygems'
5
6
 
6
7
  class TestRbYAML < Test::Unit::TestCase
7
8
  def test_basic_load
@@ -19,7 +20,7 @@ class TestRbYAML < Test::Unit::TestCase
19
20
  end
20
21
 
21
22
  class TestRbYAMLFromFiles < Test::Unit::TestCase
22
- FILES = %w{test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test12 test13 test14 test15 test16 test18 test19 test20}
23
+ FILES = %w{test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test12 test13 test14 test15 test16 test18 test19 test20 gems2}
23
24
 
24
25
  FILES.each { |test|
25
26
  self.class_eval <<-ENDCLS
@@ -0,0 +1,31 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'rbyaml'
4
+ require 'test/unit'
5
+
6
+ class TestRbYAML < Test::Unit::TestCase
7
+ def test_basic_load
8
+ assert_equal({"A"=>"b","c"=>3.14},RbYAML.load("--- \nA: b\nc: 3.14\n"))
9
+ assert_equal(["A","b","c"],RbYAML.load("--- \n- A\n- b\n- c\n"))
10
+ assert_equal("foo",RbYAML.load("foo\n"))
11
+ end
12
+
13
+ def test_basic_dump
14
+ answ,ver1,ver2 = RbYAML.dump({"A"=>"b","c"=>3.14}),"A: b\nc: 3.14\n", "c: 3.14\nA: b\n"
15
+ assert_equal(true, ver1 == answ || ver2 == answ,"was #{answ.inspect} but should be #{ver1.inspect} or #{ver2.inspect}")
16
+ assert_equal("- A\n- b\n- c\n",RbYAML.dump(["A","b","c"]))
17
+ assert_equal("foo\n",RbYAML.dump("foo"))
18
+ end
19
+ end
20
+
21
+ class TestRbYAMLFromFiles < Test::Unit::TestCase
22
+ FILES = %w{test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test12 test13 test14 test15 test16 test18 test19 test20}
23
+
24
+ FILES.each { |test|
25
+ self.class_eval <<-ENDCLS
26
+ def test_#{test}
27
+ RbYAML.dump(open(File.expand_path(File.join(File.dirname(__FILE__),"yaml","#{test}.yml")),"r") {|f| RbYAML.load(f)})
28
+ end
29
+ ENDCLS
30
+ }
31
+ end
@@ -15,15 +15,20 @@ class Time
15
15
  end
16
16
  end
17
17
 
18
- DO_TIMES = 10000
19
- ttimes = Time.time(DO_TIMES) {
20
- open("yaml/test1.yml") { |f| RbYAML::load(f) }
18
+ FILES = %w{test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test12 test13 test14 test16 test18 test19 test20 gems2}
19
+ ttimes = 0
20
+ DO_TIMES = 100
21
+ FILES.each {|fname|
22
+ x = File.read("yaml/#{fname}.yml")
23
+ ttimes += Time.time(DO_TIMES) {
24
+ RbYAML::load(x)
25
+ }
21
26
  }
22
27
 
23
- ttimes2 = Time.time(DO_TIMES) {
24
- RbYAML::dump(open("yaml/test1.yml") { |f| RbYAML::load(f) })
25
- } - ttimes
28
+ #ttimes2 = Time.time(DO_TIMES) {
29
+ # RbYAML::dump(open("yaml/test1.yml") { |f| RbYAML::load(f) })
30
+ #} - ttimes
26
31
 
27
- puts "loading a file #{DO_TIMES} times took #{ttimes.to_f * 1000} milli seconds"
32
+ puts "loading a file #{DO_TIMES*FILES.length} times took #{ttimes.to_f * 1000} milli seconds"
28
33
 
29
- puts "dumping a file #{DO_TIMES} times took #{ttimes2.to_f * 1000} milliseconds"
34
+ #puts "dumping a file #{DO_TIMES} times took #{ttimes2.to_f * 1000} milliseconds"
@@ -0,0 +1,29 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'rbyaml'
4
+
5
+ class Time
6
+ def self.time(times = 1)
7
+ if block_given?
8
+ before = Time.now
9
+ times.times {
10
+ yield
11
+ }
12
+ after = Time.now
13
+ return after-before
14
+ end
15
+ end
16
+ end
17
+
18
+ DO_TIMES = 10000
19
+ ttimes = Time.time(DO_TIMES) {
20
+ open("yaml/test1.yml") { |f| RbYAML::load(f) }
21
+ }
22
+
23
+ ttimes2 = Time.time(DO_TIMES) {
24
+ RbYAML::dump(open("yaml/test1.yml") { |f| RbYAML::load(f) })
25
+ } - ttimes
26
+
27
+ puts "loading a file #{DO_TIMES} times took #{ttimes.to_f * 1000} milli seconds"
28
+
29
+ puts "dumping a file #{DO_TIMES} times took #{ttimes2.to_f * 1000} milliseconds"