Class: LEON::Parser

Inherits:
Object show all
Defined in:
lib/io.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Parser

Returns a new instance of Parser.



10
11
12
13
14
15
16
17
18
19
# File 'lib/io.rb', line 10

def initialize(*args)
  if args.length > 1
    @spec = args[1]
    @hasSpec = true
  end
  @buffer = BufferIterator.new(args[0])
  @state = 0
  @stringIndex = Array.new
  @objectLayoutIndex = Array.new
end

Instance Method Details

#parseOLIObject



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/io.rb', line 61

def parseOLI()
  if (@state & 0x01) === 0
    parseSI()
  end
  if @stringIndex.length === 0
    return self
  end
  @OLItype = @buffer.readUInt8()
  case @OLItype
    when Constants::UNSIGNED_CHAR, Constants::UNSIGNED_SHORT, Constants::UNSIGNED_INT
      count = @buffer.readValue(@OLItype)
    when 0xFF
      return self
    else
      return self
  end
  for i in 0..(count - 1)
    @objectLayoutIndex.push Array.new
    numFields = @buffer.readValue(@buffer.readUInt8())
    for j in 0..(numFields - 1)
      @objectLayoutIndex[i].push(@buffer.readValue(@stringIndexType))
    end
  end
  return self
end

#parseSIObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/io.rb', line 42

def parseSI()
  if (@state & 0x01) != 0
    return
  end
  @stringIndexType = @buffer.readUInt8()
  case @stringIndexType
    when Constants::UNSIGNED_CHAR, Constants::UNSIGNED_SHORT, Constants::UNSIGNED_INT
      stringCount = @buffer.readValue(@stringIndexType)
    when 0xFF
      stringCount = 0
    else
      return self
  end
  for i in (0..(stringCount - 1))
    @stringIndex.push readString()
  end
  @state |= 0x01
  return self
end

#parseValue(*args) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/io.rb', line 114

def parseValue(*args)
  if args.length === 0
    type = @buffer.readUInt8()
  else
    type = args[0]
  end
  if type < Constants::OBJECT
    return @buffer.readValue(type)
  elsif type === Constants::ARRAY
    len = @buffer.readValue(@buffer.readUInt8())
    ret = Array.new
    for i in 0..(len - 1)
      ret.push parseValue()
    end
    return ret
  elsif type === Constants::OBJECT
    index = @objectLayoutIndex[@buffer.readValue(@OLItype)]
    ret = Hash.new
    index.each_with_index { |val, idx|
      ret[@stringIndex[val]] = parseValue()
    }
    return ret
  elsif type === Constants::STRING
    return @stringIndex[@buffer.readValue(@stringIndexType)]
  elsif type === Constants::UNDEFINED
    return LEON::Undefined.new
  elsif type === Constants::BOOLEAN
    return true
  elsif type === Constants::BOOLEAN + 1
    return false
  elsif type === Constants::NULL
    return nil
  elsif type === Constants::NAN
    return LEON::NaN.new
  elsif type === Constants::DATE
    return readDate()
  elsif type === Constants::REGEXP
    return readRegExp()
  elsif type === Constants::BUFFER
    return readBuffer()
  elsif type === Constants::INFINITY
    return Float::INFINITY
  elsif type === Constants::MINUS_INFINITY
    return -1/0.0
  else
    return
  end
end

#parseValueWithSpec(*args) ⇒ Object



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

def parseValueWithSpec(*args)
  if args.length === 0
    spec = @spec
  else
    spec = args[0]
  end
  if spec === Constants::STRING
    return readString()
  elsif spec.kind_of?(Array)
    ret = Array.new
    spec = spec[0]
    length = @buffer.readValue(@buffer.readUInt8())
    for i in (0..(length - 1))
      ret.push parseValueWithSpec(spec)
    end
    return ret
  elsif spec.is_a?(Hash)
    ret = Hash.new
    Hash[spec.sort].each do |k, v|
      ret[k] = parseValueWithSpec(v)
    end
    return ret
  elsif spec === Constants::BOOLEAN
    return parseValue()
  else
    return parseValue(spec)
  end
end

#readBufferObject



28
29
30
31
32
33
34
35
# File 'lib/io.rb', line 28

def readBuffer()
  ret = StringBuffer.new
  len = @buffer.readValue(@buffer.readUInt8())
  for i in 0..(len - 1)
    ret.writeUInt8(@buffer.readUInt8(), i)
  end
  return ret
end

#readDateObject



39
40
41
# File 'lib/io.rb', line 39

def readDate()
  return Time.at(@buffer.readUInt32())
end

#readRegExpObject



36
37
38
# File 'lib/io.rb', line 36

def readRegExp()
  return RegExp.new(readString(), readString())
end

#readStringObject



20
21
22
23
24
25
26
27
# File 'lib/io.rb', line 20

def readString()
  ret = ''
  len = @buffer.readValue(@buffer.readUInt8())
  for i in 0...len
    ret += @buffer.readUInt8.chr
  end
  return ret
end