Class: DBus::Type::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/dbus/type.rb

Overview

D-Bus type parser class

Helper class to parse a type signature in the protocol.

Instance Method Summary collapse

Constructor Details

#initialize(signature) ⇒ Parser

Create a new parser for the given signature.



161
162
163
164
# File 'lib/dbus/type.rb', line 161

def initialize(signature)
  @signature = signature
  @idx = 0
end

Instance Method Details

#nextcharObject

Returns the next character from the signature.



167
168
169
170
171
# File 'lib/dbus/type.rb', line 167

def nextchar
  c = @signature[@idx]
  @idx += 1
  c
end

#parseObject

Parse the entire signature, return a DBus::Type object.



200
201
202
203
204
205
206
207
# File 'lib/dbus/type.rb', line 200

def parse
  @idx = 0
  ret = Array.new
  while (c = nextchar)
    ret << parse_one(c)
  end
  ret
end

#parse_one(c) ⇒ Object

Parse one character c of the signature.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/dbus/type.rb', line 174

def parse_one(c)
  res = nil
  case c
  when ?a
    res = Type.new(ARRAY)
    child = parse_one(nextchar)
    res << child
  when ?(
    res = Type.new(STRUCT)
    while (c = nextchar) != nil and c != ?)
      res << parse_one(c)
    end
    raise SignatureException, "Parse error in #{@signature}" if c == nil
  when ?{
    res = Type.new(DICT_ENTRY)
    while (c = nextchar) != nil and c != ?}
      res << parse_one(c)
    end
    raise SignatureException, "Parse error in #{@signature}" if c == nil
  else
    res = Type.new(c)
  end
  res
end