Class: DBus::Type::Parser Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

D-Bus type parser class

Helper class to parse a type signature in the protocol.

Instance Method Summary collapse

Constructor Details

#initialize(signature) ⇒ Parser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new parser for the given signature.

Parameters:



197
198
199
200
201
202
203
204
205
# File 'lib/dbus/type.rb', line 197

def initialize(signature)
  @signature = signature
  if signature.size > 255
    msg = "Potential signature is longer than 255 characters (#{@signature.size}): #{@signature}"
    raise SignatureException, msg
  end

  @idx = 0
end

Instance Method Details

#nextcharObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the next character from the signature.



208
209
210
211
212
# File 'lib/dbus/type.rb', line 208

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

#parseArray<Type>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:



263
264
265
266
267
268
269
270
# File 'lib/dbus/type.rb', line 263

def parse
  @idx = 0
  ret = []
  while (c = nextchar)
    ret << parse_one(c)
  end
  ret.freeze
end

#parse1Type

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse one SingleCompleteType

Returns:

Raises:



274
275
276
277
278
279
280
281
282
# File 'lib/dbus/type.rb', line 274

def parse1
  c = nextchar
  raise SignatureException, "Empty signature, expecting a Single Complete Type" if c.nil?

  t = parse_one(c)
  raise SignatureException, "Has more than a Single Complete Type: #{@signature}" unless nextchar.nil?

  t.freeze
end

#parse_one(char, for_array: false) ⇒ Type

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse one character char of the signature.

Parameters:

  • for_array (Boolean) (defaults to: false)

    are we parsing an immediate child of an ARRAY

Returns:



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/dbus/type.rb', line 217

def parse_one(char, for_array: false)
  res = nil
  case char
  when "a"
    res = Type.new(ARRAY)
    char = nextchar
    raise SignatureException, "Empty ARRAY in #{@signature}" if char.nil?

    child = parse_one(char, for_array: true)
    res << child
  when "("
    res = Type.new(STRUCT, abstract: true)
    while (char = nextchar) && char != ")"
      res << parse_one(char)
    end
    raise SignatureException, "STRUCT not closed in #{@signature}" if char.nil?
    raise SignatureException, "Empty STRUCT in #{@signature}" if res.members.empty?
  when "{"
    raise SignatureException, "DICT_ENTRY not an immediate child of an ARRAY" unless for_array

    res = Type.new(DICT_ENTRY, abstract: true)

    # key type, value type
    2.times do |i|
      char = nextchar
      raise SignatureException, "DICT_ENTRY not closed in #{@signature}" if char.nil?

      raise SignatureException, "DICT_ENTRY must have 2 subtypes, found #{i} in #{@signature}" if char == "}"

      res << parse_one(char)
    end

    # closing "}"
    char = nextchar
    raise SignatureException, "DICT_ENTRY not closed in #{@signature}" if char.nil?

    raise SignatureException, "DICT_ENTRY must have 2 subtypes, found 3 or more in #{@signature}" if char != "}"
  else
    res = Type.new(char)
  end
  res.members.freeze
  res
end