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:



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

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.



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

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:



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

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:



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

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:



216
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
# File 'lib/dbus/type.rb', line 216

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