summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-01-15 19:27:25 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2010-01-15 19:27:25 -0800
commit68591a6889c05ff40409429524297d39470921e2 (patch)
treec2c826c553d8d99b370dde5ddce51178d452ac0a /lib
parent506f5883dfb3cfa0f3e399ef496f491d8ff3ab7a (diff)
downloadpsych-68591a6889c05ff40409429524297d39470921e2.zip
removing tuples, using static method on scalar scanner
Diffstat (limited to 'lib')
-rw-r--r--lib/psych/scalar_scanner.rb59
-rw-r--r--lib/psych/visitors/to_ruby.rb10
-rw-r--r--lib/psych/visitors/yaml_tree.rb2
3 files changed, 34 insertions, 37 deletions
diff --git a/lib/psych/scalar_scanner.rb b/lib/psych/scalar_scanner.rb
index f852418..aa00d37 100644
--- a/lib/psych/scalar_scanner.rb
+++ b/lib/psych/scalar_scanner.rb
@@ -12,25 +12,29 @@ module Psych
end
def tokenize
- return [:NULL, nil] if @string.empty?
+ self.class.tokenize @string
+ end
+
+ def self.tokenize string
+ return nil if string.empty?
- case @string
+ case string
when /^[A-Za-z~]/
- return [:SCALAR, @string] if @string.length > 5
- case @string
+ return string if string.length > 5
+ case string
when /^[^ytonf~]/i
- [:SCALAR, @string]
+ string
when '~', /^null$/i
- [:NULL, nil]
+ nil
when /^(yes|true|on)$/i
- [:BOOLEAN, true]
+ true
when /^(no|false|off)$/i
- [:BOOLEAN, false]
+ false
else
- [:SCALAR, @string]
+ string
end
when TIME
- date, time = *(@string.split(/[ tT]/, 2))
+ date, time = *(string.split(/[ tT]/, 2))
(yy, m, dd) = date.split('-').map { |x| x.to_i }
md = time.match(/(\d+:\d+:\d+)(\.\d*)?\s*(Z|[-+]\d+(:\d\d)?)?/)
@@ -39,44 +43,41 @@ module Psych
time = Time.utc(yy, m, dd, hh, mm, ss, us)
- return [:TIME, time] if 'Z' == md[3]
+ return time if 'Z' == md[3]
tz = md[3] ? Integer(md[3].split(':').first) : 0
- [:TIME, Time.at((time - (tz * 3600)).to_i, us)]
+ Time.at((time - (tz * 3600)).to_i, us)
when /^\d{4}-\d{1,2}-\d{1,2}$/
require 'date'
- [:DATE, Date.strptime(@string, '%Y-%m-%d')]
+ Date.strptime(string, '%Y-%m-%d')
when /^\.inf$/i
- [:POSITIVE_INFINITY, 1 / 0.0]
+ 1 / 0.0
when /^-\.inf$/i
- [:NEGATIVE_INFINITY, -1 / 0.0]
+ -1 / 0.0
when /^\.nan$/i
- [:NAN, 0.0 / 0.0]
+ 0.0 / 0.0
when /^:./
- if @string =~ /^:(["'])(.*)\1/
- [:SYMBOL, $2.sub(/^:/, '').to_sym]
+ if string =~ /^:(["'])(.*)\1/
+ $2.sub(/^:/, '').to_sym
else
- [:SYMBOL, @string.sub(/^:/, '').to_sym]
+ string.sub(/^:/, '').to_sym
end
when /^[-+]?[1-9][0-9_]*(:[0-5]?[0-9])+$/
i = 0
- @string.split(':').each_with_index do |n,e|
+ string.split(':').each_with_index do |n,e|
i += (n.to_i * 60 ** (e - 2).abs)
end
-
- [:INTEGER, i]
+ i
when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+\.[0-9_]*$/
i = 0
- @string.split(':').each_with_index do |n,e|
+ string.split(':').each_with_index do |n,e|
i += (n.to_f * 60 ** (e - 2).abs)
end
-
- [:FLOAT, i]
+ i
else
- return [:INTEGER, Integer(@string.gsub(/[,_]/, ''))] rescue ArgumentError
- return [:FLOAT, Float(@string.gsub(/[,_]/, ''))] rescue ArgumentError
-
- [:SCALAR, @string]
+ return Integer(string.gsub(/[,_]/, '')) rescue ArgumentError
+ return Float(string.gsub(/[,_]/, '')) rescue ArgumentError
+ string
end
end
end
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index 5c2d2ae..9217ccc 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -28,7 +28,7 @@ module Psych
@st[o.anchor] = o.value if o.anchor
return o.value if o.quoted
- return resolve_unknown(o) unless o.tag
+ return ScalarScanner.tokenize(o.value) unless o.tag
case o.tag
when '!binary', 'tag:yaml.org,2002:binary'
@@ -40,7 +40,7 @@ module Psych
when "!ruby/object:Rational"
Rational(o.value)
when "tag:yaml.org,2002:float", "!float"
- Float(ScalarScanner.new(o.value).tokenize.last)
+ Float(ScalarScanner.tokenize(o.value))
when "!ruby/regexp"
o.value =~ /^\/(.*)\/([mix]*)$/
source = $1
@@ -62,7 +62,7 @@ module Psych
args.push(args.delete_at(1) == '...')
Range.new(*args)
else
- resolve_unknown o
+ ScalarScanner.tokenize o.value
end
end
@@ -199,10 +199,6 @@ module Psych
raise ex
end
end
-
- def resolve_unknown o
- ScalarScanner.new(o.value).tokenize.last
- end
end
end
end
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb
index bbcb1c0..6b97799 100644
--- a/lib/psych/visitors/yaml_tree.rb
+++ b/lib/psych/visitors/yaml_tree.rb
@@ -149,7 +149,7 @@ module Psych
else
str = o
tag = nil
- quote = ScalarScanner.new(o).tokenize.first != :SCALAR
+ quote = !(String === ScalarScanner.new(o).tokenize)
plain = !quote
end