Class: YKScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/project/scanner.rb

Overview

Special thanks to github.com/tenderlove/psych

Constant Summary collapse

TIME =
/^-?\d{4}-\d{1,2}-\d{1,2}(?:[Tt]|\s+)\d{1,2}:\d\d:\d\d(?:\.\d*)?(?:\s*(?:Z|[-+]\d{1,2}:?(?:\d\d)?))?$/
FLOAT =
/^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10)
|[-+]?[0-9][0-9_,]*(:[0-5]?[0-9])+\.[0-9_]*(?# base 60)
|[-+]?\.(inf|Inf|INF)(?# infinity)
|\.(nan|NaN|NAN)(?# not a number))$/x
INTEGER =
/^(?:[-+]?0b[0-1_,]+          (?# base 2)
|[-+]?0[0-7_,]+           (?# base 8)
|[-+]?(?:0|[1-9][0-9_,]*) (?# base 10)
|[-+]?0x[0-9a-fA-F_,]+    (?# base 16))$/x

Instance Method Summary collapse

Instance Method Details

#parse_time(string) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/project/scanner.rb', line 87

def parse_time(string)
  date, time = *(string.split(/[ tT]/, 2))
  (yy, m, dd) = date.match(/^(-?\d{4})-(\d{1,2})-(\d{1,2})/).captures.map { |x| x.to_i }
  md = time.match(/(\d+:\d+:\d+)(?:\.(\d*))?\s*(Z|[-+]\d+(:\d\d)?)?/)

  (hh, mm, ss) = md[1].split(':').map { |x| x.to_i }
  us = (md[2] ? Rational("0.#{md[2]}") : 0) * 1000000

  time = Time.utc(yy, m, dd, hh, mm, ss, us)

  return time if 'Z' == md[3]
  return Time.at(time.to_i, us) unless md[3]

  tz = md[3].match(/^([+\-]?\d{1,2})\:?(\d{1,2})?$/)[1..-1].compact.map { |digit| Integer(digit, 10) }
  offset = tz.first * 3600

  if offset < 0
    offset -= ((tz[1] || 0) * 60)
  else
    offset += ((tz[1] || 0) * 60)
  end

  Time.at((time - offset).to_i, us)
end

#tokenize_from(string) ⇒ Object

Tokenize string returning the Ruby object NOTE: This method will be called from Objective-C.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/project/scanner.rb', line 44

def tokenize_from(string)
  case string
  when /^\+?\.inf$/i
    Float::INFINITY
  when /^-\.inf$/i
    -Float::INFINITY
  when /^\.nan$/i
    Float::NAN
  when /^:(.*)/
    string = $1
    if string =~ /^["'](.+)["']$/
      string = $1
    end
    string.to_sym
  when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+$/
    i = 0
    string.split(':').each_with_index do |n,e|
      i += (n.to_i * 60 ** (e - 2).abs)
    end
    i
  when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+\.[0-9_]*$/
    i = 0
    string.split(':').each_with_index do |n,e|
      i += (n.to_f * 60 ** (e - 2).abs)
    end
    i
  when INTEGER
    Integer(string.gsub(/[,_]/, ''))
  when FLOAT
    Float(string.gsub(/[,_]/, ''))
  when TIME
    self.parse_time(string)
  when /^(yes|y|true|on)$/i
    true
  when /^(no|n|false|off)$/i
    false
  when '~'
    nil
  else
    string
  end
end